3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Alfredo Braunstein
9 * \author Angus Leeming
11 * \author André Pönitz
13 * Full author contact details are available in file CREDITS.
21 #include "BranchList.h"
22 #include "FloatList.h"
23 #include "FuncStatus.h"
25 #include "buffer_funcs.h"
26 #include "BufferParams.h"
27 #include "BufferView.h"
29 #include "CutAndPaste.h"
30 #include "DispatchResult.h"
31 #include "ErrorList.h"
33 #include "FuncRequest.h"
34 #include "InsetList.h"
38 #include "LyXAction.h"
42 #include "Paragraph.h"
43 #include "paragraph_funcs.h"
44 #include "ParagraphParameters.h"
45 #include "TextClass.h"
46 #include "TextMetrics.h"
49 #include "frontends/Clipboard.h"
50 #include "frontends/Selection.h"
52 #include "insets/InsetCollapsable.h"
53 #include "insets/InsetCommand.h"
54 #include "insets/InsetFloatList.h"
55 #include "insets/InsetNewline.h"
56 #include "insets/InsetQuotes.h"
57 #include "insets/InsetSpecialChar.h"
58 #include "insets/InsetText.h"
59 #include "insets/InsetInfo.h"
60 #include "insets/InsetGraphics.h"
61 #include "insets/InsetGraphicsParams.h"
63 #include "support/convert.h"
64 #include "support/debug.h"
65 #include "support/gettext.h"
66 #include "support/lstrings.h"
67 #include "support/lyxtime.h"
69 #include "mathed/InsetMathHull.h"
70 #include "mathed/MathMacroTemplate.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);
132 #ifdef ENABLE_ASSERTIONS
133 const int old_pos = cur.pos();
135 cur.insert(new InsetMathHull(hullSimple));
136 #ifdef ENABLE_ASSERTIONS
137 LASSERT(old_pos == cur.pos(), /**/);
139 cur.nextInset()->edit(cur, true);
140 // don't do that also for LFUN_MATH_MODE
141 // unless you want end up with always changing
142 // to mathrm when opening an inlined inset --
143 // I really hate "LyXfunc overloading"...
145 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
146 // Avoid an unnecessary undo step if cmd.argument
148 if (!cmd.argument().empty())
149 cur.dispatch(FuncRequest(LFUN_MATH_INSERT,
152 // create a macro if we see "\\newcommand"
153 // somewhere, and an ordinary formula
155 if (sel.find(from_ascii("\\newcommand")) == string::npos
156 && sel.find(from_ascii("\\newlyxcommand")) == string::npos
157 && sel.find(from_ascii("\\def")) == string::npos)
159 InsetMathHull * formula = new InsetMathHull;
160 istringstream is(to_utf8(sel));
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 Buffer & buffer = cur.bv().buffer();
190 BufferParams const & bparams = buffer.params();
191 Inset * inset = createInset(buffer, cmd);
195 if (InsetCollapsable * ci = inset->asInsetCollapsable())
196 ci->setLayout(bparams);
199 if (cmd.action == LFUN_INDEX_INSERT) {
200 docstring ds = subst(text->getStringToIndex(cur), '\n', ' ');
201 text->insertInset(cur, inset);
203 inset->edit(cur, true);
204 // Now put this into inset
205 static_cast<InsetCollapsable *>(inset)->text().insertStringAsParagraphs(cur, ds);
210 if (cur.selection()) {
211 cutSelection(cur, false, pastesel);
212 cur.clearSelection();
215 text->insertInset(cur, inset);
218 inset->edit(cur, true);
220 if (!gotsel || !pastesel)
223 pasteFromStack(cur, cur.buffer().errorList("Paste"), 0);
224 cur.buffer().errors("Paste");
225 cur.clearSelection(); // bug 393
227 InsetText * insetText = dynamic_cast<InsetText *>(inset);
228 if (insetText && (!insetText->allowMultiPar() || cur.lastpit() == 0)) {
229 // reset first par to default
230 cur.text()->paragraphs().begin()
231 ->setPlainOrDefaultLayout(bparams.documentClass());
234 // Merge multiple paragraphs -- hack
235 while (cur.lastpit() > 0)
236 mergeParagraph(bparams, cur.text()->paragraphs(), 0);
237 cur.leaveInset(*inset);
239 cur.leaveInset(*inset);
240 // reset surrounding par to default
241 DocumentClass const & dc = bparams.documentClass();
242 docstring const layoutname = inset->usePlainLayout()
243 ? dc.plainLayoutName()
244 : dc.defaultLayoutName();
245 text->setLayout(cur, layoutname);
252 string const freefont2string()
254 return freefont.toString(toggleall);
258 /// the type of outline operation
260 OutlineUp, // Move this header with text down
261 OutlineDown, // Move this header with text up
262 OutlineIn, // Make this header deeper
263 OutlineOut // Make this header shallower
267 static void outline(OutlineOp mode, Cursor & cur)
269 Buffer & buf = cur.buffer();
270 pit_type & pit = cur.pit();
271 ParagraphList & pars = buf.text().paragraphs();
272 ParagraphList::iterator bgn = pars.begin();
273 // The first paragraph of the area to be copied:
274 ParagraphList::iterator start = boost::next(bgn, pit);
275 // The final paragraph of area to be copied:
276 ParagraphList::iterator finish = start;
277 ParagraphList::iterator end = pars.end();
279 DocumentClass const & tc = buf.params().documentClass();
281 int const thistoclevel = start->layout().toclevel;
284 // Move out (down) from this section header
287 // Seek the one (on same level) below
288 for (; finish != end; ++finish) {
289 toclevel = finish->layout().toclevel;
290 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel) {
297 if (start == pars.begin())
300 ParagraphList::iterator dest = start;
301 // Move out (up) from this header
304 // Search previous same-level header above
307 toclevel = dest->layout().toclevel;
309 && (toclevel == Layout::NOT_IN_TOC
310 || toclevel > thistoclevel));
311 // Not found; do nothing
312 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
314 pit_type const newpit = distance(bgn, dest);
315 pit_type const len = distance(start, finish);
316 pit_type const deletepit = pit + len;
317 buf.undo().recordUndo(cur, ATOMIC_UNDO, newpit, deletepit - 1);
318 pars.insert(dest, start, finish);
319 start = boost::next(pars.begin(), deletepit);
321 pars.erase(start, finish);
328 // Go one down from *this* header:
329 ParagraphList::iterator dest = boost::next(finish, 1);
330 // Go further down to find header to insert in front of:
331 for (; dest != end; ++dest) {
332 toclevel = dest->layout().toclevel;
333 if (toclevel != Layout::NOT_IN_TOC
334 && toclevel <= thistoclevel) {
338 // One such was found:
339 pit_type newpit = distance(bgn, dest);
340 pit_type const len = distance(start, finish);
341 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, newpit - 1);
342 pars.insert(dest, start, finish);
343 start = boost::next(bgn, pit);
345 pars.erase(start, finish);
349 pit_type const len = distance(start, finish);
350 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
351 for (; start != finish; ++start) {
352 toclevel = start->layout().toclevel;
353 if (toclevel == Layout::NOT_IN_TOC)
355 DocumentClass::const_iterator lit = tc.begin();
356 DocumentClass::const_iterator len = tc.end();
357 for (; lit != len; ++lit) {
358 if (lit->toclevel == toclevel + 1 &&
359 start->layout().labeltype == lit->labeltype) {
360 start->setLayout(*lit);
368 pit_type const len = distance(start, finish);
369 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
370 for (; start != finish; ++start) {
371 toclevel = start->layout().toclevel;
372 if (toclevel == Layout::NOT_IN_TOC)
374 DocumentClass::const_iterator lit = tc.begin();
375 DocumentClass::const_iterator len = tc.end();
376 for (; lit != len; ++lit) {
377 if (lit->toclevel == toclevel - 1 &&
378 start->layout().labeltype == lit->labeltype) {
379 start->setLayout(*lit);
390 void Text::number(Cursor & cur)
392 FontInfo font = ignore_font;
393 font.setNumber(FONT_TOGGLE);
394 toggleAndShow(cur, this, Font(font, ignore_language));
398 bool Text::isRTL(Buffer const & buffer, Paragraph const & par) const
400 return par.isRTL(buffer.params());
404 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
406 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
408 BufferView * bv = &cur.bv();
409 TextMetrics & tm = bv->textMetrics(this);
410 if (!tm.contains(cur.pit()))
411 lyx::dispatch(FuncRequest(LFUN_SCREEN_RECENTER));
413 // FIXME: We use the update flag to indicates wether a singlePar or a
414 // full screen update is needed. We reset it here but shall we restore it
418 LASSERT(cur.text() == this, /**/);
419 CursorSlice oldTopSlice = cur.top();
420 bool oldBoundary = cur.boundary();
421 bool sel = cur.selection();
422 // Signals that, even if needsUpdate == false, an update of the
423 // cursor paragraph is required
424 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
425 LyXAction::SingleParUpdate);
426 // Signals that a full-screen update is required
427 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
428 LyXAction::NoUpdate) || singleParUpdate);
430 switch (cmd.action) {
432 case LFUN_PARAGRAPH_MOVE_DOWN: {
433 pit_type const pit = cur.pit();
434 recUndo(cur, pit, pit + 1);
436 swap(pars_[pit], pars_[pit + 1]);
437 updateLabels(cur.buffer());
443 case LFUN_PARAGRAPH_MOVE_UP: {
444 pit_type const pit = cur.pit();
445 recUndo(cur, pit - 1, pit);
447 swap(pars_[pit], pars_[pit - 1]);
448 updateLabels(cur.buffer());
454 case LFUN_APPENDIX: {
455 Paragraph & par = cur.paragraph();
456 bool start = !par.params().startOfAppendix();
458 // FIXME: The code below only makes sense at top level.
459 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
460 // ensure that we have only one start_of_appendix in this document
461 // FIXME: this don't work for multipart document!
462 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
463 if (pars_[tmp].params().startOfAppendix()) {
465 pars_[tmp].params().startOfAppendix(false);
471 par.params().startOfAppendix(start);
473 // we can set the refreshing parameters now
474 updateLabels(cur.buffer());
478 case LFUN_WORD_DELETE_FORWARD:
479 if (cur.selection()) {
480 cutSelection(cur, true, false);
482 deleteWordForward(cur);
483 finishChange(cur, false);
486 case LFUN_WORD_DELETE_BACKWARD:
487 if (cur.selection()) {
488 cutSelection(cur, true, false);
490 deleteWordBackward(cur);
491 finishChange(cur, false);
494 case LFUN_LINE_DELETE:
495 if (cur.selection()) {
496 cutSelection(cur, true, false);
498 tm.deleteLineForward(cur);
499 finishChange(cur, false);
502 case LFUN_BUFFER_BEGIN:
503 case LFUN_BUFFER_BEGIN_SELECT:
504 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_BEGIN_SELECT);
505 if (cur.depth() == 1) {
506 needsUpdate |= cursorTop(cur);
510 cur.updateFlags(Update::FitCursor);
513 case LFUN_BUFFER_END:
514 case LFUN_BUFFER_END_SELECT:
515 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_END_SELECT);
516 if (cur.depth() == 1) {
517 needsUpdate |= cursorBottom(cur);
521 cur.updateFlags(Update::FitCursor);
524 case LFUN_CHAR_FORWARD:
525 case LFUN_CHAR_FORWARD_SELECT:
526 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
527 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
528 needsUpdate |= cursorForward(cur);
530 if (!needsUpdate && oldTopSlice == cur.top()
531 && cur.boundary() == oldBoundary) {
533 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
537 case LFUN_CHAR_BACKWARD:
538 case LFUN_CHAR_BACKWARD_SELECT:
539 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
540 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
541 needsUpdate |= cursorBackward(cur);
543 if (!needsUpdate && oldTopSlice == cur.top()
544 && cur.boundary() == oldBoundary) {
546 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
551 case LFUN_CHAR_LEFT_SELECT:
552 if (lyxrc.visual_cursor) {
553 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_LEFT_SELECT);
554 needsUpdate |= cursorVisLeft(cur);
555 if (!needsUpdate && oldTopSlice == cur.top()
556 && cur.boundary() == oldBoundary) {
558 cmd = FuncRequest(LFUN_FINISHED_LEFT);
561 if (reverseDirectionNeeded(cur)) {
562 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
563 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
565 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
566 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
573 case LFUN_CHAR_RIGHT:
574 case LFUN_CHAR_RIGHT_SELECT:
575 if (lyxrc.visual_cursor) {
576 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_RIGHT_SELECT);
577 needsUpdate |= cursorVisRight(cur);
578 if (!needsUpdate && oldTopSlice == cur.top()
579 && cur.boundary() == oldBoundary) {
581 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
584 if (reverseDirectionNeeded(cur)) {
585 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
586 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
588 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
589 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
598 case LFUN_DOWN_SELECT:
601 // stop/start the selection
602 bool select = cmd.action == LFUN_DOWN_SELECT ||
603 cmd.action == LFUN_UP_SELECT;
604 cur.selHandle(select);
606 // move cursor up/down
607 bool up = cmd.action == LFUN_UP_SELECT || cmd.action == LFUN_UP;
608 bool const successful = cur.upDownInText(up, needsUpdate);
610 // redraw if you leave mathed (for the decorations)
611 needsUpdate |= cur.beforeDispatchCursor().inMathed();
618 case LFUN_PARAGRAPH_UP:
619 case LFUN_PARAGRAPH_UP_SELECT:
620 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
621 needsUpdate |= cursorUpParagraph(cur);
624 case LFUN_PARAGRAPH_DOWN:
625 case LFUN_PARAGRAPH_DOWN_SELECT:
626 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
627 needsUpdate |= cursorDownParagraph(cur);
630 case LFUN_LINE_BEGIN:
631 case LFUN_LINE_BEGIN_SELECT:
632 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_BEGIN_SELECT);
633 needsUpdate |= tm.cursorHome(cur);
637 case LFUN_LINE_END_SELECT:
638 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
639 needsUpdate |= tm.cursorEnd(cur);
642 case LFUN_WORD_RIGHT:
643 case LFUN_WORD_RIGHT_SELECT:
644 if (lyxrc.visual_cursor) {
645 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_RIGHT_SELECT);
646 needsUpdate |= cursorVisRightOneWord(cur);
647 if (!needsUpdate && oldTopSlice == cur.top()
648 && cur.boundary() == oldBoundary) {
650 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
653 if (reverseDirectionNeeded(cur)) {
654 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
655 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
657 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
658 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
665 case LFUN_WORD_FORWARD:
666 case LFUN_WORD_FORWARD_SELECT:
667 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT);
668 needsUpdate |= cursorForwardOneWord(cur);
672 case LFUN_WORD_LEFT_SELECT:
673 if (lyxrc.visual_cursor) {
674 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_LEFT_SELECT);
675 needsUpdate |= cursorVisLeftOneWord(cur);
676 if (!needsUpdate && oldTopSlice == cur.top()
677 && cur.boundary() == oldBoundary) {
679 cmd = FuncRequest(LFUN_FINISHED_LEFT);
682 if (reverseDirectionNeeded(cur)) {
683 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
684 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
686 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
687 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
694 case LFUN_WORD_BACKWARD:
695 case LFUN_WORD_BACKWARD_SELECT:
696 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT);
697 needsUpdate |= cursorBackwardOneWord(cur);
700 case LFUN_WORD_SELECT: {
701 selectWord(cur, WHOLE_WORD);
702 finishChange(cur, true);
706 case LFUN_NEWLINE_INSERT: {
707 InsetNewlineParams inp;
708 docstring arg = cmd.argument();
709 // this avoids a double undo
710 // FIXME: should not be needed, ideally
711 if (!cur.selection())
713 cap::replaceSelection(cur);
714 if (arg == "linebreak")
715 inp.kind = InsetNewlineParams::LINEBREAK;
717 inp.kind = InsetNewlineParams::NEWLINE;
718 cur.insert(new InsetNewline(inp));
720 moveCursor(cur, false);
724 case LFUN_CHAR_DELETE_FORWARD:
725 if (!cur.selection()) {
726 if (cur.pos() == cur.paragraph().size())
727 // Par boundary, force full-screen update
728 singleParUpdate = false;
729 needsUpdate |= erase(cur);
731 // It is possible to make it a lot faster still
732 // just comment out the line below...
734 cutSelection(cur, true, false);
735 singleParUpdate = false;
737 moveCursor(cur, false);
740 case LFUN_CHAR_DELETE_BACKWARD:
741 if (!cur.selection()) {
742 if (bv->getIntl().getTransManager().backspace()) {
743 // Par boundary, full-screen update
745 singleParUpdate = false;
746 needsUpdate |= backspace(cur);
748 // It is possible to make it a lot faster still
749 // just comment out the line below...
752 cutSelection(cur, true, false);
753 singleParUpdate = false;
757 case LFUN_BREAK_PARAGRAPH:
758 cap::replaceSelection(cur);
759 breakParagraph(cur, cmd.argument() == "inverse");
764 // With the creation of LFUN_PARAGRAPH_PARAMS, this is now redundant,
765 // as its duties can be performed there. Should it be removed??
766 // FIXME For now, it can just dispatch LFUN_PARAGRAPH_PARAMS...
767 case LFUN_PARAGRAPH_SPACING: {
768 Paragraph & par = cur.paragraph();
769 Spacing::Space cur_spacing = par.params().spacing().getSpace();
770 string cur_value = "1.0";
771 if (cur_spacing == Spacing::Other)
772 cur_value = par.params().spacing().getValueAsString();
774 istringstream is(to_utf8(cmd.argument()));
777 Spacing::Space new_spacing = cur_spacing;
778 string new_value = cur_value;
780 lyxerr << "Missing argument to `paragraph-spacing'"
782 } else if (tmp == "single") {
783 new_spacing = Spacing::Single;
784 } else if (tmp == "onehalf") {
785 new_spacing = Spacing::Onehalf;
786 } else if (tmp == "double") {
787 new_spacing = Spacing::Double;
788 } else if (tmp == "other") {
789 new_spacing = Spacing::Other;
790 string tmpval = "0.0";
792 lyxerr << "new_value = " << tmpval << endl;
795 } else if (tmp == "default") {
796 new_spacing = Spacing::Default;
798 lyxerr << to_utf8(_("Unknown spacing argument: "))
799 << to_utf8(cmd.argument()) << endl;
801 if (cur_spacing != new_spacing || cur_value != new_value)
802 par.params().spacing(Spacing(new_spacing, new_value));
806 case LFUN_INSET_INSERT: {
808 Inset * inset = createInset(bv->buffer(), cmd);
810 // FIXME (Abdel 01/02/2006):
811 // What follows would be a partial fix for bug 2154:
812 // http://bugzilla.lyx.org/show_bug.cgi?id=2154
813 // This automatically put the label inset _after_ a
814 // numbered section. It should be possible to extend the mechanism
815 // to any kind of LateX environement.
816 // The correct way to fix that bug would be at LateX generation.
817 // I'll let the code here for reference as it could be used for some
818 // other feature like "automatic labelling".
820 Paragraph & par = pars_[cur.pit()];
821 if (inset->lyxCode() == LABEL_CODE
822 && par.layout().labeltype == LABEL_COUNTER) {
823 // Go to the end of the paragraph
824 // Warning: Because of Change-Tracking, the last
825 // position is 'size()' and not 'size()-1':
826 cur.pos() = par.size();
827 // Insert a new paragraph
828 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
833 cutSelection(cur, true, false);
840 case LFUN_INSET_DISSOLVE:
841 needsUpdate |= dissolveInset(cur);
844 case LFUN_INSET_SETTINGS: {
845 Inset & inset = cur.inset();
846 if (cmd.getArg(0) == insetName(inset.lyxCode())) {
847 // This inset dialog has been explicitely requested.
848 inset.showInsetDialog(bv);
851 // else, if there is an inset at the cursor, access this
852 Inset * next_inset = cur.nextInset();
854 next_inset->showInsetDialog(bv);
857 // if not then access the underlying inset.
858 inset.showInsetDialog(bv);
862 case LFUN_SET_GRAPHICS_GROUP: {
863 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
867 cur.recordUndoFullDocument();
869 string id = to_utf8(cmd.argument());
870 string grp = graphics::getGroupParams(bv->buffer(), id);
871 InsetGraphicsParams tmp, inspar = ins->getParams();
874 inspar.groupId = to_utf8(cmd.argument());
876 InsetGraphics::string2params(grp, bv->buffer(), tmp);
877 tmp.filename = inspar.filename;
881 ins->setParams(inspar);
884 case LFUN_SPACE_INSERT:
885 if (cur.paragraph().layout().free_spacing)
886 insertChar(cur, ' ');
888 doInsertInset(cur, this, cmd, false, false);
891 moveCursor(cur, false);
894 case LFUN_SPECIALCHAR_INSERT: {
895 string const name = to_utf8(cmd.argument());
896 if (name == "hyphenation")
897 specialChar(cur, InsetSpecialChar::HYPHENATION);
898 else if (name == "ligature-break")
899 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
900 else if (name == "slash")
901 specialChar(cur, InsetSpecialChar::SLASH);
902 else if (name == "nobreakdash")
903 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
904 else if (name == "dots")
905 specialChar(cur, InsetSpecialChar::LDOTS);
906 else if (name == "end-of-sentence")
907 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
908 else if (name == "menu-separator")
909 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
910 else if (name.empty())
911 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
913 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
917 case LFUN_WORD_UPCASE:
918 changeCase(cur, text_uppercase);
921 case LFUN_WORD_LOWCASE:
922 changeCase(cur, text_lowercase);
925 case LFUN_WORD_CAPITALIZE:
926 changeCase(cur, text_capitalization);
929 case LFUN_CHARS_TRANSPOSE:
934 cur.message(_("Paste"));
935 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), /**/);
936 cap::replaceSelection(cur);
939 string const arg = to_utf8(cmd.argument());
941 if (theClipboard().isInternal())
942 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
943 else if (theClipboard().hasGraphicsContents())
944 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
946 pasteClipboardText(cur, bv->buffer().errorList("Paste"));
947 } else if (isStrUnsignedInt(arg)) {
948 // we have a numerical argument
949 pasteFromStack(cur, bv->buffer().errorList("Paste"),
950 convert<unsigned int>(arg));
952 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
954 type = Clipboard::PdfGraphicsType;
955 else if (arg == "png")
956 type = Clipboard::PngGraphicsType;
957 else if (arg == "jpeg")
958 type = Clipboard::JpegGraphicsType;
959 else if (arg == "linkback")
960 type = Clipboard::LinkBackGraphicsType;
962 LASSERT(false, /**/);
964 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
967 bv->buffer().errors("Paste");
968 cur.clearSelection(); // bug 393
974 cutSelection(cur, true, true);
975 cur.message(_("Cut"));
980 cur.message(_("Copy"));
983 case LFUN_SERVER_GET_XY:
984 cur.message(from_utf8(
985 convert<string>(tm.cursorX(cur.top(), cur.boundary()))
986 + ' ' + convert<string>(tm.cursorY(cur.top(), cur.boundary()))));
989 case LFUN_SERVER_SET_XY: {
992 istringstream is(to_utf8(cmd.argument()));
995 lyxerr << "SETXY: Could not parse coordinates in '"
996 << to_utf8(cmd.argument()) << endl;
998 tm.setCursorFromCoordinates(cur, x, y);
1002 case LFUN_SERVER_GET_LAYOUT:
1003 cur.message(cur.paragraph().layout().name());
1007 docstring layout = cmd.argument();
1008 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1010 Paragraph const & para = cur.paragraph();
1011 docstring const old_layout = para.layout().name();
1012 DocumentClass const & tclass = bv->buffer().params().documentClass();
1015 layout = tclass.defaultLayoutName();
1017 if (para.forcePlainLayout())
1018 // in this case only the empty layout is allowed
1019 layout = tclass.plainLayoutName();
1020 else if (para.usePlainLayout()) {
1021 // in this case, default layout maps to empty layout
1022 if (layout == tclass.defaultLayoutName())
1023 layout = tclass.plainLayoutName();
1025 // otherwise, the empty layout maps to the default
1026 if (layout == tclass.plainLayoutName())
1027 layout = tclass.defaultLayoutName();
1030 bool hasLayout = tclass.hasLayout(layout);
1032 // If the entry is obsolete, use the new one instead.
1034 docstring const & obs = tclass[layout].obsoleted_by();
1040 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1041 from_utf8(N_(" not known")));
1045 bool change_layout = (old_layout != layout);
1047 if (!change_layout && cur.selection() &&
1048 cur.selBegin().pit() != cur.selEnd().pit())
1050 pit_type spit = cur.selBegin().pit();
1051 pit_type epit = cur.selEnd().pit() + 1;
1052 while (spit != epit) {
1053 if (pars_[spit].layout().name() != old_layout) {
1054 change_layout = true;
1062 setLayout(cur, layout);
1067 case LFUN_CLIPBOARD_PASTE:
1068 cur.clearSelection();
1069 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1070 cmd.argument() == "paragraph");
1071 bv->buffer().errors("Paste");
1074 case LFUN_PRIMARY_SELECTION_PASTE:
1075 pasteString(cur, theSelection().get(),
1076 cmd.argument() == "paragraph");
1079 case LFUN_UNICODE_INSERT: {
1080 if (cmd.argument().empty())
1082 docstring hexstring = cmd.argument();
1083 if (isHex(hexstring)) {
1084 char_type c = hexToInt(hexstring);
1085 if (c >= 32 && c < 0x10ffff) {
1086 lyxerr << "Inserting c: " << c << endl;
1087 docstring s = docstring(1, c);
1088 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1094 case LFUN_QUOTE_INSERT: {
1095 Paragraph & par = cur.paragraph();
1096 pos_type pos = cur.pos();
1097 BufferParams const & bufparams = bv->buffer().params();
1098 Layout const & style = par.layout();
1099 if (!style.pass_thru
1100 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1101 // this avoids a double undo
1102 // FIXME: should not be needed, ideally
1103 if (!cur.selection())
1105 cap::replaceSelection(cur);
1110 else if (cur.prevInset() && cur.prevInset()->isSpace())
1113 c = par.getChar(pos - 1);
1114 string arg = to_utf8(cmd.argument());
1115 cur.insert(new InsetQuotes(c, bufparams.quotes_language,
1116 (arg == "single") ? InsetQuotes::SingleQuotes
1117 : InsetQuotes::DoubleQuotes));
1121 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1125 case LFUN_DATE_INSERT: {
1126 string const format = cmd.argument().empty()
1127 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1128 string const time = formatted_time(current_time(), format);
1129 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1133 case LFUN_MOUSE_TRIPLE:
1134 if (cmd.button() == mouse_button::button1) {
1143 case LFUN_MOUSE_DOUBLE:
1144 if (cmd.button() == mouse_button::button1) {
1145 selectWord(cur, WHOLE_WORD_STRICT);
1150 // Single-click on work area
1151 case LFUN_MOUSE_PRESS:
1152 // We are not marking a selection with the keyboard in any case.
1153 cur.bv().cursor().mark() = false;
1154 switch (cmd.button()) {
1155 case mouse_button::button1:
1157 if (!bv->mouseSetCursor(cur, cmd.argument() == "region-select"))
1158 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1161 case mouse_button::button2:
1162 // Middle mouse pasting.
1163 if (!cap::selection()) {
1164 // There is no local selection in the current buffer, so try to
1165 // paste primary selection instead.
1166 lyx::dispatch(FuncRequest(LFUN_PRIMARY_SELECTION_PASTE,
1168 // Nothing else to do.
1172 // Copy the selection buffer to the clipboard stack, because we want it
1173 // to appear in the "Edit->Paste recent" menu.
1174 cap::copySelectionToStack();
1175 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1176 cur.updateFlags(Update::Force | Update::FitCursor);
1177 bv->buffer().errors("Paste");
1178 bv->buffer().markDirty();
1179 bv->cursor().finishUndo();
1182 case mouse_button::button3:
1183 if (cur.selection()) {
1184 DocIterator const selbeg = cur.selectionBegin();
1185 DocIterator const selend = cur.selectionEnd();
1186 Cursor tmpcur = cur;
1187 tm.setCursorFromCoordinates(tmpcur, cmd.x, cmd.y);
1188 // Don't do anything if we right-click a selection, a selection
1189 // context menu should popup instead.
1190 if (tmpcur < selbeg || tmpcur >= selend) {
1195 if (!bv->mouseSetCursor(cur, false)) {
1196 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1201 } // switch (cmd.button())
1204 case LFUN_MOUSE_MOTION: {
1205 // Mouse motion with right or middle mouse do nothing for now.
1206 if (cmd.button() != mouse_button::button1) {
1210 // ignore motions deeper nested than the real anchor
1211 Cursor & bvcur = cur.bv().cursor();
1212 if (!bvcur.anchor_.hasPart(cur)) {
1216 CursorSlice old = bvcur.top();
1218 int const wh = bv->workHeight();
1219 int const y = max(0, min(wh - 1, cmd.y));
1221 tm.setCursorFromCoordinates(cur, cmd.x, y);
1222 cur.setTargetX(cmd.x);
1224 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1226 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1227 // This is to allow jumping over large insets
1228 if (cur.top() == old) {
1230 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1232 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1234 // We continue with our existing selection or start a new one, so don't
1235 // reset the anchor.
1236 bvcur.setCursor(cur);
1237 bvcur.selection() = true;
1238 if (cur.top() == old) {
1239 // We didn't move one iota, so no need to update the screen.
1240 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1247 case LFUN_MOUSE_RELEASE:
1248 switch (cmd.button()) {
1249 case mouse_button::button1:
1250 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1251 // If there is a new selection, update persistent selection;
1252 // otherwise, single click does not clear persistent selection
1254 if (cur.selection()) {
1255 // Finish selection.
1256 // If double click, cur is moved to the end of word by selectWord
1257 // but bvcur is current mouse position.
1258 cur.bv().cursor().selection() = true;
1260 // FIXME: We could try to handle drag and drop of selection here.
1264 case mouse_button::button2:
1265 // Middle mouse pasting is handled at mouse press time,
1266 // see LFUN_MOUSE_PRESS.
1270 case mouse_button::button3:
1271 // Cursor was set at LFUN_MOUSE_PRESS time.
1272 // FIXME: If there is a selection we could try to handle a special
1273 // drag & drop context menu.
1277 case mouse_button::none:
1278 case mouse_button::button4:
1279 case mouse_button::button5:
1281 } // switch (cmd.button())
1285 case LFUN_SELF_INSERT: {
1286 if (cmd.argument().empty())
1289 // Automatically delete the currently selected
1290 // text and replace it with what is being
1291 // typed in now. Depends on lyxrc settings
1292 // "auto_region_delete", which defaults to
1295 if (lyxrc.auto_region_delete && cur.selection())
1296 cutSelection(cur, false, false);
1298 cur.clearSelection();
1299 Font const old_font = cur.real_current_font;
1301 docstring::const_iterator cit = cmd.argument().begin();
1302 docstring::const_iterator end = cmd.argument().end();
1303 for (; cit != end; ++cit)
1304 bv->translateAndInsert(*cit, this, cur);
1307 moveCursor(cur, false);
1311 case LFUN_HYPERLINK_INSERT: {
1312 InsetCommandParams p(HYPERLINK_CODE);
1314 if (cur.selection()) {
1315 content = cur.selectionAsString(false);
1316 cutSelection(cur, true, false);
1318 p["target"] = (cmd.argument().empty()) ?
1319 content : cmd.argument();
1320 string const data = InsetCommand::params2string("href", p);
1321 if (p["target"].empty()) {
1322 bv->showDialog("href", data);
1324 FuncRequest fr(LFUN_INSET_INSERT, data);
1330 case LFUN_LABEL_INSERT: {
1331 InsetCommandParams p(LABEL_CODE);
1332 // Try to generate a valid label
1333 p["name"] = (cmd.argument().empty()) ?
1334 cur.getPossibleLabel() :
1336 string const data = InsetCommand::params2string("label", p);
1338 if (cmd.argument().empty()) {
1339 bv->showDialog("label", data);
1341 FuncRequest fr(LFUN_INSET_INSERT, data);
1347 case LFUN_INFO_INSERT: {
1349 if (cmd.argument().empty() && cur.selection()) {
1350 // if command argument is empty use current selection as parameter.
1351 docstring ds = cur.selectionAsString(false);
1352 cutSelection(cur, true, false);
1353 FuncRequest cmd0(cmd, ds);
1354 inset = createInset(cur.bv().buffer(), cmd0);
1356 inset = createInset(cur.bv().buffer(), cmd);
1360 insertInset(cur, inset);
1364 case LFUN_CAPTION_INSERT:
1365 case LFUN_FOOTNOTE_INSERT:
1366 case LFUN_NOTE_INSERT:
1367 case LFUN_FLEX_INSERT:
1368 case LFUN_BOX_INSERT:
1369 case LFUN_BRANCH_INSERT:
1370 case LFUN_ERT_INSERT:
1371 case LFUN_LISTING_INSERT:
1372 case LFUN_MARGINALNOTE_INSERT:
1373 case LFUN_OPTIONAL_INSERT:
1374 case LFUN_INDEX_INSERT:
1375 // Open the inset, and move the current selection
1377 doInsertInset(cur, this, cmd, true, true);
1379 // Some insets are numbered, others are shown in the outline pane so
1380 // let's update the labels and the toc backend.
1381 updateLabels(bv->buffer());
1384 case LFUN_TABULAR_INSERT:
1385 // if there were no arguments, just open the dialog
1386 if (doInsertInset(cur, this, cmd, false, true))
1389 bv->showDialog("tabularcreate");
1393 case LFUN_FLOAT_INSERT:
1394 case LFUN_FLOAT_WIDE_INSERT:
1395 case LFUN_WRAP_INSERT: {
1396 bool content = cur.selection(); // will some text be moved into the inset?
1398 doInsertInset(cur, this, cmd, true, true);
1400 ParagraphList & pars = cur.text()->paragraphs();
1402 DocumentClass const & tclass = bv->buffer().params().documentClass();
1404 // add a separate paragraph for the caption inset
1405 pars.push_back(Paragraph());
1406 pars.back().setInsetOwner(pars[0].inInset());
1407 pars.back().setPlainOrDefaultLayout(tclass);
1408 int cap_pit = pars.size() - 1;
1410 // if an empty inset was created, we create an additional empty
1411 // paragraph at the bottom so that the user can choose where to put
1412 // the graphics (or table).
1414 pars.push_back(Paragraph());
1415 pars.back().setInsetOwner(pars[0].inInset());
1416 pars.back().setPlainOrDefaultLayout(tclass);
1419 // reposition the cursor to the caption
1420 cur.pit() = cap_pit;
1422 // FIXME: This Text/Cursor dispatch handling is a mess!
1423 // We cannot use Cursor::dispatch here it needs access to up to
1425 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1426 cur.text()->dispatch(cur, cmd_caption);
1427 cur.updateFlags(Update::Force);
1428 // FIXME: When leaving the Float (or Wrap) inset we should
1429 // delete any empty paragraph left above or below the
1434 case LFUN_NOMENCL_INSERT: {
1435 InsetCommandParams p(NOMENCL_CODE);
1436 if (cmd.argument().empty())
1437 p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
1439 p["symbol"] = cmd.argument();
1440 string const data = InsetCommand::params2string("nomenclature", p);
1441 bv->showDialog("nomenclature", data);
1445 case LFUN_INDEX_PRINT:
1446 case LFUN_NOMENCL_PRINT:
1447 case LFUN_TOC_INSERT:
1448 case LFUN_LINE_INSERT:
1449 case LFUN_NEWPAGE_INSERT:
1451 doInsertInset(cur, this, cmd, false, false);
1455 case LFUN_DEPTH_DECREMENT:
1456 changeDepth(cur, DEC_DEPTH);
1459 case LFUN_DEPTH_INCREMENT:
1460 changeDepth(cur, INC_DEPTH);
1463 case LFUN_MATH_DISPLAY:
1464 mathDispatch(cur, cmd, true);
1467 case LFUN_MATH_MODE:
1468 if (cmd.argument() == "on")
1469 // don't pass "on" as argument
1470 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1472 mathDispatch(cur, cmd, false);
1475 case LFUN_MATH_MACRO:
1476 if (cmd.argument().empty())
1477 cur.errorMessage(from_utf8(N_("Missing argument")));
1479 string s = to_utf8(cmd.argument());
1480 string const s1 = token(s, ' ', 1);
1481 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1482 string const s2 = token(s, ' ', 2);
1483 MacroType type = MacroTypeNewcommand;
1485 type = MacroTypeDef;
1486 cur.insert(new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, false, type));
1487 //cur.nextInset()->edit(cur, true);
1491 // passthrough hat and underscore outside mathed:
1492 case LFUN_MATH_SUBSCRIPT:
1493 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1495 case LFUN_MATH_SUPERSCRIPT:
1496 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1499 case LFUN_MATH_INSERT:
1500 case LFUN_MATH_MATRIX:
1501 case LFUN_MATH_DELIM:
1502 case LFUN_MATH_BIGDELIM: {
1503 cap::replaceSelection(cur);
1504 cur.insert(new InsetMathHull(hullSimple));
1505 checkAndActivateInset(cur, true);
1506 LASSERT(cur.inMathed(), /**/);
1511 case LFUN_FONT_EMPH: {
1512 Font font(ignore_font, ignore_language);
1513 font.fontInfo().setEmph(FONT_TOGGLE);
1514 toggleAndShow(cur, this, font);
1518 case LFUN_FONT_BOLD: {
1519 Font font(ignore_font, ignore_language);
1520 font.fontInfo().setSeries(BOLD_SERIES);
1521 toggleAndShow(cur, this, font);
1525 case LFUN_FONT_NOUN: {
1526 Font font(ignore_font, ignore_language);
1527 font.fontInfo().setNoun(FONT_TOGGLE);
1528 toggleAndShow(cur, this, font);
1532 case LFUN_FONT_TYPEWRITER: {
1533 Font font(ignore_font, ignore_language);
1534 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1535 toggleAndShow(cur, this, font);
1539 case LFUN_FONT_SANS: {
1540 Font font(ignore_font, ignore_language);
1541 font.fontInfo().setFamily(SANS_FAMILY);
1542 toggleAndShow(cur, this, font);
1546 case LFUN_FONT_ROMAN: {
1547 Font font(ignore_font, ignore_language);
1548 font.fontInfo().setFamily(ROMAN_FAMILY);
1549 toggleAndShow(cur, this, font);
1553 case LFUN_FONT_DEFAULT: {
1554 Font font(inherit_font, ignore_language);
1555 toggleAndShow(cur, this, font);
1559 case LFUN_FONT_UNDERLINE: {
1560 Font font(ignore_font, ignore_language);
1561 font.fontInfo().setUnderbar(FONT_TOGGLE);
1562 toggleAndShow(cur, this, font);
1566 case LFUN_FONT_SIZE: {
1567 Font font(ignore_font, ignore_language);
1568 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1569 toggleAndShow(cur, this, font);
1573 case LFUN_LANGUAGE: {
1574 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1577 Font font(ignore_font, lang);
1578 toggleAndShow(cur, this, font);
1582 case LFUN_TEXTSTYLE_APPLY:
1583 toggleAndShow(cur, this, freefont, toggleall);
1584 cur.message(_("Character set"));
1587 // Set the freefont using the contents of \param data dispatched from
1588 // the frontends and apply it at the current cursor location.
1589 case LFUN_TEXTSTYLE_UPDATE: {
1592 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1595 toggleAndShow(cur, this, freefont, toggleall);
1596 cur.message(_("Character set"));
1598 lyxerr << "Argument not ok";
1603 case LFUN_FINISHED_LEFT:
1604 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
1605 // We're leaving an inset, going left. If the inset is LTR, we're
1606 // leaving from the front, so we should not move (remain at --- but
1607 // not in --- the inset). If the inset is RTL, move left, without
1608 // entering the inset itself; i.e., move to after the inset.
1609 if (cur.paragraph().getFontSettings(
1610 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1611 cursorVisLeft(cur, true);
1614 case LFUN_FINISHED_RIGHT:
1615 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
1616 // We're leaving an inset, going right. If the inset is RTL, we're
1617 // leaving from the front, so we should not move (remain at --- but
1618 // not in --- the inset). If the inset is LTR, move right, without
1619 // entering the inset itself; i.e., move to after the inset.
1620 if (!cur.paragraph().getFontSettings(
1621 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1622 cursorVisRight(cur, true);
1625 case LFUN_FINISHED_BACKWARD:
1626 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
1629 case LFUN_FINISHED_FORWARD:
1630 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
1632 cur.setCurrentFont();
1635 case LFUN_LAYOUT_PARAGRAPH: {
1637 params2string(cur.paragraph(), data);
1638 data = "show\n" + data;
1639 bv->showDialog("paragraph", data);
1643 case LFUN_PARAGRAPH_UPDATE: {
1645 params2string(cur.paragraph(), data);
1647 // Will the paragraph accept changes from the dialog?
1649 cur.inset().allowParagraphCustomization(cur.idx());
1651 data = "update " + convert<string>(accept) + '\n' + data;
1652 bv->updateDialog("paragraph", data);
1656 case LFUN_ACCENT_UMLAUT:
1657 case LFUN_ACCENT_CIRCUMFLEX:
1658 case LFUN_ACCENT_GRAVE:
1659 case LFUN_ACCENT_ACUTE:
1660 case LFUN_ACCENT_TILDE:
1661 case LFUN_ACCENT_CEDILLA:
1662 case LFUN_ACCENT_MACRON:
1663 case LFUN_ACCENT_DOT:
1664 case LFUN_ACCENT_UNDERDOT:
1665 case LFUN_ACCENT_UNDERBAR:
1666 case LFUN_ACCENT_CARON:
1667 case LFUN_ACCENT_BREVE:
1668 case LFUN_ACCENT_TIE:
1669 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1670 case LFUN_ACCENT_CIRCLE:
1671 case LFUN_ACCENT_OGONEK:
1672 theLyXFunc().handleKeyFunc(cmd.action);
1673 if (!cmd.argument().empty())
1674 // FIXME: Are all these characters encoded in one byte in utf8?
1675 bv->translateAndInsert(cmd.argument()[0], this, cur);
1678 case LFUN_FLOAT_LIST_INSERT: {
1679 DocumentClass const & tclass = bv->buffer().params().documentClass();
1680 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1682 if (cur.selection())
1683 cutSelection(cur, true, false);
1684 breakParagraph(cur);
1686 if (cur.lastpos() != 0) {
1687 cursorBackward(cur);
1688 breakParagraph(cur);
1691 docstring const laystr = cur.inset().usePlainLayout() ?
1692 tclass.plainLayoutName() :
1693 tclass.defaultLayoutName();
1694 setLayout(cur, laystr);
1695 ParagraphParameters p;
1696 setParagraphs(cur, p);
1697 // FIXME This should be simplified when InsetFloatList takes a
1698 // Buffer in its constructor.
1699 InsetFloatList * ifl = new InsetFloatList(to_utf8(cmd.argument()));
1700 ifl->setBuffer(bv->buffer());
1701 insertInset(cur, ifl);
1704 lyxerr << "Non-existent float type: "
1705 << to_utf8(cmd.argument()) << endl;
1710 case LFUN_CHANGE_ACCEPT: {
1711 acceptOrRejectChanges(cur, ACCEPT);
1715 case LFUN_CHANGE_REJECT: {
1716 acceptOrRejectChanges(cur, REJECT);
1720 case LFUN_THESAURUS_ENTRY: {
1721 docstring arg = cmd.argument();
1723 arg = cur.selectionAsString(false);
1725 if (arg.size() > 100 || arg.empty()) {
1726 // Get word or selection
1727 selectWordWhenUnderCursor(cur, WHOLE_WORD);
1728 arg = cur.selectionAsString(false);
1731 bv->showDialog("thesaurus", to_utf8(arg));
1735 case LFUN_PARAGRAPH_PARAMS_APPLY: {
1736 // Given data, an encoding of the ParagraphParameters
1737 // generated in the Paragraph dialog, this function sets
1738 // the current paragraph, or currently selected paragraphs,
1740 // NOTE: This function overrides all existing settings.
1741 setParagraphs(cur, cmd.argument());
1742 cur.message(_("Paragraph layout set"));
1746 case LFUN_PARAGRAPH_PARAMS: {
1747 // Given data, an encoding of the ParagraphParameters as we'd
1748 // find them in a LyX file, this function modifies the current paragraph,
1749 // or currently selected paragraphs.
1750 // NOTE: This function only modifies, and does not override, existing
1752 setParagraphs(cur, cmd.argument(), true);
1753 cur.message(_("Paragraph layout set"));
1758 if (cur.selection()) {
1759 cur.selection() = false;
1762 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
1763 // correct, but I'm not 100% sure -- dov, 071019
1764 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1768 case LFUN_OUTLINE_UP:
1769 outline(OutlineUp, cur);
1770 setCursor(cur, cur.pit(), 0);
1771 updateLabels(cur.buffer());
1775 case LFUN_OUTLINE_DOWN:
1776 outline(OutlineDown, cur);
1777 setCursor(cur, cur.pit(), 0);
1778 updateLabels(cur.buffer());
1782 case LFUN_OUTLINE_IN:
1783 outline(OutlineIn, cur);
1784 updateLabels(cur.buffer());
1788 case LFUN_OUTLINE_OUT:
1789 outline(OutlineOut, cur);
1790 updateLabels(cur.buffer());
1795 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
1800 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
1802 // FIXME: The cursor flag is reset two lines below
1803 // so we need to check here if some of the LFUN did touch that.
1804 // for now only Text::erase() and Text::backspace() do that.
1805 // The plan is to verify all the LFUNs and then to remove this
1806 // singleParUpdate boolean altogether.
1807 if (cur.result().update() & Update::Force) {
1808 singleParUpdate = false;
1812 // FIXME: the following code should go in favor of fine grained
1813 // update flag treatment.
1814 if (singleParUpdate) {
1815 // Inserting characters does not change par height in general. So, try
1816 // to update _only_ this paragraph. BufferView will detect if a full
1817 // metrics update is needed anyway.
1818 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1823 && &oldTopSlice.inset() == &cur.inset()
1824 && oldTopSlice.idx() == cur.idx()
1825 && !sel // sel is a backup of cur.selection() at the biginning of the function.
1826 && !cur.selection())
1827 // FIXME: it would be better if we could just do this
1829 //if (cur.result().update() != Update::FitCursor)
1832 // But some LFUNs do not set Update::FitCursor when needed, so we
1833 // do it for all. This is not very harmfull as FitCursor will provoke
1834 // a full redraw only if needed but still, a proper review of all LFUN
1835 // should be done and this needsUpdate boolean can then be removed.
1836 cur.updateFlags(Update::FitCursor);
1838 cur.updateFlags(Update::Force | Update::FitCursor);
1842 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
1843 FuncStatus & flag) const
1845 LASSERT(cur.text() == this, /**/);
1847 Font const & font = cur.real_current_font;
1848 FontInfo const & fontinfo = font.fontInfo();
1850 InsetCode code = NO_CODE;
1852 switch (cmd.action) {
1854 case LFUN_DEPTH_DECREMENT:
1855 enable = changeDepthAllowed(cur, DEC_DEPTH);
1858 case LFUN_DEPTH_INCREMENT:
1859 enable = changeDepthAllowed(cur, INC_DEPTH);
1863 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1866 case LFUN_DIALOG_SHOW_NEW_INSET:
1867 if (cmd.argument() == "bibitem")
1868 code = BIBITEM_CODE;
1869 else if (cmd.argument() == "bibtex")
1871 else if (cmd.argument() == "box")
1873 else if (cmd.argument() == "branch")
1875 else if (cmd.argument() == "citation")
1877 else if (cmd.argument() == "ert")
1879 else if (cmd.argument() == "external")
1880 code = EXTERNAL_CODE;
1881 else if (cmd.argument() == "float")
1883 else if (cmd.argument() == "graphics")
1884 code = GRAPHICS_CODE;
1885 else if (cmd.argument() == "href")
1886 code = HYPERLINK_CODE;
1887 else if (cmd.argument() == "include")
1888 code = INCLUDE_CODE;
1889 else if (cmd.argument() == "index")
1891 else if (cmd.argument() == "nomenclature")
1892 code = NOMENCL_CODE;
1893 else if (cmd.argument() == "label")
1895 else if (cmd.argument() == "note")
1897 else if (cmd.argument() == "ref")
1899 else if (cmd.argument() == "space")
1901 else if (cmd.argument() == "toc")
1903 else if (cmd.argument() == "vspace")
1905 else if (cmd.argument() == "wrap")
1907 else if (cmd.argument() == "listings")
1908 code = LISTINGS_CODE;
1911 case LFUN_ERT_INSERT:
1914 case LFUN_LISTING_INSERT:
1915 code = LISTINGS_CODE;
1917 case LFUN_FOOTNOTE_INSERT:
1920 case LFUN_TABULAR_INSERT:
1921 code = TABULAR_CODE;
1923 case LFUN_MARGINALNOTE_INSERT:
1926 case LFUN_FLOAT_INSERT:
1927 case LFUN_FLOAT_WIDE_INSERT:
1930 case LFUN_WRAP_INSERT:
1933 case LFUN_FLOAT_LIST_INSERT:
1934 code = FLOAT_LIST_CODE;
1936 case LFUN_CAPTION_INSERT:
1937 code = CAPTION_CODE;
1939 case LFUN_NOTE_INSERT:
1941 // in commands (sections etc., only Notes are allowed)
1942 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
1943 !cur.paragraph().layout().isCommand());
1945 case LFUN_FLEX_INSERT: {
1947 string s = cmd.getArg(0);
1949 cur.buffer().params().documentClass().insetLayout(from_utf8(s));
1950 if (il.lyxtype() != "charstyle" &&
1951 il.lyxtype() != "custom" &&
1952 il.lyxtype() != "element" &&
1953 il.lyxtype ()!= "standard")
1957 case LFUN_BOX_INSERT:
1960 case LFUN_BRANCH_INSERT:
1962 if (cur.buffer().masterBuffer()->params().branchlist().empty())
1965 case LFUN_LABEL_INSERT:
1968 case LFUN_INFO_INSERT:
1971 case LFUN_OPTIONAL_INSERT:
1973 enable = cur.paragraph().insetList().count(OPTARG_CODE)
1974 < cur.paragraph().layout().optionalargs;
1976 case LFUN_INDEX_INSERT:
1979 case LFUN_INDEX_PRINT:
1980 code = INDEX_PRINT_CODE;
1982 case LFUN_NOMENCL_INSERT:
1983 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
1987 code = NOMENCL_CODE;
1989 case LFUN_NOMENCL_PRINT:
1990 code = NOMENCL_PRINT_CODE;
1992 case LFUN_TOC_INSERT:
1995 case LFUN_HYPERLINK_INSERT:
1996 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2000 code = HYPERLINK_CODE;
2002 case LFUN_QUOTE_INSERT:
2003 // always allow this, since we will inset a raw quote
2004 // if an inset is not allowed.
2006 case LFUN_SPECIALCHAR_INSERT:
2007 code = SPECIALCHAR_CODE;
2009 case LFUN_SPACE_INSERT:
2010 // slight hack: we know this is allowed in math mode
2015 case LFUN_INSET_MODIFY:
2016 // We need to disable this, because we may get called for a
2018 // InsetTabular::getStatus() -> InsetText::getStatus()
2019 // and we don't handle LFUN_INSET_MODIFY.
2023 case LFUN_FONT_EMPH:
2024 flag.setOnOff(fontinfo.emph() == FONT_ON);
2027 case LFUN_FONT_NOUN:
2028 flag.setOnOff(fontinfo.noun() == FONT_ON);
2031 case LFUN_FONT_BOLD:
2032 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2035 case LFUN_FONT_SANS:
2036 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2039 case LFUN_FONT_ROMAN:
2040 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2043 case LFUN_FONT_TYPEWRITER:
2044 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2049 enable = cur.selection();
2053 if (cmd.argument().empty()) {
2054 if (theClipboard().isInternal())
2055 enable = cap::numberOfSelections() > 0;
2057 enable = !theClipboard().empty();
2061 // we have an argument
2062 string const arg = to_utf8(cmd.argument());
2063 if (isStrUnsignedInt(arg)) {
2064 // it's a number and therefore means the internal stack
2065 unsigned int n = convert<unsigned int>(arg);
2066 enable = cap::numberOfSelections() > n;
2070 // explicit graphics type?
2071 if ((arg == "pdf" && theClipboard().hasGraphicsContents(Clipboard::PdfGraphicsType))
2072 || (arg == "png" && theClipboard().hasGraphicsContents(Clipboard::PngGraphicsType))
2073 || (arg == "jpeg" && theClipboard().hasGraphicsContents(Clipboard::JpegGraphicsType))
2074 || (arg == "linkback" && theClipboard().hasGraphicsContents(Clipboard::LinkBackGraphicsType))) {
2084 case LFUN_CLIPBOARD_PASTE:
2085 enable = !theClipboard().empty();
2088 case LFUN_PRIMARY_SELECTION_PASTE:
2089 enable = cur.selection() || !theSelection().empty();
2092 case LFUN_PARAGRAPH_MOVE_UP:
2093 enable = cur.pit() > 0 && !cur.selection();
2096 case LFUN_PARAGRAPH_MOVE_DOWN:
2097 enable = cur.pit() < cur.lastpit() && !cur.selection();
2100 case LFUN_INSET_DISSOLVE:
2101 if (!cmd.argument().empty()) {
2102 InsetLayout il = cur.inset().getLayout(cur.buffer().params());
2103 enable = cur.inset().lyxCode() == FLEX_CODE
2104 && il.lyxtype() == to_utf8(cmd.argument());
2106 enable = !isMainText(cur.bv().buffer())
2107 && cur.inset().nargs() == 1;
2111 case LFUN_CHANGE_ACCEPT:
2112 case LFUN_CHANGE_REJECT:
2113 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
2114 // In principle, these LFUNs should only be enabled if there
2115 // is a change at the current position/in the current selection.
2116 // However, without proper optimizations, this will inevitably
2117 // result in unacceptable performance - just imagine a user who
2118 // wants to select the complete content of a long document.
2122 case LFUN_OUTLINE_UP:
2123 case LFUN_OUTLINE_DOWN:
2124 case LFUN_OUTLINE_IN:
2125 case LFUN_OUTLINE_OUT:
2126 // FIXME: LyX is not ready for outlining within inset.
2127 enable = isMainText(cur.bv().buffer())
2128 && cur.paragraph().layout().toclevel != Layout::NOT_IN_TOC;
2131 case LFUN_NEWLINE_INSERT:
2132 // LaTeX restrictions (labels or empty par)
2133 enable = (cur.pos() > cur.paragraph().beginOfBody());
2136 case LFUN_SET_GRAPHICS_GROUP: {
2137 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
2141 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
2145 case LFUN_WORD_DELETE_FORWARD:
2146 case LFUN_WORD_DELETE_BACKWARD:
2147 case LFUN_LINE_DELETE:
2148 case LFUN_WORD_FORWARD:
2149 case LFUN_WORD_BACKWARD:
2150 case LFUN_WORD_RIGHT:
2151 case LFUN_WORD_LEFT:
2152 case LFUN_CHAR_FORWARD:
2153 case LFUN_CHAR_FORWARD_SELECT:
2154 case LFUN_CHAR_BACKWARD:
2155 case LFUN_CHAR_BACKWARD_SELECT:
2156 case LFUN_CHAR_LEFT:
2157 case LFUN_CHAR_LEFT_SELECT:
2158 case LFUN_CHAR_RIGHT:
2159 case LFUN_CHAR_RIGHT_SELECT:
2161 case LFUN_UP_SELECT:
2163 case LFUN_DOWN_SELECT:
2164 case LFUN_PARAGRAPH_UP_SELECT:
2165 case LFUN_PARAGRAPH_DOWN_SELECT:
2166 case LFUN_LINE_BEGIN_SELECT:
2167 case LFUN_LINE_END_SELECT:
2168 case LFUN_WORD_FORWARD_SELECT:
2169 case LFUN_WORD_BACKWARD_SELECT:
2170 case LFUN_WORD_RIGHT_SELECT:
2171 case LFUN_WORD_LEFT_SELECT:
2172 case LFUN_WORD_SELECT:
2173 case LFUN_PARAGRAPH_UP:
2174 case LFUN_PARAGRAPH_DOWN:
2175 case LFUN_LINE_BEGIN:
2177 case LFUN_CHAR_DELETE_FORWARD:
2178 case LFUN_CHAR_DELETE_BACKWARD:
2179 case LFUN_BREAK_PARAGRAPH:
2180 case LFUN_PARAGRAPH_SPACING:
2181 case LFUN_INSET_INSERT:
2182 case LFUN_WORD_UPCASE:
2183 case LFUN_WORD_LOWCASE:
2184 case LFUN_WORD_CAPITALIZE:
2185 case LFUN_CHARS_TRANSPOSE:
2186 case LFUN_SERVER_GET_XY:
2187 case LFUN_SERVER_SET_XY:
2188 case LFUN_SERVER_GET_LAYOUT:
2190 case LFUN_DATE_INSERT:
2191 case LFUN_SELF_INSERT:
2192 case LFUN_LINE_INSERT:
2193 case LFUN_NEWPAGE_INSERT:
2194 case LFUN_MATH_DISPLAY:
2195 case LFUN_MATH_MODE:
2196 case LFUN_MATH_MACRO:
2197 case LFUN_MATH_MATRIX:
2198 case LFUN_MATH_DELIM:
2199 case LFUN_MATH_BIGDELIM:
2200 case LFUN_MATH_INSERT:
2201 case LFUN_MATH_SUBSCRIPT:
2202 case LFUN_MATH_SUPERSCRIPT:
2203 case LFUN_FONT_DEFAULT:
2204 case LFUN_FONT_UNDERLINE:
2205 case LFUN_FONT_SIZE:
2207 case LFUN_TEXTSTYLE_APPLY:
2208 case LFUN_TEXTSTYLE_UPDATE:
2209 case LFUN_LAYOUT_PARAGRAPH:
2210 case LFUN_PARAGRAPH_UPDATE:
2211 case LFUN_ACCENT_UMLAUT:
2212 case LFUN_ACCENT_CIRCUMFLEX:
2213 case LFUN_ACCENT_GRAVE:
2214 case LFUN_ACCENT_ACUTE:
2215 case LFUN_ACCENT_TILDE:
2216 case LFUN_ACCENT_CEDILLA:
2217 case LFUN_ACCENT_MACRON:
2218 case LFUN_ACCENT_DOT:
2219 case LFUN_ACCENT_UNDERDOT:
2220 case LFUN_ACCENT_UNDERBAR:
2221 case LFUN_ACCENT_CARON:
2222 case LFUN_ACCENT_BREVE:
2223 case LFUN_ACCENT_TIE:
2224 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2225 case LFUN_ACCENT_CIRCLE:
2226 case LFUN_ACCENT_OGONEK:
2227 case LFUN_THESAURUS_ENTRY:
2228 case LFUN_PARAGRAPH_PARAMS_APPLY:
2229 case LFUN_PARAGRAPH_PARAMS:
2231 case LFUN_BUFFER_END:
2232 case LFUN_BUFFER_BEGIN:
2233 case LFUN_BUFFER_BEGIN_SELECT:
2234 case LFUN_BUFFER_END_SELECT:
2235 case LFUN_UNICODE_INSERT:
2236 // these are handled in our dispatch()
2245 && (cur.empty() || !cur.inset().insetAllowed(code)))
2248 flag.setEnabled(enable);
2253 void Text::pasteString(Cursor & cur, docstring const & clip,
2256 cur.clearSelection();
2257 if (!clip.empty()) {
2260 insertStringAsParagraphs(cur, clip);
2262 insertStringAsLines(cur, clip);