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 Inset * inset = createInset(cur.bv().buffer(), cmd);
189 if (InsetCollapsable * ci = inset->asInsetCollapsable())
190 ci->setLayout(cur.bv().buffer().params());
193 if (cmd.action == LFUN_INDEX_INSERT) {
194 docstring ds = subst(text->getStringToIndex(cur), '\n', ' ');
195 text->insertInset(cur, inset);
197 inset->edit(cur, true);
198 // Now put this into inset
199 static_cast<InsetCollapsable *>(inset)->text_.insertStringAsParagraphs(cur, ds);
202 if (cur.selection()) {
203 lyx::dispatch(FuncRequest(LFUN_CUT));
206 text->insertInset(cur, inset);
209 inset->edit(cur, true);
211 if (gotsel && pastesel) {
212 lyx::dispatch(FuncRequest(LFUN_PASTE, "0"));
213 InsetText * insetText = dynamic_cast<InsetText *>(inset);
214 if (insetText && !insetText->allowMultiPar()
215 || cur.lastpit() == 0) {
216 // reset first par to default
217 LayoutPtr const layout =
218 cur.buffer().params().getTextClass().defaultLayout();
219 cur.text()->paragraphs().begin()->layout(layout);
222 // Merge multiple paragraphs -- hack
223 while (cur.lastpit() > 0) {
224 mergeParagraph(cur.buffer().params(),
225 cur.text()->paragraphs(), 0);
228 // reset surrounding par to default
229 docstring const layoutname =
230 cur.buffer().params().getTextClass().defaultLayoutName();
231 cur.leaveInset(*inset);
232 text->setLayout(cur, layoutname);
240 string const freefont2string()
242 return freefont.toString(toggleall);
246 /// the type of outline operation
248 OutlineUp, // Move this header with text down
249 OutlineDown, // Move this header with text up
250 OutlineIn, // Make this header deeper
251 OutlineOut // Make this header shallower
255 static void outline(OutlineOp mode, Cursor & cur)
257 Buffer & buf = cur.buffer();
258 pit_type & pit = cur.pit();
259 ParagraphList & pars = buf.text().paragraphs();
260 ParagraphList::iterator bgn = pars.begin();
261 // The first paragraph of the area to be copied:
262 ParagraphList::iterator start = boost::next(bgn, pit);
263 // The final paragraph of area to be copied:
264 ParagraphList::iterator finish = start;
265 ParagraphList::iterator end = pars.end();
267 TextClass::const_iterator lit =
268 buf.params().getTextClass().begin();
269 TextClass::const_iterator const lend =
270 buf.params().getTextClass().end();
272 int const thistoclevel = start->layout()->toclevel;
276 // Move out (down) from this section header
279 // Seek the one (on same level) below
280 for (; finish != end; ++finish) {
281 toclevel = finish->layout()->toclevel;
282 if (toclevel != Layout::NOT_IN_TOC
283 && toclevel <= thistoclevel) {
287 ParagraphList::iterator dest = start;
288 // Move out (up) from this header
291 // Search previous same-level header above
294 toclevel = dest->layout()->toclevel;
296 && (toclevel == Layout::NOT_IN_TOC
297 || toclevel > thistoclevel));
298 // Not found; do nothing
299 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
301 pit_type const newpit = distance(bgn, dest);
302 pit_type const len = distance(start, finish);
303 pit_type const deletepit = pit + len;
304 buf.undo().recordUndo(cur, ATOMIC_UNDO, newpit, deletepit - 1);
305 pars.insert(dest, start, finish);
306 start = boost::next(pars.begin(), deletepit);
308 pars.erase(start, finish);
312 // Go down out of current header:
315 // Find next same-level header:
316 for (; finish != end; ++finish) {
317 toclevel = finish->layout()->toclevel;
318 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
321 ParagraphList::iterator dest = finish;
322 // Go one down from *this* header:
327 // Go further down to find header to insert in front of:
328 for (; dest != end; ++dest) {
329 toclevel = dest->layout()->toclevel;
330 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
333 // One such was found:
334 pit_type newpit = distance(bgn, dest);
335 pit_type const len = distance(start, finish);
336 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, newpit - 1);
337 pars.insert(dest, start, finish);
338 start = boost::next(bgn, pit);
340 pars.erase(start, finish);
344 buf.undo().recordUndo(cur);
345 for (; lit != lend; ++lit) {
346 if ((*lit)->toclevel == thistoclevel + 1 &&
347 start->layout()->labeltype == (*lit)->labeltype) {
348 start->layout((*lit));
354 buf.undo().recordUndo(cur);
355 for (; lit != lend; ++lit) {
356 if ((*lit)->toclevel == thistoclevel - 1 &&
357 start->layout()->labeltype == (*lit)->labeltype) {
358 start->layout((*lit));
369 void Text::number(Cursor & cur)
371 FontInfo font = ignore_font;
372 font.setNumber(FONT_TOGGLE);
373 toggleAndShow(cur, this, Font(font));
377 bool Text::isRTL(Buffer const & buffer, Paragraph const & par) const
379 return par.isRTL(buffer.params());
383 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
385 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
387 // FIXME: We use the update flag to indicates wether a singlePar or a
388 // full screen update is needed. We reset it here but shall we restore it
392 BOOST_ASSERT(cur.text() == this);
393 BufferView * bv = &cur.bv();
394 TextMetrics & tm = cur.bv().textMetrics(this);
395 CursorSlice oldTopSlice = cur.top();
396 bool oldBoundary = cur.boundary();
397 bool sel = cur.selection();
398 // Signals that, even if needsUpdate == false, an update of the
399 // cursor paragraph is required
400 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
401 LyXAction::SingleParUpdate);
402 // Signals that a full-screen update is required
403 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
404 LyXAction::NoUpdate) || singleParUpdate);
405 // Remember the old paragraph metric (_outer_ paragraph!)
406 ParagraphMetrics const & pm = cur.bv().parMetrics(
407 cur.bottom().text(), cur.bottom().pit());
408 Dimension olddim = pm.dim();
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 //FIXME: for visual cursor, really move left
533 if (reverseDirectionNeeded(cur)) {
534 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
535 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
537 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
538 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
543 case LFUN_CHAR_RIGHT:
544 case LFUN_CHAR_RIGHT_SELECT:
545 //FIXME: for visual cursor, really move right
546 if (reverseDirectionNeeded(cur)) {
547 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
548 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
550 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
551 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
557 case LFUN_DOWN_SELECT:
560 // stop/start the selection
561 bool select = cmd.action == LFUN_DOWN_SELECT ||
562 cmd.action == LFUN_UP_SELECT;
563 cur.selHandle(select);
565 // move cursor up/down
566 bool up = cmd.action == LFUN_UP_SELECT || cmd.action == LFUN_UP;
567 bool const successful = cur.upDownInText(up, needsUpdate);
569 // notify insets which were left and get their update flags
570 notifyCursorLeaves(cur.beforeDispatchCursor(), cur);
573 // redraw if you leave mathed (for the decorations)
574 needsUpdate |= cur.beforeDispatchCursor().inMathed();
581 case LFUN_PARAGRAPH_UP:
582 case LFUN_PARAGRAPH_UP_SELECT:
583 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
584 needsUpdate |= cursorUpParagraph(cur);
587 case LFUN_PARAGRAPH_DOWN:
588 case LFUN_PARAGRAPH_DOWN_SELECT:
589 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
590 needsUpdate |= cursorDownParagraph(cur);
593 case LFUN_SCREEN_UP_SELECT:
594 needsUpdate |= cur.selHandle(true);
595 if (cur.pit() == 0 && cur.textRow().pos() == 0)
598 tm.cursorPrevious(cur);
602 case LFUN_SCREEN_DOWN_SELECT:
603 needsUpdate |= cur.selHandle(true);
604 if (cur.pit() == cur.lastpit()
605 && cur.textRow().endpos() == cur.lastpos())
612 case LFUN_LINE_BEGIN:
613 case LFUN_LINE_BEGIN_SELECT:
614 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_BEGIN_SELECT);
615 needsUpdate |= tm.cursorHome(cur);
619 case LFUN_LINE_END_SELECT:
620 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
621 needsUpdate |= tm.cursorEnd(cur);
624 case LFUN_WORD_RIGHT:
625 case LFUN_WORD_RIGHT_SELECT:
626 //FIXME: for visual cursor mode, really move right
627 if (reverseDirectionNeeded(cur)) {
628 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
629 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
631 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
632 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
637 case LFUN_WORD_FORWARD:
638 case LFUN_WORD_FORWARD_SELECT:
639 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT);
640 needsUpdate |= cursorForwardOneWord(cur);
644 case LFUN_WORD_LEFT_SELECT:
645 //FIXME: for visual cursor mode, really move left
646 if (reverseDirectionNeeded(cur)) {
647 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
648 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
650 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
651 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
656 case LFUN_WORD_BACKWARD:
657 case LFUN_WORD_BACKWARD_SELECT:
658 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT);
659 needsUpdate |= cursorBackwardOneWord(cur);
662 case LFUN_WORD_SELECT: {
663 selectWord(cur, WHOLE_WORD);
664 finishChange(cur, true);
668 case LFUN_NEW_LINE: {
669 // Not allowed by LaTeX (labels or empty par)
670 if (cur.pos() > cur.paragraph().beginOfBody()) {
671 // this avoids a double undo
672 // FIXME: should not be needed, ideally
673 if (!cur.selection())
675 cap::replaceSelection(cur);
676 cur.insert(new InsetNewline);
678 moveCursor(cur, false);
683 case LFUN_LINE_BREAK: {
684 // Not allowed by LaTeX (labels or empty par)
685 if (cur.pos() > cur.paragraph().beginOfBody()) {
686 // this avoids a double undo
687 // FIXME: should not be needed, ideally
688 if (!cur.selection())
690 cap::replaceSelection(cur);
691 cur.insert(new InsetLinebreak);
693 moveCursor(cur, false);
698 case LFUN_CHAR_DELETE_FORWARD:
699 if (!cur.selection()) {
700 if (cur.pos() == cur.paragraph().size())
701 // Par boundary, force full-screen update
702 singleParUpdate = false;
703 needsUpdate |= erase(cur);
705 // It is possible to make it a lot faster still
706 // just comment out the line below...
708 cutSelection(cur, true, false);
709 singleParUpdate = false;
711 moveCursor(cur, false);
714 case LFUN_DELETE_FORWARD_SKIP:
715 // Reverse the effect of LFUN_BREAK_PARAGRAPH_SKIP.
716 if (!cur.selection()) {
717 if (cur.pos() == cur.lastpos()) {
724 cutSelection(cur, true, false);
729 case LFUN_CHAR_DELETE_BACKWARD:
730 if (!cur.selection()) {
731 if (bv->getIntl().getTransManager().backspace()) {
732 // Par boundary, full-screen update
734 singleParUpdate = false;
735 needsUpdate |= backspace(cur);
737 // It is possible to make it a lot faster still
738 // just comment out the line below...
741 cutSelection(cur, true, false);
742 singleParUpdate = false;
746 case LFUN_DELETE_BACKWARD_SKIP:
747 // Reverse the effect of LFUN_BREAK_PARAGRAPH_SKIP.
748 if (!cur.selection()) {
750 //CursorSlice cur = cursor();
754 cutSelection(cur, true, false);
758 case LFUN_BREAK_PARAGRAPH:
759 cap::replaceSelection(cur);
760 breakParagraph(cur, cmd.argument() == "inverse");
764 case LFUN_BREAK_PARAGRAPH_SKIP: {
765 // When at the beginning of a paragraph, remove
766 // indentation. Otherwise, do the same as LFUN_BREAK_PARAGRAPH.
767 cap::replaceSelection(cur);
769 cur.paragraph().params().labelWidthString(docstring());
771 breakParagraph(cur, false);
777 // With the creation of LFUN_PARAGRAPH_PARAMS, this is now redundant,
778 // as its duties can be performed there. Should it be removed??
779 // FIXME For now, it can just dispatch LFUN_PARAGRAPH_PARAMS...
780 case LFUN_PARAGRAPH_SPACING: {
781 Paragraph & par = cur.paragraph();
782 Spacing::Space cur_spacing = par.params().spacing().getSpace();
783 string cur_value = "1.0";
784 if (cur_spacing == Spacing::Other)
785 cur_value = par.params().spacing().getValueAsString();
787 istringstream is(to_utf8(cmd.argument()));
790 Spacing::Space new_spacing = cur_spacing;
791 string new_value = cur_value;
793 lyxerr << "Missing argument to `paragraph-spacing'"
795 } else if (tmp == "single") {
796 new_spacing = Spacing::Single;
797 } else if (tmp == "onehalf") {
798 new_spacing = Spacing::Onehalf;
799 } else if (tmp == "double") {
800 new_spacing = Spacing::Double;
801 } else if (tmp == "other") {
802 new_spacing = Spacing::Other;
803 string tmpval = "0.0";
805 lyxerr << "new_value = " << tmpval << endl;
808 } else if (tmp == "default") {
809 new_spacing = Spacing::Default;
811 lyxerr << to_utf8(_("Unknown spacing argument: "))
812 << to_utf8(cmd.argument()) << endl;
814 if (cur_spacing != new_spacing || cur_value != new_value)
815 par.params().spacing(Spacing(new_spacing, new_value));
819 case LFUN_INSET_INSERT: {
821 Inset * inset = createInset(bv->buffer(), cmd);
823 // FIXME (Abdel 01/02/2006):
824 // What follows would be a partial fix for bug 2154:
825 // http://bugzilla.lyx.org/show_bug.cgi?id=2154
826 // This automatically put the label inset _after_ a
827 // numbered section. It should be possible to extend the mechanism
828 // to any kind of LateX environement.
829 // The correct way to fix that bug would be at LateX generation.
830 // I'll let the code here for reference as it could be used for some
831 // other feature like "automatic labelling".
833 Paragraph & par = pars_[cur.pit()];
834 if (inset->lyxCode() == LABEL_CODE
835 && par.layout()->labeltype == LABEL_COUNTER) {
836 // Go to the end of the paragraph
837 // Warning: Because of Change-Tracking, the last
838 // position is 'size()' and not 'size()-1':
839 cur.pos() = par.size();
840 // Insert a new paragraph
841 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
846 cutSelection(cur, true, false);
847 insertInset(cur, inset);
853 case LFUN_INSET_DISSOLVE:
854 needsUpdate |= dissolveInset(cur);
857 case LFUN_INSET_SETTINGS:
858 cur.inset().showInsetDialog(bv);
861 case LFUN_SPACE_INSERT:
862 if (cur.paragraph().layout()->free_spacing)
863 insertChar(cur, ' ');
865 doInsertInset(cur, this, cmd, false, false);
868 moveCursor(cur, false);
871 case LFUN_SPECIALCHAR_INSERT: {
872 string const name = to_utf8(cmd.argument());
873 if (name == "hyphenation")
874 specialChar(cur, InsetSpecialChar::HYPHENATION);
875 else if (name == "ligature-break")
876 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
877 else if (name == "slash")
878 specialChar(cur, InsetSpecialChar::SLASH);
879 else if (name == "nobreakdash")
880 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
881 else if (name == "dots")
882 specialChar(cur, InsetSpecialChar::LDOTS);
883 else if (name == "end-of-sentence")
884 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
885 else if (name == "menu-separator")
886 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
887 else if (name.empty())
888 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
890 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
894 case LFUN_WORD_UPCASE:
895 changeCase(cur, text_uppercase);
898 case LFUN_WORD_LOWCASE:
899 changeCase(cur, text_lowercase);
902 case LFUN_WORD_CAPITALIZE:
903 changeCase(cur, text_capitalization);
906 case LFUN_CHARS_TRANSPOSE:
911 cur.message(_("Paste"));
912 cap::replaceSelection(cur);
915 string const arg = to_utf8(cmd.argument());
917 if (theClipboard().isInternal())
918 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
919 else if (theClipboard().hasGraphicsContents())
920 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
922 pasteClipboardText(cur, bv->buffer().errorList("Paste"));
923 } else if (isStrUnsignedInt(arg)) {
924 // we have a numerical argument
925 pasteFromStack(cur, bv->buffer().errorList("Paste"),
926 convert<unsigned int>(arg));
928 Clipboard::GraphicsType type;
930 type = Clipboard::PdfGraphicsType;
931 else if (arg == "png")
932 type = Clipboard::PngGraphicsType;
933 else if (arg == "jpeg")
934 type = Clipboard::JpegGraphicsType;
935 else if (arg == "linkback")
936 type = Clipboard::LinkBackGraphicsType;
940 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
943 bv->buffer().errors("Paste");
944 cur.clearSelection(); // bug 393
950 cutSelection(cur, true, true);
951 cur.message(_("Cut"));
956 cur.message(_("Copy"));
959 case LFUN_SERVER_GET_XY:
960 cur.message(from_utf8(
961 convert<string>(tm.cursorX(cur.top(), cur.boundary()))
962 + ' ' + convert<string>(tm.cursorY(cur.top(), cur.boundary()))));
965 case LFUN_SERVER_SET_XY: {
968 istringstream is(to_utf8(cmd.argument()));
971 lyxerr << "SETXY: Could not parse coordinates in '"
972 << to_utf8(cmd.argument()) << endl;
974 tm.setCursorFromCoordinates(cur, x, y);
978 case LFUN_SERVER_GET_FONT:
979 if (cur.current_font.fontInfo().shape() == ITALIC_SHAPE)
980 cur.message(from_ascii("E"));
981 else if (cur.current_font.fontInfo().shape() == SMALLCAPS_SHAPE)
982 cur.message(from_ascii("N"));
984 cur.message(from_ascii("0"));
987 case LFUN_SERVER_GET_LAYOUT:
988 cur.message(cur.paragraph().layout()->name());
992 docstring layout = cmd.argument();
993 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
995 docstring const old_layout = cur.paragraph().layout()->name();
997 // Derive layout number from given argument (string)
998 // and current buffer's textclass (number)
999 TextClass const & tclass = bv->buffer().params().getTextClass();
1001 layout = tclass.defaultLayoutName();
1002 bool hasLayout = tclass.hasLayout(layout);
1004 // If the entry is obsolete, use the new one instead.
1006 docstring const & obs = tclass[layout]->obsoleted_by();
1012 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1013 from_utf8(N_(" not known")));
1017 bool change_layout = (old_layout != layout);
1019 if (!change_layout && cur.selection() &&
1020 cur.selBegin().pit() != cur.selEnd().pit())
1022 pit_type spit = cur.selBegin().pit();
1023 pit_type epit = cur.selEnd().pit() + 1;
1024 while (spit != epit) {
1025 if (pars_[spit].layout()->name() != old_layout) {
1026 change_layout = true;
1034 setLayout(cur, layout);
1039 case LFUN_CLIPBOARD_PASTE:
1040 cur.clearSelection();
1041 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1042 cmd.argument() == "paragraph");
1043 bv->buffer().errors("Paste");
1046 case LFUN_PRIMARY_SELECTION_PASTE:
1047 pasteString(cur, theSelection().get(),
1048 cmd.argument() == "paragraph");
1051 case LFUN_UNICODE_INSERT: {
1052 if (cmd.argument().empty())
1054 docstring hexstring = cmd.argument();
1055 if (isHex(hexstring)) {
1056 char_type c = hexToInt(hexstring);
1057 if (c >= 32 && c < 0x10ffff) {
1058 lyxerr << "Inserting c: " << c << endl;
1059 docstring s = docstring(1, c);
1060 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1066 case LFUN_QUOTE_INSERT: {
1067 Paragraph & par = cur.paragraph();
1068 pos_type pos = cur.pos();
1069 BufferParams const & bufparams = bv->buffer().params();
1070 LayoutPtr const & style = par.layout();
1071 if (!style->pass_thru
1072 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1073 // this avoids a double undo
1074 // FIXME: should not be needed, ideally
1075 if (!cur.selection())
1077 cap::replaceSelection(cur);
1082 else if (cur.prevInset() && cur.prevInset()->isSpace())
1085 c = par.getChar(pos - 1);
1086 string arg = to_utf8(cmd.argument());
1087 if (arg == "single")
1088 cur.insert(new InsetQuotes(c,
1089 bufparams.quotes_language,
1090 InsetQuotes::SingleQ));
1092 cur.insert(new InsetQuotes(c,
1093 bufparams.quotes_language,
1094 InsetQuotes::DoubleQ));
1098 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1102 case LFUN_DATE_INSERT: {
1103 string const format = cmd.argument().empty()
1104 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1105 string const time = formatted_time(current_time(), format);
1106 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1110 case LFUN_MOUSE_TRIPLE:
1111 if (cmd.button() == mouse_button::button1) {
1120 case LFUN_MOUSE_DOUBLE:
1121 if (cmd.button() == mouse_button::button1) {
1122 selectWord(cur, WHOLE_WORD_STRICT);
1127 // Single-click on work area
1128 case LFUN_MOUSE_PRESS: {
1129 // Right click on a footnote flag opens float menu
1130 // FIXME: Why should we clear the selection in this case?
1131 if (cmd.button() == mouse_button::button3)
1132 cur.clearSelection();
1134 bool do_selection = cmd.button() == mouse_button::button1
1135 && cmd.argument() == "region-select";
1137 bool update = bv->mouseSetCursor(cur, do_selection);
1139 // Insert primary selection with middle mouse
1140 // if there is a local selection in the current buffer,
1142 if (cmd.button() == mouse_button::button2) {
1143 if (cap::selection()) {
1144 // Copy the selection buffer to the clipboard
1145 // stack, because we want it to appear in the
1146 // "Edit->Paste recent" menu.
1147 cap::copySelectionToStack();
1149 cap::pasteSelection(bv->cursor(),
1150 bv->buffer().errorList("Paste"));
1151 bv->buffer().errors("Paste");
1152 bv->buffer().markDirty();
1153 bv->cursor().finishUndo();
1155 lyx::dispatch(FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph"));
1159 // we have to update after dEPM triggered
1160 if (!update && cmd.button() == mouse_button::button1) {
1161 needsUpdate = false;
1168 case LFUN_MOUSE_MOTION: {
1169 // Only use motion with button 1
1170 //if (cmd.button() != mouse_button::button1)
1173 // ignore motions deeper nested than the real anchor
1174 Cursor & bvcur = cur.bv().cursor();
1175 if (!bvcur.anchor_.hasPart(cur)) {
1179 CursorSlice old = bvcur.top();
1181 int const wh = bv->workHeight();
1182 int const y = max(0, min(wh - 1, cmd.y));
1184 tm.setCursorFromCoordinates(cur, cmd.x, y);
1185 cur.setTargetX(cmd.x);
1187 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1189 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1190 // This is to allow jumping over large insets
1191 if (cur.top() == old) {
1193 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1195 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1198 if (cur.top() == old)
1201 // FIXME: This is brute force! But without it the selected
1202 // area is not corrected updated while moving the mouse.
1203 cur.updateFlags(Update::Force | Update::FitCursor);
1204 // don't set anchor_
1205 bvcur.setCursor(cur);
1206 bvcur.selection() = true;
1207 //lyxerr << "MOTION: " << bv->cursor() << endl;
1212 case LFUN_MOUSE_RELEASE: {
1213 if (cmd.button() == mouse_button::button2)
1216 if (cmd.button() == mouse_button::button1) {
1217 // if there is new selection, update persistent
1218 // selection, otherwise, single click does not
1219 // clear persistent selection buffer
1220 if (cur.selection()) {
1222 // if double click, cur is moved to the end of word by selectWord
1223 // but bvcur is current mouse position
1224 Cursor & bvcur = cur.bv().cursor();
1225 bvcur.selection() = true;
1227 needsUpdate = false;
1234 case LFUN_SELF_INSERT: {
1235 if (cmd.argument().empty())
1238 // Automatically delete the currently selected
1239 // text and replace it with what is being
1240 // typed in now. Depends on lyxrc settings
1241 // "auto_region_delete", which defaults to
1244 if (lyxrc.auto_region_delete && cur.selection())
1245 cutSelection(cur, false, false);
1247 cur.clearSelection();
1248 Font const old_font = cur.real_current_font;
1250 docstring::const_iterator cit = cmd.argument().begin();
1251 docstring::const_iterator end = cmd.argument().end();
1252 for (; cit != end; ++cit)
1253 bv->translateAndInsert(*cit, this, cur);
1256 moveCursor(cur, false);
1260 case LFUN_HYPERLINK_INSERT: {
1261 InsetCommandParams p(HYPERLINK_CODE);
1263 if (cur.selection()) {
1264 content = cur.selectionAsString(false);
1265 cutSelection(cur, true, false);
1267 p["target"] = (cmd.argument().empty()) ?
1268 content : cmd.argument();
1269 string const data = InsetCommandMailer::params2string("href", p);
1270 if (p["target"].empty()) {
1271 bv->showDialog("href", data);
1273 FuncRequest fr(LFUN_INSET_INSERT, data);
1279 case LFUN_LABEL_INSERT: {
1280 InsetCommandParams p(LABEL_CODE);
1281 // Try to generate a valid label
1282 p["name"] = (cmd.argument().empty()) ?
1283 cur.getPossibleLabel() :
1285 string const data = InsetCommandMailer::params2string("label", p);
1287 if (cmd.argument().empty()) {
1288 bv->showDialog("label", data);
1290 FuncRequest fr(LFUN_INSET_INSERT, data);
1296 case LFUN_INFO_INSERT: {
1297 Inset * inset = createInset(cur.bv().buffer(), cmd);
1300 // if an empty inset is created (cmd.argument() is empty)
1301 // use current selection as parameter.
1302 if (cmd.argument().empty() && cur.selection()) {
1303 // use selected text as info to avoid a separate UI
1304 docstring ds = cur.selectionAsString(false);
1305 cutSelection(cur, true, false);
1306 static_cast<InsetInfo *>(inset)->setInfo(to_utf8(ds));
1307 static_cast<InsetInfo *>(inset)->updateInfo(cur.bv().buffer());
1309 insertInset(cur, inset);
1314 case LFUN_LIST_INSERT:
1315 case LFUN_THEOREM_INSERT:
1317 case LFUN_CAPTION_INSERT:
1318 case LFUN_FOOTNOTE_INSERT:
1319 // Open the inset, and move the current selection
1321 doInsertInset(cur, this, cmd, true, true);
1323 // These insets are numbered.
1324 updateLabels(bv->buffer());
1326 case LFUN_NOTE_INSERT:
1327 case LFUN_FLEX_INSERT:
1328 case LFUN_BOX_INSERT:
1329 case LFUN_BRANCH_INSERT:
1330 case LFUN_BIBITEM_INSERT:
1331 case LFUN_ERT_INSERT:
1332 case LFUN_LISTING_INSERT:
1333 case LFUN_MARGINALNOTE_INSERT:
1334 case LFUN_OPTIONAL_INSERT:
1335 case LFUN_ENVIRONMENT_INSERT:
1336 // Open the inset, and move the current selection
1338 doInsertInset(cur, this, cmd, true, true);
1342 case LFUN_TABULAR_INSERT:
1343 // if there were no arguments, just open the dialog
1344 if (doInsertInset(cur, this, cmd, false, true))
1347 bv->showDialog("tabularcreate");
1351 case LFUN_FLOAT_INSERT:
1352 case LFUN_FLOAT_WIDE_INSERT:
1353 case LFUN_WRAP_INSERT: {
1354 bool content = cur.selection(); // will some text be moved into the inset?
1356 doInsertInset(cur, this, cmd, true, true);
1358 ParagraphList & pars = cur.text()->paragraphs();
1360 TextClass const & tclass = bv->buffer().params().getTextClass();
1362 // add a separate paragraph for the caption inset
1363 pars.push_back(Paragraph());
1364 pars.back().setInsetOwner(pars[0].inInset());
1365 pars.back().layout(tclass.defaultLayout());
1367 int cap_pit = pars.size() - 1;
1369 // if an empty inset was created, we create an additional empty
1370 // paragraph at the bottom so that the user can choose where to put
1371 // the graphics (or table).
1373 pars.push_back(Paragraph());
1374 pars.back().setInsetOwner(pars[0].inInset());
1375 pars.back().layout(tclass.defaultLayout());
1379 // reposition the cursor to the caption
1380 cur.pit() = cap_pit;
1382 // FIXME: This Text/Cursor dispatch handling is a mess!
1383 // We cannot use Cursor::dispatch here it needs access to up to
1385 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1386 cur.text()->dispatch(cur, cmd_caption);
1387 cur.updateFlags(Update::Force);
1388 // FIXME: When leaving the Float (or Wrap) inset we should
1389 // delete any empty paragraph left above or below the
1394 case LFUN_INDEX_INSERT:
1395 doInsertInset(cur, this, cmd, true, true);
1399 case LFUN_NOMENCL_INSERT: {
1400 FuncRequest cmd1 = cmd;
1401 if (cmd.argument().empty())
1402 cmd1 = FuncRequest(cmd,
1403 bv->cursor().innerText()->getStringToIndex(bv->cursor()));
1404 Inset * inset = createInset(cur.bv().buffer(), cmd1);
1408 cur.clearSelection();
1409 insertInset(cur, inset);
1410 // Show the dialog for the nomenclature entry, since the
1411 // description entry still needs to be filled in.
1412 if (cmd.action == LFUN_NOMENCL_INSERT)
1413 inset->edit(cur, true);
1418 case LFUN_INDEX_PRINT:
1419 case LFUN_NOMENCL_PRINT:
1420 case LFUN_TOC_INSERT:
1421 case LFUN_HFILL_INSERT:
1422 case LFUN_LINE_INSERT:
1423 case LFUN_NEWPAGE_INSERT:
1424 case LFUN_PAGEBREAK_INSERT:
1425 case LFUN_CLEARPAGE_INSERT:
1426 case LFUN_CLEARDOUBLEPAGE_INSERT:
1428 doInsertInset(cur, this, cmd, false, false);
1432 case LFUN_DEPTH_DECREMENT:
1433 changeDepth(cur, DEC_DEPTH);
1436 case LFUN_DEPTH_INCREMENT:
1437 changeDepth(cur, INC_DEPTH);
1440 case LFUN_MATH_DISPLAY:
1441 mathDispatch(cur, cmd, true);
1444 case LFUN_MATH_IMPORT_SELECTION:
1445 case LFUN_MATH_MODE:
1446 if (cmd.argument() == "on")
1447 // don't pass "on" as argument
1448 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1450 mathDispatch(cur, cmd, false);
1453 case LFUN_MATH_MACRO:
1454 if (cmd.argument().empty())
1455 cur.errorMessage(from_utf8(N_("Missing argument")));
1457 string s = to_utf8(cmd.argument());
1458 string const s1 = token(s, ' ', 1);
1459 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1460 string const s2 = token(s, ' ', 2);
1461 MacroType type = MacroTypeNewcommand;
1463 type = MacroTypeDef;
1464 cur.insert(new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, false, type));
1465 //cur.nextInset()->edit(cur, true);
1469 // passthrough hat and underscore outside mathed:
1470 case LFUN_MATH_SUBSCRIPT:
1471 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1473 case LFUN_MATH_SUPERSCRIPT:
1474 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1477 case LFUN_MATH_INSERT:
1478 case LFUN_MATH_MATRIX:
1479 case LFUN_MATH_DELIM:
1480 case LFUN_MATH_BIGDELIM: {
1481 cap::replaceSelection(cur);
1482 cur.insert(new InsetMathHull(hullSimple));
1483 checkAndActivateInset(cur, true);
1484 BOOST_ASSERT(cur.inMathed());
1489 case LFUN_FONT_EMPH: {
1490 Font font(ignore_font, ignore_language);
1491 font.fontInfo().setEmph(FONT_TOGGLE);
1492 toggleAndShow(cur, this, font);
1496 case LFUN_FONT_BOLD: {
1497 Font font(ignore_font, ignore_language);
1498 font.fontInfo().setSeries(BOLD_SERIES);
1499 toggleAndShow(cur, this, font);
1503 case LFUN_FONT_NOUN: {
1504 Font font(ignore_font, ignore_language);
1505 font.fontInfo().setNoun(FONT_TOGGLE);
1506 toggleAndShow(cur, this, font);
1510 case LFUN_FONT_TYPEWRITER: {
1511 Font font(ignore_font, ignore_language);
1512 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1513 toggleAndShow(cur, this, font);
1517 case LFUN_FONT_SANS: {
1518 Font font(ignore_font, ignore_language);
1519 font.fontInfo().setFamily(SANS_FAMILY);
1520 toggleAndShow(cur, this, font);
1524 case LFUN_FONT_ROMAN: {
1525 Font font(ignore_font, ignore_language);
1526 font.fontInfo().setFamily(ROMAN_FAMILY);
1527 toggleAndShow(cur, this, font);
1531 case LFUN_FONT_DEFAULT: {
1532 Font font(inherit_font, ignore_language);
1533 toggleAndShow(cur, this, font);
1537 case LFUN_FONT_UNDERLINE: {
1538 Font font(ignore_font, ignore_language);
1539 font.fontInfo().setUnderbar(FONT_TOGGLE);
1540 toggleAndShow(cur, this, font);
1544 case LFUN_FONT_SIZE: {
1545 Font font(ignore_font, ignore_language);
1546 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1547 toggleAndShow(cur, this, font);
1551 case LFUN_LANGUAGE: {
1552 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1555 Font font(ignore_font, lang);
1556 toggleAndShow(cur, this, font);
1560 case LFUN_FONT_FREE_APPLY:
1561 toggleAndShow(cur, this, freefont, toggleall);
1562 cur.message(_("Character set"));
1565 // Set the freefont using the contents of \param data dispatched from
1566 // the frontends and apply it at the current cursor location.
1567 case LFUN_FONT_FREE_UPDATE: {
1570 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1573 toggleAndShow(cur, this, freefont, toggleall);
1574 cur.message(_("Character set"));
1576 lyxerr << "Argument not ok";
1581 case LFUN_FINISHED_LEFT:
1582 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
1583 if (reverseDirectionNeeded(cur)) {
1585 cur.setCurrentFont();
1589 case LFUN_FINISHED_RIGHT:
1590 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
1591 if (!reverseDirectionNeeded(cur)) {
1593 cur.setCurrentFont();
1597 case LFUN_FINISHED_BACKWARD:
1598 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
1601 case LFUN_FINISHED_FORWARD:
1602 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
1604 cur.setCurrentFont();
1607 case LFUN_LAYOUT_PARAGRAPH: {
1609 params2string(cur.paragraph(), data);
1610 data = "show\n" + data;
1611 bv->showDialog("paragraph", data);
1615 case LFUN_PARAGRAPH_UPDATE: {
1617 params2string(cur.paragraph(), data);
1619 // Will the paragraph accept changes from the dialog?
1620 bool const accept = !cur.inset().forceDefaultParagraphs(cur.idx());
1622 data = "update " + convert<string>(accept) + '\n' + data;
1623 bv->updateDialog("paragraph", data);
1627 case LFUN_ACCENT_UMLAUT:
1628 case LFUN_ACCENT_CIRCUMFLEX:
1629 case LFUN_ACCENT_GRAVE:
1630 case LFUN_ACCENT_ACUTE:
1631 case LFUN_ACCENT_TILDE:
1632 case LFUN_ACCENT_CEDILLA:
1633 case LFUN_ACCENT_MACRON:
1634 case LFUN_ACCENT_DOT:
1635 case LFUN_ACCENT_UNDERDOT:
1636 case LFUN_ACCENT_UNDERBAR:
1637 case LFUN_ACCENT_CARON:
1638 case LFUN_ACCENT_SPECIAL_CARON:
1639 case LFUN_ACCENT_BREVE:
1640 case LFUN_ACCENT_TIE:
1641 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1642 case LFUN_ACCENT_CIRCLE:
1643 case LFUN_ACCENT_OGONEK:
1644 theLyXFunc().handleKeyFunc(cmd.action);
1645 if (!cmd.argument().empty())
1646 // FIXME: Are all these characters encoded in one byte in utf8?
1647 bv->translateAndInsert(cmd.argument()[0], this, cur);
1650 case LFUN_FLOAT_LIST: {
1651 TextClass const & tclass = bv->buffer().params().getTextClass();
1652 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1654 if (cur.selection())
1655 cutSelection(cur, true, false);
1656 breakParagraph(cur);
1658 if (cur.lastpos() != 0) {
1659 cursorBackward(cur);
1660 breakParagraph(cur);
1663 setLayout(cur, tclass.defaultLayoutName());
1664 ParagraphParameters p;
1665 setParagraphs(cur, p);
1666 insertInset(cur, new InsetFloatList(to_utf8(cmd.argument())));
1669 lyxerr << "Non-existent float type: "
1670 << to_utf8(cmd.argument()) << endl;
1675 case LFUN_CHANGE_ACCEPT: {
1676 acceptOrRejectChanges(cur, ACCEPT);
1680 case LFUN_CHANGE_REJECT: {
1681 acceptOrRejectChanges(cur, REJECT);
1685 case LFUN_THESAURUS_ENTRY: {
1686 docstring arg = cmd.argument();
1688 arg = cur.selectionAsString(false);
1690 if (arg.size() > 100 || arg.empty()) {
1691 // Get word or selection
1692 selectWordWhenUnderCursor(cur, WHOLE_WORD);
1693 arg = cur.selectionAsString(false);
1696 bv->showDialog("thesaurus", to_utf8(arg));
1700 case LFUN_PARAGRAPH_PARAMS_APPLY: {
1701 // Given data, an encoding of the ParagraphParameters
1702 // generated in the Paragraph dialog, this function sets
1703 // the current paragraph, or currently selected paragraphs,
1705 // NOTE: This function overrides all existing settings.
1706 setParagraphs(cur, cmd.argument());
1707 cur.message(_("Paragraph layout set"));
1711 case LFUN_PARAGRAPH_PARAMS: {
1712 // Given data, an encoding of the ParagraphParameters as we'd
1713 // find them in a LyX file, this function modifies the current paragraph,
1714 // or currently selected paragraphs.
1715 // NOTE: This function only modifies, and does not override, existing
1717 setParagraphs(cur, cmd.argument(), true);
1718 cur.message(_("Paragraph layout set"));
1723 if (cur.selection()) {
1724 cur.selection() = false;
1727 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
1728 // correct, but I'm not 100% sure -- dov, 071019
1729 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1733 case LFUN_OUTLINE_UP:
1734 outline(OutlineUp, cur);
1735 setCursor(cur, cur.pit(), 0);
1736 updateLabels(cur.buffer());
1740 case LFUN_OUTLINE_DOWN:
1741 outline(OutlineDown, cur);
1742 setCursor(cur, cur.pit(), 0);
1743 updateLabels(cur.buffer());
1747 case LFUN_OUTLINE_IN:
1748 outline(OutlineIn, cur);
1749 updateLabels(cur.buffer());
1753 case LFUN_OUTLINE_OUT:
1754 outline(OutlineOut, cur);
1755 updateLabels(cur.buffer());
1760 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
1765 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
1767 // FIXME: The cursor flag is reset two lines below
1768 // so we need to check here if some of the LFUN did touch that.
1769 // for now only Text::erase() and Text::backspace() do that.
1770 // The plan is to verify all the LFUNs and then to remove this
1771 // singleParUpdate boolean altogether.
1772 if (cur.result().update() & Update::Force) {
1773 singleParUpdate = false;
1777 // FIXME: the following code should go in favor of fine grained
1778 // update flag treatment.
1779 if (singleParUpdate) {
1780 // Inserting characters does not change par height
1781 ParagraphMetrics const & pms
1782 = cur.bv().parMetrics(cur.bottom().text(), cur.bottom().pit());
1783 if (pms.dim().height() == olddim.height()) {
1784 // if so, update _only_ this paragraph
1785 cur.updateFlags(Update::SinglePar |
1793 && &oldTopSlice.inset() == &cur.inset()
1794 && oldTopSlice.idx() == cur.idx()
1795 && !sel // sel is a backup of cur.selection() at the biginning of the function.
1796 && !cur.selection())
1797 // FIXME: it would be better if we could just do this
1799 //if (cur.result().update() != Update::FitCursor)
1802 // But some LFUNs do not set Update::FitCursor when needed, so we
1803 // do it for all. This is not very harmfull as FitCursor will provoke
1804 // a full redraw only if needed but still, a proper review of all LFUN
1805 // should be done and this needsUpdate boolean can then be removed.
1806 cur.updateFlags(Update::FitCursor);
1808 cur.updateFlags(Update::Force | Update::FitCursor);
1812 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
1813 FuncStatus & flag) const
1815 BOOST_ASSERT(cur.text() == this);
1817 Font const & font = cur.real_current_font;
1818 FontInfo const & fontinfo = font.fontInfo();
1820 InsetCode code = NO_CODE;
1822 switch (cmd.action) {
1824 case LFUN_DEPTH_DECREMENT:
1825 enable = changeDepthAllowed(cur, DEC_DEPTH);
1828 case LFUN_DEPTH_INCREMENT:
1829 enable = changeDepthAllowed(cur, INC_DEPTH);
1833 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1836 case LFUN_BIBITEM_INSERT:
1837 enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO
1841 case LFUN_DIALOG_SHOW_NEW_INSET:
1842 if (cmd.argument() == "bibitem")
1843 code = BIBITEM_CODE;
1844 else if (cmd.argument() == "bibtex")
1846 else if (cmd.argument() == "box")
1848 else if (cmd.argument() == "branch")
1850 else if (cmd.argument() == "citation")
1852 else if (cmd.argument() == "ert")
1854 else if (cmd.argument() == "external")
1855 code = EXTERNAL_CODE;
1856 else if (cmd.argument() == "float")
1858 else if (cmd.argument() == "graphics")
1859 code = GRAPHICS_CODE;
1860 else if (cmd.argument() == "href")
1861 code = HYPERLINK_CODE;
1862 else if (cmd.argument() == "include")
1863 code = INCLUDE_CODE;
1864 else if (cmd.argument() == "index")
1866 else if (cmd.argument() == "nomenclature")
1867 code = NOMENCL_CODE;
1868 else if (cmd.argument() == "label")
1870 else if (cmd.argument() == "note")
1872 else if (cmd.argument() == "ref")
1874 else if (cmd.argument() == "toc")
1876 else if (cmd.argument() == "vspace")
1878 else if (cmd.argument() == "wrap")
1880 else if (cmd.argument() == "listings")
1881 code = LISTINGS_CODE;
1884 case LFUN_ERT_INSERT:
1887 case LFUN_LISTING_INSERT:
1888 code = LISTINGS_CODE;
1890 case LFUN_FOOTNOTE_INSERT:
1893 case LFUN_TABULAR_INSERT:
1894 code = TABULAR_CODE;
1896 case LFUN_MARGINALNOTE_INSERT:
1899 case LFUN_FLOAT_INSERT:
1900 case LFUN_FLOAT_WIDE_INSERT:
1903 case LFUN_WRAP_INSERT:
1906 case LFUN_FLOAT_LIST:
1907 code = FLOAT_LIST_CODE;
1910 case LFUN_LIST_INSERT:
1913 case LFUN_THEOREM_INSERT:
1914 code = THEOREM_CODE;
1917 case LFUN_CAPTION_INSERT:
1918 code = CAPTION_CODE;
1920 case LFUN_NOTE_INSERT:
1923 case LFUN_FLEX_INSERT: {
1925 string s = cmd.getArg(0);
1926 InsetLayout il = cur.buffer().params().getTextClass().insetlayout(from_utf8(s));
1927 if (il.lyxtype != "charstyle" &&
1928 il.lyxtype != "custom" &&
1929 il.lyxtype != "element" &&
1930 il.lyxtype != "standard")
1934 case LFUN_BOX_INSERT:
1937 case LFUN_BRANCH_INSERT:
1939 if (cur.buffer().masterBuffer()->params().branchlist().empty())
1942 case LFUN_LABEL_INSERT:
1945 case LFUN_INFO_INSERT:
1948 case LFUN_OPTIONAL_INSERT:
1950 enable = cur.paragraph().insetList().count(OPTARG_CODE)
1951 < cur.paragraph().layout()->optionalargs;
1953 case LFUN_ENVIRONMENT_INSERT:
1956 case LFUN_INDEX_INSERT:
1959 case LFUN_INDEX_PRINT:
1960 code = INDEX_PRINT_CODE;
1962 case LFUN_NOMENCL_INSERT:
1963 code = NOMENCL_CODE;
1965 case LFUN_NOMENCL_PRINT:
1966 code = NOMENCL_PRINT_CODE;
1968 case LFUN_TOC_INSERT:
1971 case LFUN_HYPERLINK_INSERT:
1972 code = HYPERLINK_CODE;
1974 case LFUN_QUOTE_INSERT:
1975 // always allow this, since we will inset a raw quote
1976 // if an inset is not allowed.
1978 case LFUN_HFILL_INSERT:
1979 case LFUN_SPECIALCHAR_INSERT:
1980 code = SPECIALCHAR_CODE;
1982 case LFUN_SPACE_INSERT:
1983 // slight hack: we know this is allowed in math mode
1988 case LFUN_INSET_MODIFY:
1989 // We need to disable this, because we may get called for a
1991 // InsetTabular::getStatus() -> InsetText::getStatus()
1992 // and we don't handle LFUN_INSET_MODIFY.
1996 case LFUN_FONT_EMPH:
1997 flag.setOnOff(fontinfo.emph() == FONT_ON);
2000 case LFUN_FONT_NOUN:
2001 flag.setOnOff(fontinfo.noun() == FONT_ON);
2004 case LFUN_FONT_BOLD:
2005 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2008 case LFUN_FONT_SANS:
2009 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2012 case LFUN_FONT_ROMAN:
2013 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2016 case LFUN_FONT_TYPEWRITER:
2017 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2022 enable = cur.selection();
2026 if (cmd.argument().empty()) {
2027 if (theClipboard().isInternal())
2028 enable = cap::numberOfSelections() > 0;
2030 enable = !theClipboard().empty();
2034 // we have an argument
2035 string const arg = to_utf8(cmd.argument());
2036 if (isStrUnsignedInt(arg)) {
2037 // it's a number and therefore means the internal stack
2038 unsigned int n = convert<unsigned int>(arg);
2039 enable = cap::numberOfSelections() > n;
2043 // explicit graphics type?
2044 if ((arg == "pdf" && theClipboard().hasGraphicsContents(Clipboard::PdfGraphicsType))
2045 || (arg == "png" && theClipboard().hasGraphicsContents(Clipboard::PngGraphicsType))
2046 || (arg == "jpeg" && theClipboard().hasGraphicsContents(Clipboard::JpegGraphicsType))
2047 || (arg == "linkback" && theClipboard().hasGraphicsContents(Clipboard::LinkBackGraphicsType))) {
2057 case LFUN_CLIPBOARD_PASTE:
2058 enable = !theClipboard().empty();
2061 case LFUN_PRIMARY_SELECTION_PASTE:
2062 enable = cur.selection() || !theSelection().empty();
2065 case LFUN_PARAGRAPH_MOVE_UP:
2066 enable = cur.pit() > 0 && !cur.selection();
2069 case LFUN_PARAGRAPH_MOVE_DOWN:
2070 enable = cur.pit() < cur.lastpit() && !cur.selection();
2073 case LFUN_INSET_DISSOLVE:
2074 if (!cmd.argument().empty()) {
2075 InsetLayout il = cur.inset().getLayout(cur.buffer().params());
2076 enable = cur.inset().lyxCode() == FLEX_CODE
2077 && il.lyxtype == to_utf8(cmd.argument());
2079 enable = !isMainText(cur.bv().buffer())
2080 && cur.inset().nargs() == 1;
2084 case LFUN_CHANGE_ACCEPT:
2085 case LFUN_CHANGE_REJECT:
2086 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
2087 // In principle, these LFUNs should only be enabled if there
2088 // is a change at the current position/in the current selection.
2089 // However, without proper optimizations, this will inevitably
2090 // result in unacceptable performance - just imagine a user who
2091 // wants to select the complete content of a long document.
2095 case LFUN_OUTLINE_UP:
2096 case LFUN_OUTLINE_DOWN:
2097 case LFUN_OUTLINE_IN:
2098 case LFUN_OUTLINE_OUT:
2099 enable = (cur.paragraph().layout()->toclevel != Layout::NOT_IN_TOC);
2102 case LFUN_WORD_DELETE_FORWARD:
2103 case LFUN_WORD_DELETE_BACKWARD:
2104 case LFUN_LINE_DELETE:
2105 case LFUN_WORD_FORWARD:
2106 case LFUN_WORD_BACKWARD:
2107 case LFUN_WORD_RIGHT:
2108 case LFUN_WORD_LEFT:
2109 case LFUN_CHAR_FORWARD:
2110 case LFUN_CHAR_FORWARD_SELECT:
2111 case LFUN_CHAR_BACKWARD:
2112 case LFUN_CHAR_BACKWARD_SELECT:
2113 case LFUN_CHAR_LEFT:
2114 case LFUN_CHAR_LEFT_SELECT:
2115 case LFUN_CHAR_RIGHT:
2116 case LFUN_CHAR_RIGHT_SELECT:
2118 case LFUN_UP_SELECT:
2120 case LFUN_DOWN_SELECT:
2121 case LFUN_PARAGRAPH_UP_SELECT:
2122 case LFUN_PARAGRAPH_DOWN_SELECT:
2123 case LFUN_SCREEN_UP_SELECT:
2124 case LFUN_SCREEN_DOWN_SELECT:
2125 case LFUN_LINE_BEGIN_SELECT:
2126 case LFUN_LINE_END_SELECT:
2127 case LFUN_WORD_FORWARD_SELECT:
2128 case LFUN_WORD_BACKWARD_SELECT:
2129 case LFUN_WORD_RIGHT_SELECT:
2130 case LFUN_WORD_LEFT_SELECT:
2131 case LFUN_WORD_SELECT:
2132 case LFUN_PARAGRAPH_UP:
2133 case LFUN_PARAGRAPH_DOWN:
2134 case LFUN_LINE_BEGIN:
2135 case LFUN_LINE_BREAK:
2138 case LFUN_CHAR_DELETE_FORWARD:
2139 case LFUN_DELETE_FORWARD_SKIP:
2140 case LFUN_CHAR_DELETE_BACKWARD:
2141 case LFUN_DELETE_BACKWARD_SKIP:
2142 case LFUN_BREAK_PARAGRAPH:
2143 case LFUN_BREAK_PARAGRAPH_SKIP:
2144 case LFUN_PARAGRAPH_SPACING:
2145 case LFUN_INSET_INSERT:
2146 case LFUN_WORD_UPCASE:
2147 case LFUN_WORD_LOWCASE:
2148 case LFUN_WORD_CAPITALIZE:
2149 case LFUN_CHARS_TRANSPOSE:
2150 case LFUN_SERVER_GET_XY:
2151 case LFUN_SERVER_SET_XY:
2152 case LFUN_SERVER_GET_FONT:
2153 case LFUN_SERVER_GET_LAYOUT:
2155 case LFUN_DATE_INSERT:
2156 case LFUN_SELF_INSERT:
2157 case LFUN_LINE_INSERT:
2158 case LFUN_NEWPAGE_INSERT:
2159 case LFUN_PAGEBREAK_INSERT:
2160 case LFUN_CLEARPAGE_INSERT:
2161 case LFUN_CLEARDOUBLEPAGE_INSERT:
2162 case LFUN_MATH_DISPLAY:
2163 case LFUN_MATH_IMPORT_SELECTION:
2164 case LFUN_MATH_MODE:
2165 case LFUN_MATH_MACRO:
2166 case LFUN_MATH_MATRIX:
2167 case LFUN_MATH_DELIM:
2168 case LFUN_MATH_BIGDELIM:
2169 case LFUN_MATH_INSERT:
2170 case LFUN_MATH_SUBSCRIPT:
2171 case LFUN_MATH_SUPERSCRIPT:
2172 case LFUN_FONT_DEFAULT:
2173 case LFUN_FONT_UNDERLINE:
2174 case LFUN_FONT_SIZE:
2176 case LFUN_FONT_FREE_APPLY:
2177 case LFUN_FONT_FREE_UPDATE:
2178 case LFUN_LAYOUT_PARAGRAPH:
2179 case LFUN_PARAGRAPH_UPDATE:
2180 case LFUN_ACCENT_UMLAUT:
2181 case LFUN_ACCENT_CIRCUMFLEX:
2182 case LFUN_ACCENT_GRAVE:
2183 case LFUN_ACCENT_ACUTE:
2184 case LFUN_ACCENT_TILDE:
2185 case LFUN_ACCENT_CEDILLA:
2186 case LFUN_ACCENT_MACRON:
2187 case LFUN_ACCENT_DOT:
2188 case LFUN_ACCENT_UNDERDOT:
2189 case LFUN_ACCENT_UNDERBAR:
2190 case LFUN_ACCENT_CARON:
2191 case LFUN_ACCENT_SPECIAL_CARON:
2192 case LFUN_ACCENT_BREVE:
2193 case LFUN_ACCENT_TIE:
2194 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2195 case LFUN_ACCENT_CIRCLE:
2196 case LFUN_ACCENT_OGONEK:
2197 case LFUN_THESAURUS_ENTRY:
2198 case LFUN_PARAGRAPH_PARAMS_APPLY:
2199 case LFUN_PARAGRAPH_PARAMS:
2201 case LFUN_BUFFER_END:
2202 case LFUN_BUFFER_BEGIN:
2203 case LFUN_BUFFER_BEGIN_SELECT:
2204 case LFUN_BUFFER_END_SELECT:
2205 case LFUN_UNICODE_INSERT:
2206 // these are handled in our dispatch()
2215 && (cur.empty() || !cur.inset().insetAllowed(code)))
2218 flag.enabled(enable);
2223 void Text::pasteString(Cursor & cur, docstring const & clip,
2226 cur.clearSelection();
2227 if (!clip.empty()) {
2230 insertStringAsParagraphs(cur, clip);
2232 insertStringAsLines(cur, clip);