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().buffer(), 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->buffer(), 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 pit_type const begpit = cur.selBegin().pit();
1087 pit_type const endpit = cur.selEnd().pit();
1088 cutSelection(cur, false, false);
1089 // When a selection spans multiple paragraphs, the metrics update
1090 // mechanism sometimes fail to detect that a full update is needed.
1091 // In this case, we force the full update:
1092 // (see http://bugzilla.lyx.org/show_bug.cgi?id=4317)
1093 if (isMainText(cur.bv().buffer()) && begpit != endpit)
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().buffer(), 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);
1248 case LFUN_NOMENCL_INSERT: {
1249 FuncRequest cmd1 = cmd;
1250 if (cmd.argument().empty())
1251 cmd1 = FuncRequest(cmd,
1252 bv->cursor().innerText()->getStringToIndex(bv->cursor()));
1253 Inset * inset = createInset(cur.bv().buffer(), cmd1);
1257 cur.clearSelection();
1258 insertInset(cur, inset);
1259 // Show the dialog for the nomenclature entry, since the
1260 // description entry still needs to be filled in.
1261 if (cmd.action == LFUN_NOMENCL_INSERT)
1262 inset->edit(cur, true);
1267 case LFUN_INDEX_PRINT:
1268 case LFUN_NOMENCL_PRINT:
1269 case LFUN_TOC_INSERT:
1270 case LFUN_HFILL_INSERT:
1271 case LFUN_LINE_INSERT:
1272 case LFUN_PAGEBREAK_INSERT:
1273 case LFUN_CLEARPAGE_INSERT:
1274 case LFUN_CLEARDOUBLEPAGE_INSERT:
1276 doInsertInset(cur, this, cmd, false, false);
1280 case LFUN_DEPTH_DECREMENT:
1281 changeDepth(cur, DEC_DEPTH);
1284 case LFUN_DEPTH_INCREMENT:
1285 changeDepth(cur, INC_DEPTH);
1288 case LFUN_MATH_DISPLAY:
1289 mathDispatch(cur, cmd, true);
1292 case LFUN_MATH_IMPORT_SELECTION:
1293 case LFUN_MATH_MODE:
1294 if (cmd.argument() == "on")
1295 // don't pass "on" as argument
1296 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1298 mathDispatch(cur, cmd, false);
1301 case LFUN_MATH_MACRO:
1302 if (cmd.argument().empty())
1303 cur.errorMessage(from_utf8(N_("Missing argument")));
1305 string s = to_utf8(cmd.argument());
1306 string const s1 = token(s, ' ', 1);
1307 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1308 string const s2 = token(s, ' ', 2);
1309 string const type = s2.empty() ? "newcommand" : s2;
1310 cur.insert(new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, false, from_utf8(type)));
1311 //cur.nextInset()->edit(cur, true);
1315 // passthrough hat and underscore outside mathed:
1316 case LFUN_MATH_SUBSCRIPT:
1317 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1319 case LFUN_MATH_SUPERSCRIPT:
1320 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1323 case LFUN_MATH_INSERT:
1324 case LFUN_MATH_MATRIX:
1325 case LFUN_MATH_DELIM:
1326 case LFUN_MATH_BIGDELIM: {
1327 if (cur.selection())
1328 cur.clearSelection();
1329 // FIXME: instead of the above, this one
1330 // should be used (but it asserts with Bidi enabled)
1331 // cf. http://bugzilla.lyx.org/show_bug.cgi?id=4055
1332 // cap::replaceSelection(cur);
1333 cur.insert(new InsetMathHull(hullSimple));
1334 checkAndActivateInset(cur, true);
1335 BOOST_ASSERT(cur.inMathed());
1340 case LFUN_FONT_EMPH: {
1341 Font font(ignore_font);
1342 font.fontInfo().setEmph(FONT_TOGGLE);
1343 toggleAndShow(cur, this, font);
1347 case LFUN_FONT_BOLD: {
1348 Font font(ignore_font);
1349 font.fontInfo().setSeries(BOLD_SERIES);
1350 toggleAndShow(cur, this, font);
1354 case LFUN_FONT_NOUN: {
1355 Font font(ignore_font);
1356 font.fontInfo().setNoun(FONT_TOGGLE);
1357 toggleAndShow(cur, this, font);
1361 case LFUN_FONT_TYPEWRITER: {
1362 Font font(ignore_font);
1363 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1364 toggleAndShow(cur, this, font);
1368 case LFUN_FONT_SANS: {
1369 Font font(ignore_font);
1370 font.fontInfo().setFamily(SANS_FAMILY);
1371 toggleAndShow(cur, this, font);
1375 case LFUN_FONT_ROMAN: {
1376 Font font(ignore_font);
1377 font.fontInfo().setFamily(ROMAN_FAMILY);
1378 toggleAndShow(cur, this, font);
1382 case LFUN_FONT_DEFAULT: {
1383 Font font(inherit_font, ignore_language);
1384 toggleAndShow(cur, this, font);
1388 case LFUN_FONT_UNDERLINE: {
1389 Font font(ignore_font);
1390 font.fontInfo().setUnderbar(FONT_TOGGLE);
1391 toggleAndShow(cur, this, font);
1395 case LFUN_FONT_SIZE: {
1396 Font font(ignore_font);
1397 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1398 toggleAndShow(cur, this, font);
1402 case LFUN_LANGUAGE: {
1403 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1406 Font font(ignore_font);
1407 font.setLanguage(lang);
1408 toggleAndShow(cur, this, font);
1412 case LFUN_FONT_FREE_APPLY:
1413 toggleAndShow(cur, this, freefont, toggleall);
1414 cur.message(_("Character set"));
1417 // Set the freefont using the contents of \param data dispatched from
1418 // the frontends and apply it at the current cursor location.
1419 case LFUN_FONT_FREE_UPDATE: {
1422 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1425 toggleAndShow(cur, this, freefont, toggleall);
1426 cur.message(_("Character set"));
1428 lyxerr << "Argument not ok";
1433 case LFUN_FINISHED_LEFT:
1434 LYXERR(Debug::DEBUG) << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1435 if (reverseDirectionNeeded(cur)) {
1437 cur.setCurrentFont();
1441 case LFUN_FINISHED_RIGHT:
1442 LYXERR(Debug::DEBUG) << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1443 if (!reverseDirectionNeeded(cur)) {
1445 cur.setCurrentFont();
1449 case LFUN_FINISHED_BACKWARD:
1450 LYXERR(Debug::DEBUG) << "handle LFUN_FINISHED_BACKWARD:\n" << cur << endl;
1453 case LFUN_FINISHED_FORWARD:
1454 LYXERR(Debug::DEBUG) << "handle LFUN_FINISHED_FORWARD:\n" << cur << endl;
1456 cur.setCurrentFont();
1459 case LFUN_LAYOUT_PARAGRAPH: {
1461 params2string(cur.paragraph(), data);
1462 data = "show\n" + data;
1463 bv->showDialogWithData("paragraph", data);
1467 case LFUN_PARAGRAPH_UPDATE: {
1469 params2string(cur.paragraph(), data);
1471 // Will the paragraph accept changes from the dialog?
1472 bool const accept = !cur.inset().forceDefaultParagraphs(cur.idx());
1474 data = "update " + convert<string>(accept) + '\n' + data;
1475 bv->updateDialog("paragraph", data);
1479 case LFUN_ACCENT_UMLAUT:
1480 case LFUN_ACCENT_CIRCUMFLEX:
1481 case LFUN_ACCENT_GRAVE:
1482 case LFUN_ACCENT_ACUTE:
1483 case LFUN_ACCENT_TILDE:
1484 case LFUN_ACCENT_CEDILLA:
1485 case LFUN_ACCENT_MACRON:
1486 case LFUN_ACCENT_DOT:
1487 case LFUN_ACCENT_UNDERDOT:
1488 case LFUN_ACCENT_UNDERBAR:
1489 case LFUN_ACCENT_CARON:
1490 case LFUN_ACCENT_SPECIAL_CARON:
1491 case LFUN_ACCENT_BREVE:
1492 case LFUN_ACCENT_TIE:
1493 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1494 case LFUN_ACCENT_CIRCLE:
1495 case LFUN_ACCENT_OGONEK:
1496 theLyXFunc().handleKeyFunc(cmd.action);
1497 if (!cmd.argument().empty())
1498 // FIXME: Are all these characters encoded in one byte in utf8?
1499 bv->translateAndInsert(cmd.argument()[0], this, cur);
1502 case LFUN_FLOAT_LIST: {
1503 TextClass const & tclass = bv->buffer().params().getTextClass();
1504 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1506 if (cur.selection())
1507 cutSelection(cur, true, false);
1508 breakParagraph(cur);
1510 if (cur.lastpos() != 0) {
1511 cursorBackward(cur);
1512 breakParagraph(cur);
1515 setLayout(cur, tclass.defaultLayoutName());
1516 ParagraphParameters p;
1517 setParagraphs(cur, p);
1518 insertInset(cur, new InsetFloatList(to_utf8(cmd.argument())));
1521 lyxerr << "Non-existent float type: "
1522 << to_utf8(cmd.argument()) << endl;
1527 case LFUN_CHANGE_ACCEPT: {
1528 acceptOrRejectChanges(cur, ACCEPT);
1532 case LFUN_CHANGE_REJECT: {
1533 acceptOrRejectChanges(cur, REJECT);
1537 case LFUN_THESAURUS_ENTRY: {
1538 docstring arg = cmd.argument();
1540 arg = cur.selectionAsString(false);
1542 if (arg.size() > 100 || arg.empty()) {
1543 // Get word or selection
1544 selectWordWhenUnderCursor(cur, WHOLE_WORD);
1545 arg = cur.selectionAsString(false);
1548 bv->showDialogWithData("thesaurus", to_utf8(arg));
1552 case LFUN_PARAGRAPH_PARAMS_APPLY: {
1553 // Given data, an encoding of the ParagraphParameters
1554 // generated in the Paragraph dialog, this function sets
1555 // the current paragraph, or currently selected paragraphs,
1557 // NOTE: This function overrides all existing settings.
1558 setParagraphs(cur, cmd.argument());
1559 cur.message(_("Paragraph layout set"));
1563 case LFUN_PARAGRAPH_PARAMS: {
1564 // Given data, an encoding of the ParagraphParameters as we'd
1565 // find them in a LyX file, this function modifies the current paragraph,
1566 // or currently selected paragraphs.
1567 // NOTE: This function only modifies, and does not override, existing
1569 setParagraphs(cur, cmd.argument(), true);
1570 cur.message(_("Paragraph layout set"));
1575 if (cur.selection()) {
1576 cur.selection() = false;
1579 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
1580 // correct, but I'm not 100% sure -- dov, 071019
1581 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1586 LYXERR(Debug::ACTION)
1587 << BOOST_CURRENT_FUNCTION
1588 << ": Command " << cmd
1589 << " not DISPATCHED by Text" << endl;
1594 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
1596 // FIXME: The cursor flag is reset two lines below
1597 // so we need to check here if some of the LFUN did touch that.
1598 // for now only Text::erase() and Text::backspace() do that.
1599 // The plan is to verify all the LFUNs and then to remove this
1600 // singleParUpdate boolean altogether.
1601 if (cur.result().update() & Update::Force) {
1602 singleParUpdate = false;
1606 // FIXME: the following code should go in favor of fine grained
1607 // update flag treatment.
1608 if (singleParUpdate) {
1609 // Inserting characters does not change par height
1610 ParagraphMetrics const & pms
1611 = cur.bv().parMetrics(cur.bottom().text(), cur.bottom().pit());
1612 if (pms.dim().height()
1613 == olddim.height()) {
1614 // if so, update _only_ this paragraph
1615 cur.updateFlags(Update::SinglePar |
1623 && &oldTopSlice.inset() == &cur.inset()
1624 && oldTopSlice.idx() == cur.idx()
1625 && !sel // sel is a backup of cur.selection() at the biginning of the function.
1626 && !cur.selection())
1627 // FIXME: it would be better if we could just do this
1629 //if (cur.result().update() != Update::FitCursor)
1632 // But some LFUNs do not set Update::FitCursor when needed, so we
1633 // do it for all. This is not very harmfull as FitCursor will provoke
1634 // a full redraw only if needed but still, a proper review of all LFUN
1635 // should be done and this needsUpdate boolean can then be removed.
1636 cur.updateFlags(Update::FitCursor);
1638 cur.updateFlags(Update::Force | Update::FitCursor);
1642 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
1643 FuncStatus & flag) const
1645 BOOST_ASSERT(cur.text() == this);
1647 Font const & font = cur.real_current_font;
1648 FontInfo const & fontinfo = font.fontInfo();
1650 InsetCode code = NO_CODE;
1652 switch (cmd.action) {
1654 case LFUN_DEPTH_DECREMENT:
1655 enable = changeDepthAllowed(cur, DEC_DEPTH);
1658 case LFUN_DEPTH_INCREMENT:
1659 enable = changeDepthAllowed(cur, INC_DEPTH);
1663 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1666 case LFUN_BIBITEM_INSERT:
1667 enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO
1671 case LFUN_DIALOG_SHOW_NEW_INSET:
1672 if (cmd.argument() == "bibitem")
1673 code = BIBITEM_CODE;
1674 else if (cmd.argument() == "bibtex")
1676 else if (cmd.argument() == "box")
1678 else if (cmd.argument() == "branch")
1680 else if (cmd.argument() == "citation")
1682 else if (cmd.argument() == "ert")
1684 else if (cmd.argument() == "external")
1685 code = EXTERNAL_CODE;
1686 else if (cmd.argument() == "float")
1688 else if (cmd.argument() == "graphics")
1689 code = GRAPHICS_CODE;
1690 else if (cmd.argument() == "href")
1691 code = HYPERLINK_CODE;
1692 else if (cmd.argument() == "include")
1693 code = INCLUDE_CODE;
1694 else if (cmd.argument() == "index")
1696 else if (cmd.argument() == "nomenclature")
1697 code = NOMENCL_CODE;
1698 else if (cmd.argument() == "label")
1700 else if (cmd.argument() == "note")
1702 else if (cmd.argument() == "ref")
1704 else if (cmd.argument() == "toc")
1706 else if (cmd.argument() == "vspace")
1708 else if (cmd.argument() == "wrap")
1710 else if (cmd.argument() == "listings")
1711 code = LISTINGS_CODE;
1714 case LFUN_ERT_INSERT:
1717 case LFUN_LISTING_INSERT:
1718 code = LISTINGS_CODE;
1720 case LFUN_FOOTNOTE_INSERT:
1723 case LFUN_TABULAR_INSERT:
1724 code = TABULAR_CODE;
1726 case LFUN_MARGINALNOTE_INSERT:
1729 case LFUN_FLOAT_INSERT:
1730 case LFUN_FLOAT_WIDE_INSERT:
1733 case LFUN_WRAP_INSERT:
1736 case LFUN_FLOAT_LIST:
1737 code = FLOAT_LIST_CODE;
1740 case LFUN_LIST_INSERT:
1743 case LFUN_THEOREM_INSERT:
1744 code = THEOREM_CODE;
1747 case LFUN_CAPTION_INSERT:
1748 code = CAPTION_CODE;
1750 case LFUN_NOTE_INSERT:
1753 case LFUN_FLEX_INSERT: {
1755 string s = cmd.getArg(0);
1756 InsetLayout il = cur.buffer().params().getTextClass().insetlayout(from_utf8(s));
1757 if (il.lyxtype != "charstyle" &&
1758 il.lyxtype != "custom" &&
1759 il.lyxtype != "element")
1763 case LFUN_BOX_INSERT:
1766 case LFUN_BRANCH_INSERT:
1768 if (cur.buffer().masterBuffer()->params().branchlist().empty())
1771 case LFUN_LABEL_INSERT:
1774 case LFUN_INFO_INSERT:
1777 case LFUN_OPTIONAL_INSERT:
1779 enable = cur.paragraph().insetList().count(OPTARG_CODE)
1780 < cur.paragraph().layout()->optionalargs;
1782 case LFUN_ENVIRONMENT_INSERT:
1785 case LFUN_INDEX_INSERT:
1788 case LFUN_INDEX_PRINT:
1789 code = INDEX_PRINT_CODE;
1791 case LFUN_NOMENCL_INSERT:
1792 code = NOMENCL_CODE;
1794 case LFUN_NOMENCL_PRINT:
1795 code = NOMENCL_PRINT_CODE;
1797 case LFUN_TOC_INSERT:
1800 case LFUN_HYPERLINK_INSERT:
1801 code = HYPERLINK_CODE;
1803 case LFUN_QUOTE_INSERT:
1804 // always allow this, since we will inset a raw quote
1805 // if an inset is not allowed.
1807 case LFUN_HYPHENATION_POINT_INSERT:
1808 case LFUN_LIGATURE_BREAK_INSERT:
1809 case LFUN_HFILL_INSERT:
1810 case LFUN_MENU_SEPARATOR_INSERT:
1811 case LFUN_DOTS_INSERT:
1812 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
1813 code = SPECIALCHAR_CODE;
1815 case LFUN_SPACE_INSERT:
1816 // slight hack: we know this is allowed in math mode
1821 case LFUN_INSET_MODIFY:
1822 // We need to disable this, because we may get called for a
1824 // InsetTabular::getStatus() -> InsetText::getStatus()
1825 // and we don't handle LFUN_INSET_MODIFY.
1829 case LFUN_FONT_EMPH:
1830 flag.setOnOff(fontinfo.emph() == FONT_ON);
1833 case LFUN_FONT_NOUN:
1834 flag.setOnOff(fontinfo.noun() == FONT_ON);
1837 case LFUN_FONT_BOLD:
1838 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
1841 case LFUN_FONT_SANS:
1842 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
1845 case LFUN_FONT_ROMAN:
1846 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
1849 case LFUN_FONT_TYPEWRITER:
1850 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
1855 enable = cur.selection();
1859 if (cmd.argument().empty()) {
1860 if (theClipboard().isInternal())
1861 enable = cap::numberOfSelections() > 0;
1863 enable = !theClipboard().empty();
1865 string const arg = to_utf8(cmd.argument());
1866 if (isStrUnsignedInt(arg)) {
1867 unsigned int n = convert<unsigned int>(arg);
1868 enable = cap::numberOfSelections() > n;
1875 case LFUN_CLIPBOARD_PASTE:
1876 enable = !theClipboard().empty();
1879 case LFUN_PRIMARY_SELECTION_PASTE:
1880 enable = cur.selection() || !theSelection().empty();
1883 case LFUN_PARAGRAPH_MOVE_UP:
1884 enable = cur.pit() > 0 && !cur.selection();
1887 case LFUN_PARAGRAPH_MOVE_DOWN:
1888 enable = cur.pit() < cur.lastpit() && !cur.selection();
1891 case LFUN_INSET_DISSOLVE:
1892 if (!cmd.argument().empty()) {
1893 InsetLayout il = cur.inset().getLayout(cur.buffer().params());
1894 enable = cur.inset().lyxCode() == FLEX_CODE
1895 && il.lyxtype == to_utf8(cmd.argument());
1897 enable = !isMainText(cur.bv().buffer())
1898 && cur.inset().nargs() == 1;
1902 case LFUN_CHANGE_ACCEPT:
1903 case LFUN_CHANGE_REJECT:
1904 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
1905 // In principle, these LFUNs should only be enabled if there
1906 // is a change at the current position/in the current selection.
1907 // However, without proper optimizations, this will inevitably
1908 // result in unacceptable performance - just imagine a user who
1909 // wants to select the complete content of a long document.
1913 case LFUN_WORD_DELETE_FORWARD:
1914 case LFUN_WORD_DELETE_BACKWARD:
1915 case LFUN_LINE_DELETE:
1916 case LFUN_WORD_FORWARD:
1917 case LFUN_WORD_BACKWARD:
1918 case LFUN_WORD_RIGHT:
1919 case LFUN_WORD_LEFT:
1920 case LFUN_CHAR_FORWARD:
1921 case LFUN_CHAR_FORWARD_SELECT:
1922 case LFUN_CHAR_BACKWARD:
1923 case LFUN_CHAR_BACKWARD_SELECT:
1924 case LFUN_CHAR_LEFT:
1925 case LFUN_CHAR_LEFT_SELECT:
1926 case LFUN_CHAR_RIGHT:
1927 case LFUN_CHAR_RIGHT_SELECT:
1929 case LFUN_UP_SELECT:
1931 case LFUN_DOWN_SELECT:
1932 case LFUN_PARAGRAPH_UP_SELECT:
1933 case LFUN_PARAGRAPH_DOWN_SELECT:
1934 case LFUN_SCREEN_UP_SELECT:
1935 case LFUN_SCREEN_DOWN_SELECT:
1936 case LFUN_LINE_BEGIN_SELECT:
1937 case LFUN_LINE_END_SELECT:
1938 case LFUN_WORD_FORWARD_SELECT:
1939 case LFUN_WORD_BACKWARD_SELECT:
1940 case LFUN_WORD_RIGHT_SELECT:
1941 case LFUN_WORD_LEFT_SELECT:
1942 case LFUN_WORD_SELECT:
1943 case LFUN_PARAGRAPH_UP:
1944 case LFUN_PARAGRAPH_DOWN:
1945 case LFUN_LINE_BEGIN:
1947 case LFUN_BREAK_LINE:
1948 case LFUN_CHAR_DELETE_FORWARD:
1949 case LFUN_DELETE_FORWARD_SKIP:
1950 case LFUN_CHAR_DELETE_BACKWARD:
1951 case LFUN_DELETE_BACKWARD_SKIP:
1952 case LFUN_BREAK_PARAGRAPH:
1953 case LFUN_BREAK_PARAGRAPH_SKIP:
1954 case LFUN_PARAGRAPH_SPACING:
1955 case LFUN_INSET_INSERT:
1956 case LFUN_WORD_UPCASE:
1957 case LFUN_WORD_LOWCASE:
1958 case LFUN_WORD_CAPITALIZE:
1959 case LFUN_CHARS_TRANSPOSE:
1960 case LFUN_SERVER_GET_XY:
1961 case LFUN_SERVER_SET_XY:
1962 case LFUN_SERVER_GET_FONT:
1963 case LFUN_SERVER_GET_LAYOUT:
1965 case LFUN_DATE_INSERT:
1966 case LFUN_SELF_INSERT:
1967 case LFUN_LINE_INSERT:
1968 case LFUN_PAGEBREAK_INSERT:
1969 case LFUN_CLEARPAGE_INSERT:
1970 case LFUN_CLEARDOUBLEPAGE_INSERT:
1971 case LFUN_MATH_DISPLAY:
1972 case LFUN_MATH_IMPORT_SELECTION:
1973 case LFUN_MATH_MODE:
1974 case LFUN_MATH_MACRO:
1975 case LFUN_MATH_MATRIX:
1976 case LFUN_MATH_DELIM:
1977 case LFUN_MATH_BIGDELIM:
1978 case LFUN_MATH_INSERT:
1979 case LFUN_MATH_SUBSCRIPT:
1980 case LFUN_MATH_SUPERSCRIPT:
1981 case LFUN_FONT_DEFAULT:
1982 case LFUN_FONT_UNDERLINE:
1983 case LFUN_FONT_SIZE:
1985 case LFUN_FONT_FREE_APPLY:
1986 case LFUN_FONT_FREE_UPDATE:
1987 case LFUN_LAYOUT_PARAGRAPH:
1988 case LFUN_PARAGRAPH_UPDATE:
1989 case LFUN_ACCENT_UMLAUT:
1990 case LFUN_ACCENT_CIRCUMFLEX:
1991 case LFUN_ACCENT_GRAVE:
1992 case LFUN_ACCENT_ACUTE:
1993 case LFUN_ACCENT_TILDE:
1994 case LFUN_ACCENT_CEDILLA:
1995 case LFUN_ACCENT_MACRON:
1996 case LFUN_ACCENT_DOT:
1997 case LFUN_ACCENT_UNDERDOT:
1998 case LFUN_ACCENT_UNDERBAR:
1999 case LFUN_ACCENT_CARON:
2000 case LFUN_ACCENT_SPECIAL_CARON:
2001 case LFUN_ACCENT_BREVE:
2002 case LFUN_ACCENT_TIE:
2003 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2004 case LFUN_ACCENT_CIRCLE:
2005 case LFUN_ACCENT_OGONEK:
2006 case LFUN_THESAURUS_ENTRY:
2007 case LFUN_PARAGRAPH_PARAMS_APPLY:
2008 case LFUN_PARAGRAPH_PARAMS:
2010 case LFUN_BUFFER_END:
2011 case LFUN_BUFFER_BEGIN:
2012 case LFUN_BUFFER_BEGIN_SELECT:
2013 case LFUN_BUFFER_END_SELECT:
2014 case LFUN_UNICODE_INSERT:
2015 // these are handled in our dispatch()
2024 && (cur.empty() || !cur.inset().insetAllowed(code)))
2027 flag.enabled(enable);
2032 void Text::pasteString(Cursor & cur, docstring const & clip,
2035 cur.clearSelection();
2036 if (!clip.empty()) {
2039 insertStringAsParagraphs(cur, clip);
2041 insertStringAsLines(cur, clip);