3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Alfredo Braunstein
9 * \author Angus Leeming
11 * \author André Pönitz
13 * Full author contact details are available in file CREDITS.
21 #include "BranchList.h"
22 #include "FloatList.h"
23 #include "FuncStatus.h"
25 #include "buffer_funcs.h"
26 #include "BufferParams.h"
27 #include "BufferView.h"
29 #include "CutAndPaste.h"
30 #include "DispatchResult.h"
31 #include "ErrorList.h"
33 #include "FuncRequest.h"
34 #include "InsetList.h"
38 #include "LyXAction.h"
42 #include "Paragraph.h"
43 #include "paragraph_funcs.h"
44 #include "ParagraphParameters.h"
45 #include "TextClass.h"
46 #include "TextMetrics.h"
49 #include "frontends/Clipboard.h"
50 #include "frontends/Selection.h"
52 #include "insets/InsetCollapsable.h"
53 #include "insets/InsetCommand.h"
54 #include "insets/InsetFloatList.h"
55 #include "insets/InsetNewline.h"
56 #include "insets/InsetQuotes.h"
57 #include "insets/InsetSpecialChar.h"
58 #include "insets/InsetText.h"
59 #include "insets/InsetGraphics.h"
60 #include "insets/InsetGraphicsParams.h"
62 #include "support/convert.h"
63 #include "support/debug.h"
64 #include "support/gettext.h"
65 #include "support/lstrings.h"
66 #include "support/lyxtime.h"
68 #include "mathed/InsetMathHull.h"
69 #include "mathed/MathMacroTemplate.h"
70 #include "mathed/MathParser.h"
72 #include <boost/next_prior.hpp>
78 using namespace lyx::support;
82 using cap::copySelection;
83 using cap::cutSelection;
84 using cap::pasteFromStack;
85 using cap::pasteClipboardText;
86 using cap::pasteClipboardGraphics;
87 using cap::replaceSelection;
90 static Font freefont(ignore_font, ignore_language);
91 static bool toggleall = false;
93 static void toggleAndShow(Cursor & cur, Text * text,
94 Font const & font, bool toggleall = true)
96 text->toggleFree(cur, font, toggleall);
98 if (font.language() != ignore_language ||
99 font.fontInfo().number() != FONT_IGNORE) {
100 TextMetrics const & tm = cur.bv().textMetrics(text);
101 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
102 cur.real_current_font))
103 text->setCursor(cur, cur.pit(), cur.pos(),
104 false, !cur.boundary());
109 static void moveCursor(Cursor & cur, bool selecting)
111 if (selecting || cur.mark())
116 static void finishChange(Cursor & cur, bool selecting)
119 moveCursor(cur, selecting);
123 static void mathDispatch(Cursor & cur, FuncRequest const & cmd, bool display)
126 docstring sel = cur.selectionAsString(false);
128 // It may happen that sel is empty but there is a selection
129 replaceSelection(cur);
131 // Is this a valid formula?
135 #ifdef ENABLE_ASSERTIONS
136 const int old_pos = cur.pos();
138 cur.insert(new InsetMathHull(hullSimple));
139 #ifdef ENABLE_ASSERTIONS
140 LASSERT(old_pos == cur.pos(), /**/);
142 cur.nextInset()->edit(cur, true);
143 // don't do that also for LFUN_MATH_MODE
144 // unless you want end up with always changing
145 // to mathrm when opening an inlined inset --
146 // I really hate "LyXfunc overloading"...
148 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
149 // Avoid an unnecessary undo step if cmd.argument
151 if (!cmd.argument().empty())
152 cur.dispatch(FuncRequest(LFUN_MATH_INSERT,
155 // create a macro if we see "\\newcommand"
156 // somewhere, and an ordinary formula
158 if (sel.find(from_ascii("\\newcommand")) == string::npos
159 && sel.find(from_ascii("\\newlyxcommand")) == string::npos
160 && sel.find(from_ascii("\\def")) == string::npos)
162 InsetMathHull * formula = new InsetMathHull;
163 string const selstr = to_utf8(sel);
164 istringstream is(selstr);
167 formula->readQuiet(lex);
168 if (formula->getType() == hullNone) {
169 // No valid formula, let's try with delims
170 is.str("$" + selstr + "$");
172 formula->readQuiet(lex);
173 if (formula->getType() == hullNone) {
174 // Still not valid, leave it as is
183 cur.insert(new MathMacroTemplate(sel));
187 cur.message(from_utf8(N_("Math editor mode")));
189 cur.message(from_utf8(N_("No valid math formula")));
193 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
196 cap::replaceSelection(cur);
197 cur.insert(new InsetSpecialChar(kind));
202 static bool doInsertInset(Cursor & cur, Text * text,
203 FuncRequest const & cmd, bool edit, bool pastesel)
205 Buffer & buffer = cur.bv().buffer();
206 BufferParams const & bparams = buffer.params();
207 Inset * inset = createInset(buffer, cmd);
211 if (InsetCollapsable * ci = inset->asInsetCollapsable())
212 ci->setLayout(bparams);
215 if (cmd.action == LFUN_INDEX_INSERT) {
216 docstring ds = subst(text->getStringToIndex(cur), '\n', ' ');
217 text->insertInset(cur, inset);
219 inset->edit(cur, true);
220 // Now put this into inset
221 static_cast<InsetCollapsable *>(inset)->text().insertStringAsParagraphs(cur, ds);
226 if (cur.selection()) {
227 cutSelection(cur, false, pastesel);
228 cur.clearSelection();
231 text->insertInset(cur, inset);
234 inset->edit(cur, true);
236 if (!gotsel || !pastesel)
239 pasteFromStack(cur, cur.buffer().errorList("Paste"), 0);
240 cur.buffer().errors("Paste");
241 cur.clearSelection(); // bug 393
243 InsetText * insetText = dynamic_cast<InsetText *>(inset);
244 if (insetText && (!insetText->allowMultiPar() || cur.lastpit() == 0)) {
245 // reset first par to default
246 cur.text()->paragraphs().begin()
247 ->setPlainOrDefaultLayout(bparams.documentClass());
250 // Merge multiple paragraphs -- hack
251 while (cur.lastpit() > 0)
252 mergeParagraph(bparams, cur.text()->paragraphs(), 0);
253 cur.leaveInset(*inset);
255 cur.leaveInset(*inset);
256 // reset surrounding par to default
257 DocumentClass const & dc = bparams.documentClass();
258 docstring const layoutname = inset->usePlainLayout()
259 ? dc.plainLayoutName()
260 : dc.defaultLayoutName();
261 text->setLayout(cur, layoutname);
268 string const freefont2string()
270 return freefont.toString(toggleall);
274 /// the type of outline operation
276 OutlineUp, // Move this header with text down
277 OutlineDown, // Move this header with text up
278 OutlineIn, // Make this header deeper
279 OutlineOut // Make this header shallower
283 static void outline(OutlineOp mode, Cursor & cur)
285 Buffer & buf = cur.buffer();
286 pit_type & pit = cur.pit();
287 ParagraphList & pars = buf.text().paragraphs();
288 ParagraphList::iterator bgn = pars.begin();
289 // The first paragraph of the area to be copied:
290 ParagraphList::iterator start = boost::next(bgn, pit);
291 // The final paragraph of area to be copied:
292 ParagraphList::iterator finish = start;
293 ParagraphList::iterator end = pars.end();
295 DocumentClass const & tc = buf.params().documentClass();
297 int const thistoclevel = start->layout().toclevel;
300 // Move out (down) from this section header
303 // Seek the one (on same level) below
304 for (; finish != end; ++finish) {
305 toclevel = finish->layout().toclevel;
306 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel) {
313 if (start == pars.begin())
316 ParagraphList::iterator dest = start;
317 // Move out (up) from this header
320 // Search previous same-level header above
323 toclevel = dest->layout().toclevel;
325 && (toclevel == Layout::NOT_IN_TOC
326 || toclevel > thistoclevel));
327 // Not found; do nothing
328 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
330 pit_type const newpit = distance(bgn, dest);
331 pit_type const len = distance(start, finish);
332 pit_type const deletepit = pit + len;
333 buf.undo().recordUndo(cur, ATOMIC_UNDO, newpit, deletepit - 1);
334 pars.insert(dest, start, finish);
335 start = boost::next(pars.begin(), deletepit);
337 pars.erase(start, finish);
344 // Go one down from *this* header:
345 ParagraphList::iterator dest = boost::next(finish, 1);
346 // Go further down to find header to insert in front of:
347 for (; dest != end; ++dest) {
348 toclevel = dest->layout().toclevel;
349 if (toclevel != Layout::NOT_IN_TOC
350 && toclevel <= thistoclevel) {
354 // One such was found:
355 pit_type newpit = distance(bgn, dest);
356 pit_type const len = distance(start, finish);
357 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, newpit - 1);
358 pars.insert(dest, start, finish);
359 start = boost::next(bgn, pit);
361 pars.erase(start, finish);
365 pit_type const len = distance(start, finish);
366 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
367 for (; start != finish; ++start) {
368 toclevel = start->layout().toclevel;
369 if (toclevel == Layout::NOT_IN_TOC)
371 DocumentClass::const_iterator lit = tc.begin();
372 DocumentClass::const_iterator len = tc.end();
373 for (; lit != len; ++lit) {
374 if (lit->toclevel == toclevel + 1 &&
375 start->layout().labeltype == lit->labeltype) {
376 start->setLayout(*lit);
384 pit_type const len = distance(start, finish);
385 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
386 for (; start != finish; ++start) {
387 toclevel = start->layout().toclevel;
388 if (toclevel == Layout::NOT_IN_TOC)
390 DocumentClass::const_iterator lit = tc.begin();
391 DocumentClass::const_iterator len = tc.end();
392 for (; lit != len; ++lit) {
393 if (lit->toclevel == toclevel - 1 &&
394 start->layout().labeltype == lit->labeltype) {
395 start->setLayout(*lit);
406 void Text::number(Cursor & cur)
408 FontInfo font = ignore_font;
409 font.setNumber(FONT_TOGGLE);
410 toggleAndShow(cur, this, Font(font, ignore_language));
414 bool Text::isRTL(Buffer const & buffer, Paragraph const & par) const
416 return par.isRTL(buffer.params());
420 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
422 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
424 BufferView * bv = &cur.bv();
425 TextMetrics & tm = bv->textMetrics(this);
426 if (!tm.contains(cur.pit()))
427 lyx::dispatch(FuncRequest(LFUN_SCREEN_RECENTER));
429 // FIXME: We use the update flag to indicates wether a singlePar or a
430 // full screen update is needed. We reset it here but shall we restore it
434 LASSERT(cur.text() == this, /**/);
435 CursorSlice oldTopSlice = cur.top();
436 bool oldBoundary = cur.boundary();
437 bool sel = cur.selection();
438 // Signals that, even if needsUpdate == false, an update of the
439 // cursor paragraph is required
440 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
441 LyXAction::SingleParUpdate);
442 // Signals that a full-screen update is required
443 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
444 LyXAction::NoUpdate) || singleParUpdate);
446 switch (cmd.action) {
448 case LFUN_PARAGRAPH_MOVE_DOWN: {
449 pit_type const pit = cur.pit();
450 recUndo(cur, pit, pit + 1);
452 swap(pars_[pit], pars_[pit + 1]);
453 updateLabels(cur.buffer());
459 case LFUN_PARAGRAPH_MOVE_UP: {
460 pit_type const pit = cur.pit();
461 recUndo(cur, pit - 1, pit);
463 swap(pars_[pit], pars_[pit - 1]);
464 updateLabels(cur.buffer());
470 case LFUN_APPENDIX: {
471 Paragraph & par = cur.paragraph();
472 bool start = !par.params().startOfAppendix();
474 // FIXME: The code below only makes sense at top level.
475 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
476 // ensure that we have only one start_of_appendix in this document
477 // FIXME: this don't work for multipart document!
478 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
479 if (pars_[tmp].params().startOfAppendix()) {
481 pars_[tmp].params().startOfAppendix(false);
487 par.params().startOfAppendix(start);
489 // we can set the refreshing parameters now
490 updateLabels(cur.buffer());
494 case LFUN_WORD_DELETE_FORWARD:
495 if (cur.selection()) {
496 cutSelection(cur, true, false);
498 deleteWordForward(cur);
499 finishChange(cur, false);
502 case LFUN_WORD_DELETE_BACKWARD:
503 if (cur.selection()) {
504 cutSelection(cur, true, false);
506 deleteWordBackward(cur);
507 finishChange(cur, false);
510 case LFUN_LINE_DELETE:
511 if (cur.selection()) {
512 cutSelection(cur, true, false);
514 tm.deleteLineForward(cur);
515 finishChange(cur, false);
518 case LFUN_BUFFER_BEGIN:
519 case LFUN_BUFFER_BEGIN_SELECT:
520 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_BEGIN_SELECT);
521 if (cur.depth() == 1) {
522 needsUpdate |= cursorTop(cur);
526 cur.updateFlags(Update::FitCursor);
529 case LFUN_BUFFER_END:
530 case LFUN_BUFFER_END_SELECT:
531 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_END_SELECT);
532 if (cur.depth() == 1) {
533 needsUpdate |= cursorBottom(cur);
537 cur.updateFlags(Update::FitCursor);
540 case LFUN_CHAR_FORWARD:
541 case LFUN_CHAR_FORWARD_SELECT:
542 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
543 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
544 needsUpdate |= cursorForward(cur);
546 if (!needsUpdate && oldTopSlice == cur.top()
547 && cur.boundary() == oldBoundary) {
549 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
553 case LFUN_CHAR_BACKWARD:
554 case LFUN_CHAR_BACKWARD_SELECT:
555 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
556 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
557 needsUpdate |= cursorBackward(cur);
559 if (!needsUpdate && oldTopSlice == cur.top()
560 && cur.boundary() == oldBoundary) {
562 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
567 case LFUN_CHAR_LEFT_SELECT:
568 if (lyxrc.visual_cursor) {
569 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_LEFT_SELECT);
570 needsUpdate |= cursorVisLeft(cur);
571 if (!needsUpdate && oldTopSlice == cur.top()
572 && cur.boundary() == oldBoundary) {
574 cmd = FuncRequest(LFUN_FINISHED_LEFT);
577 if (reverseDirectionNeeded(cur)) {
578 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
579 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
581 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
582 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
589 case LFUN_CHAR_RIGHT:
590 case LFUN_CHAR_RIGHT_SELECT:
591 if (lyxrc.visual_cursor) {
592 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_RIGHT_SELECT);
593 needsUpdate |= cursorVisRight(cur);
594 if (!needsUpdate && oldTopSlice == cur.top()
595 && cur.boundary() == oldBoundary) {
597 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
600 if (reverseDirectionNeeded(cur)) {
601 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
602 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
604 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
605 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
614 case LFUN_DOWN_SELECT:
617 // stop/start the selection
618 bool select = cmd.action == LFUN_DOWN_SELECT ||
619 cmd.action == LFUN_UP_SELECT;
620 cur.selHandle(select);
622 // move cursor up/down
623 bool up = cmd.action == LFUN_UP_SELECT || cmd.action == LFUN_UP;
624 bool const successful = cur.upDownInText(up, needsUpdate);
626 // redraw if you leave mathed (for the decorations)
627 needsUpdate |= cur.beforeDispatchCursor().inMathed();
634 case LFUN_PARAGRAPH_UP:
635 case LFUN_PARAGRAPH_UP_SELECT:
636 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
637 needsUpdate |= cursorUpParagraph(cur);
640 case LFUN_PARAGRAPH_DOWN:
641 case LFUN_PARAGRAPH_DOWN_SELECT:
642 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
643 needsUpdate |= cursorDownParagraph(cur);
646 case LFUN_LINE_BEGIN:
647 case LFUN_LINE_BEGIN_SELECT:
648 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_BEGIN_SELECT);
649 needsUpdate |= tm.cursorHome(cur);
653 case LFUN_LINE_END_SELECT:
654 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
655 needsUpdate |= tm.cursorEnd(cur);
658 case LFUN_WORD_RIGHT:
659 case LFUN_WORD_RIGHT_SELECT:
660 if (lyxrc.visual_cursor) {
661 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_RIGHT_SELECT);
662 needsUpdate |= cursorVisRightOneWord(cur);
663 if (!needsUpdate && oldTopSlice == cur.top()
664 && cur.boundary() == oldBoundary) {
666 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
669 if (reverseDirectionNeeded(cur)) {
670 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
671 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
673 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
674 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
681 case LFUN_WORD_FORWARD:
682 case LFUN_WORD_FORWARD_SELECT:
683 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT);
684 needsUpdate |= cursorForwardOneWord(cur);
688 case LFUN_WORD_LEFT_SELECT:
689 if (lyxrc.visual_cursor) {
690 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_LEFT_SELECT);
691 needsUpdate |= cursorVisLeftOneWord(cur);
692 if (!needsUpdate && oldTopSlice == cur.top()
693 && cur.boundary() == oldBoundary) {
695 cmd = FuncRequest(LFUN_FINISHED_LEFT);
698 if (reverseDirectionNeeded(cur)) {
699 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
700 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
702 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
703 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
710 case LFUN_WORD_BACKWARD:
711 case LFUN_WORD_BACKWARD_SELECT:
712 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT);
713 needsUpdate |= cursorBackwardOneWord(cur);
716 case LFUN_WORD_SELECT: {
717 selectWord(cur, WHOLE_WORD);
718 finishChange(cur, true);
722 case LFUN_NEWLINE_INSERT: {
723 InsetNewlineParams inp;
724 docstring arg = cmd.argument();
725 // this avoids a double undo
726 // FIXME: should not be needed, ideally
727 if (!cur.selection())
729 cap::replaceSelection(cur);
730 if (arg == "linebreak")
731 inp.kind = InsetNewlineParams::LINEBREAK;
733 inp.kind = InsetNewlineParams::NEWLINE;
734 cur.insert(new InsetNewline(inp));
736 moveCursor(cur, false);
740 case LFUN_CHAR_DELETE_FORWARD:
741 if (!cur.selection()) {
742 if (cur.pos() == cur.paragraph().size())
743 // Par boundary, force full-screen update
744 singleParUpdate = false;
745 needsUpdate |= erase(cur);
747 // It is possible to make it a lot faster still
748 // just comment out the line below...
750 cutSelection(cur, true, false);
751 singleParUpdate = false;
753 moveCursor(cur, false);
756 case LFUN_CHAR_DELETE_BACKWARD:
757 if (!cur.selection()) {
758 if (bv->getIntl().getTransManager().backspace()) {
759 // Par boundary, full-screen update
761 singleParUpdate = false;
762 needsUpdate |= backspace(cur);
764 // It is possible to make it a lot faster still
765 // just comment out the line below...
768 cutSelection(cur, true, false);
769 singleParUpdate = false;
773 case LFUN_BREAK_PARAGRAPH:
774 cap::replaceSelection(cur);
775 breakParagraph(cur, cmd.argument() == "inverse");
780 // With the creation of LFUN_PARAGRAPH_PARAMS, this is now redundant,
781 // as its duties can be performed there. Should it be removed??
782 // FIXME For now, it can just dispatch LFUN_PARAGRAPH_PARAMS...
783 case LFUN_PARAGRAPH_SPACING: {
784 Paragraph & par = cur.paragraph();
785 Spacing::Space cur_spacing = par.params().spacing().getSpace();
786 string cur_value = "1.0";
787 if (cur_spacing == Spacing::Other)
788 cur_value = par.params().spacing().getValueAsString();
790 istringstream is(to_utf8(cmd.argument()));
793 Spacing::Space new_spacing = cur_spacing;
794 string new_value = cur_value;
796 lyxerr << "Missing argument to `paragraph-spacing'"
798 } else if (tmp == "single") {
799 new_spacing = Spacing::Single;
800 } else if (tmp == "onehalf") {
801 new_spacing = Spacing::Onehalf;
802 } else if (tmp == "double") {
803 new_spacing = Spacing::Double;
804 } else if (tmp == "other") {
805 new_spacing = Spacing::Other;
806 string tmpval = "0.0";
808 lyxerr << "new_value = " << tmpval << endl;
811 } else if (tmp == "default") {
812 new_spacing = Spacing::Default;
814 lyxerr << to_utf8(_("Unknown spacing argument: "))
815 << to_utf8(cmd.argument()) << endl;
817 if (cur_spacing != new_spacing || cur_value != new_value)
818 par.params().spacing(Spacing(new_spacing, new_value));
822 case LFUN_INSET_INSERT: {
824 Inset * inset = createInset(bv->buffer(), cmd);
826 // FIXME (Abdel 01/02/2006):
827 // What follows would be a partial fix for bug 2154:
828 // http://bugzilla.lyx.org/show_bug.cgi?id=2154
829 // This automatically put the label inset _after_ a
830 // numbered section. It should be possible to extend the mechanism
831 // to any kind of LateX environement.
832 // The correct way to fix that bug would be at LateX generation.
833 // I'll let the code here for reference as it could be used for some
834 // other feature like "automatic labelling".
836 Paragraph & par = pars_[cur.pit()];
837 if (inset->lyxCode() == LABEL_CODE
838 && par.layout().labeltype == LABEL_COUNTER) {
839 // Go to the end of the paragraph
840 // Warning: Because of Change-Tracking, the last
841 // position is 'size()' and not 'size()-1':
842 cur.pos() = par.size();
843 // Insert a new paragraph
844 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
849 cutSelection(cur, true, false);
856 case LFUN_INSET_DISSOLVE:
857 needsUpdate |= dissolveInset(cur);
860 case LFUN_INSET_SETTINGS: {
861 Inset & inset = cur.inset();
862 if (cmd.getArg(0) == insetName(inset.lyxCode())) {
863 // This inset dialog has been explicitely requested.
864 inset.showInsetDialog(bv);
867 // else, if there is an inset at the cursor, access this
868 Inset * next_inset = cur.nextInset();
870 next_inset->showInsetDialog(bv);
873 // if not then access the underlying inset.
874 inset.showInsetDialog(bv);
878 case LFUN_SET_GRAPHICS_GROUP: {
879 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
885 string id = to_utf8(cmd.argument());
886 string grp = graphics::getGroupParams(bv->buffer(), id);
887 InsetGraphicsParams tmp, inspar = ins->getParams();
890 inspar.groupId = to_utf8(cmd.argument());
892 InsetGraphics::string2params(grp, bv->buffer(), tmp);
893 tmp.filename = inspar.filename;
897 ins->setParams(inspar);
900 case LFUN_SPACE_INSERT:
901 if (cur.paragraph().layout().free_spacing)
902 insertChar(cur, ' ');
904 doInsertInset(cur, this, cmd, false, false);
907 moveCursor(cur, false);
910 case LFUN_SPECIALCHAR_INSERT: {
911 string const name = to_utf8(cmd.argument());
912 if (name == "hyphenation")
913 specialChar(cur, InsetSpecialChar::HYPHENATION);
914 else if (name == "ligature-break")
915 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
916 else if (name == "slash")
917 specialChar(cur, InsetSpecialChar::SLASH);
918 else if (name == "nobreakdash")
919 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
920 else if (name == "dots")
921 specialChar(cur, InsetSpecialChar::LDOTS);
922 else if (name == "end-of-sentence")
923 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
924 else if (name == "menu-separator")
925 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
926 else if (name.empty())
927 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
929 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
933 case LFUN_WORD_UPCASE:
934 changeCase(cur, text_uppercase);
937 case LFUN_WORD_LOWCASE:
938 changeCase(cur, text_lowercase);
941 case LFUN_WORD_CAPITALIZE:
942 changeCase(cur, text_capitalization);
945 case LFUN_CHARS_TRANSPOSE:
950 cur.message(_("Paste"));
951 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), /**/);
952 cap::replaceSelection(cur);
955 string const arg = to_utf8(cmd.argument());
957 if (theClipboard().isInternal())
958 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
959 else if (theClipboard().hasGraphicsContents())
960 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
962 pasteClipboardText(cur, bv->buffer().errorList("Paste"));
963 } else if (isStrUnsignedInt(arg)) {
964 // we have a numerical argument
965 pasteFromStack(cur, bv->buffer().errorList("Paste"),
966 convert<unsigned int>(arg));
968 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
970 type = Clipboard::PdfGraphicsType;
971 else if (arg == "png")
972 type = Clipboard::PngGraphicsType;
973 else if (arg == "jpeg")
974 type = Clipboard::JpegGraphicsType;
975 else if (arg == "linkback")
976 type = Clipboard::LinkBackGraphicsType;
978 LASSERT(false, /**/);
980 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
983 bv->buffer().errors("Paste");
984 cur.clearSelection(); // bug 393
990 cutSelection(cur, true, true);
991 cur.message(_("Cut"));
996 cur.message(_("Copy"));
999 case LFUN_SERVER_GET_XY:
1000 cur.message(from_utf8(
1001 convert<string>(tm.cursorX(cur.top(), cur.boundary()))
1002 + ' ' + convert<string>(tm.cursorY(cur.top(), cur.boundary()))));
1005 case LFUN_SERVER_SET_XY: {
1008 istringstream is(to_utf8(cmd.argument()));
1011 lyxerr << "SETXY: Could not parse coordinates in '"
1012 << to_utf8(cmd.argument()) << endl;
1014 tm.setCursorFromCoordinates(cur, x, y);
1018 case LFUN_SERVER_GET_LAYOUT:
1019 cur.message(cur.paragraph().layout().name());
1023 docstring layout = cmd.argument();
1024 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1026 Paragraph const & para = cur.paragraph();
1027 docstring const old_layout = para.layout().name();
1028 DocumentClass const & tclass = bv->buffer().params().documentClass();
1031 layout = tclass.defaultLayoutName();
1033 if (para.forcePlainLayout())
1034 // in this case only the empty layout is allowed
1035 layout = tclass.plainLayoutName();
1036 else if (para.usePlainLayout()) {
1037 // in this case, default layout maps to empty layout
1038 if (layout == tclass.defaultLayoutName())
1039 layout = tclass.plainLayoutName();
1041 // otherwise, the empty layout maps to the default
1042 if (layout == tclass.plainLayoutName())
1043 layout = tclass.defaultLayoutName();
1046 bool hasLayout = tclass.hasLayout(layout);
1048 // If the entry is obsolete, use the new one instead.
1050 docstring const & obs = tclass[layout].obsoleted_by();
1056 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1057 from_utf8(N_(" not known")));
1061 bool change_layout = (old_layout != layout);
1063 if (!change_layout && cur.selection() &&
1064 cur.selBegin().pit() != cur.selEnd().pit())
1066 pit_type spit = cur.selBegin().pit();
1067 pit_type epit = cur.selEnd().pit() + 1;
1068 while (spit != epit) {
1069 if (pars_[spit].layout().name() != old_layout) {
1070 change_layout = true;
1078 setLayout(cur, layout);
1083 case LFUN_CLIPBOARD_PASTE:
1084 cur.clearSelection();
1085 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1086 cmd.argument() == "paragraph");
1087 bv->buffer().errors("Paste");
1090 case LFUN_PRIMARY_SELECTION_PASTE:
1091 pasteString(cur, theSelection().get(),
1092 cmd.argument() == "paragraph");
1095 case LFUN_UNICODE_INSERT: {
1096 if (cmd.argument().empty())
1098 docstring hexstring = cmd.argument();
1099 if (isHex(hexstring)) {
1100 char_type c = hexToInt(hexstring);
1101 if (c >= 32 && c < 0x10ffff) {
1102 lyxerr << "Inserting c: " << c << endl;
1103 docstring s = docstring(1, c);
1104 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1110 case LFUN_QUOTE_INSERT: {
1111 Paragraph & par = cur.paragraph();
1112 pos_type pos = cur.pos();
1113 BufferParams const & bufparams = bv->buffer().params();
1114 Layout const & style = par.layout();
1115 if (!style.pass_thru
1116 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1117 // this avoids a double undo
1118 // FIXME: should not be needed, ideally
1119 if (!cur.selection())
1121 cap::replaceSelection(cur);
1126 else if (cur.prevInset() && cur.prevInset()->isSpace())
1129 c = par.getChar(pos - 1);
1130 string arg = to_utf8(cmd.argument());
1131 cur.insert(new InsetQuotes(bv->buffer(), c, (arg == "single")
1132 ? InsetQuotes::SingleQuotes : InsetQuotes::DoubleQuotes));
1136 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1140 case LFUN_DATE_INSERT: {
1141 string const format = cmd.argument().empty()
1142 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1143 string const time = formatted_time(current_time(), format);
1144 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1148 case LFUN_MOUSE_TRIPLE:
1149 if (cmd.button() == mouse_button::button1) {
1158 case LFUN_MOUSE_DOUBLE:
1159 if (cmd.button() == mouse_button::button1) {
1160 selectWord(cur, WHOLE_WORD_STRICT);
1165 // Single-click on work area
1166 case LFUN_MOUSE_PRESS:
1167 // We are not marking a selection with the keyboard in any case.
1168 cur.bv().cursor().setMark(false);
1169 switch (cmd.button()) {
1170 case mouse_button::button1:
1172 if (!bv->mouseSetCursor(cur, cmd.argument() == "region-select"))
1173 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1176 case mouse_button::button2:
1177 // Middle mouse pasting.
1178 bv->mouseSetCursor(cur);
1179 if (!cap::selection()) {
1180 // There is no local selection in the current buffer, so try to
1181 // paste primary selection instead.
1182 lyx::dispatch(FuncRequest(LFUN_PRIMARY_SELECTION_PASTE,
1184 // Nothing else to do.
1188 // Copy the selection buffer to the clipboard stack, because we want it
1189 // to appear in the "Edit->Paste recent" menu.
1190 cap::copySelectionToStack();
1191 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1192 cur.updateFlags(Update::Force | Update::FitCursor);
1193 bv->buffer().errors("Paste");
1194 bv->buffer().markDirty();
1195 bv->cursor().finishUndo();
1198 case mouse_button::button3: {
1199 Cursor const & bvcur = cur.bv().cursor();
1200 // Don't do anything if we right-click a
1201 // selection, a context menu will popup.
1202 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1203 && cur < bvcur.selectionEnd()) {
1207 if (!bv->mouseSetCursor(cur, false))
1208 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1214 } // switch (cmd.button())
1217 case LFUN_MOUSE_MOTION: {
1218 // Mouse motion with right or middle mouse do nothing for now.
1219 if (cmd.button() != mouse_button::button1) {
1223 // ignore motions deeper nested than the real anchor
1224 Cursor & bvcur = cur.bv().cursor();
1225 if (!bvcur.anchor_.hasPart(cur)) {
1229 CursorSlice old = bvcur.top();
1231 int const wh = bv->workHeight();
1232 int const y = max(0, min(wh - 1, cmd.y));
1234 tm.setCursorFromCoordinates(cur, cmd.x, y);
1235 cur.setTargetX(cmd.x);
1237 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1239 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1240 // This is to allow jumping over large insets
1241 if (cur.top() == old) {
1243 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1245 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1247 // We continue with our existing selection or start a new one, so don't
1248 // reset the anchor.
1249 bvcur.setCursor(cur);
1250 bvcur.setSelection(true);
1251 if (cur.top() == old) {
1252 // We didn't move one iota, so no need to update the screen.
1253 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1260 case LFUN_MOUSE_RELEASE:
1261 switch (cmd.button()) {
1262 case mouse_button::button1:
1263 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1264 // If there is a new selection, update persistent selection;
1265 // otherwise, single click does not clear persistent selection
1267 if (cur.selection()) {
1268 // Finish selection. If double click,
1269 // cur is moved to the end of word by
1270 // selectWord but bvcur is current
1272 cur.bv().cursor().setSelection();
1273 // We might have removed an empty but drawn selection
1274 // (probably a margin)
1275 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1278 // FIXME: We could try to handle drag and drop of selection here.
1281 case mouse_button::button2:
1282 // Middle mouse pasting is handled at mouse press time,
1283 // see LFUN_MOUSE_PRESS.
1287 case mouse_button::button3:
1288 // Cursor was set at LFUN_MOUSE_PRESS time.
1289 // FIXME: If there is a selection we could try to handle a special
1290 // drag & drop context menu.
1294 case mouse_button::none:
1295 case mouse_button::button4:
1296 case mouse_button::button5:
1298 } // switch (cmd.button())
1302 case LFUN_SELF_INSERT: {
1303 if (cmd.argument().empty())
1306 // Automatically delete the currently selected
1307 // text and replace it with what is being
1308 // typed in now. Depends on lyxrc settings
1309 // "auto_region_delete", which defaults to
1312 if (lyxrc.auto_region_delete && cur.selection())
1313 cutSelection(cur, false, false);
1315 cur.clearSelection();
1317 docstring::const_iterator cit = cmd.argument().begin();
1318 docstring::const_iterator const end = cmd.argument().end();
1319 for (; cit != end; ++cit)
1320 bv->translateAndInsert(*cit, this, cur);
1323 moveCursor(cur, false);
1327 case LFUN_HYPERLINK_INSERT: {
1328 InsetCommandParams p(HYPERLINK_CODE);
1330 if (cur.selection()) {
1331 content = cur.selectionAsString(false);
1332 cutSelection(cur, true, false);
1334 p["target"] = (cmd.argument().empty()) ?
1335 content : cmd.argument();
1336 string const data = InsetCommand::params2string("href", p);
1337 if (p["target"].empty()) {
1338 bv->showDialog("href", data);
1340 FuncRequest fr(LFUN_INSET_INSERT, data);
1346 case LFUN_LABEL_INSERT: {
1347 InsetCommandParams p(LABEL_CODE);
1348 // Try to generate a valid label
1349 p["name"] = (cmd.argument().empty()) ?
1350 cur.getPossibleLabel() :
1352 string const data = InsetCommand::params2string("label", p);
1354 if (cmd.argument().empty()) {
1355 bv->showDialog("label", data);
1357 FuncRequest fr(LFUN_INSET_INSERT, data);
1363 case LFUN_INFO_INSERT: {
1365 if (cmd.argument().empty() && cur.selection()) {
1366 // if command argument is empty use current selection as parameter.
1367 docstring ds = cur.selectionAsString(false);
1368 cutSelection(cur, true, false);
1369 FuncRequest cmd0(cmd, ds);
1370 inset = createInset(cur.bv().buffer(), cmd0);
1372 inset = createInset(cur.bv().buffer(), cmd);
1376 insertInset(cur, inset);
1380 case LFUN_CAPTION_INSERT:
1381 case LFUN_FOOTNOTE_INSERT:
1382 case LFUN_NOTE_INSERT:
1383 case LFUN_FLEX_INSERT:
1384 case LFUN_BOX_INSERT:
1385 case LFUN_BRANCH_INSERT:
1386 case LFUN_ERT_INSERT:
1387 case LFUN_LISTING_INSERT:
1388 case LFUN_MARGINALNOTE_INSERT:
1389 case LFUN_OPTIONAL_INSERT:
1390 case LFUN_INDEX_INSERT:
1391 // Open the inset, and move the current selection
1393 doInsertInset(cur, this, cmd, true, true);
1395 // Some insets are numbered, others are shown in the outline pane so
1396 // let's update the labels and the toc backend.
1397 updateLabels(bv->buffer());
1400 case LFUN_TABULAR_INSERT:
1401 // if there were no arguments, just open the dialog
1402 if (doInsertInset(cur, this, cmd, false, true))
1405 bv->showDialog("tabularcreate");
1409 case LFUN_FLOAT_INSERT:
1410 case LFUN_FLOAT_WIDE_INSERT:
1411 case LFUN_WRAP_INSERT: {
1412 bool content = cur.selection(); // will some text be moved into the inset?
1414 doInsertInset(cur, this, cmd, true, true);
1416 ParagraphList & pars = cur.text()->paragraphs();
1418 DocumentClass const & tclass = bv->buffer().params().documentClass();
1420 // add a separate paragraph for the caption inset
1421 pars.push_back(Paragraph());
1422 pars.back().setInsetOwner(&pars[0].inInset());
1423 pars.back().setPlainOrDefaultLayout(tclass);
1424 int cap_pit = pars.size() - 1;
1426 // if an empty inset was created, we create an additional empty
1427 // paragraph at the bottom so that the user can choose where to put
1428 // the graphics (or table).
1430 pars.push_back(Paragraph());
1431 pars.back().setInsetOwner(&pars[0].inInset());
1432 pars.back().setPlainOrDefaultLayout(tclass);
1435 // reposition the cursor to the caption
1436 cur.pit() = cap_pit;
1438 // FIXME: This Text/Cursor dispatch handling is a mess!
1439 // We cannot use Cursor::dispatch here it needs access to up to
1441 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1442 cur.text()->dispatch(cur, cmd_caption);
1443 cur.updateFlags(Update::Force);
1444 // FIXME: When leaving the Float (or Wrap) inset we should
1445 // delete any empty paragraph left above or below the
1450 case LFUN_NOMENCL_INSERT: {
1451 InsetCommandParams p(NOMENCL_CODE);
1452 if (cmd.argument().empty())
1453 p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
1455 p["symbol"] = cmd.argument();
1456 string const data = InsetCommand::params2string("nomenclature", p);
1457 bv->showDialog("nomenclature", data);
1461 case LFUN_INDEX_PRINT:
1462 case LFUN_NOMENCL_PRINT:
1463 case LFUN_TOC_INSERT:
1464 case LFUN_LINE_INSERT:
1465 case LFUN_NEWPAGE_INSERT:
1467 doInsertInset(cur, this, cmd, false, false);
1471 case LFUN_DEPTH_DECREMENT:
1472 changeDepth(cur, DEC_DEPTH);
1475 case LFUN_DEPTH_INCREMENT:
1476 changeDepth(cur, INC_DEPTH);
1479 case LFUN_MATH_DISPLAY:
1480 mathDispatch(cur, cmd, true);
1483 case LFUN_MATH_MODE:
1484 if (cmd.argument() == "on")
1485 // don't pass "on" as argument
1486 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1488 mathDispatch(cur, cmd, false);
1491 case LFUN_MATH_MACRO:
1492 if (cmd.argument().empty())
1493 cur.errorMessage(from_utf8(N_("Missing argument")));
1495 string s = to_utf8(cmd.argument());
1496 string const s1 = token(s, ' ', 1);
1497 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1498 string const s2 = token(s, ' ', 2);
1499 MacroType type = MacroTypeNewcommand;
1501 type = MacroTypeDef;
1502 MathMacroTemplate * inset = new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, false, type);
1503 inset->setBuffer(bv->buffer());
1504 insertInset(cur, inset);
1506 // enter macro inset and select the name
1508 cur.top().pos() = cur.top().lastpos();
1510 cur.setSelection(true);
1511 cur.top().pos() = 0;
1515 // passthrough hat and underscore outside mathed:
1516 case LFUN_MATH_SUBSCRIPT:
1517 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1519 case LFUN_MATH_SUPERSCRIPT:
1520 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1523 case LFUN_MATH_INSERT:
1524 case LFUN_MATH_MATRIX:
1525 case LFUN_MATH_DELIM:
1526 case LFUN_MATH_BIGDELIM: {
1528 cap::replaceSelection(cur);
1529 cur.insert(new InsetMathHull(hullSimple));
1530 checkAndActivateInset(cur, true);
1531 LASSERT(cur.inMathed(), /**/);
1536 case LFUN_FONT_EMPH: {
1537 Font font(ignore_font, ignore_language);
1538 font.fontInfo().setEmph(FONT_TOGGLE);
1539 toggleAndShow(cur, this, font);
1543 case LFUN_FONT_BOLD:
1544 case LFUN_FONT_BOLDSYMBOL: {
1545 Font font(ignore_font, ignore_language);
1546 font.fontInfo().setSeries(BOLD_SERIES);
1547 toggleAndShow(cur, this, font);
1551 case LFUN_FONT_NOUN: {
1552 Font font(ignore_font, ignore_language);
1553 font.fontInfo().setNoun(FONT_TOGGLE);
1554 toggleAndShow(cur, this, font);
1558 case LFUN_FONT_TYPEWRITER: {
1559 Font font(ignore_font, ignore_language);
1560 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1561 toggleAndShow(cur, this, font);
1565 case LFUN_FONT_SANS: {
1566 Font font(ignore_font, ignore_language);
1567 font.fontInfo().setFamily(SANS_FAMILY);
1568 toggleAndShow(cur, this, font);
1572 case LFUN_FONT_ROMAN: {
1573 Font font(ignore_font, ignore_language);
1574 font.fontInfo().setFamily(ROMAN_FAMILY);
1575 toggleAndShow(cur, this, font);
1579 case LFUN_FONT_DEFAULT: {
1580 Font font(inherit_font, ignore_language);
1581 toggleAndShow(cur, this, font);
1585 case LFUN_FONT_UNDERLINE: {
1586 Font font(ignore_font, ignore_language);
1587 font.fontInfo().setUnderbar(FONT_TOGGLE);
1588 toggleAndShow(cur, this, font);
1592 case LFUN_FONT_SIZE: {
1593 Font font(ignore_font, ignore_language);
1594 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1595 toggleAndShow(cur, this, font);
1599 case LFUN_LANGUAGE: {
1600 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1603 Font font(ignore_font, lang);
1604 toggleAndShow(cur, this, font);
1608 case LFUN_TEXTSTYLE_APPLY:
1609 toggleAndShow(cur, this, freefont, toggleall);
1610 cur.message(_("Character set"));
1613 // Set the freefont using the contents of \param data dispatched from
1614 // the frontends and apply it at the current cursor location.
1615 case LFUN_TEXTSTYLE_UPDATE: {
1618 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1621 toggleAndShow(cur, this, freefont, toggleall);
1622 cur.message(_("Character set"));
1624 lyxerr << "Argument not ok";
1629 case LFUN_FINISHED_LEFT:
1630 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
1631 // We're leaving an inset, going left. If the inset is LTR, we're
1632 // leaving from the front, so we should not move (remain at --- but
1633 // not in --- the inset). If the inset is RTL, move left, without
1634 // entering the inset itself; i.e., move to after the inset.
1635 if (cur.paragraph().getFontSettings(
1636 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1637 cursorVisLeft(cur, true);
1640 case LFUN_FINISHED_RIGHT:
1641 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
1642 // We're leaving an inset, going right. If the inset is RTL, we're
1643 // leaving from the front, so we should not move (remain at --- but
1644 // not in --- the inset). If the inset is LTR, move right, without
1645 // entering the inset itself; i.e., move to after the inset.
1646 if (!cur.paragraph().getFontSettings(
1647 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1648 cursorVisRight(cur, true);
1651 case LFUN_FINISHED_BACKWARD:
1652 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
1655 case LFUN_FINISHED_FORWARD:
1656 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
1658 cur.setCurrentFont();
1661 case LFUN_LAYOUT_PARAGRAPH: {
1663 params2string(cur.paragraph(), data);
1664 data = "show\n" + data;
1665 bv->showDialog("paragraph", data);
1669 case LFUN_PARAGRAPH_UPDATE: {
1671 params2string(cur.paragraph(), data);
1673 // Will the paragraph accept changes from the dialog?
1675 cur.inset().allowParagraphCustomization(cur.idx());
1677 data = "update " + convert<string>(accept) + '\n' + data;
1678 bv->updateDialog("paragraph", data);
1682 case LFUN_ACCENT_UMLAUT:
1683 case LFUN_ACCENT_CIRCUMFLEX:
1684 case LFUN_ACCENT_GRAVE:
1685 case LFUN_ACCENT_ACUTE:
1686 case LFUN_ACCENT_TILDE:
1687 case LFUN_ACCENT_CEDILLA:
1688 case LFUN_ACCENT_MACRON:
1689 case LFUN_ACCENT_DOT:
1690 case LFUN_ACCENT_UNDERDOT:
1691 case LFUN_ACCENT_UNDERBAR:
1692 case LFUN_ACCENT_CARON:
1693 case LFUN_ACCENT_BREVE:
1694 case LFUN_ACCENT_TIE:
1695 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1696 case LFUN_ACCENT_CIRCLE:
1697 case LFUN_ACCENT_OGONEK:
1698 theLyXFunc().handleKeyFunc(cmd.action);
1699 if (!cmd.argument().empty())
1700 // FIXME: Are all these characters encoded in one byte in utf8?
1701 bv->translateAndInsert(cmd.argument()[0], this, cur);
1704 case LFUN_FLOAT_LIST_INSERT: {
1705 DocumentClass const & tclass = bv->buffer().params().documentClass();
1706 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1708 if (cur.selection())
1709 cutSelection(cur, true, false);
1710 breakParagraph(cur);
1712 if (cur.lastpos() != 0) {
1713 cursorBackward(cur);
1714 breakParagraph(cur);
1717 docstring const laystr = cur.inset().usePlainLayout() ?
1718 tclass.plainLayoutName() :
1719 tclass.defaultLayoutName();
1720 setLayout(cur, laystr);
1721 ParagraphParameters p;
1722 // FIXME If this call were replaced with one to clearParagraphParams(),
1723 // then we could get rid of this method altogether.
1724 setParagraphs(cur, p);
1725 // FIXME This should be simplified when InsetFloatList takes a
1726 // Buffer in its constructor.
1727 InsetFloatList * ifl = new InsetFloatList(to_utf8(cmd.argument()));
1728 ifl->setBuffer(bv->buffer());
1729 insertInset(cur, ifl);
1732 lyxerr << "Non-existent float type: "
1733 << to_utf8(cmd.argument()) << endl;
1738 case LFUN_CHANGE_ACCEPT: {
1739 acceptOrRejectChanges(cur, ACCEPT);
1743 case LFUN_CHANGE_REJECT: {
1744 acceptOrRejectChanges(cur, REJECT);
1748 case LFUN_THESAURUS_ENTRY: {
1749 docstring arg = cmd.argument();
1751 arg = cur.selectionAsString(false);
1753 if (arg.size() > 100 || arg.empty()) {
1754 // Get word or selection
1755 selectWordWhenUnderCursor(cur, WHOLE_WORD);
1756 arg = cur.selectionAsString(false);
1759 bv->showDialog("thesaurus", to_utf8(arg));
1763 case LFUN_PARAGRAPH_PARAMS_APPLY: {
1764 // Given data, an encoding of the ParagraphParameters
1765 // generated in the Paragraph dialog, this function sets
1766 // the current paragraph, or currently selected paragraphs,
1768 // NOTE: This function overrides all existing settings.
1769 setParagraphs(cur, cmd.argument());
1770 cur.message(_("Paragraph layout set"));
1774 case LFUN_PARAGRAPH_PARAMS: {
1775 // Given data, an encoding of the ParagraphParameters as we'd
1776 // find them in a LyX file, this function modifies the current paragraph,
1777 // or currently selected paragraphs.
1778 // NOTE: This function only modifies, and does not override, existing
1780 setParagraphs(cur, cmd.argument(), true);
1781 cur.message(_("Paragraph layout set"));
1786 if (cur.selection()) {
1787 cur.setSelection(false);
1790 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
1791 // correct, but I'm not 100% sure -- dov, 071019
1792 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1796 case LFUN_OUTLINE_UP:
1797 outline(OutlineUp, cur);
1798 setCursor(cur, cur.pit(), 0);
1799 updateLabels(cur.buffer());
1803 case LFUN_OUTLINE_DOWN:
1804 outline(OutlineDown, cur);
1805 setCursor(cur, cur.pit(), 0);
1806 updateLabels(cur.buffer());
1810 case LFUN_OUTLINE_IN:
1811 outline(OutlineIn, cur);
1812 updateLabels(cur.buffer());
1816 case LFUN_OUTLINE_OUT:
1817 outline(OutlineOut, cur);
1818 updateLabels(cur.buffer());
1823 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
1828 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
1830 // FIXME: The cursor flag is reset two lines below
1831 // so we need to check here if some of the LFUN did touch that.
1832 // for now only Text::erase() and Text::backspace() do that.
1833 // The plan is to verify all the LFUNs and then to remove this
1834 // singleParUpdate boolean altogether.
1835 if (cur.result().update() & Update::Force) {
1836 singleParUpdate = false;
1840 // FIXME: the following code should go in favor of fine grained
1841 // update flag treatment.
1842 if (singleParUpdate) {
1843 // Inserting characters does not change par height in general. So, try
1844 // to update _only_ this paragraph. BufferView will detect if a full
1845 // metrics update is needed anyway.
1846 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1851 && &oldTopSlice.inset() == &cur.inset()
1852 && oldTopSlice.idx() == cur.idx()
1853 && !sel // sel is a backup of cur.selection() at the biginning of the function.
1854 && !cur.selection())
1855 // FIXME: it would be better if we could just do this
1857 //if (cur.result().update() != Update::FitCursor)
1860 // But some LFUNs do not set Update::FitCursor when needed, so we
1861 // do it for all. This is not very harmfull as FitCursor will provoke
1862 // a full redraw only if needed but still, a proper review of all LFUN
1863 // should be done and this needsUpdate boolean can then be removed.
1864 cur.updateFlags(Update::FitCursor);
1866 cur.updateFlags(Update::Force | Update::FitCursor);
1870 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
1871 FuncStatus & flag) const
1873 LASSERT(cur.text() == this, /**/);
1875 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
1877 InsetCode code = NO_CODE;
1879 switch (cmd.action) {
1881 case LFUN_DEPTH_DECREMENT:
1882 enable = changeDepthAllowed(cur, DEC_DEPTH);
1885 case LFUN_DEPTH_INCREMENT:
1886 enable = changeDepthAllowed(cur, INC_DEPTH);
1890 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1893 case LFUN_DIALOG_SHOW_NEW_INSET:
1894 if (cmd.argument() == "bibitem")
1895 code = BIBITEM_CODE;
1896 else if (cmd.argument() == "bibtex")
1898 else if (cmd.argument() == "box")
1900 else if (cmd.argument() == "branch")
1902 else if (cmd.argument() == "citation")
1904 else if (cmd.argument() == "ert")
1906 else if (cmd.argument() == "external")
1907 code = EXTERNAL_CODE;
1908 else if (cmd.argument() == "float")
1910 else if (cmd.argument() == "graphics")
1911 code = GRAPHICS_CODE;
1912 else if (cmd.argument() == "href")
1913 code = HYPERLINK_CODE;
1914 else if (cmd.argument() == "include")
1915 code = INCLUDE_CODE;
1916 else if (cmd.argument() == "index")
1918 else if (cmd.argument() == "nomenclature")
1919 code = NOMENCL_CODE;
1920 else if (cmd.argument() == "label")
1922 else if (cmd.argument() == "note")
1924 else if (cmd.argument() == "ref")
1926 else if (cmd.argument() == "space")
1928 else if (cmd.argument() == "toc")
1930 else if (cmd.argument() == "vspace")
1932 else if (cmd.argument() == "wrap")
1934 else if (cmd.argument() == "listings")
1935 code = LISTINGS_CODE;
1938 case LFUN_ERT_INSERT:
1941 case LFUN_LISTING_INSERT:
1942 code = LISTINGS_CODE;
1943 // not allowed in description items
1944 enable = !inDescriptionItem(cur);
1946 case LFUN_FOOTNOTE_INSERT:
1949 case LFUN_TABULAR_INSERT:
1950 code = TABULAR_CODE;
1952 case LFUN_MARGINALNOTE_INSERT:
1955 case LFUN_FLOAT_INSERT:
1956 case LFUN_FLOAT_WIDE_INSERT:
1958 // not allowed in description items
1959 enable = !inDescriptionItem(cur);
1961 case LFUN_WRAP_INSERT:
1963 // not allowed in description items
1964 enable = !inDescriptionItem(cur);
1966 case LFUN_FLOAT_LIST_INSERT:
1967 code = FLOAT_LIST_CODE;
1969 case LFUN_CAPTION_INSERT:
1970 code = CAPTION_CODE;
1971 // not allowed in description items
1972 enable = !inDescriptionItem(cur);
1974 case LFUN_NOTE_INSERT:
1976 // in commands (sections etc.) and description items,
1977 // only Notes are allowed
1978 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
1979 (!cur.paragraph().layout().isCommand()
1980 && !inDescriptionItem(cur)));
1982 case LFUN_FLEX_INSERT: {
1984 string s = cmd.getArg(0);
1986 cur.buffer().params().documentClass().insetLayout(from_utf8(s));
1987 if (il.lyxtype() != "charstyle" &&
1988 il.lyxtype() != "custom" &&
1989 il.lyxtype() != "element" &&
1990 il.lyxtype ()!= "standard")
1994 case LFUN_BOX_INSERT:
1997 case LFUN_BRANCH_INSERT:
1999 if (cur.buffer().masterBuffer()->params().branchlist().empty())
2002 case LFUN_LABEL_INSERT:
2005 case LFUN_INFO_INSERT:
2008 case LFUN_OPTIONAL_INSERT:
2010 enable = cur.paragraph().insetList().count(OPTARG_CODE)
2011 < cur.paragraph().layout().optionalargs;
2013 case LFUN_INDEX_INSERT:
2016 case LFUN_INDEX_PRINT:
2017 code = INDEX_PRINT_CODE;
2019 case LFUN_NOMENCL_INSERT:
2020 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2024 code = NOMENCL_CODE;
2026 case LFUN_NOMENCL_PRINT:
2027 code = NOMENCL_PRINT_CODE;
2029 case LFUN_TOC_INSERT:
2032 case LFUN_HYPERLINK_INSERT:
2033 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2037 code = HYPERLINK_CODE;
2039 case LFUN_QUOTE_INSERT:
2040 // always allow this, since we will inset a raw quote
2041 // if an inset is not allowed.
2043 case LFUN_SPECIALCHAR_INSERT:
2044 code = SPECIALCHAR_CODE;
2046 case LFUN_SPACE_INSERT:
2047 // slight hack: we know this is allowed in math mode
2052 case LFUN_INSET_MODIFY:
2053 // We need to disable this, because we may get called for a
2055 // InsetTabular::getStatus() -> InsetText::getStatus()
2056 // and we don't handle LFUN_INSET_MODIFY.
2060 case LFUN_FONT_EMPH:
2061 flag.setOnOff(fontinfo.emph() == FONT_ON);
2064 case LFUN_FONT_NOUN:
2065 flag.setOnOff(fontinfo.noun() == FONT_ON);
2068 case LFUN_FONT_BOLD:
2069 case LFUN_FONT_BOLDSYMBOL:
2070 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2073 case LFUN_FONT_SANS:
2074 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2077 case LFUN_FONT_ROMAN:
2078 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2081 case LFUN_FONT_TYPEWRITER:
2082 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2087 enable = cur.selection();
2091 if (cmd.argument().empty()) {
2092 if (theClipboard().isInternal())
2093 enable = cap::numberOfSelections() > 0;
2095 enable = !theClipboard().empty();
2099 // we have an argument
2100 string const arg = to_utf8(cmd.argument());
2101 if (isStrUnsignedInt(arg)) {
2102 // it's a number and therefore means the internal stack
2103 unsigned int n = convert<unsigned int>(arg);
2104 enable = cap::numberOfSelections() > n;
2108 // explicit graphics type?
2109 if ((arg == "pdf" && theClipboard().hasGraphicsContents(Clipboard::PdfGraphicsType))
2110 || (arg == "png" && theClipboard().hasGraphicsContents(Clipboard::PngGraphicsType))
2111 || (arg == "jpeg" && theClipboard().hasGraphicsContents(Clipboard::JpegGraphicsType))
2112 || (arg == "linkback" && theClipboard().hasGraphicsContents(Clipboard::LinkBackGraphicsType))) {
2122 case LFUN_CLIPBOARD_PASTE:
2123 enable = !theClipboard().empty();
2126 case LFUN_PRIMARY_SELECTION_PASTE:
2127 enable = cur.selection() || !theSelection().empty();
2130 case LFUN_PARAGRAPH_MOVE_UP:
2131 enable = cur.pit() > 0 && !cur.selection();
2134 case LFUN_PARAGRAPH_MOVE_DOWN:
2135 enable = cur.pit() < cur.lastpit() && !cur.selection();
2138 case LFUN_INSET_DISSOLVE:
2139 if (!cmd.argument().empty()) {
2140 InsetLayout il = cur.inset().getLayout(cur.buffer().params());
2141 enable = cur.inset().lyxCode() == FLEX_CODE
2142 && il.lyxtype() == to_utf8(cmd.argument());
2144 enable = !isMainText(cur.bv().buffer())
2145 && cur.inset().nargs() == 1;
2149 case LFUN_CHANGE_ACCEPT:
2150 case LFUN_CHANGE_REJECT:
2151 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
2152 // In principle, these LFUNs should only be enabled if there
2153 // is a change at the current position/in the current selection.
2154 // However, without proper optimizations, this will inevitably
2155 // result in unacceptable performance - just imagine a user who
2156 // wants to select the complete content of a long document.
2160 case LFUN_OUTLINE_UP:
2161 case LFUN_OUTLINE_DOWN:
2162 case LFUN_OUTLINE_IN:
2163 case LFUN_OUTLINE_OUT:
2164 // FIXME: LyX is not ready for outlining within inset.
2165 enable = isMainText(cur.bv().buffer())
2166 && cur.paragraph().layout().toclevel != Layout::NOT_IN_TOC;
2169 case LFUN_NEWLINE_INSERT:
2170 // LaTeX restrictions (labels or empty par)
2171 enable = (cur.pos() > cur.paragraph().beginOfBody());
2174 case LFUN_SET_GRAPHICS_GROUP: {
2175 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
2179 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
2183 case LFUN_NEWPAGE_INSERT:
2184 // not allowed in description items
2185 enable = !inDescriptionItem(cur);
2188 case LFUN_WORD_DELETE_FORWARD:
2189 case LFUN_WORD_DELETE_BACKWARD:
2190 case LFUN_LINE_DELETE:
2191 case LFUN_WORD_FORWARD:
2192 case LFUN_WORD_BACKWARD:
2193 case LFUN_WORD_RIGHT:
2194 case LFUN_WORD_LEFT:
2195 case LFUN_CHAR_FORWARD:
2196 case LFUN_CHAR_FORWARD_SELECT:
2197 case LFUN_CHAR_BACKWARD:
2198 case LFUN_CHAR_BACKWARD_SELECT:
2199 case LFUN_CHAR_LEFT:
2200 case LFUN_CHAR_LEFT_SELECT:
2201 case LFUN_CHAR_RIGHT:
2202 case LFUN_CHAR_RIGHT_SELECT:
2204 case LFUN_UP_SELECT:
2206 case LFUN_DOWN_SELECT:
2207 case LFUN_PARAGRAPH_UP_SELECT:
2208 case LFUN_PARAGRAPH_DOWN_SELECT:
2209 case LFUN_LINE_BEGIN_SELECT:
2210 case LFUN_LINE_END_SELECT:
2211 case LFUN_WORD_FORWARD_SELECT:
2212 case LFUN_WORD_BACKWARD_SELECT:
2213 case LFUN_WORD_RIGHT_SELECT:
2214 case LFUN_WORD_LEFT_SELECT:
2215 case LFUN_WORD_SELECT:
2216 case LFUN_PARAGRAPH_UP:
2217 case LFUN_PARAGRAPH_DOWN:
2218 case LFUN_LINE_BEGIN:
2220 case LFUN_CHAR_DELETE_FORWARD:
2221 case LFUN_CHAR_DELETE_BACKWARD:
2222 case LFUN_BREAK_PARAGRAPH:
2223 case LFUN_PARAGRAPH_SPACING:
2224 case LFUN_INSET_INSERT:
2225 case LFUN_WORD_UPCASE:
2226 case LFUN_WORD_LOWCASE:
2227 case LFUN_WORD_CAPITALIZE:
2228 case LFUN_CHARS_TRANSPOSE:
2229 case LFUN_SERVER_GET_XY:
2230 case LFUN_SERVER_SET_XY:
2231 case LFUN_SERVER_GET_LAYOUT:
2233 case LFUN_DATE_INSERT:
2234 case LFUN_SELF_INSERT:
2235 case LFUN_LINE_INSERT:
2236 case LFUN_MATH_DISPLAY:
2237 case LFUN_MATH_MODE:
2238 case LFUN_MATH_MACRO:
2239 case LFUN_MATH_MATRIX:
2240 case LFUN_MATH_DELIM:
2241 case LFUN_MATH_BIGDELIM:
2242 case LFUN_MATH_INSERT:
2243 case LFUN_MATH_SUBSCRIPT:
2244 case LFUN_MATH_SUPERSCRIPT:
2245 case LFUN_FONT_DEFAULT:
2246 case LFUN_FONT_UNDERLINE:
2247 case LFUN_FONT_SIZE:
2249 case LFUN_TEXTSTYLE_APPLY:
2250 case LFUN_TEXTSTYLE_UPDATE:
2251 case LFUN_LAYOUT_PARAGRAPH:
2252 case LFUN_PARAGRAPH_UPDATE:
2253 case LFUN_ACCENT_UMLAUT:
2254 case LFUN_ACCENT_CIRCUMFLEX:
2255 case LFUN_ACCENT_GRAVE:
2256 case LFUN_ACCENT_ACUTE:
2257 case LFUN_ACCENT_TILDE:
2258 case LFUN_ACCENT_CEDILLA:
2259 case LFUN_ACCENT_MACRON:
2260 case LFUN_ACCENT_DOT:
2261 case LFUN_ACCENT_UNDERDOT:
2262 case LFUN_ACCENT_UNDERBAR:
2263 case LFUN_ACCENT_CARON:
2264 case LFUN_ACCENT_BREVE:
2265 case LFUN_ACCENT_TIE:
2266 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2267 case LFUN_ACCENT_CIRCLE:
2268 case LFUN_ACCENT_OGONEK:
2269 case LFUN_THESAURUS_ENTRY:
2270 case LFUN_PARAGRAPH_PARAMS_APPLY:
2271 case LFUN_PARAGRAPH_PARAMS:
2273 case LFUN_BUFFER_END:
2274 case LFUN_BUFFER_BEGIN:
2275 case LFUN_BUFFER_BEGIN_SELECT:
2276 case LFUN_BUFFER_END_SELECT:
2277 case LFUN_UNICODE_INSERT:
2278 // these are handled in our dispatch()
2287 && (cur.empty() || !cur.inset().insetAllowed(code)))
2290 flag.setEnabled(enable);
2295 void Text::pasteString(Cursor & cur, docstring const & clip,
2298 cur.clearSelection();
2299 if (!clip.empty()) {
2302 insertStringAsParagraphs(cur, clip);
2304 insertStringAsLines(cur, clip);
2309 // FIXME: an item inset would make things much easier.
2310 bool Text::inDescriptionItem(Cursor & cur) const
2312 Paragraph & par = cur.paragraph();
2313 pos_type const pos = cur.pos();
2314 pos_type const body_pos = par.beginOfBody();
2316 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
2317 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
2318 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
2321 return (pos < body_pos
2323 && (pos == 0 || par.getChar(pos - 1) != ' ')));