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 "support/debug.h"
31 #include "DispatchResult.h"
32 #include "ErrorList.h"
34 #include "FuncRequest.h"
35 #include "support/gettext.h"
36 #include "InsetList.h"
40 #include "LyXAction.h"
44 #include "Paragraph.h"
45 #include "paragraph_funcs.h"
46 #include "ParagraphParameters.h"
47 #include "ParIterator.h"
48 #include "TextClass.h"
49 #include "TextMetrics.h"
52 #include "frontends/Clipboard.h"
53 #include "frontends/Selection.h"
55 #include "insets/InsetCollapsable.h"
56 #include "insets/InsetCommand.h"
57 #include "insets/InsetFloatList.h"
58 #include "insets/InsetNewline.h"
59 #include "insets/InsetQuotes.h"
60 #include "insets/InsetSpecialChar.h"
61 #include "insets/InsetText.h"
62 #include "insets/InsetInfo.h"
64 #include "support/lstrings.h"
65 #include "support/convert.h"
66 #include "support/lyxtime.h"
68 #include "mathed/InsetMathHull.h"
69 #include "mathed/MathMacroTemplate.h"
71 #include <boost/next_prior.hpp>
77 using namespace lyx::support;
81 using cap::copySelection;
82 using cap::cutSelection;
83 using cap::pasteFromStack;
84 using cap::pasteClipboardText;
85 using cap::pasteClipboardGraphics;
86 using cap::replaceSelection;
89 static Font freefont(ignore_font, ignore_language);
90 static bool toggleall = false;
92 static void toggleAndShow(Cursor & cur, Text * text,
93 Font const & font, bool toggleall = true)
95 text->toggleFree(cur, font, toggleall);
97 if (font.language() != ignore_language ||
98 font.fontInfo().number() != FONT_IGNORE) {
99 TextMetrics const & tm = cur.bv().textMetrics(text);
100 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
101 cur.real_current_font))
102 text->setCursor(cur, cur.pit(), cur.pos(),
103 false, !cur.boundary());
108 static void moveCursor(Cursor & cur, bool selecting)
110 if (selecting || cur.mark())
115 static void finishChange(Cursor & cur, bool selecting)
118 moveCursor(cur, selecting);
122 static void mathDispatch(Cursor & cur, FuncRequest const & cmd, bool display)
125 docstring sel = cur.selectionAsString(false);
127 // It may happen that sel is empty but there is a selection
128 replaceSelection(cur);
131 #ifdef ENABLE_ASSERTIONS
132 const int old_pos = cur.pos();
134 cur.insert(new InsetMathHull(hullSimple));
135 BOOST_ASSERT(old_pos == cur.pos());
136 cur.nextInset()->edit(cur, true);
137 // don't do that also for LFUN_MATH_MODE
138 // unless you want end up with always changing
139 // to mathrm when opening an inlined inset --
140 // I really hate "LyXfunc overloading"...
142 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
143 // Avoid an unnecessary undo step if cmd.argument
145 if (!cmd.argument().empty())
146 cur.dispatch(FuncRequest(LFUN_MATH_INSERT,
149 // create a macro if we see "\\newcommand"
150 // somewhere, and an ordinary formula
152 if (sel.find(from_ascii("\\newcommand")) == string::npos
153 && sel.find(from_ascii("\\newlyxcommand")) == string::npos
154 && sel.find(from_ascii("\\def")) == string::npos)
156 InsetMathHull * formula = new InsetMathHull;
157 istringstream is(to_utf8(sel));
160 formula->read(cur.buffer(), lex);
161 if (formula->getType() == hullNone)
162 // Don't create pseudo formulas if
163 // delimiters are left out
164 formula->mutate(hullSimple);
167 cur.insert(new MathMacroTemplate(sel));
170 cur.message(from_utf8(N_("Math editor mode")));
174 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
177 cap::replaceSelection(cur);
178 cur.insert(new InsetSpecialChar(kind));
183 static bool doInsertInset(Cursor & cur, Text * text,
184 FuncRequest const & cmd, bool edit, bool pastesel)
186 Inset * inset = createInset(cur.bv().buffer(), cmd);
190 if (InsetCollapsable * ci = inset->asInsetCollapsable())
191 ci->setLayout(cur.bv().buffer().params());
194 if (cmd.action == LFUN_INDEX_INSERT) {
195 docstring ds = subst(text->getStringToIndex(cur), '\n', ' ');
196 text->insertInset(cur, inset);
198 inset->edit(cur, true);
199 // Now put this into inset
200 static_cast<InsetCollapsable *>(inset)->text_.insertStringAsParagraphs(cur, ds);
203 if (cur.selection()) {
204 lyx::dispatch(FuncRequest(LFUN_CUT));
207 text->insertInset(cur, inset);
210 inset->edit(cur, true);
212 if (gotsel && pastesel) {
213 lyx::dispatch(FuncRequest(LFUN_PASTE, "0"));
214 InsetText * insetText = dynamic_cast<InsetText *>(inset);
215 if (insetText && !insetText->allowMultiPar()
216 || cur.lastpit() == 0) {
217 // reset first par to default
218 LayoutPtr const layout =
219 cur.buffer().params().getTextClass().defaultLayout();
220 cur.text()->paragraphs().begin()->layout(layout);
223 // Merge multiple paragraphs -- hack
224 while (cur.lastpit() > 0) {
225 mergeParagraph(cur.buffer().params(),
226 cur.text()->paragraphs(), 0);
229 // reset surrounding par to default
230 docstring const layoutname =
231 cur.buffer().params().getTextClass().defaultLayoutName();
232 cur.leaveInset(*inset);
233 text->setLayout(cur, layoutname);
241 string const freefont2string()
243 return freefont.toString(toggleall);
247 /// the type of outline operation
249 OutlineUp, // Move this header with text down
250 OutlineDown, // Move this header with text up
251 OutlineIn, // Make this header deeper
252 OutlineOut // Make this header shallower
256 static void outline(OutlineOp mode, Cursor & cur)
258 Buffer & buf = cur.buffer();
259 pit_type & pit = cur.pit();
260 ParagraphList & pars = buf.text().paragraphs();
261 ParagraphList::iterator bgn = pars.begin();
262 // The first paragraph of the area to be copied:
263 ParagraphList::iterator start = boost::next(bgn, pit);
264 // The final paragraph of area to be copied:
265 ParagraphList::iterator finish = start;
266 ParagraphList::iterator end = pars.end();
268 TextClass::const_iterator lit =
269 buf.params().getTextClass().begin();
270 TextClass::const_iterator const lend =
271 buf.params().getTextClass().end();
273 int const thistoclevel = start->layout()->toclevel;
277 // Move out (down) from this section header
280 // Seek the one (on same level) below
281 for (; finish != end; ++finish) {
282 toclevel = finish->layout()->toclevel;
283 if (toclevel != Layout::NOT_IN_TOC
284 && toclevel <= thistoclevel) {
288 ParagraphList::iterator dest = start;
289 // Move out (up) from this header
292 // Search previous same-level header above
295 toclevel = dest->layout()->toclevel;
297 && (toclevel == Layout::NOT_IN_TOC
298 || toclevel > thistoclevel));
299 // Not found; do nothing
300 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
302 pit_type const newpit = distance(bgn, dest);
303 pit_type const len = distance(start, finish);
304 pit_type const deletepit = pit + len;
305 buf.undo().recordUndo(cur, ATOMIC_UNDO, newpit, deletepit - 1);
306 pars.insert(dest, start, finish);
307 start = boost::next(pars.begin(), deletepit);
309 pars.erase(start, finish);
313 // Go down out of current header:
316 // Find next same-level header:
317 for (; finish != end; ++finish) {
318 toclevel = finish->layout()->toclevel;
319 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
322 ParagraphList::iterator dest = finish;
323 // Go one down from *this* header:
328 // Go further down to find header to insert in front of:
329 for (; dest != end; ++dest) {
330 toclevel = dest->layout()->toclevel;
331 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
334 // One such was found:
335 pit_type newpit = distance(bgn, dest);
336 pit_type const len = distance(start, finish);
337 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, newpit - 1);
338 pars.insert(dest, start, finish);
339 start = boost::next(bgn, pit);
341 pars.erase(start, finish);
345 buf.undo().recordUndo(cur);
346 for (; lit != lend; ++lit) {
347 if ((*lit)->toclevel == thistoclevel + 1 &&
348 start->layout()->labeltype == (*lit)->labeltype) {
349 start->layout((*lit));
355 buf.undo().recordUndo(cur);
356 for (; lit != lend; ++lit) {
357 if ((*lit)->toclevel == thistoclevel - 1 &&
358 start->layout()->labeltype == (*lit)->labeltype) {
359 start->layout((*lit));
370 void Text::number(Cursor & cur)
372 FontInfo font = ignore_font;
373 font.setNumber(FONT_TOGGLE);
374 toggleAndShow(cur, this, Font(font));
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 // FIXME: We use the update flag to indicates wether a singlePar or a
389 // full screen update is needed. We reset it here but shall we restore it
393 BOOST_ASSERT(cur.text() == this);
394 BufferView * bv = &cur.bv();
395 TextMetrics & tm = cur.bv().textMetrics(this);
396 CursorSlice oldTopSlice = cur.top();
397 bool oldBoundary = cur.boundary();
398 bool sel = cur.selection();
399 // Signals that, even if needsUpdate == false, an update of the
400 // cursor paragraph is required
401 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
402 LyXAction::SingleParUpdate);
403 // Signals that a full-screen update is required
404 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
405 LyXAction::NoUpdate) || singleParUpdate);
406 // Remember the old paragraph metric (_outer_ paragraph!)
407 ParagraphMetrics const & pm = cur.bv().parMetrics(
408 cur.bottom().text(), cur.bottom().pit());
409 Dimension olddim = pm.dim();
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 //FIXME: for visual cursor, really move left
534 if (reverseDirectionNeeded(cur)) {
535 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
536 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
538 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
539 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
544 case LFUN_CHAR_RIGHT:
545 case LFUN_CHAR_RIGHT_SELECT:
546 //FIXME: for visual cursor, really move right
547 if (reverseDirectionNeeded(cur)) {
548 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
549 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
551 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
552 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
558 case LFUN_DOWN_SELECT:
561 // stop/start the selection
562 bool select = cmd.action == LFUN_DOWN_SELECT ||
563 cmd.action == LFUN_UP_SELECT;
564 cur.selHandle(select);
566 // move cursor up/down
567 bool up = cmd.action == LFUN_UP_SELECT || cmd.action == LFUN_UP;
568 bool const successful = cur.upDownInText(up, needsUpdate);
570 // notify insets which were left and get their update flags
571 notifyCursorLeaves(cur.beforeDispatchCursor(), cur);
574 // redraw if you leave mathed (for the decorations)
575 needsUpdate |= cur.beforeDispatchCursor().inMathed();
582 case LFUN_PARAGRAPH_UP:
583 case LFUN_PARAGRAPH_UP_SELECT:
584 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
585 needsUpdate |= cursorUpParagraph(cur);
588 case LFUN_PARAGRAPH_DOWN:
589 case LFUN_PARAGRAPH_DOWN_SELECT:
590 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
591 needsUpdate |= cursorDownParagraph(cur);
594 case LFUN_SCREEN_UP_SELECT:
595 needsUpdate |= cur.selHandle(true);
596 if (cur.pit() == 0 && cur.textRow().pos() == 0)
599 tm.cursorPrevious(cur);
603 case LFUN_SCREEN_DOWN_SELECT:
604 needsUpdate |= cur.selHandle(true);
605 if (cur.pit() == cur.lastpit()
606 && cur.textRow().endpos() == cur.lastpos())
613 case LFUN_LINE_BEGIN:
614 case LFUN_LINE_BEGIN_SELECT:
615 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_BEGIN_SELECT);
616 needsUpdate |= tm.cursorHome(cur);
620 case LFUN_LINE_END_SELECT:
621 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
622 needsUpdate |= tm.cursorEnd(cur);
625 case LFUN_WORD_RIGHT:
626 case LFUN_WORD_RIGHT_SELECT:
627 //FIXME: for visual cursor mode, really move right
628 if (reverseDirectionNeeded(cur)) {
629 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
630 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
632 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
633 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
638 case LFUN_WORD_FORWARD:
639 case LFUN_WORD_FORWARD_SELECT:
640 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT);
641 needsUpdate |= cursorForwardOneWord(cur);
645 case LFUN_WORD_LEFT_SELECT:
646 //FIXME: for visual cursor mode, really move left
647 if (reverseDirectionNeeded(cur)) {
648 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
649 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
651 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
652 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
657 case LFUN_WORD_BACKWARD:
658 case LFUN_WORD_BACKWARD_SELECT:
659 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT);
660 needsUpdate |= cursorBackwardOneWord(cur);
663 case LFUN_WORD_SELECT: {
664 selectWord(cur, WHOLE_WORD);
665 finishChange(cur, true);
669 case LFUN_NEW_LINE: {
670 // Not allowed by LaTeX (labels or empty par)
671 if (cur.pos() > cur.paragraph().beginOfBody()) {
672 // this avoids a double undo
673 // FIXME: should not be needed, ideally
674 if (!cur.selection())
676 cap::replaceSelection(cur);
677 cur.insert(new InsetNewline);
679 moveCursor(cur, false);
684 case LFUN_LINE_BREAK: {
685 // Not allowed by LaTeX (labels or empty par)
686 if (cur.pos() > cur.paragraph().beginOfBody()) {
687 // this avoids a double undo
688 // FIXME: should not be needed, ideally
689 if (!cur.selection())
691 cap::replaceSelection(cur);
692 cur.insert(new InsetLinebreak);
694 moveCursor(cur, false);
699 case LFUN_CHAR_DELETE_FORWARD:
700 if (!cur.selection()) {
701 if (cur.pos() == cur.paragraph().size())
702 // Par boundary, force full-screen update
703 singleParUpdate = false;
704 needsUpdate |= erase(cur);
706 // It is possible to make it a lot faster still
707 // just comment out the line below...
709 cutSelection(cur, true, false);
710 singleParUpdate = false;
712 moveCursor(cur, false);
715 case LFUN_DELETE_FORWARD_SKIP:
716 // Reverse the effect of LFUN_BREAK_PARAGRAPH_SKIP.
717 if (!cur.selection()) {
718 if (cur.pos() == cur.lastpos()) {
725 cutSelection(cur, true, false);
730 case LFUN_CHAR_DELETE_BACKWARD:
731 if (!cur.selection()) {
732 if (bv->getIntl().getTransManager().backspace()) {
733 // Par boundary, full-screen update
735 singleParUpdate = false;
736 needsUpdate |= backspace(cur);
738 // It is possible to make it a lot faster still
739 // just comment out the line below...
742 cutSelection(cur, true, false);
743 singleParUpdate = false;
747 case LFUN_DELETE_BACKWARD_SKIP:
748 // Reverse the effect of LFUN_BREAK_PARAGRAPH_SKIP.
749 if (!cur.selection()) {
751 //CursorSlice cur = cursor();
755 cutSelection(cur, true, false);
759 case LFUN_BREAK_PARAGRAPH:
760 cap::replaceSelection(cur);
761 breakParagraph(cur, cmd.argument() == "inverse");
765 case LFUN_BREAK_PARAGRAPH_SKIP: {
766 // When at the beginning of a paragraph, remove
767 // indentation. Otherwise, do the same as LFUN_BREAK_PARAGRAPH.
768 cap::replaceSelection(cur);
770 cur.paragraph().params().labelWidthString(docstring());
772 breakParagraph(cur, false);
778 // With the creation of LFUN_PARAGRAPH_PARAMS, this is now redundant,
779 // as its duties can be performed there. Should it be removed??
780 // FIXME For now, it can just dispatch LFUN_PARAGRAPH_PARAMS...
781 case LFUN_PARAGRAPH_SPACING: {
782 Paragraph & par = cur.paragraph();
783 Spacing::Space cur_spacing = par.params().spacing().getSpace();
784 string cur_value = "1.0";
785 if (cur_spacing == Spacing::Other)
786 cur_value = par.params().spacing().getValueAsString();
788 istringstream is(to_utf8(cmd.argument()));
791 Spacing::Space new_spacing = cur_spacing;
792 string new_value = cur_value;
794 lyxerr << "Missing argument to `paragraph-spacing'"
796 } else if (tmp == "single") {
797 new_spacing = Spacing::Single;
798 } else if (tmp == "onehalf") {
799 new_spacing = Spacing::Onehalf;
800 } else if (tmp == "double") {
801 new_spacing = Spacing::Double;
802 } else if (tmp == "other") {
803 new_spacing = Spacing::Other;
804 string tmpval = "0.0";
806 lyxerr << "new_value = " << tmpval << endl;
809 } else if (tmp == "default") {
810 new_spacing = Spacing::Default;
812 lyxerr << to_utf8(_("Unknown spacing argument: "))
813 << to_utf8(cmd.argument()) << endl;
815 if (cur_spacing != new_spacing || cur_value != new_value)
816 par.params().spacing(Spacing(new_spacing, new_value));
820 case LFUN_INSET_INSERT: {
822 Inset * inset = createInset(bv->buffer(), cmd);
824 // FIXME (Abdel 01/02/2006):
825 // What follows would be a partial fix for bug 2154:
826 // http://bugzilla.lyx.org/show_bug.cgi?id=2154
827 // This automatically put the label inset _after_ a
828 // numbered section. It should be possible to extend the mechanism
829 // to any kind of LateX environement.
830 // The correct way to fix that bug would be at LateX generation.
831 // I'll let the code here for reference as it could be used for some
832 // other feature like "automatic labelling".
834 Paragraph & par = pars_[cur.pit()];
835 if (inset->lyxCode() == LABEL_CODE
836 && par.layout()->labeltype == LABEL_COUNTER) {
837 // Go to the end of the paragraph
838 // Warning: Because of Change-Tracking, the last
839 // position is 'size()' and not 'size()-1':
840 cur.pos() = par.size();
841 // Insert a new paragraph
842 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
847 cutSelection(cur, true, false);
848 insertInset(cur, inset);
854 case LFUN_INSET_DISSOLVE:
855 needsUpdate |= dissolveInset(cur);
858 case LFUN_INSET_SETTINGS:
859 cur.inset().showInsetDialog(bv);
862 case LFUN_SPACE_INSERT:
863 if (cur.paragraph().layout()->free_spacing)
864 insertChar(cur, ' ');
866 doInsertInset(cur, this, cmd, false, false);
869 moveCursor(cur, false);
872 case LFUN_SPECIALCHAR_INSERT: {
873 string const name = to_utf8(cmd.argument());
874 if (name == "hyphenation")
875 specialChar(cur, InsetSpecialChar::HYPHENATION);
876 else if (name == "ligature-break")
877 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
878 else if (name == "slash")
879 specialChar(cur, InsetSpecialChar::SLASH);
880 else if (name == "nobreakdash")
881 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
882 else if (name == "dots")
883 specialChar(cur, InsetSpecialChar::LDOTS);
884 else if (name == "end-of-sentence")
885 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
886 else if (name == "menu-separator")
887 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
888 else if (name.empty())
889 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
891 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
895 case LFUN_WORD_UPCASE:
896 changeCase(cur, text_uppercase);
899 case LFUN_WORD_LOWCASE:
900 changeCase(cur, text_lowercase);
903 case LFUN_WORD_CAPITALIZE:
904 changeCase(cur, text_capitalization);
907 case LFUN_CHARS_TRANSPOSE:
912 cur.message(_("Paste"));
913 cap::replaceSelection(cur);
916 string const arg = to_utf8(cmd.argument());
918 if (theClipboard().isInternal())
919 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
920 else if (theClipboard().hasGraphicsContents())
921 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
923 pasteClipboardText(cur, bv->buffer().errorList("Paste"));
924 } else if (isStrUnsignedInt(arg)) {
925 // we have a numerical argument
926 pasteFromStack(cur, bv->buffer().errorList("Paste"),
927 convert<unsigned int>(arg));
929 Clipboard::GraphicsType type;
931 type = Clipboard::PdfGraphicsType;
932 else if (arg == "png")
933 type = Clipboard::PngGraphicsType;
934 else if (arg == "jpeg")
935 type = Clipboard::JpegGraphicsType;
936 else if (arg == "linkback")
937 type = Clipboard::LinkBackGraphicsType;
941 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
944 bv->buffer().errors("Paste");
945 cur.clearSelection(); // bug 393
951 cutSelection(cur, true, true);
952 cur.message(_("Cut"));
957 cur.message(_("Copy"));
960 case LFUN_SERVER_GET_XY:
961 cur.message(from_utf8(
962 convert<string>(tm.cursorX(cur.top(), cur.boundary()))
963 + ' ' + convert<string>(tm.cursorY(cur.top(), cur.boundary()))));
966 case LFUN_SERVER_SET_XY: {
969 istringstream is(to_utf8(cmd.argument()));
972 lyxerr << "SETXY: Could not parse coordinates in '"
973 << to_utf8(cmd.argument()) << endl;
975 tm.setCursorFromCoordinates(cur, x, y);
979 case LFUN_SERVER_GET_FONT:
980 if (cur.current_font.fontInfo().shape() == ITALIC_SHAPE)
981 cur.message(from_ascii("E"));
982 else if (cur.current_font.fontInfo().shape() == SMALLCAPS_SHAPE)
983 cur.message(from_ascii("N"));
985 cur.message(from_ascii("0"));
988 case LFUN_SERVER_GET_LAYOUT:
989 cur.message(cur.paragraph().layout()->name());
993 docstring layout = cmd.argument();
994 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
996 docstring const old_layout = cur.paragraph().layout()->name();
998 // Derive layout number from given argument (string)
999 // and current buffer's textclass (number)
1000 TextClass const & tclass = bv->buffer().params().getTextClass();
1002 layout = tclass.defaultLayoutName();
1003 bool hasLayout = tclass.hasLayout(layout);
1005 // If the entry is obsolete, use the new one instead.
1007 docstring const & obs = tclass[layout]->obsoleted_by();
1013 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1014 from_utf8(N_(" not known")));
1018 bool change_layout = (old_layout != layout);
1020 if (!change_layout && cur.selection() &&
1021 cur.selBegin().pit() != cur.selEnd().pit())
1023 pit_type spit = cur.selBegin().pit();
1024 pit_type epit = cur.selEnd().pit() + 1;
1025 while (spit != epit) {
1026 if (pars_[spit].layout()->name() != old_layout) {
1027 change_layout = true;
1035 setLayout(cur, layout);
1040 case LFUN_CLIPBOARD_PASTE:
1041 cur.clearSelection();
1042 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1043 cmd.argument() == "paragraph");
1044 bv->buffer().errors("Paste");
1047 case LFUN_PRIMARY_SELECTION_PASTE:
1048 pasteString(cur, theSelection().get(),
1049 cmd.argument() == "paragraph");
1052 case LFUN_UNICODE_INSERT: {
1053 if (cmd.argument().empty())
1055 docstring hexstring = cmd.argument();
1056 if (isHex(hexstring)) {
1057 char_type c = hexToInt(hexstring);
1058 if (c >= 32 && c < 0x10ffff) {
1059 lyxerr << "Inserting c: " << c << endl;
1060 docstring s = docstring(1, c);
1061 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1067 case LFUN_QUOTE_INSERT: {
1068 Paragraph & par = cur.paragraph();
1069 pos_type pos = cur.pos();
1070 BufferParams const & bufparams = bv->buffer().params();
1071 LayoutPtr const & style = par.layout();
1072 if (!style->pass_thru
1073 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1074 // this avoids a double undo
1075 // FIXME: should not be needed, ideally
1076 if (!cur.selection())
1078 cap::replaceSelection(cur);
1083 else if (cur.prevInset() && cur.prevInset()->isSpace())
1086 c = par.getChar(pos - 1);
1087 string arg = to_utf8(cmd.argument());
1088 if (arg == "single")
1089 cur.insert(new InsetQuotes(c,
1090 bufparams.quotes_language,
1091 InsetQuotes::SingleQ));
1093 cur.insert(new InsetQuotes(c,
1094 bufparams.quotes_language,
1095 InsetQuotes::DoubleQ));
1099 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1103 case LFUN_DATE_INSERT: {
1104 string const format = cmd.argument().empty()
1105 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1106 string const time = formatted_time(current_time(), format);
1107 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1111 case LFUN_MOUSE_TRIPLE:
1112 if (cmd.button() == mouse_button::button1) {
1121 case LFUN_MOUSE_DOUBLE:
1122 if (cmd.button() == mouse_button::button1) {
1123 selectWord(cur, WHOLE_WORD_STRICT);
1128 // Single-click on work area
1129 case LFUN_MOUSE_PRESS: {
1130 // Right click on a footnote flag opens float menu
1131 // FIXME: Why should we clear the selection in this case?
1132 if (cmd.button() == mouse_button::button3)
1133 cur.clearSelection();
1135 bool do_selection = cmd.button() == mouse_button::button1
1136 && cmd.argument() == "region-select";
1138 bool update = bv->mouseSetCursor(cur, do_selection);
1140 // Insert primary selection with middle mouse
1141 // if there is a local selection in the current buffer,
1143 if (cmd.button() == mouse_button::button2) {
1144 if (cap::selection()) {
1145 // Copy the selection buffer to the clipboard
1146 // stack, because we want it to appear in the
1147 // "Edit->Paste recent" menu.
1148 cap::copySelectionToStack();
1150 cap::pasteSelection(bv->cursor(),
1151 bv->buffer().errorList("Paste"));
1152 bv->buffer().errors("Paste");
1153 bv->buffer().markDirty();
1154 bv->cursor().finishUndo();
1156 lyx::dispatch(FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph"));
1160 // we have to update after dEPM triggered
1161 if (!update && cmd.button() == mouse_button::button1) {
1162 needsUpdate = false;
1169 case LFUN_MOUSE_MOTION: {
1170 // Only use motion with button 1
1171 //if (cmd.button() != mouse_button::button1)
1174 // ignore motions deeper nested than the real anchor
1175 Cursor & bvcur = cur.bv().cursor();
1176 if (!bvcur.anchor_.hasPart(cur)) {
1180 CursorSlice old = bvcur.top();
1182 int const wh = bv->workHeight();
1183 int const y = max(0, min(wh - 1, cmd.y));
1185 tm.setCursorFromCoordinates(cur, cmd.x, y);
1186 cur.setTargetX(cmd.x);
1188 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1190 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1191 // This is to allow jumping over large insets
1192 if (cur.top() == old) {
1194 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1196 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1199 if (cur.top() == old)
1202 // FIXME: This is brute force! But without it the selected
1203 // area is not corrected updated while moving the mouse.
1204 cur.updateFlags(Update::Force | Update::FitCursor);
1205 // don't set anchor_
1206 bvcur.setCursor(cur);
1207 bvcur.selection() = true;
1208 //lyxerr << "MOTION: " << bv->cursor() << endl;
1213 case LFUN_MOUSE_RELEASE: {
1214 if (cmd.button() == mouse_button::button2)
1217 if (cmd.button() == mouse_button::button1) {
1218 // if there is new selection, update persistent
1219 // selection, otherwise, single click does not
1220 // clear persistent selection buffer
1221 if (cur.selection()) {
1223 // if double click, cur is moved to the end of word by selectWord
1224 // but bvcur is current mouse position
1225 Cursor & bvcur = cur.bv().cursor();
1226 bvcur.selection() = true;
1228 needsUpdate = false;
1235 case LFUN_SELF_INSERT: {
1236 if (cmd.argument().empty())
1239 // Automatically delete the currently selected
1240 // text and replace it with what is being
1241 // typed in now. Depends on lyxrc settings
1242 // "auto_region_delete", which defaults to
1245 if (lyxrc.auto_region_delete && cur.selection())
1246 cutSelection(cur, false, false);
1248 cur.clearSelection();
1249 Font const old_font = cur.real_current_font;
1251 docstring::const_iterator cit = cmd.argument().begin();
1252 docstring::const_iterator end = cmd.argument().end();
1253 for (; cit != end; ++cit)
1254 bv->translateAndInsert(*cit, this, cur);
1257 moveCursor(cur, false);
1261 case LFUN_HYPERLINK_INSERT: {
1262 InsetCommandParams p(HYPERLINK_CODE);
1264 if (cur.selection()) {
1265 content = cur.selectionAsString(false);
1266 cutSelection(cur, true, false);
1268 p["target"] = (cmd.argument().empty()) ?
1269 content : cmd.argument();
1270 string const data = InsetCommandMailer::params2string("href", p);
1271 if (p["target"].empty()) {
1272 bv->showDialog("href", data);
1274 FuncRequest fr(LFUN_INSET_INSERT, data);
1280 case LFUN_LABEL_INSERT: {
1281 InsetCommandParams p(LABEL_CODE);
1282 // Try to generate a valid label
1283 p["name"] = (cmd.argument().empty()) ?
1284 cur.getPossibleLabel() :
1286 string const data = InsetCommandMailer::params2string("label", p);
1288 if (cmd.argument().empty()) {
1289 bv->showDialog("label", data);
1291 FuncRequest fr(LFUN_INSET_INSERT, data);
1297 case LFUN_INFO_INSERT: {
1298 Inset * inset = createInset(cur.bv().buffer(), cmd);
1301 // if an empty inset is created (cmd.argument() is empty)
1302 // use current selection as parameter.
1303 if (cmd.argument().empty() && cur.selection()) {
1304 // use selected text as info to avoid a separate UI
1305 docstring ds = cur.selectionAsString(false);
1306 cutSelection(cur, true, false);
1307 static_cast<InsetInfo *>(inset)->setInfo(to_utf8(ds));
1308 static_cast<InsetInfo *>(inset)->updateInfo(cur.bv().buffer());
1310 insertInset(cur, inset);
1315 case LFUN_LIST_INSERT:
1316 case LFUN_THEOREM_INSERT:
1318 case LFUN_CAPTION_INSERT:
1319 case LFUN_FOOTNOTE_INSERT:
1320 // Open the inset, and move the current selection
1322 doInsertInset(cur, this, cmd, true, true);
1324 // These insets are numbered.
1325 updateLabels(bv->buffer());
1327 case LFUN_NOTE_INSERT:
1328 case LFUN_FLEX_INSERT:
1329 case LFUN_BOX_INSERT:
1330 case LFUN_BRANCH_INSERT:
1331 case LFUN_BIBITEM_INSERT:
1332 case LFUN_ERT_INSERT:
1333 case LFUN_LISTING_INSERT:
1334 case LFUN_MARGINALNOTE_INSERT:
1335 case LFUN_OPTIONAL_INSERT:
1336 case LFUN_ENVIRONMENT_INSERT:
1337 // Open the inset, and move the current selection
1339 doInsertInset(cur, this, cmd, true, true);
1343 case LFUN_TABULAR_INSERT:
1344 // if there were no arguments, just open the dialog
1345 if (doInsertInset(cur, this, cmd, false, true))
1348 bv->showDialog("tabularcreate");
1352 case LFUN_FLOAT_INSERT:
1353 case LFUN_FLOAT_WIDE_INSERT:
1354 case LFUN_WRAP_INSERT: {
1355 bool content = cur.selection(); // will some text be moved into the inset?
1357 doInsertInset(cur, this, cmd, true, true);
1359 ParagraphList & pars = cur.text()->paragraphs();
1361 TextClass const & tclass = bv->buffer().params().getTextClass();
1363 // add a separate paragraph for the caption inset
1364 pars.push_back(Paragraph());
1365 pars.back().setInsetOwner(pars[0].inInset());
1366 pars.back().layout(tclass.defaultLayout());
1368 int cap_pit = pars.size() - 1;
1370 // if an empty inset was created, we create an additional empty
1371 // paragraph at the bottom so that the user can choose where to put
1372 // the graphics (or table).
1374 pars.push_back(Paragraph());
1375 pars.back().setInsetOwner(pars[0].inInset());
1376 pars.back().layout(tclass.defaultLayout());
1380 // reposition the cursor to the caption
1381 cur.pit() = cap_pit;
1383 // FIXME: This Text/Cursor dispatch handling is a mess!
1384 // We cannot use Cursor::dispatch here it needs access to up to
1386 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1387 cur.text()->dispatch(cur, cmd_caption);
1388 cur.updateFlags(Update::Force);
1389 // FIXME: When leaving the Float (or Wrap) inset we should
1390 // delete any empty paragraph left above or below the
1395 case LFUN_INDEX_INSERT:
1396 doInsertInset(cur, this, cmd, true, true);
1400 case LFUN_NOMENCL_INSERT: {
1401 FuncRequest cmd1 = cmd;
1402 if (cmd.argument().empty())
1403 cmd1 = FuncRequest(cmd,
1404 bv->cursor().innerText()->getStringToIndex(bv->cursor()));
1405 Inset * inset = createInset(cur.bv().buffer(), cmd1);
1409 cur.clearSelection();
1410 insertInset(cur, inset);
1411 // Show the dialog for the nomenclature entry, since the
1412 // description entry still needs to be filled in.
1413 if (cmd.action == LFUN_NOMENCL_INSERT)
1414 inset->edit(cur, true);
1419 case LFUN_INDEX_PRINT:
1420 case LFUN_NOMENCL_PRINT:
1421 case LFUN_TOC_INSERT:
1422 case LFUN_HFILL_INSERT:
1423 case LFUN_LINE_INSERT:
1424 case LFUN_NEWPAGE_INSERT:
1425 case LFUN_PAGEBREAK_INSERT:
1426 case LFUN_CLEARPAGE_INSERT:
1427 case LFUN_CLEARDOUBLEPAGE_INSERT:
1429 doInsertInset(cur, this, cmd, false, false);
1433 case LFUN_DEPTH_DECREMENT:
1434 changeDepth(cur, DEC_DEPTH);
1437 case LFUN_DEPTH_INCREMENT:
1438 changeDepth(cur, INC_DEPTH);
1441 case LFUN_MATH_DISPLAY:
1442 mathDispatch(cur, cmd, true);
1445 case LFUN_MATH_IMPORT_SELECTION:
1446 case LFUN_MATH_MODE:
1447 if (cmd.argument() == "on")
1448 // don't pass "on" as argument
1449 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1451 mathDispatch(cur, cmd, false);
1454 case LFUN_MATH_MACRO:
1455 if (cmd.argument().empty())
1456 cur.errorMessage(from_utf8(N_("Missing argument")));
1458 string s = to_utf8(cmd.argument());
1459 string const s1 = token(s, ' ', 1);
1460 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1461 string const s2 = token(s, ' ', 2);
1462 MacroType type = MacroTypeNewcommand;
1464 type = MacroTypeDef;
1465 cur.insert(new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, false, type));
1466 //cur.nextInset()->edit(cur, true);
1470 // passthrough hat and underscore outside mathed:
1471 case LFUN_MATH_SUBSCRIPT:
1472 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1474 case LFUN_MATH_SUPERSCRIPT:
1475 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1478 case LFUN_MATH_INSERT:
1479 case LFUN_MATH_MATRIX:
1480 case LFUN_MATH_DELIM:
1481 case LFUN_MATH_BIGDELIM: {
1482 cap::replaceSelection(cur);
1483 cur.insert(new InsetMathHull(hullSimple));
1484 checkAndActivateInset(cur, true);
1485 BOOST_ASSERT(cur.inMathed());
1490 case LFUN_FONT_EMPH: {
1491 Font font(ignore_font, ignore_language);
1492 font.fontInfo().setEmph(FONT_TOGGLE);
1493 toggleAndShow(cur, this, font);
1497 case LFUN_FONT_BOLD: {
1498 Font font(ignore_font, ignore_language);
1499 font.fontInfo().setSeries(BOLD_SERIES);
1500 toggleAndShow(cur, this, font);
1504 case LFUN_FONT_NOUN: {
1505 Font font(ignore_font, ignore_language);
1506 font.fontInfo().setNoun(FONT_TOGGLE);
1507 toggleAndShow(cur, this, font);
1511 case LFUN_FONT_TYPEWRITER: {
1512 Font font(ignore_font, ignore_language);
1513 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1514 toggleAndShow(cur, this, font);
1518 case LFUN_FONT_SANS: {
1519 Font font(ignore_font, ignore_language);
1520 font.fontInfo().setFamily(SANS_FAMILY);
1521 toggleAndShow(cur, this, font);
1525 case LFUN_FONT_ROMAN: {
1526 Font font(ignore_font, ignore_language);
1527 font.fontInfo().setFamily(ROMAN_FAMILY);
1528 toggleAndShow(cur, this, font);
1532 case LFUN_FONT_DEFAULT: {
1533 Font font(inherit_font, ignore_language);
1534 toggleAndShow(cur, this, font);
1538 case LFUN_FONT_UNDERLINE: {
1539 Font font(ignore_font, ignore_language);
1540 font.fontInfo().setUnderbar(FONT_TOGGLE);
1541 toggleAndShow(cur, this, font);
1545 case LFUN_FONT_SIZE: {
1546 Font font(ignore_font, ignore_language);
1547 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1548 toggleAndShow(cur, this, font);
1552 case LFUN_LANGUAGE: {
1553 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1556 Font font(ignore_font, lang);
1557 toggleAndShow(cur, this, font);
1561 case LFUN_FONT_FREE_APPLY:
1562 toggleAndShow(cur, this, freefont, toggleall);
1563 cur.message(_("Character set"));
1566 // Set the freefont using the contents of \param data dispatched from
1567 // the frontends and apply it at the current cursor location.
1568 case LFUN_FONT_FREE_UPDATE: {
1571 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1574 toggleAndShow(cur, this, freefont, toggleall);
1575 cur.message(_("Character set"));
1577 lyxerr << "Argument not ok";
1582 case LFUN_FINISHED_LEFT:
1583 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
1584 if (reverseDirectionNeeded(cur)) {
1586 cur.setCurrentFont();
1590 case LFUN_FINISHED_RIGHT:
1591 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
1592 if (!reverseDirectionNeeded(cur)) {
1594 cur.setCurrentFont();
1598 case LFUN_FINISHED_BACKWARD:
1599 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
1602 case LFUN_FINISHED_FORWARD:
1603 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
1605 cur.setCurrentFont();
1608 case LFUN_LAYOUT_PARAGRAPH: {
1610 params2string(cur.paragraph(), data);
1611 data = "show\n" + data;
1612 bv->showDialog("paragraph", data);
1616 case LFUN_PARAGRAPH_UPDATE: {
1618 params2string(cur.paragraph(), data);
1620 // Will the paragraph accept changes from the dialog?
1621 bool const accept = !cur.inset().forceDefaultParagraphs(cur.idx());
1623 data = "update " + convert<string>(accept) + '\n' + data;
1624 bv->updateDialog("paragraph", data);
1628 case LFUN_ACCENT_UMLAUT:
1629 case LFUN_ACCENT_CIRCUMFLEX:
1630 case LFUN_ACCENT_GRAVE:
1631 case LFUN_ACCENT_ACUTE:
1632 case LFUN_ACCENT_TILDE:
1633 case LFUN_ACCENT_CEDILLA:
1634 case LFUN_ACCENT_MACRON:
1635 case LFUN_ACCENT_DOT:
1636 case LFUN_ACCENT_UNDERDOT:
1637 case LFUN_ACCENT_UNDERBAR:
1638 case LFUN_ACCENT_CARON:
1639 case LFUN_ACCENT_SPECIAL_CARON:
1640 case LFUN_ACCENT_BREVE:
1641 case LFUN_ACCENT_TIE:
1642 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1643 case LFUN_ACCENT_CIRCLE:
1644 case LFUN_ACCENT_OGONEK:
1645 theLyXFunc().handleKeyFunc(cmd.action);
1646 if (!cmd.argument().empty())
1647 // FIXME: Are all these characters encoded in one byte in utf8?
1648 bv->translateAndInsert(cmd.argument()[0], this, cur);
1651 case LFUN_FLOAT_LIST: {
1652 TextClass const & tclass = bv->buffer().params().getTextClass();
1653 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1655 if (cur.selection())
1656 cutSelection(cur, true, false);
1657 breakParagraph(cur);
1659 if (cur.lastpos() != 0) {
1660 cursorBackward(cur);
1661 breakParagraph(cur);
1664 setLayout(cur, tclass.defaultLayoutName());
1665 ParagraphParameters p;
1666 setParagraphs(cur, p);
1667 insertInset(cur, new InsetFloatList(to_utf8(cmd.argument())));
1670 lyxerr << "Non-existent float type: "
1671 << to_utf8(cmd.argument()) << endl;
1676 case LFUN_CHANGE_ACCEPT: {
1677 acceptOrRejectChanges(cur, ACCEPT);
1681 case LFUN_CHANGE_REJECT: {
1682 acceptOrRejectChanges(cur, REJECT);
1686 case LFUN_THESAURUS_ENTRY: {
1687 docstring arg = cmd.argument();
1689 arg = cur.selectionAsString(false);
1691 if (arg.size() > 100 || arg.empty()) {
1692 // Get word or selection
1693 selectWordWhenUnderCursor(cur, WHOLE_WORD);
1694 arg = cur.selectionAsString(false);
1697 bv->showDialog("thesaurus", to_utf8(arg));
1701 case LFUN_PARAGRAPH_PARAMS_APPLY: {
1702 // Given data, an encoding of the ParagraphParameters
1703 // generated in the Paragraph dialog, this function sets
1704 // the current paragraph, or currently selected paragraphs,
1706 // NOTE: This function overrides all existing settings.
1707 setParagraphs(cur, cmd.argument());
1708 cur.message(_("Paragraph layout set"));
1712 case LFUN_PARAGRAPH_PARAMS: {
1713 // Given data, an encoding of the ParagraphParameters as we'd
1714 // find them in a LyX file, this function modifies the current paragraph,
1715 // or currently selected paragraphs.
1716 // NOTE: This function only modifies, and does not override, existing
1718 setParagraphs(cur, cmd.argument(), true);
1719 cur.message(_("Paragraph layout set"));
1724 if (cur.selection()) {
1725 cur.selection() = false;
1728 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
1729 // correct, but I'm not 100% sure -- dov, 071019
1730 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1734 case LFUN_OUTLINE_UP:
1735 outline(OutlineUp, cur);
1736 setCursor(cur, cur.pit(), 0);
1737 updateLabels(cur.buffer());
1741 case LFUN_OUTLINE_DOWN:
1742 outline(OutlineDown, cur);
1743 setCursor(cur, cur.pit(), 0);
1744 updateLabels(cur.buffer());
1748 case LFUN_OUTLINE_IN:
1749 outline(OutlineIn, cur);
1750 updateLabels(cur.buffer());
1754 case LFUN_OUTLINE_OUT:
1755 outline(OutlineOut, cur);
1756 updateLabels(cur.buffer());
1761 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
1766 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
1768 // FIXME: The cursor flag is reset two lines below
1769 // so we need to check here if some of the LFUN did touch that.
1770 // for now only Text::erase() and Text::backspace() do that.
1771 // The plan is to verify all the LFUNs and then to remove this
1772 // singleParUpdate boolean altogether.
1773 if (cur.result().update() & Update::Force) {
1774 singleParUpdate = false;
1778 // FIXME: the following code should go in favor of fine grained
1779 // update flag treatment.
1780 if (singleParUpdate) {
1781 // Inserting characters does not change par height
1782 ParagraphMetrics const & pms
1783 = cur.bv().parMetrics(cur.bottom().text(), cur.bottom().pit());
1784 if (pms.dim().height() == olddim.height()) {
1785 // if so, update _only_ this paragraph
1786 cur.updateFlags(Update::SinglePar |
1794 && &oldTopSlice.inset() == &cur.inset()
1795 && oldTopSlice.idx() == cur.idx()
1796 && !sel // sel is a backup of cur.selection() at the biginning of the function.
1797 && !cur.selection())
1798 // FIXME: it would be better if we could just do this
1800 //if (cur.result().update() != Update::FitCursor)
1803 // But some LFUNs do not set Update::FitCursor when needed, so we
1804 // do it for all. This is not very harmfull as FitCursor will provoke
1805 // a full redraw only if needed but still, a proper review of all LFUN
1806 // should be done and this needsUpdate boolean can then be removed.
1807 cur.updateFlags(Update::FitCursor);
1809 cur.updateFlags(Update::Force | Update::FitCursor);
1813 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
1814 FuncStatus & flag) const
1816 BOOST_ASSERT(cur.text() == this);
1818 Font const & font = cur.real_current_font;
1819 FontInfo const & fontinfo = font.fontInfo();
1821 InsetCode code = NO_CODE;
1823 switch (cmd.action) {
1825 case LFUN_DEPTH_DECREMENT:
1826 enable = changeDepthAllowed(cur, DEC_DEPTH);
1829 case LFUN_DEPTH_INCREMENT:
1830 enable = changeDepthAllowed(cur, INC_DEPTH);
1834 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1837 case LFUN_BIBITEM_INSERT:
1838 enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO
1842 case LFUN_DIALOG_SHOW_NEW_INSET:
1843 if (cmd.argument() == "bibitem")
1844 code = BIBITEM_CODE;
1845 else if (cmd.argument() == "bibtex")
1847 else if (cmd.argument() == "box")
1849 else if (cmd.argument() == "branch")
1851 else if (cmd.argument() == "citation")
1853 else if (cmd.argument() == "ert")
1855 else if (cmd.argument() == "external")
1856 code = EXTERNAL_CODE;
1857 else if (cmd.argument() == "float")
1859 else if (cmd.argument() == "graphics")
1860 code = GRAPHICS_CODE;
1861 else if (cmd.argument() == "href")
1862 code = HYPERLINK_CODE;
1863 else if (cmd.argument() == "include")
1864 code = INCLUDE_CODE;
1865 else if (cmd.argument() == "index")
1867 else if (cmd.argument() == "nomenclature")
1868 code = NOMENCL_CODE;
1869 else if (cmd.argument() == "label")
1871 else if (cmd.argument() == "note")
1873 else if (cmd.argument() == "ref")
1875 else if (cmd.argument() == "toc")
1877 else if (cmd.argument() == "vspace")
1879 else if (cmd.argument() == "wrap")
1881 else if (cmd.argument() == "listings")
1882 code = LISTINGS_CODE;
1885 case LFUN_ERT_INSERT:
1888 case LFUN_LISTING_INSERT:
1889 code = LISTINGS_CODE;
1891 case LFUN_FOOTNOTE_INSERT:
1894 case LFUN_TABULAR_INSERT:
1895 code = TABULAR_CODE;
1897 case LFUN_MARGINALNOTE_INSERT:
1900 case LFUN_FLOAT_INSERT:
1901 case LFUN_FLOAT_WIDE_INSERT:
1904 case LFUN_WRAP_INSERT:
1907 case LFUN_FLOAT_LIST:
1908 code = FLOAT_LIST_CODE;
1911 case LFUN_LIST_INSERT:
1914 case LFUN_THEOREM_INSERT:
1915 code = THEOREM_CODE;
1918 case LFUN_CAPTION_INSERT:
1919 code = CAPTION_CODE;
1921 case LFUN_NOTE_INSERT:
1924 case LFUN_FLEX_INSERT: {
1926 string s = cmd.getArg(0);
1927 InsetLayout il = cur.buffer().params().getTextClass().insetlayout(from_utf8(s));
1928 if (il.lyxtype != "charstyle" &&
1929 il.lyxtype != "custom" &&
1930 il.lyxtype != "element" &&
1931 il.lyxtype != "standard")
1935 case LFUN_BOX_INSERT:
1938 case LFUN_BRANCH_INSERT:
1940 if (cur.buffer().masterBuffer()->params().branchlist().empty())
1943 case LFUN_LABEL_INSERT:
1946 case LFUN_INFO_INSERT:
1949 case LFUN_OPTIONAL_INSERT:
1951 enable = cur.paragraph().insetList().count(OPTARG_CODE)
1952 < cur.paragraph().layout()->optionalargs;
1954 case LFUN_ENVIRONMENT_INSERT:
1957 case LFUN_INDEX_INSERT:
1960 case LFUN_INDEX_PRINT:
1961 code = INDEX_PRINT_CODE;
1963 case LFUN_NOMENCL_INSERT:
1964 code = NOMENCL_CODE;
1966 case LFUN_NOMENCL_PRINT:
1967 code = NOMENCL_PRINT_CODE;
1969 case LFUN_TOC_INSERT:
1972 case LFUN_HYPERLINK_INSERT:
1973 code = HYPERLINK_CODE;
1975 case LFUN_QUOTE_INSERT:
1976 // always allow this, since we will inset a raw quote
1977 // if an inset is not allowed.
1979 case LFUN_HFILL_INSERT:
1980 case LFUN_SPECIALCHAR_INSERT:
1981 code = SPECIALCHAR_CODE;
1983 case LFUN_SPACE_INSERT:
1984 // slight hack: we know this is allowed in math mode
1989 case LFUN_INSET_MODIFY:
1990 // We need to disable this, because we may get called for a
1992 // InsetTabular::getStatus() -> InsetText::getStatus()
1993 // and we don't handle LFUN_INSET_MODIFY.
1997 case LFUN_FONT_EMPH:
1998 flag.setOnOff(fontinfo.emph() == FONT_ON);
2001 case LFUN_FONT_NOUN:
2002 flag.setOnOff(fontinfo.noun() == FONT_ON);
2005 case LFUN_FONT_BOLD:
2006 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2009 case LFUN_FONT_SANS:
2010 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2013 case LFUN_FONT_ROMAN:
2014 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2017 case LFUN_FONT_TYPEWRITER:
2018 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2023 enable = cur.selection();
2027 if (cmd.argument().empty()) {
2028 if (theClipboard().isInternal())
2029 enable = cap::numberOfSelections() > 0;
2031 enable = !theClipboard().empty();
2035 // we have an argument
2036 string const arg = to_utf8(cmd.argument());
2037 if (isStrUnsignedInt(arg)) {
2038 // it's a number and therefore means the internal stack
2039 unsigned int n = convert<unsigned int>(arg);
2040 enable = cap::numberOfSelections() > n;
2044 // explicit graphics type?
2045 if ((arg == "pdf" && theClipboard().hasGraphicsContents(Clipboard::PdfGraphicsType))
2046 || (arg == "png" && theClipboard().hasGraphicsContents(Clipboard::PngGraphicsType))
2047 || (arg == "jpeg" && theClipboard().hasGraphicsContents(Clipboard::JpegGraphicsType))
2048 || (arg == "linkback" && theClipboard().hasGraphicsContents(Clipboard::LinkBackGraphicsType))) {
2058 case LFUN_CLIPBOARD_PASTE:
2059 enable = !theClipboard().empty();
2062 case LFUN_PRIMARY_SELECTION_PASTE:
2063 enable = cur.selection() || !theSelection().empty();
2066 case LFUN_PARAGRAPH_MOVE_UP:
2067 enable = cur.pit() > 0 && !cur.selection();
2070 case LFUN_PARAGRAPH_MOVE_DOWN:
2071 enable = cur.pit() < cur.lastpit() && !cur.selection();
2074 case LFUN_INSET_DISSOLVE:
2075 if (!cmd.argument().empty()) {
2076 InsetLayout il = cur.inset().getLayout(cur.buffer().params());
2077 enable = cur.inset().lyxCode() == FLEX_CODE
2078 && il.lyxtype == to_utf8(cmd.argument());
2080 enable = !isMainText(cur.bv().buffer())
2081 && cur.inset().nargs() == 1;
2085 case LFUN_CHANGE_ACCEPT:
2086 case LFUN_CHANGE_REJECT:
2087 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
2088 // In principle, these LFUNs should only be enabled if there
2089 // is a change at the current position/in the current selection.
2090 // However, without proper optimizations, this will inevitably
2091 // result in unacceptable performance - just imagine a user who
2092 // wants to select the complete content of a long document.
2096 case LFUN_OUTLINE_UP:
2097 case LFUN_OUTLINE_DOWN:
2098 case LFUN_OUTLINE_IN:
2099 case LFUN_OUTLINE_OUT:
2100 enable = (cur.paragraph().layout()->toclevel != Layout::NOT_IN_TOC);
2103 case LFUN_WORD_DELETE_FORWARD:
2104 case LFUN_WORD_DELETE_BACKWARD:
2105 case LFUN_LINE_DELETE:
2106 case LFUN_WORD_FORWARD:
2107 case LFUN_WORD_BACKWARD:
2108 case LFUN_WORD_RIGHT:
2109 case LFUN_WORD_LEFT:
2110 case LFUN_CHAR_FORWARD:
2111 case LFUN_CHAR_FORWARD_SELECT:
2112 case LFUN_CHAR_BACKWARD:
2113 case LFUN_CHAR_BACKWARD_SELECT:
2114 case LFUN_CHAR_LEFT:
2115 case LFUN_CHAR_LEFT_SELECT:
2116 case LFUN_CHAR_RIGHT:
2117 case LFUN_CHAR_RIGHT_SELECT:
2119 case LFUN_UP_SELECT:
2121 case LFUN_DOWN_SELECT:
2122 case LFUN_PARAGRAPH_UP_SELECT:
2123 case LFUN_PARAGRAPH_DOWN_SELECT:
2124 case LFUN_SCREEN_UP_SELECT:
2125 case LFUN_SCREEN_DOWN_SELECT:
2126 case LFUN_LINE_BEGIN_SELECT:
2127 case LFUN_LINE_END_SELECT:
2128 case LFUN_WORD_FORWARD_SELECT:
2129 case LFUN_WORD_BACKWARD_SELECT:
2130 case LFUN_WORD_RIGHT_SELECT:
2131 case LFUN_WORD_LEFT_SELECT:
2132 case LFUN_WORD_SELECT:
2133 case LFUN_PARAGRAPH_UP:
2134 case LFUN_PARAGRAPH_DOWN:
2135 case LFUN_LINE_BEGIN:
2136 case LFUN_LINE_BREAK:
2139 case LFUN_CHAR_DELETE_FORWARD:
2140 case LFUN_DELETE_FORWARD_SKIP:
2141 case LFUN_CHAR_DELETE_BACKWARD:
2142 case LFUN_DELETE_BACKWARD_SKIP:
2143 case LFUN_BREAK_PARAGRAPH:
2144 case LFUN_BREAK_PARAGRAPH_SKIP:
2145 case LFUN_PARAGRAPH_SPACING:
2146 case LFUN_INSET_INSERT:
2147 case LFUN_WORD_UPCASE:
2148 case LFUN_WORD_LOWCASE:
2149 case LFUN_WORD_CAPITALIZE:
2150 case LFUN_CHARS_TRANSPOSE:
2151 case LFUN_SERVER_GET_XY:
2152 case LFUN_SERVER_SET_XY:
2153 case LFUN_SERVER_GET_FONT:
2154 case LFUN_SERVER_GET_LAYOUT:
2156 case LFUN_DATE_INSERT:
2157 case LFUN_SELF_INSERT:
2158 case LFUN_LINE_INSERT:
2159 case LFUN_NEWPAGE_INSERT:
2160 case LFUN_PAGEBREAK_INSERT:
2161 case LFUN_CLEARPAGE_INSERT:
2162 case LFUN_CLEARDOUBLEPAGE_INSERT:
2163 case LFUN_MATH_DISPLAY:
2164 case LFUN_MATH_IMPORT_SELECTION:
2165 case LFUN_MATH_MODE:
2166 case LFUN_MATH_MACRO:
2167 case LFUN_MATH_MATRIX:
2168 case LFUN_MATH_DELIM:
2169 case LFUN_MATH_BIGDELIM:
2170 case LFUN_MATH_INSERT:
2171 case LFUN_MATH_SUBSCRIPT:
2172 case LFUN_MATH_SUPERSCRIPT:
2173 case LFUN_FONT_DEFAULT:
2174 case LFUN_FONT_UNDERLINE:
2175 case LFUN_FONT_SIZE:
2177 case LFUN_FONT_FREE_APPLY:
2178 case LFUN_FONT_FREE_UPDATE:
2179 case LFUN_LAYOUT_PARAGRAPH:
2180 case LFUN_PARAGRAPH_UPDATE:
2181 case LFUN_ACCENT_UMLAUT:
2182 case LFUN_ACCENT_CIRCUMFLEX:
2183 case LFUN_ACCENT_GRAVE:
2184 case LFUN_ACCENT_ACUTE:
2185 case LFUN_ACCENT_TILDE:
2186 case LFUN_ACCENT_CEDILLA:
2187 case LFUN_ACCENT_MACRON:
2188 case LFUN_ACCENT_DOT:
2189 case LFUN_ACCENT_UNDERDOT:
2190 case LFUN_ACCENT_UNDERBAR:
2191 case LFUN_ACCENT_CARON:
2192 case LFUN_ACCENT_SPECIAL_CARON:
2193 case LFUN_ACCENT_BREVE:
2194 case LFUN_ACCENT_TIE:
2195 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2196 case LFUN_ACCENT_CIRCLE:
2197 case LFUN_ACCENT_OGONEK:
2198 case LFUN_THESAURUS_ENTRY:
2199 case LFUN_PARAGRAPH_PARAMS_APPLY:
2200 case LFUN_PARAGRAPH_PARAMS:
2202 case LFUN_BUFFER_END:
2203 case LFUN_BUFFER_BEGIN:
2204 case LFUN_BUFFER_BEGIN_SELECT:
2205 case LFUN_BUFFER_END_SELECT:
2206 case LFUN_UNICODE_INSERT:
2207 // these are handled in our dispatch()
2216 && (cur.empty() || !cur.inset().insetAllowed(code)))
2219 flag.enabled(enable);
2224 void Text::pasteString(Cursor & cur, docstring const & clip,
2227 cur.clearSelection();
2228 if (!clip.empty()) {
2231 insertStringAsParagraphs(cur, clip);
2233 insertStringAsLines(cur, clip);