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"
31 #include "DispatchResult.h"
32 #include "ErrorList.h"
34 #include "FuncRequest.h"
36 #include "InsetList.h"
40 #include "LyXAction.h"
44 #include "Paragraph.h"
45 #include "paragraph_funcs.h"
46 #include "ParagraphParameters.h"
47 #include "TextMetrics.h"
49 #include "ParIterator.h"
51 #include "frontends/Clipboard.h"
52 #include "frontends/Selection.h"
54 #include "insets/InsetCollapsable.h"
55 #include "insets/InsetCommand.h"
56 #include "insets/InsetFloatList.h"
57 #include "insets/InsetNewline.h"
58 #include "insets/InsetQuotes.h"
59 #include "insets/InsetSpecialChar.h"
60 #include "insets/InsetText.h"
61 #include "insets/InsetInfo.h"
63 #include "support/lstrings.h"
64 #include "support/lyxlib.h"
65 #include "support/convert.h"
66 #include "support/lyxtime.h"
68 #include "mathed/InsetMathHull.h"
69 #include "mathed/MathMacroTemplate.h"
71 #include <boost/current_function.hpp>
78 using std::istringstream;
79 using std::ostringstream;
83 using cap::copySelection;
84 using cap::cutSelection;
85 using cap::pasteFromStack;
86 using cap::pasteClipboard;
87 using cap::replaceSelection;
89 using support::isStrUnsignedInt;
93 static Font freefont(ignore_font);
94 static bool toggleall = false;
96 static void toggleAndShow(Cursor & cur, Text * text,
97 Font const & font, bool toggleall = true)
99 text->toggleFree(cur, font, toggleall);
101 if (font.language() != ignore_language ||
102 font.fontInfo().number() != FONT_IGNORE) {
103 TextMetrics const & tm = cur.bv().textMetrics(text);
104 if (cur.boundary() != tm.isRTLBoundary(cur.pit(),
105 cur.pos(), cur.real_current_font))
106 text->setCursor(cur, cur.pit(), cur.pos(),
107 false, !cur.boundary());
112 static void moveCursor(Cursor & cur, bool selecting)
114 if (selecting || cur.mark())
119 static void finishChange(Cursor & cur, bool selecting)
122 moveCursor(cur, selecting);
126 static void mathDispatch(Cursor & cur, FuncRequest const & cmd, bool display)
129 docstring sel = cur.selectionAsString(false);
131 // It may happen that sel is empty but there is a selection
132 replaceSelection(cur);
135 #ifdef ENABLE_ASSERTIONS
136 const int old_pos = cur.pos();
138 cur.insert(new InsetMathHull(hullSimple));
139 BOOST_ASSERT(old_pos == cur.pos());
140 cur.nextInset()->edit(cur, true);
141 // don't do that also for LFUN_MATH_MODE
142 // unless you want end up with always changing
143 // to mathrm when opening an inlined inset --
144 // I really hate "LyXfunc overloading"...
146 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
147 // Avoid an unnecessary undo step if cmd.argument
149 if (!cmd.argument().empty())
150 cur.dispatch(FuncRequest(LFUN_MATH_INSERT,
153 // create a macro if we see "\\newcommand"
154 // somewhere, and an ordinary formula
156 if (sel.find(from_ascii("\\newcommand")) == string::npos
157 && sel.find(from_ascii("\\def")) == string::npos)
159 InsetMathHull * formula = new InsetMathHull;
160 istringstream is(to_utf8(sel));
163 formula->read(cur.buffer(), lex);
164 if (formula->getType() == hullNone)
165 // Don't create pseudo formulas if
166 // delimiters are left out
167 formula->mutate(hullSimple);
170 cur.insert(new MathMacroTemplate(sel));
173 cur.message(from_utf8(N_("Math editor mode")));
177 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
180 cap::replaceSelection(cur);
181 cur.insert(new InsetSpecialChar(kind));
186 static bool doInsertInset(Cursor & cur, Text * text,
187 FuncRequest const & cmd, bool edit, bool pastesel)
189 Inset * inset = createInset(&cur.bv(), cmd);
193 if (InsetCollapsable * ci = inset->asInsetCollapsable())
194 ci->setLayout(cur.bv().buffer().params());
197 if (cmd.action == LFUN_INDEX_INSERT) {
198 docstring ds = support::subst(text->getStringToIndex(cur), '\n', ' ');
199 text->insertInset(cur, inset);
201 inset->edit(cur, true);
202 // Now put this into inset
203 static_cast<InsetCollapsable *>(inset)->text_.insertStringAsParagraphs(cur, ds);
206 if (cur.selection()) {
207 lyx::dispatch(FuncRequest(LFUN_CUT));
210 text->insertInset(cur, inset);
213 inset->edit(cur, true);
215 if (gotsel && pastesel) {
216 lyx::dispatch(FuncRequest(LFUN_PASTE, "0"));
217 InsetText * insetText = dynamic_cast<InsetText *>(inset);
218 if (insetText && !insetText->allowMultiPar()
219 || cur.lastpit() == 0) {
220 // reset first par to default
221 LayoutPtr const layout =
222 cur.buffer().params().getTextClass().defaultLayout();
223 cur.text()->paragraphs().begin()->layout(layout);
226 // Merge multiple paragraphs -- hack
227 while (cur.lastpit() > 0) {
228 mergeParagraph(cur.buffer().params(),
229 cur.text()->paragraphs(), 0);
232 // reset surrounding par to default
233 docstring const layoutname =
234 cur.buffer().params().getTextClass().defaultLayoutName();
235 cur.leaveInset(*inset);
236 text->setLayout(cur, layoutname);
244 string const freefont2string()
246 return freefont.toString(toggleall);
250 void Text::number(Cursor & cur)
252 FontInfo font = ignore_font;
253 font.setNumber(FONT_TOGGLE);
254 toggleAndShow(cur, this, Font(font));
258 bool Text::isRTL(Buffer const & buffer, Paragraph const & par) const
260 return par.isRTL(buffer.params());
264 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
266 LYXERR(Debug::ACTION) << "Text::dispatch: cmd: " << cmd << endl;
268 // FIXME: We use the update flag to indicates wether a singlePar or a
269 // full screen update is needed. We reset it here but shall we restore it
273 BOOST_ASSERT(cur.text() == this);
274 BufferView * bv = &cur.bv();
275 TextMetrics & tm = cur.bv().textMetrics(this);
276 CursorSlice oldTopSlice = cur.top();
277 bool oldBoundary = cur.boundary();
278 bool sel = cur.selection();
279 // Signals that, even if needsUpdate == false, an update of the
280 // cursor paragraph is required
281 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
282 LyXAction::SingleParUpdate);
283 // Signals that a full-screen update is required
284 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
285 LyXAction::NoUpdate) || singleParUpdate);
286 // Remember the old paragraph metric (_outer_ paragraph!)
287 ParagraphMetrics const & pm = cur.bv().parMetrics(
288 cur.bottom().text(), cur.bottom().pit());
289 Dimension olddim = pm.dim();
291 switch (cmd.action) {
293 case LFUN_PARAGRAPH_MOVE_DOWN: {
294 pit_type const pit = cur.pit();
295 recUndo(cur, pit, pit + 1);
297 std::swap(pars_[pit], pars_[pit + 1]);
298 updateLabels(cur.buffer());
304 case LFUN_PARAGRAPH_MOVE_UP: {
305 pit_type const pit = cur.pit();
306 recUndo(cur, pit - 1, pit);
308 std::swap(pars_[pit], pars_[pit - 1]);
309 updateLabels(cur.buffer());
315 case LFUN_APPENDIX: {
316 Paragraph & par = cur.paragraph();
317 bool start = !par.params().startOfAppendix();
319 // FIXME: The code below only makes sense at top level.
320 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
321 // ensure that we have only one start_of_appendix in this document
322 // FIXME: this don't work for multipart document!
323 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
324 if (pars_[tmp].params().startOfAppendix()) {
326 pars_[tmp].params().startOfAppendix(false);
332 par.params().startOfAppendix(start);
334 // we can set the refreshing parameters now
335 updateLabels(cur.buffer());
339 case LFUN_WORD_DELETE_FORWARD:
340 if (cur.selection()) {
341 cutSelection(cur, true, false);
343 deleteWordForward(cur);
344 finishChange(cur, false);
347 case LFUN_WORD_DELETE_BACKWARD:
348 if (cur.selection()) {
349 cutSelection(cur, true, false);
351 deleteWordBackward(cur);
352 finishChange(cur, false);
355 case LFUN_LINE_DELETE:
356 if (cur.selection()) {
357 cutSelection(cur, true, false);
359 tm.deleteLineForward(cur);
360 finishChange(cur, false);
363 case LFUN_BUFFER_BEGIN:
364 case LFUN_BUFFER_BEGIN_SELECT:
365 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_BEGIN_SELECT);
366 if (cur.depth() == 1) {
367 needsUpdate |= cursorTop(cur);
373 case LFUN_BUFFER_END:
374 case LFUN_BUFFER_END_SELECT:
375 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_END_SELECT);
376 if (cur.depth() == 1) {
377 needsUpdate |= cursorBottom(cur);
383 case LFUN_CHAR_FORWARD:
384 case LFUN_CHAR_FORWARD_SELECT:
385 //lyxerr << BOOST_CURRENT_FUNCTION
386 // << " LFUN_CHAR_FORWARD[SEL]:\n" << cur << endl;
387 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
388 needsUpdate |= cursorForward(cur);
390 if (!needsUpdate && oldTopSlice == cur.top()
391 && cur.boundary() == oldBoundary) {
393 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
397 case LFUN_CHAR_BACKWARD:
398 case LFUN_CHAR_BACKWARD_SELECT:
399 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
400 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
401 needsUpdate |= cursorBackward(cur);
403 if (!needsUpdate && oldTopSlice == cur.top()
404 && cur.boundary() == oldBoundary) {
406 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
411 case LFUN_CHAR_LEFT_SELECT:
412 //FIXME: for visual cursor, really move left
413 if (reverseDirectionNeeded(cur)) {
414 lyx::dispatch(FuncRequest(
415 cmd.action == LFUN_CHAR_LEFT_SELECT ?
416 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
418 lyx::dispatch(FuncRequest(
419 cmd.action == LFUN_CHAR_LEFT_SELECT ?
420 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
424 case LFUN_CHAR_RIGHT:
425 case LFUN_CHAR_RIGHT_SELECT:
426 //FIXME: for visual cursor, really move right
427 if (reverseDirectionNeeded(cur)) {
428 lyx::dispatch(FuncRequest(
429 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
430 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
432 lyx::dispatch(FuncRequest(
433 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
434 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
439 case LFUN_DOWN_SELECT:
442 // stop/start the selection
443 bool select = cmd.action == LFUN_DOWN_SELECT ||
444 cmd.action == LFUN_UP_SELECT;
445 cur.selHandle(select);
447 // move cursor up/down
448 bool up = cmd.action == LFUN_UP_SELECT || cmd.action == LFUN_UP;
449 bool const successful = cur.upDownInText(up, needsUpdate);
451 // notify insets which were left and get their update flags
452 notifyCursorLeaves(cur.beforeDispatchCursor(), cur);
455 // redraw if you leave mathed (for the decorations)
456 needsUpdate |= cur.beforeDispatchCursor().inMathed();
463 case LFUN_PARAGRAPH_UP:
464 case LFUN_PARAGRAPH_UP_SELECT:
465 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
466 needsUpdate |= cursorUpParagraph(cur);
469 case LFUN_PARAGRAPH_DOWN:
470 case LFUN_PARAGRAPH_DOWN_SELECT:
471 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
472 needsUpdate |= cursorDownParagraph(cur);
475 case LFUN_SCREEN_UP_SELECT:
476 needsUpdate |= cur.selHandle(true);
477 if (cur.pit() == 0 && cur.textRow().pos() == 0)
480 tm.cursorPrevious(cur);
484 case LFUN_SCREEN_DOWN_SELECT:
485 needsUpdate |= cur.selHandle(true);
486 if (cur.pit() == cur.lastpit()
487 && cur.textRow().endpos() == cur.lastpos())
494 case LFUN_LINE_BEGIN:
495 case LFUN_LINE_BEGIN_SELECT:
496 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_BEGIN_SELECT);
497 needsUpdate |= tm.cursorHome(cur);
501 case LFUN_LINE_END_SELECT:
502 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
503 needsUpdate |= tm.cursorEnd(cur);
506 case LFUN_WORD_RIGHT:
507 case LFUN_WORD_RIGHT_SELECT:
508 //FIXME: for visual cursor mode, really move right
509 if (reverseDirectionNeeded(cur)) {
510 lyx::dispatch(FuncRequest(
511 cmd.action == LFUN_WORD_RIGHT_SELECT ?
512 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD));
514 lyx::dispatch(FuncRequest(
515 cmd.action == LFUN_WORD_RIGHT_SELECT ?
516 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD));
520 case LFUN_WORD_FORWARD:
521 case LFUN_WORD_FORWARD_SELECT:
522 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT);
523 needsUpdate |= cursorForwardOneWord(cur);
527 case LFUN_WORD_LEFT_SELECT:
528 //FIXME: for visual cursor mode, really move left
529 if (reverseDirectionNeeded(cur)) {
530 lyx::dispatch(FuncRequest(
531 cmd.action == LFUN_WORD_LEFT_SELECT ?
532 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD));
534 lyx::dispatch(FuncRequest(
535 cmd.action == LFUN_WORD_LEFT_SELECT ?
536 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD));
540 case LFUN_WORD_BACKWARD:
541 case LFUN_WORD_BACKWARD_SELECT:
542 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT);
543 needsUpdate |= cursorBackwardOneWord(cur);
546 case LFUN_WORD_SELECT: {
547 selectWord(cur, WHOLE_WORD);
548 finishChange(cur, true);
552 case LFUN_BREAK_LINE: {
553 // Not allowed by LaTeX (labels or empty par)
554 if (cur.pos() > cur.paragraph().beginOfBody()) {
555 // this avoids a double undo
556 // FIXME: should not be needed, ideally
557 if (!cur.selection())
559 cap::replaceSelection(cur);
560 cur.insert(new InsetNewline);
562 moveCursor(cur, false);
567 case LFUN_CHAR_DELETE_FORWARD:
568 if (!cur.selection()) {
569 if (cur.pos() == cur.paragraph().size())
570 // Par boundary, force full-screen update
571 singleParUpdate = false;
572 needsUpdate |= erase(cur);
574 // It is possible to make it a lot faster still
575 // just comment out the line below...
577 cutSelection(cur, true, false);
578 singleParUpdate = false;
580 moveCursor(cur, false);
583 case LFUN_DELETE_FORWARD_SKIP:
584 // Reverse the effect of LFUN_BREAK_PARAGRAPH_SKIP.
585 if (!cur.selection()) {
586 if (cur.pos() == cur.lastpos()) {
593 cutSelection(cur, true, false);
598 case LFUN_CHAR_DELETE_BACKWARD:
599 if (!cur.selection()) {
600 if (bv->getIntl().getTransManager().backspace()) {
601 // Par boundary, full-screen update
603 singleParUpdate = false;
604 needsUpdate |= backspace(cur);
606 // It is possible to make it a lot faster still
607 // just comment out the line below...
610 cutSelection(cur, true, false);
611 singleParUpdate = false;
615 case LFUN_DELETE_BACKWARD_SKIP:
616 // Reverse the effect of LFUN_BREAK_PARAGRAPH_SKIP.
617 if (!cur.selection()) {
619 //CursorSlice cur = cursor();
623 cutSelection(cur, true, false);
627 case LFUN_BREAK_PARAGRAPH:
628 cap::replaceSelection(cur);
629 breakParagraph(cur, cmd.argument() == "inverse");
633 case LFUN_BREAK_PARAGRAPH_SKIP: {
634 // When at the beginning of a paragraph, remove
635 // indentation. Otherwise, do the same as LFUN_BREAK_PARAGRAPH.
636 cap::replaceSelection(cur);
638 cur.paragraph().params().labelWidthString(docstring());
640 breakParagraph(cur, false);
646 // With the creation of LFUN_PARAGRAPH_PARAMS, this is now redundant,
647 // as its duties can be performed there. Should it be removed??
648 // FIXME For now, it can just dispatch LFUN_PARAGRAPH_PARAMS...
649 case LFUN_PARAGRAPH_SPACING: {
650 Paragraph & par = cur.paragraph();
651 Spacing::Space cur_spacing = par.params().spacing().getSpace();
652 string cur_value = "1.0";
653 if (cur_spacing == Spacing::Other)
654 cur_value = par.params().spacing().getValueAsString();
656 istringstream is(to_utf8(cmd.argument()));
659 Spacing::Space new_spacing = cur_spacing;
660 string new_value = cur_value;
662 lyxerr << "Missing argument to `paragraph-spacing'"
664 } else if (tmp == "single") {
665 new_spacing = Spacing::Single;
666 } else if (tmp == "onehalf") {
667 new_spacing = Spacing::Onehalf;
668 } else if (tmp == "double") {
669 new_spacing = Spacing::Double;
670 } else if (tmp == "other") {
671 new_spacing = Spacing::Other;
672 string tmpval = "0.0";
674 lyxerr << "new_value = " << tmpval << endl;
677 } else if (tmp == "default") {
678 new_spacing = Spacing::Default;
680 lyxerr << to_utf8(_("Unknown spacing argument: "))
681 << to_utf8(cmd.argument()) << endl;
683 if (cur_spacing != new_spacing || cur_value != new_value)
684 par.params().spacing(Spacing(new_spacing, new_value));
688 case LFUN_INSET_INSERT: {
690 Inset * inset = createInset(bv, cmd);
692 // FIXME (Abdel 01/02/2006):
693 // What follows would be a partial fix for bug 2154:
694 // http://bugzilla.lyx.org/show_bug.cgi?id=2154
695 // This automatically put the label inset _after_ a
696 // numbered section. It should be possible to extend the mechanism
697 // to any kind of LateX environement.
698 // The correct way to fix that bug would be at LateX generation.
699 // I'll let the code here for reference as it could be used for some
700 // other feature like "automatic labelling".
702 Paragraph & par = pars_[cur.pit()];
703 if (inset->lyxCode() == LABEL_CODE
704 && par.layout()->labeltype == LABEL_COUNTER) {
705 // Go to the end of the paragraph
706 // Warning: Because of Change-Tracking, the last
707 // position is 'size()' and not 'size()-1':
708 cur.pos() = par.size();
709 // Insert a new paragraph
710 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
715 cutSelection(cur, true, false);
716 insertInset(cur, inset);
722 case LFUN_INSET_DISSOLVE:
723 needsUpdate |= dissolveInset(cur);
726 case LFUN_INSET_SETTINGS:
727 cur.inset().showInsetDialog(bv);
730 case LFUN_SPACE_INSERT:
731 if (cur.paragraph().layout()->free_spacing)
732 insertChar(cur, ' ');
734 doInsertInset(cur, this, cmd, false, false);
737 moveCursor(cur, false);
740 case LFUN_HYPHENATION_POINT_INSERT:
741 specialChar(cur, InsetSpecialChar::HYPHENATION);
744 case LFUN_LIGATURE_BREAK_INSERT:
745 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
748 case LFUN_DOTS_INSERT:
749 specialChar(cur, InsetSpecialChar::LDOTS);
752 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
753 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
756 case LFUN_MENU_SEPARATOR_INSERT:
757 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
760 case LFUN_WORD_UPCASE:
761 changeCase(cur, text_uppercase);
764 case LFUN_WORD_LOWCASE:
765 changeCase(cur, text_lowercase);
768 case LFUN_WORD_CAPITALIZE:
769 changeCase(cur, text_capitalization);
772 case LFUN_CHARS_TRANSPOSE:
777 cur.message(_("Paste"));
778 cap::replaceSelection(cur);
779 if (cmd.argument().empty() && !theClipboard().isInternal())
780 pasteClipboard(cur, bv->buffer().errorList("Paste"));
782 string const arg(to_utf8(cmd.argument()));
783 pasteFromStack(cur, bv->buffer().errorList("Paste"),
784 isStrUnsignedInt(arg) ?
785 convert<unsigned int>(arg) :
788 bv->buffer().errors("Paste");
789 cur.clearSelection(); // bug 393
794 cutSelection(cur, true, true);
795 cur.message(_("Cut"));
800 cur.message(_("Copy"));
803 case LFUN_SERVER_GET_XY:
804 cur.message(from_utf8(
805 convert<string>(tm.cursorX(cur.top(), cur.boundary()))
806 + ' ' + convert<string>(tm.cursorY(cur.top(), cur.boundary()))));
809 case LFUN_SERVER_SET_XY: {
812 istringstream is(to_utf8(cmd.argument()));
815 lyxerr << "SETXY: Could not parse coordinates in '"
816 << to_utf8(cmd.argument()) << std::endl;
818 tm.setCursorFromCoordinates(cur, x, y);
822 case LFUN_SERVER_GET_FONT:
823 if (cur.current_font.fontInfo().shape() == ITALIC_SHAPE)
824 cur.message(from_ascii("E"));
825 else if (cur.current_font.fontInfo().shape() == SMALLCAPS_SHAPE)
826 cur.message(from_ascii("N"));
828 cur.message(from_ascii("0"));
831 case LFUN_SERVER_GET_LAYOUT:
832 cur.message(cur.paragraph().layout()->name());
836 docstring layout = cmd.argument();
837 LYXERR(Debug::INFO) << "LFUN_LAYOUT: (arg) " << to_utf8(layout) << endl;
839 docstring const old_layout = cur.paragraph().layout()->name();
841 // Derive layout number from given argument (string)
842 // and current buffer's textclass (number)
843 TextClass const & tclass = bv->buffer().params().getTextClass();
845 layout = tclass.defaultLayoutName();
846 bool hasLayout = tclass.hasLayout(layout);
848 // If the entry is obsolete, use the new one instead.
850 docstring const & obs = tclass[layout]->obsoleted_by();
856 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
857 from_utf8(N_(" not known")));
861 bool change_layout = (old_layout != layout);
863 if (!change_layout && cur.selection() &&
864 cur.selBegin().pit() != cur.selEnd().pit())
866 pit_type spit = cur.selBegin().pit();
867 pit_type epit = cur.selEnd().pit() + 1;
868 while (spit != epit) {
869 if (pars_[spit].layout()->name() != old_layout) {
870 change_layout = true;
878 setLayout(cur, layout);
883 case LFUN_CLIPBOARD_PASTE:
884 cur.clearSelection();
885 pasteClipboard(cur, bv->buffer().errorList("Paste"),
886 cmd.argument() == "paragraph");
887 bv->buffer().errors("Paste");
890 case LFUN_PRIMARY_SELECTION_PASTE:
891 pasteString(cur, theSelection().get(),
892 cmd.argument() == "paragraph");
895 case LFUN_UNICODE_INSERT: {
896 if (cmd.argument().empty())
898 docstring hexstring = cmd.argument();
899 if (lyx::support::isHex(hexstring)) {
900 char_type c = lyx::support::hexToInt(hexstring);
901 if (c >= 32 && c < 0x10ffff) {
902 lyxerr << "Inserting c: " << c << endl;
903 docstring s = docstring(1, c);
904 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
910 case LFUN_QUOTE_INSERT: {
911 Paragraph & par = cur.paragraph();
912 pos_type pos = cur.pos();
913 BufferParams const & bufparams = bv->buffer().params();
914 LayoutPtr const & style = par.layout();
915 if (!style->pass_thru
916 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
917 // this avoids a double undo
918 // FIXME: should not be needed, ideally
919 if (!cur.selection())
921 cap::replaceSelection(cur);
926 else if (cur.prevInset() && cur.prevInset()->isSpace())
929 c = par.getChar(pos - 1);
930 string arg = to_utf8(cmd.argument());
932 cur.insert(new InsetQuotes(c,
933 bufparams.quotes_language,
934 InsetQuotes::SingleQ));
936 cur.insert(new InsetQuotes(c,
937 bufparams.quotes_language,
938 InsetQuotes::DoubleQ));
942 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
946 case LFUN_DATE_INSERT: {
947 string const format = cmd.argument().empty()
948 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
949 string const time = formatted_time(current_time(), format);
950 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
954 case LFUN_MOUSE_TRIPLE:
955 if (cmd.button() == mouse_button::button1) {
964 case LFUN_MOUSE_DOUBLE:
965 if (cmd.button() == mouse_button::button1) {
966 selectWord(cur, WHOLE_WORD_STRICT);
971 // Single-click on work area
972 case LFUN_MOUSE_PRESS: {
973 // Right click on a footnote flag opens float menu
974 // FIXME: Why should we clear the selection in this case?
975 if (cmd.button() == mouse_button::button3)
976 cur.clearSelection();
978 bool do_selection = cmd.button() == mouse_button::button1
979 && cmd.argument() == "region-select";
981 bool update = bv->mouseSetCursor(cur, do_selection);
983 // Insert primary selection with middle mouse
984 // if there is a local selection in the current buffer,
986 if (cmd.button() == mouse_button::button2) {
987 if (cap::selection()) {
988 // Copy the selection buffer to the clipboard
989 // stack, because we want it to appear in the
990 // "Edit->Paste recent" menu.
991 cap::copySelectionToStack();
993 cap::pasteSelection(bv->cursor(),
994 bv->buffer().errorList("Paste"));
995 bv->buffer().errors("Paste");
996 bv->buffer().markDirty();
997 bv->cursor().finishUndo();
999 lyx::dispatch(FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph"));
1003 // we have to update after dEPM triggered
1004 if (!update && cmd.button() == mouse_button::button1) {
1005 needsUpdate = false;
1012 case LFUN_MOUSE_MOTION: {
1013 // Only use motion with button 1
1014 //if (cmd.button() != mouse_button::button1)
1017 // ignore motions deeper nested than the real anchor
1018 Cursor & bvcur = cur.bv().cursor();
1019 if (bvcur.anchor_.hasPart(cur)) {
1020 CursorSlice old = bvcur.top();
1022 int const wh = bv->workHeight();
1023 int const y = std::max(0, std::min(wh - 1, cmd.y));
1025 tm.setCursorFromCoordinates(cur, cmd.x, y);
1026 cur.setTargetX(cmd.x);
1028 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1030 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1031 // This is to allow jumping over large insets
1032 if (cur.top() == old) {
1034 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1036 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1039 if (cur.top() == old)
1042 // don't set anchor_
1043 bvcur.setCursor(cur);
1044 bvcur.selection() = true;
1045 //lyxerr << "MOTION: " << bv->cursor() << endl;
1053 case LFUN_MOUSE_RELEASE: {
1054 if (cmd.button() == mouse_button::button2)
1057 if (cmd.button() == mouse_button::button1) {
1058 // if there is new selection, update persistent
1059 // selection, otherwise, single click does not
1060 // clear persistent selection buffer
1061 if (cur.selection()) {
1063 // if double click, cur is moved to the end of word by selectWord
1064 // but bvcur is current mouse position
1065 Cursor & bvcur = cur.bv().cursor();
1066 bvcur.selection() = true;
1068 needsUpdate = false;
1075 case LFUN_SELF_INSERT: {
1076 if (cmd.argument().empty())
1079 // Automatically delete the currently selected
1080 // text and replace it with what is being
1081 // typed in now. Depends on lyxrc settings
1082 // "auto_region_delete", which defaults to
1085 if (lyxrc.auto_region_delete && cur.selection()) {
1086 cutSelection(cur, false, false);
1087 // When change tracking is set to off, the metrics update
1088 // mechanism correctly detects if a full update is needed or not.
1089 // This detection fails when a selection spans multiple rows and
1090 // change tracking is enabled because the paragraph metrics stays
1091 // the same. In this case, we force the full update:
1092 // (see http://bugzilla.lyx.org/show_bug.cgi?id=3992)
1093 if (cur.buffer().params().trackChanges)
1094 cur.updateFlags(Update::Force);
1097 cur.clearSelection();
1098 Font const old_font = cur.real_current_font;
1100 docstring::const_iterator cit = cmd.argument().begin();
1101 docstring::const_iterator end = cmd.argument().end();
1102 for (; cit != end; ++cit)
1103 bv->translateAndInsert(*cit, this, cur);
1106 moveCursor(cur, false);
1110 case LFUN_HYPERLINK_INSERT: {
1111 InsetCommandParams p(HYPERLINK_CODE);
1113 if (cur.selection()) {
1114 content = cur.selectionAsString(false);
1115 cutSelection(cur, true, false);
1117 p["target"] = (cmd.argument().empty()) ?
1118 content : cmd.argument();
1119 string const data = InsetCommandMailer::params2string("href", p);
1120 if (p["target"].empty()) {
1121 bv->showInsetDialog("href", data, 0);
1123 FuncRequest fr(LFUN_INSET_INSERT, data);
1129 case LFUN_LABEL_INSERT: {
1130 InsetCommandParams p(LABEL_CODE);
1131 // Try to generate a valid label
1132 p["name"] = (cmd.argument().empty()) ?
1133 cur.getPossibleLabel() :
1135 string const data = InsetCommandMailer::params2string("label", p);
1137 if (cmd.argument().empty()) {
1138 bv->showInsetDialog("label", data, 0);
1140 FuncRequest fr(LFUN_INSET_INSERT, data);
1146 case LFUN_INFO_INSERT: {
1147 Inset * inset = createInset(&cur.bv(), cmd);
1150 // if an empty inset is created (cmd.argument() is empty)
1151 // use current selection as parameter.
1152 if (cmd.argument().empty() && cur.selection()) {
1153 // use selected text as info to avoid a separate UI
1154 docstring ds = cur.selectionAsString(false);
1155 cutSelection(cur, true, false);
1156 static_cast<InsetInfo *>(inset)->setInfo(to_utf8(ds));
1158 insertInset(cur, inset);
1163 case LFUN_LIST_INSERT:
1164 case LFUN_THEOREM_INSERT:
1166 case LFUN_CAPTION_INSERT:
1167 case LFUN_FOOTNOTE_INSERT:
1168 // Open the inset, and move the current selection
1170 doInsertInset(cur, this, cmd, true, true);
1172 // These insets are numbered.
1173 updateLabels(bv->buffer());
1175 case LFUN_NOTE_INSERT:
1176 case LFUN_FLEX_INSERT:
1177 case LFUN_BOX_INSERT:
1178 case LFUN_BRANCH_INSERT:
1179 case LFUN_BIBITEM_INSERT:
1180 case LFUN_ERT_INSERT:
1181 case LFUN_LISTING_INSERT:
1182 case LFUN_MARGINALNOTE_INSERT:
1183 case LFUN_OPTIONAL_INSERT:
1184 case LFUN_ENVIRONMENT_INSERT:
1185 // Open the inset, and move the current selection
1187 doInsertInset(cur, this, cmd, true, true);
1191 case LFUN_TABULAR_INSERT:
1192 // if there were no arguments, just open the dialog
1193 if (doInsertInset(cur, this, cmd, false, true))
1196 bv->showDialog("tabularcreate");
1200 case LFUN_FLOAT_INSERT:
1201 case LFUN_FLOAT_WIDE_INSERT:
1202 case LFUN_WRAP_INSERT: {
1203 bool content = cur.selection(); // will some text be moved into the inset?
1205 doInsertInset(cur, this, cmd, true, true);
1207 ParagraphList & pars = cur.text()->paragraphs();
1209 TextClass const & tclass = bv->buffer().params().getTextClass();
1211 // add a separate paragraph for the caption inset
1212 pars.push_back(Paragraph());
1213 pars.back().setInsetOwner(pars[0].inInset());
1214 pars.back().layout(tclass.defaultLayout());
1216 int cap_pit = pars.size() - 1;
1218 // if an empty inset was created, we create an additional empty
1219 // paragraph at the bottom so that the user can choose where to put
1220 // the graphics (or table).
1222 pars.push_back(Paragraph());
1223 pars.back().setInsetOwner(pars[0].inInset());
1224 pars.back().layout(tclass.defaultLayout());
1228 // reposition the cursor to the caption
1229 cur.pit() = cap_pit;
1231 // FIXME: This Text/Cursor dispatch handling is a mess!
1232 // We cannot use Cursor::dispatch here it needs access to up to
1234 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1235 cur.text()->dispatch(cur, cmd_caption);
1236 cur.updateFlags(Update::Force);
1237 // FIXME: When leaving the Float (or Wrap) inset we should
1238 // delete any empty paragraph left above or below the
1243 case LFUN_INDEX_INSERT:
1244 doInsertInset(cur, this, cmd, true, true);
1247 case LFUN_NOMENCL_INSERT: {
1248 Inset * inset = createInset(&cur.bv(), cmd);
1252 cur.clearSelection();
1253 insertInset(cur, inset);
1254 // Show the dialog for the nomenclature entry, since the
1255 // description entry still needs to be filled in.
1256 if (cmd.action == LFUN_NOMENCL_INSERT)
1257 inset->edit(cur, true);
1262 case LFUN_INDEX_PRINT:
1263 case LFUN_NOMENCL_PRINT:
1264 case LFUN_TOC_INSERT:
1265 case LFUN_HFILL_INSERT:
1266 case LFUN_LINE_INSERT:
1267 case LFUN_PAGEBREAK_INSERT:
1268 case LFUN_CLEARPAGE_INSERT:
1269 case LFUN_CLEARDOUBLEPAGE_INSERT:
1271 doInsertInset(cur, this, cmd, false, false);
1275 case LFUN_DEPTH_DECREMENT:
1276 changeDepth(cur, DEC_DEPTH);
1279 case LFUN_DEPTH_INCREMENT:
1280 changeDepth(cur, INC_DEPTH);
1283 case LFUN_MATH_DISPLAY:
1284 mathDispatch(cur, cmd, true);
1287 case LFUN_MATH_IMPORT_SELECTION:
1288 case LFUN_MATH_MODE:
1289 if (cmd.argument() == "on")
1290 // don't pass "on" as argument
1291 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1293 mathDispatch(cur, cmd, false);
1296 case LFUN_MATH_MACRO:
1297 if (cmd.argument().empty())
1298 cur.errorMessage(from_utf8(N_("Missing argument")));
1300 string s = to_utf8(cmd.argument());
1301 string const s1 = token(s, ' ', 1);
1302 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1303 string const s2 = token(s, ' ', 2);
1304 string const type = s2.empty() ? "newcommand" : s2;
1305 cur.insert(new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, false, from_utf8(type)));
1306 //cur.nextInset()->edit(cur, true);
1310 // passthrough hat and underscore outside mathed:
1311 case LFUN_MATH_SUBSCRIPT:
1312 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1314 case LFUN_MATH_SUPERSCRIPT:
1315 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1318 case LFUN_MATH_INSERT:
1319 case LFUN_MATH_MATRIX:
1320 case LFUN_MATH_DELIM:
1321 case LFUN_MATH_BIGDELIM: {
1322 if (cur.selection())
1323 cur.clearSelection();
1324 // FIXME: instead of the above, this one
1325 // should be used (but it asserts with Bidi enabled)
1326 // cf. http://bugzilla.lyx.org/show_bug.cgi?id=4055
1327 // cap::replaceSelection(cur);
1328 cur.insert(new InsetMathHull(hullSimple));
1329 checkAndActivateInset(cur, true);
1330 BOOST_ASSERT(cur.inMathed());
1335 case LFUN_FONT_EMPH: {
1336 Font font(ignore_font);
1337 font.fontInfo().setEmph(FONT_TOGGLE);
1338 toggleAndShow(cur, this, font);
1342 case LFUN_FONT_BOLD: {
1343 Font font(ignore_font);
1344 font.fontInfo().setSeries(BOLD_SERIES);
1345 toggleAndShow(cur, this, font);
1349 case LFUN_FONT_NOUN: {
1350 Font font(ignore_font);
1351 font.fontInfo().setNoun(FONT_TOGGLE);
1352 toggleAndShow(cur, this, font);
1356 case LFUN_FONT_TYPEWRITER: {
1357 Font font(ignore_font);
1358 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1359 toggleAndShow(cur, this, font);
1363 case LFUN_FONT_SANS: {
1364 Font font(ignore_font);
1365 font.fontInfo().setFamily(SANS_FAMILY);
1366 toggleAndShow(cur, this, font);
1370 case LFUN_FONT_ROMAN: {
1371 Font font(ignore_font);
1372 font.fontInfo().setFamily(ROMAN_FAMILY);
1373 toggleAndShow(cur, this, font);
1377 case LFUN_FONT_DEFAULT: {
1378 Font font(inherit_font, ignore_language);
1379 toggleAndShow(cur, this, font);
1383 case LFUN_FONT_UNDERLINE: {
1384 Font font(ignore_font);
1385 font.fontInfo().setUnderbar(FONT_TOGGLE);
1386 toggleAndShow(cur, this, font);
1390 case LFUN_FONT_SIZE: {
1391 Font font(ignore_font);
1392 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1393 toggleAndShow(cur, this, font);
1397 case LFUN_LANGUAGE: {
1398 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1401 Font font(ignore_font);
1402 font.setLanguage(lang);
1403 toggleAndShow(cur, this, font);
1407 case LFUN_FONT_FREE_APPLY:
1408 toggleAndShow(cur, this, freefont, toggleall);
1409 cur.message(_("Character set"));
1412 // Set the freefont using the contents of \param data dispatched from
1413 // the frontends and apply it at the current cursor location.
1414 case LFUN_FONT_FREE_UPDATE: {
1417 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1420 toggleAndShow(cur, this, freefont, toggleall);
1421 cur.message(_("Character set"));
1423 lyxerr << "Argument not ok";
1428 case LFUN_FINISHED_LEFT:
1429 LYXERR(Debug::DEBUG) << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1430 if (reverseDirectionNeeded(cur)) {
1432 cur.setCurrentFont();
1436 case LFUN_FINISHED_RIGHT:
1437 LYXERR(Debug::DEBUG) << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1438 if (!reverseDirectionNeeded(cur)) {
1440 cur.setCurrentFont();
1444 case LFUN_FINISHED_BACKWARD:
1445 LYXERR(Debug::DEBUG) << "handle LFUN_FINISHED_BACKWARD:\n" << cur << endl;
1448 case LFUN_FINISHED_FORWARD:
1449 LYXERR(Debug::DEBUG) << "handle LFUN_FINISHED_FORWARD:\n" << cur << endl;
1451 cur.setCurrentFont();
1454 case LFUN_LAYOUT_PARAGRAPH: {
1456 params2string(cur.paragraph(), data);
1457 data = "show\n" + data;
1458 bv->showDialogWithData("paragraph", data);
1462 case LFUN_PARAGRAPH_UPDATE: {
1464 params2string(cur.paragraph(), data);
1466 // Will the paragraph accept changes from the dialog?
1467 bool const accept = !cur.inset().forceDefaultParagraphs(cur.idx());
1469 data = "update " + convert<string>(accept) + '\n' + data;
1470 bv->updateDialog("paragraph", data);
1474 case LFUN_ACCENT_UMLAUT:
1475 case LFUN_ACCENT_CIRCUMFLEX:
1476 case LFUN_ACCENT_GRAVE:
1477 case LFUN_ACCENT_ACUTE:
1478 case LFUN_ACCENT_TILDE:
1479 case LFUN_ACCENT_CEDILLA:
1480 case LFUN_ACCENT_MACRON:
1481 case LFUN_ACCENT_DOT:
1482 case LFUN_ACCENT_UNDERDOT:
1483 case LFUN_ACCENT_UNDERBAR:
1484 case LFUN_ACCENT_CARON:
1485 case LFUN_ACCENT_SPECIAL_CARON:
1486 case LFUN_ACCENT_BREVE:
1487 case LFUN_ACCENT_TIE:
1488 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1489 case LFUN_ACCENT_CIRCLE:
1490 case LFUN_ACCENT_OGONEK:
1491 theLyXFunc().handleKeyFunc(cmd.action);
1492 if (!cmd.argument().empty())
1493 // FIXME: Are all these characters encoded in one byte in utf8?
1494 bv->translateAndInsert(cmd.argument()[0], this, cur);
1497 case LFUN_FLOAT_LIST: {
1498 TextClass const & tclass = bv->buffer().params().getTextClass();
1499 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1501 if (cur.selection())
1502 cutSelection(cur, true, false);
1503 breakParagraph(cur);
1505 if (cur.lastpos() != 0) {
1506 cursorBackward(cur);
1507 breakParagraph(cur);
1510 setLayout(cur, tclass.defaultLayoutName());
1511 ParagraphParameters p;
1512 setParagraphs(cur, p);
1513 insertInset(cur, new InsetFloatList(to_utf8(cmd.argument())));
1516 lyxerr << "Non-existent float type: "
1517 << to_utf8(cmd.argument()) << endl;
1522 case LFUN_CHANGE_ACCEPT: {
1523 acceptOrRejectChanges(cur, ACCEPT);
1527 case LFUN_CHANGE_REJECT: {
1528 acceptOrRejectChanges(cur, REJECT);
1532 case LFUN_THESAURUS_ENTRY: {
1533 docstring arg = cmd.argument();
1535 arg = cur.selectionAsString(false);
1537 if (arg.size() > 100 || arg.empty()) {
1538 // Get word or selection
1539 selectWordWhenUnderCursor(cur, WHOLE_WORD);
1540 arg = cur.selectionAsString(false);
1543 bv->showDialogWithData("thesaurus", to_utf8(arg));
1547 case LFUN_PARAGRAPH_PARAMS_APPLY: {
1548 // Given data, an encoding of the ParagraphParameters
1549 // generated in the Paragraph dialog, this function sets
1550 // the current paragraph, or currently selected paragraphs,
1552 // NOTE: This function overrides all existing settings.
1553 setParagraphs(cur, cmd.argument());
1554 cur.message(_("Paragraph layout set"));
1558 case LFUN_PARAGRAPH_PARAMS: {
1559 // Given data, an encoding of the ParagraphParameters as we'd
1560 // find them in a LyX file, this function modifies the current paragraph,
1561 // or currently selected paragraphs.
1562 // NOTE: This function only modifies, and does not override, existing
1564 setParagraphs(cur, cmd.argument(), true);
1565 cur.message(_("Paragraph layout set"));
1570 if (cur.selection()) {
1571 cur.selection() = false;
1574 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
1575 // correct, but I'm not 100% sure -- dov, 071019
1576 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1581 LYXERR(Debug::ACTION)
1582 << BOOST_CURRENT_FUNCTION
1583 << ": Command " << cmd
1584 << " not DISPATCHED by Text" << endl;
1589 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
1591 // FIXME: The cursor flag is reset two lines below
1592 // so we need to check here if some of the LFUN did touch that.
1593 // for now only Text::erase() and Text::backspace() do that.
1594 // The plan is to verify all the LFUNs and then to remove this
1595 // singleParUpdate boolean altogether.
1596 if (cur.result().update() & Update::Force) {
1597 singleParUpdate = false;
1601 // FIXME: the following code should go in favor of fine grained
1602 // update flag treatment.
1603 if (singleParUpdate) {
1604 // Inserting characters does not change par height
1605 ParagraphMetrics const & pms
1606 = cur.bv().parMetrics(cur.bottom().text(), cur.bottom().pit());
1607 if (pms.dim().height()
1608 == olddim.height()) {
1609 // if so, update _only_ this paragraph
1610 cur.updateFlags(Update::SinglePar |
1612 Update::MultiParSel);
1619 && &oldTopSlice.inset() == &cur.inset()
1620 && oldTopSlice.idx() == cur.idx()
1621 && !sel // sel is a backup of cur.selection() at the biginning of the function.
1622 && !cur.selection())
1623 // FIXME: it would be better if we could just do this
1625 //if (cur.result().update() != Update::FitCursor)
1628 // But some LFUNs do not set Update::FitCursor when needed, so we
1629 // do it for all. This is not very harmfull as FitCursor will provoke
1630 // a full redraw only if needed but still, a proper review of all LFUN
1631 // should be done and this needsUpdate boolean can then be removed.
1632 cur.updateFlags(Update::FitCursor);
1634 cur.updateFlags(Update::Force | Update::FitCursor);
1638 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
1639 FuncStatus & flag) const
1641 BOOST_ASSERT(cur.text() == this);
1643 Font const & font = cur.real_current_font;
1644 FontInfo const & fontinfo = font.fontInfo();
1646 InsetCode code = NO_CODE;
1648 switch (cmd.action) {
1650 case LFUN_DEPTH_DECREMENT:
1651 enable = changeDepthAllowed(cur, DEC_DEPTH);
1654 case LFUN_DEPTH_INCREMENT:
1655 enable = changeDepthAllowed(cur, INC_DEPTH);
1659 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1662 case LFUN_BIBITEM_INSERT:
1663 enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO
1667 case LFUN_DIALOG_SHOW_NEW_INSET:
1668 if (cmd.argument() == "bibitem")
1669 code = BIBITEM_CODE;
1670 else if (cmd.argument() == "bibtex")
1672 else if (cmd.argument() == "box")
1674 else if (cmd.argument() == "branch")
1676 else if (cmd.argument() == "citation")
1678 else if (cmd.argument() == "ert")
1680 else if (cmd.argument() == "external")
1681 code = EXTERNAL_CODE;
1682 else if (cmd.argument() == "float")
1684 else if (cmd.argument() == "graphics")
1685 code = GRAPHICS_CODE;
1686 else if (cmd.argument() == "href")
1687 code = HYPERLINK_CODE;
1688 else if (cmd.argument() == "include")
1689 code = INCLUDE_CODE;
1690 else if (cmd.argument() == "index")
1692 else if (cmd.argument() == "nomenclature")
1693 code = NOMENCL_CODE;
1694 else if (cmd.argument() == "label")
1696 else if (cmd.argument() == "note")
1698 else if (cmd.argument() == "ref")
1700 else if (cmd.argument() == "toc")
1702 else if (cmd.argument() == "vspace")
1704 else if (cmd.argument() == "wrap")
1706 else if (cmd.argument() == "listings")
1707 code = LISTINGS_CODE;
1710 case LFUN_ERT_INSERT:
1713 case LFUN_LISTING_INSERT:
1714 code = LISTINGS_CODE;
1716 case LFUN_FOOTNOTE_INSERT:
1719 case LFUN_TABULAR_INSERT:
1720 code = TABULAR_CODE;
1722 case LFUN_MARGINALNOTE_INSERT:
1725 case LFUN_FLOAT_INSERT:
1726 case LFUN_FLOAT_WIDE_INSERT:
1729 case LFUN_WRAP_INSERT:
1732 case LFUN_FLOAT_LIST:
1733 code = FLOAT_LIST_CODE;
1736 case LFUN_LIST_INSERT:
1739 case LFUN_THEOREM_INSERT:
1740 code = THEOREM_CODE;
1743 case LFUN_CAPTION_INSERT:
1744 code = CAPTION_CODE;
1746 case LFUN_NOTE_INSERT:
1749 case LFUN_FLEX_INSERT: {
1751 string s = cmd.getArg(0);
1752 InsetLayout il = cur.buffer().params().getTextClass().insetlayout(from_utf8(s));
1753 if (il.lyxtype != "charstyle" &&
1754 il.lyxtype != "custom" &&
1755 il.lyxtype != "element")
1759 case LFUN_BOX_INSERT:
1762 case LFUN_BRANCH_INSERT:
1764 if (cur.buffer().masterBuffer()->params().branchlist().empty())
1767 case LFUN_LABEL_INSERT:
1770 case LFUN_INFO_INSERT:
1773 case LFUN_OPTIONAL_INSERT:
1775 enable = cur.paragraph().insetList().count(OPTARG_CODE)
1776 < cur.paragraph().layout()->optionalargs;
1778 case LFUN_ENVIRONMENT_INSERT:
1781 case LFUN_INDEX_INSERT:
1784 case LFUN_INDEX_PRINT:
1785 code = INDEX_PRINT_CODE;
1787 case LFUN_NOMENCL_INSERT:
1788 code = NOMENCL_CODE;
1790 case LFUN_NOMENCL_PRINT:
1791 code = NOMENCL_PRINT_CODE;
1793 case LFUN_TOC_INSERT:
1796 case LFUN_HYPERLINK_INSERT:
1797 code = HYPERLINK_CODE;
1799 case LFUN_QUOTE_INSERT:
1800 // always allow this, since we will inset a raw quote
1801 // if an inset is not allowed.
1803 case LFUN_HYPHENATION_POINT_INSERT:
1804 case LFUN_LIGATURE_BREAK_INSERT:
1805 case LFUN_HFILL_INSERT:
1806 case LFUN_MENU_SEPARATOR_INSERT:
1807 case LFUN_DOTS_INSERT:
1808 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
1809 code = SPECIALCHAR_CODE;
1811 case LFUN_SPACE_INSERT:
1812 // slight hack: we know this is allowed in math mode
1817 case LFUN_INSET_MODIFY:
1818 // We need to disable this, because we may get called for a
1820 // InsetTabular::getStatus() -> InsetText::getStatus()
1821 // and we don't handle LFUN_INSET_MODIFY.
1825 case LFUN_FONT_EMPH:
1826 flag.setOnOff(fontinfo.emph() == FONT_ON);
1829 case LFUN_FONT_NOUN:
1830 flag.setOnOff(fontinfo.noun() == FONT_ON);
1833 case LFUN_FONT_BOLD:
1834 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
1837 case LFUN_FONT_SANS:
1838 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
1841 case LFUN_FONT_ROMAN:
1842 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
1845 case LFUN_FONT_TYPEWRITER:
1846 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
1851 enable = cur.selection();
1855 if (cmd.argument().empty()) {
1856 if (theClipboard().isInternal())
1857 enable = cap::numberOfSelections() > 0;
1859 enable = !theClipboard().empty();
1861 string const arg = to_utf8(cmd.argument());
1862 if (isStrUnsignedInt(arg)) {
1863 unsigned int n = convert<unsigned int>(arg);
1864 enable = cap::numberOfSelections() > n;
1871 case LFUN_CLIPBOARD_PASTE:
1872 enable = !theClipboard().empty();
1875 case LFUN_PRIMARY_SELECTION_PASTE:
1876 enable = cur.selection() || !theSelection().empty();
1879 case LFUN_PARAGRAPH_MOVE_UP:
1880 enable = cur.pit() > 0 && !cur.selection();
1883 case LFUN_PARAGRAPH_MOVE_DOWN:
1884 enable = cur.pit() < cur.lastpit() && !cur.selection();
1887 case LFUN_INSET_DISSOLVE:
1888 if (!cmd.argument().empty()) {
1889 InsetLayout il = cur.inset().getLayout(cur.buffer().params());
1890 enable = cur.inset().lyxCode() == FLEX_CODE
1891 && il.lyxtype == to_utf8(cmd.argument());
1893 enable = !isMainText(cur.bv().buffer())
1894 && cur.inset().nargs() == 1;
1898 case LFUN_CHANGE_ACCEPT:
1899 case LFUN_CHANGE_REJECT:
1900 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
1901 // In principle, these LFUNs should only be enabled if there
1902 // is a change at the current position/in the current selection.
1903 // However, without proper optimizations, this will inevitably
1904 // result in unacceptable performance - just imagine a user who
1905 // wants to select the complete content of a long document.
1909 case LFUN_WORD_DELETE_FORWARD:
1910 case LFUN_WORD_DELETE_BACKWARD:
1911 case LFUN_LINE_DELETE:
1912 case LFUN_WORD_FORWARD:
1913 case LFUN_WORD_BACKWARD:
1914 case LFUN_WORD_RIGHT:
1915 case LFUN_WORD_LEFT:
1916 case LFUN_CHAR_FORWARD:
1917 case LFUN_CHAR_FORWARD_SELECT:
1918 case LFUN_CHAR_BACKWARD:
1919 case LFUN_CHAR_BACKWARD_SELECT:
1920 case LFUN_CHAR_LEFT:
1921 case LFUN_CHAR_LEFT_SELECT:
1922 case LFUN_CHAR_RIGHT:
1923 case LFUN_CHAR_RIGHT_SELECT:
1925 case LFUN_UP_SELECT:
1927 case LFUN_DOWN_SELECT:
1928 case LFUN_PARAGRAPH_UP_SELECT:
1929 case LFUN_PARAGRAPH_DOWN_SELECT:
1930 case LFUN_SCREEN_UP_SELECT:
1931 case LFUN_SCREEN_DOWN_SELECT:
1932 case LFUN_LINE_BEGIN_SELECT:
1933 case LFUN_LINE_END_SELECT:
1934 case LFUN_WORD_FORWARD_SELECT:
1935 case LFUN_WORD_BACKWARD_SELECT:
1936 case LFUN_WORD_RIGHT_SELECT:
1937 case LFUN_WORD_LEFT_SELECT:
1938 case LFUN_WORD_SELECT:
1939 case LFUN_PARAGRAPH_UP:
1940 case LFUN_PARAGRAPH_DOWN:
1941 case LFUN_LINE_BEGIN:
1943 case LFUN_BREAK_LINE:
1944 case LFUN_CHAR_DELETE_FORWARD:
1945 case LFUN_DELETE_FORWARD_SKIP:
1946 case LFUN_CHAR_DELETE_BACKWARD:
1947 case LFUN_DELETE_BACKWARD_SKIP:
1948 case LFUN_BREAK_PARAGRAPH:
1949 case LFUN_BREAK_PARAGRAPH_SKIP:
1950 case LFUN_PARAGRAPH_SPACING:
1951 case LFUN_INSET_INSERT:
1952 case LFUN_WORD_UPCASE:
1953 case LFUN_WORD_LOWCASE:
1954 case LFUN_WORD_CAPITALIZE:
1955 case LFUN_CHARS_TRANSPOSE:
1956 case LFUN_SERVER_GET_XY:
1957 case LFUN_SERVER_SET_XY:
1958 case LFUN_SERVER_GET_FONT:
1959 case LFUN_SERVER_GET_LAYOUT:
1961 case LFUN_DATE_INSERT:
1962 case LFUN_SELF_INSERT:
1963 case LFUN_LINE_INSERT:
1964 case LFUN_PAGEBREAK_INSERT:
1965 case LFUN_CLEARPAGE_INSERT:
1966 case LFUN_CLEARDOUBLEPAGE_INSERT:
1967 case LFUN_MATH_DISPLAY:
1968 case LFUN_MATH_IMPORT_SELECTION:
1969 case LFUN_MATH_MODE:
1970 case LFUN_MATH_MACRO:
1971 case LFUN_MATH_MATRIX:
1972 case LFUN_MATH_DELIM:
1973 case LFUN_MATH_BIGDELIM:
1974 case LFUN_MATH_INSERT:
1975 case LFUN_MATH_SUBSCRIPT:
1976 case LFUN_MATH_SUPERSCRIPT:
1977 case LFUN_FONT_DEFAULT:
1978 case LFUN_FONT_UNDERLINE:
1979 case LFUN_FONT_SIZE:
1981 case LFUN_FONT_FREE_APPLY:
1982 case LFUN_FONT_FREE_UPDATE:
1983 case LFUN_LAYOUT_PARAGRAPH:
1984 case LFUN_PARAGRAPH_UPDATE:
1985 case LFUN_ACCENT_UMLAUT:
1986 case LFUN_ACCENT_CIRCUMFLEX:
1987 case LFUN_ACCENT_GRAVE:
1988 case LFUN_ACCENT_ACUTE:
1989 case LFUN_ACCENT_TILDE:
1990 case LFUN_ACCENT_CEDILLA:
1991 case LFUN_ACCENT_MACRON:
1992 case LFUN_ACCENT_DOT:
1993 case LFUN_ACCENT_UNDERDOT:
1994 case LFUN_ACCENT_UNDERBAR:
1995 case LFUN_ACCENT_CARON:
1996 case LFUN_ACCENT_SPECIAL_CARON:
1997 case LFUN_ACCENT_BREVE:
1998 case LFUN_ACCENT_TIE:
1999 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2000 case LFUN_ACCENT_CIRCLE:
2001 case LFUN_ACCENT_OGONEK:
2002 case LFUN_THESAURUS_ENTRY:
2003 case LFUN_PARAGRAPH_PARAMS_APPLY:
2004 case LFUN_PARAGRAPH_PARAMS:
2006 case LFUN_BUFFER_END:
2007 case LFUN_BUFFER_BEGIN:
2008 case LFUN_BUFFER_BEGIN_SELECT:
2009 case LFUN_BUFFER_END_SELECT:
2010 case LFUN_UNICODE_INSERT:
2011 // these are handled in our dispatch()
2020 && (cur.empty() || !cur.inset().insetAllowed(code)))
2023 flag.enabled(enable);
2028 void Text::pasteString(Cursor & cur, docstring const & clip,
2031 cur.clearSelection();
2032 if (!clip.empty()) {
2035 insertStringAsParagraphs(cur, clip);
2037 insertStringAsLines(cur, clip);