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"
51 #include "frontends/Application.h"
52 #include "frontends/LyXView.h"
53 #include "frontends/WorkArea.h"
55 #include "insets/InsetCollapsable.h"
56 #include "insets/InsetCommand.h"
57 #include "insets/InsetFloatList.h"
58 #include "insets/InsetNewline.h"
59 #include "insets/InsetQuotes.h"
60 #include "insets/InsetSpecialChar.h"
61 #include "insets/InsetText.h"
62 #include "insets/InsetGraphics.h"
63 #include "insets/InsetGraphicsParams.h"
65 #include "support/convert.h"
66 #include "support/debug.h"
67 #include "support/gettext.h"
68 #include "support/lstrings.h"
69 #include "support/lyxtime.h"
71 #include "mathed/InsetMathHull.h"
72 #include "mathed/MathMacroTemplate.h"
74 #include <boost/next_prior.hpp>
80 using namespace lyx::support;
84 using cap::copySelection;
85 using cap::cutSelection;
86 using cap::pasteFromStack;
87 using cap::pasteClipboardText;
88 using cap::pasteClipboardGraphics;
89 using cap::replaceSelection;
90 using cap::grabAndEraseSelection;
91 using cap::selClearOrDel;
94 static Font freefont(ignore_font, ignore_language);
95 static bool toggleall = false;
97 static void toggleAndShow(Cursor & cur, Text * text,
98 Font const & font, bool toggleall = true)
100 text->toggleFree(cur, font, toggleall);
102 if (font.language() != ignore_language ||
103 font.fontInfo().number() != FONT_IGNORE) {
104 TextMetrics const & tm = cur.bv().textMetrics(text);
105 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
106 cur.real_current_font))
107 text->setCursor(cur, cur.pit(), cur.pos(),
108 false, !cur.boundary());
113 static void moveCursor(Cursor & cur, bool selecting)
115 if (selecting || cur.mark())
120 static void finishChange(Cursor & cur, bool selecting)
123 moveCursor(cur, selecting);
127 static void mathDispatch(Cursor & cur, FuncRequest const & cmd, bool display)
130 docstring sel = cur.selectionAsString(false);
132 // It may happen that sel is empty but there is a selection
133 replaceSelection(cur);
135 // Is this a valid formula?
139 #ifdef ENABLE_ASSERTIONS
140 const int old_pos = cur.pos();
142 cur.insert(new InsetMathHull(hullSimple));
143 #ifdef ENABLE_ASSERTIONS
144 LASSERT(old_pos == cur.pos(), /**/);
146 cur.nextInset()->edit(cur, true);
147 // don't do that also for LFUN_MATH_MODE
148 // unless you want end up with always changing
149 // to mathrm when opening an inlined inset --
150 // I really hate "LyXfunc overloading"...
152 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
153 // Avoid an unnecessary undo step if cmd.argument
155 if (!cmd.argument().empty())
156 cur.dispatch(FuncRequest(LFUN_MATH_INSERT,
159 // create a macro if we see "\\newcommand"
160 // somewhere, and an ordinary formula
162 if (sel.find(from_ascii("\\newcommand")) == string::npos
163 && sel.find(from_ascii("\\newlyxcommand")) == string::npos
164 && sel.find(from_ascii("\\def")) == string::npos)
166 InsetMathHull * formula = new InsetMathHull;
167 string const selstr = to_utf8(sel);
168 istringstream is(selstr);
171 if (!formula->readQuiet(lex)) {
172 // No valid formula, let's try with delims
173 is.str("$" + selstr + "$");
175 if (!formula->readQuiet(lex)) {
176 // Still not valid, leave it as is
185 cur.insert(new MathMacroTemplate(sel));
189 cur.message(from_utf8(N_("Math editor mode")));
191 cur.message(from_utf8(N_("No valid math formula")));
195 void regexpDispatch(Cursor & cur, FuncRequest const & cmd)
197 BOOST_ASSERT(cmd.action == LFUN_REGEXP_MODE);
198 if (cur.inRegexped()) {
199 cur.message(_("Already in regexp mode"));
203 docstring const save_selection = grabAndEraseSelection(cur);
205 // replaceSelection(cur);
207 cur.insert(new InsetMathHull(hullRegexp));
208 cur.nextInset()->edit(cur, true);
209 cur.niceInsert(save_selection);
211 cur.message(_("Regexp editor mode"));
215 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
218 cap::replaceSelection(cur);
219 cur.insert(new InsetSpecialChar(kind));
224 static bool doInsertInset(Cursor & cur, Text * text,
225 FuncRequest const & cmd, bool edit, bool pastesel)
227 Buffer & buffer = cur.bv().buffer();
228 BufferParams const & bparams = buffer.params();
229 Inset * inset = createInset(buffer, cmd);
233 if (InsetCollapsable * ci = inset->asInsetCollapsable())
234 ci->setLayout(bparams);
237 if (cmd.action == LFUN_INDEX_INSERT) {
238 docstring ds = subst(text->getStringToIndex(cur), '\n', ' ');
239 text->insertInset(cur, inset);
241 inset->edit(cur, true);
242 // Now put this into inset
243 static_cast<InsetCollapsable *>(inset)->
244 text().insertStringAsParagraphs(cur, ds);
249 if (cur.selection()) {
250 cutSelection(cur, false, pastesel);
251 cur.clearSelection();
254 text->insertInset(cur, inset);
257 inset->edit(cur, true);
259 if (!gotsel || !pastesel)
262 pasteFromStack(cur, cur.buffer()->errorList("Paste"), 0);
263 cur.buffer()->errors("Paste");
264 cur.clearSelection(); // bug 393
266 InsetText * insetText = dynamic_cast<InsetText *>(inset);
267 if (insetText && (!insetText->allowMultiPar() || cur.lastpit() == 0)) {
268 // reset first par to default
269 cur.text()->paragraphs().begin()
270 ->setPlainOrDefaultLayout(bparams.documentClass());
273 // Merge multiple paragraphs -- hack
274 while (cur.lastpit() > 0)
275 mergeParagraph(bparams, cur.text()->paragraphs(), 0);
276 cur.leaveInset(*inset);
278 cur.leaveInset(*inset);
279 // reset surrounding par to default
280 DocumentClass const & dc = bparams.documentClass();
281 docstring const layoutname = inset->usePlainLayout()
282 ? dc.plainLayoutName()
283 : dc.defaultLayoutName();
284 text->setLayout(cur, layoutname);
291 string const freefont2string()
293 return freefont.toString(toggleall);
297 /// the type of outline operation
299 OutlineUp, // Move this header with text down
300 OutlineDown, // Move this header with text up
301 OutlineIn, // Make this header deeper
302 OutlineOut // Make this header shallower
306 static void outline(OutlineOp mode, Cursor & cur)
308 Buffer & buf = *cur.buffer();
309 pit_type & pit = cur.pit();
310 ParagraphList & pars = buf.text().paragraphs();
311 ParagraphList::iterator bgn = pars.begin();
312 // The first paragraph of the area to be copied:
313 ParagraphList::iterator start = boost::next(bgn, pit);
314 // The final paragraph of area to be copied:
315 ParagraphList::iterator finish = start;
316 ParagraphList::iterator end = pars.end();
318 DocumentClass const & tc = buf.params().documentClass();
320 int const thistoclevel = start->layout().toclevel;
323 // Move out (down) from this section header
326 // Seek the one (on same level) below
327 for (; finish != end; ++finish) {
328 toclevel = finish->layout().toclevel;
329 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel) {
336 if (start == pars.begin())
339 ParagraphList::iterator dest = start;
340 // Move out (up) from this header
343 // Search previous same-level header above
346 toclevel = dest->layout().toclevel;
348 && (toclevel == Layout::NOT_IN_TOC
349 || toclevel > thistoclevel));
350 // Not found; do nothing
351 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
353 pit_type const newpit = distance(bgn, dest);
354 pit_type const len = distance(start, finish);
355 pit_type const deletepit = pit + len;
356 buf.undo().recordUndo(cur, ATOMIC_UNDO, newpit, deletepit - 1);
357 pars.insert(dest, start, finish);
358 start = boost::next(pars.begin(), deletepit);
360 pars.erase(start, finish);
367 // Go one down from *this* header:
368 ParagraphList::iterator dest = boost::next(finish, 1);
369 // Go further down to find header to insert in front of:
370 for (; dest != end; ++dest) {
371 toclevel = dest->layout().toclevel;
372 if (toclevel != Layout::NOT_IN_TOC
373 && toclevel <= thistoclevel) {
377 // One such was found:
378 pit_type newpit = distance(bgn, dest);
379 pit_type const len = distance(start, finish);
380 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, newpit - 1);
381 pars.insert(dest, start, finish);
382 start = boost::next(bgn, pit);
384 pars.erase(start, finish);
388 pit_type const len = distance(start, finish);
389 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
390 for (; start != finish; ++start) {
391 toclevel = start->layout().toclevel;
392 if (toclevel == Layout::NOT_IN_TOC)
394 DocumentClass::const_iterator lit = tc.begin();
395 DocumentClass::const_iterator len = tc.end();
396 for (; lit != len; ++lit) {
397 if (lit->toclevel == toclevel + 1 &&
398 start->layout().labeltype == lit->labeltype) {
399 start->setLayout(*lit);
407 pit_type const len = distance(start, finish);
408 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
409 for (; start != finish; ++start) {
410 toclevel = start->layout().toclevel;
411 if (toclevel == Layout::NOT_IN_TOC)
413 DocumentClass::const_iterator lit = tc.begin();
414 DocumentClass::const_iterator len = tc.end();
415 for (; lit != len; ++lit) {
416 if (lit->toclevel == toclevel - 1 &&
417 start->layout().labeltype == lit->labeltype) {
418 start->setLayout(*lit);
429 void Text::number(Cursor & cur)
431 FontInfo font = ignore_font;
432 font.setNumber(FONT_TOGGLE);
433 toggleAndShow(cur, this, Font(font, ignore_language));
437 bool Text::isRTL(Buffer const & buffer, Paragraph const & par) const
439 return par.isRTL(buffer.params());
443 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
445 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
447 BufferView * bv = &cur.bv();
448 TextMetrics & tm = bv->textMetrics(this);
449 if (!tm.contains(cur.pit()))
450 lyx::dispatch(FuncRequest(LFUN_SCREEN_RECENTER));
452 // FIXME: We use the update flag to indicates wether a singlePar or a
453 // full screen update is needed. We reset it here but shall we restore it
457 LASSERT(cur.text() == this, /**/);
458 CursorSlice oldTopSlice = cur.top();
459 bool oldBoundary = cur.boundary();
460 bool sel = cur.selection();
461 // Signals that, even if needsUpdate == false, an update of the
462 // cursor paragraph is required
463 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
464 LyXAction::SingleParUpdate);
465 // Signals that a full-screen update is required
466 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
467 LyXAction::NoUpdate) || singleParUpdate);
469 switch (cmd.action) {
471 case LFUN_PARAGRAPH_MOVE_DOWN: {
472 pit_type const pit = cur.pit();
473 recUndo(cur, pit, pit + 1);
475 swap(pars_[pit], pars_[pit + 1]);
476 cur.buffer()->updateLabels();
482 case LFUN_PARAGRAPH_MOVE_UP: {
483 pit_type const pit = cur.pit();
484 recUndo(cur, pit - 1, pit);
486 swap(pars_[pit], pars_[pit - 1]);
487 cur.buffer()->updateLabels();
493 case LFUN_APPENDIX: {
494 Paragraph & par = cur.paragraph();
495 bool start = !par.params().startOfAppendix();
497 // FIXME: The code below only makes sense at top level.
498 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
499 // ensure that we have only one start_of_appendix in this document
500 // FIXME: this don't work for multipart document!
501 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
502 if (pars_[tmp].params().startOfAppendix()) {
504 pars_[tmp].params().startOfAppendix(false);
510 par.params().startOfAppendix(start);
512 // we can set the refreshing parameters now
513 cur.buffer()->updateLabels();
517 case LFUN_WORD_DELETE_FORWARD:
519 cutSelection(cur, true, false);
521 deleteWordForward(cur);
522 finishChange(cur, false);
525 case LFUN_WORD_DELETE_BACKWARD:
527 cutSelection(cur, true, false);
529 deleteWordBackward(cur);
530 finishChange(cur, false);
533 case LFUN_LINE_DELETE:
535 cutSelection(cur, true, false);
537 tm.deleteLineForward(cur);
538 finishChange(cur, false);
541 case LFUN_BUFFER_BEGIN:
542 case LFUN_BUFFER_BEGIN_SELECT:
543 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_BEGIN_SELECT);
544 if (cur.depth() == 1)
545 needsUpdate |= cursorTop(cur);
548 cur.updateFlags(Update::FitCursor);
551 case LFUN_BUFFER_END:
552 case LFUN_BUFFER_END_SELECT:
553 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_END_SELECT);
554 if (cur.depth() == 1)
555 needsUpdate |= cursorBottom(cur);
558 cur.updateFlags(Update::FitCursor);
561 case LFUN_CHAR_FORWARD:
562 case LFUN_CHAR_FORWARD_SELECT:
563 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
564 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
565 needsUpdate |= cursorForward(cur);
567 if (!needsUpdate && oldTopSlice == cur.top()
568 && cur.boundary() == oldBoundary) {
570 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
574 case LFUN_CHAR_BACKWARD:
575 case LFUN_CHAR_BACKWARD_SELECT:
576 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
577 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
578 needsUpdate |= cursorBackward(cur);
580 if (!needsUpdate && oldTopSlice == cur.top()
581 && cur.boundary() == oldBoundary) {
583 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
588 case LFUN_CHAR_LEFT_SELECT:
589 if (lyxrc.visual_cursor) {
590 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_LEFT_SELECT);
591 needsUpdate |= cursorVisLeft(cur);
592 if (!needsUpdate && oldTopSlice == cur.top()
593 && cur.boundary() == oldBoundary) {
595 cmd = FuncRequest(LFUN_FINISHED_LEFT);
598 if (reverseDirectionNeeded(cur)) {
599 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
600 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
602 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
603 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
610 case LFUN_CHAR_RIGHT:
611 case LFUN_CHAR_RIGHT_SELECT:
612 if (lyxrc.visual_cursor) {
613 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_RIGHT_SELECT);
614 needsUpdate |= cursorVisRight(cur);
615 if (!needsUpdate && oldTopSlice == cur.top()
616 && cur.boundary() == oldBoundary) {
618 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
621 if (reverseDirectionNeeded(cur)) {
622 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
623 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
625 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
626 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
635 case LFUN_DOWN_SELECT:
638 // stop/start the selection
639 bool select = cmd.action == LFUN_DOWN_SELECT ||
640 cmd.action == LFUN_UP_SELECT;
642 // move cursor up/down
643 bool up = cmd.action == LFUN_UP_SELECT || cmd.action == LFUN_UP;
644 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
646 if (!atFirstOrLastRow) {
647 needsUpdate |= cur.selHandle(select);
648 cur.selHandle(select);
649 cur.upDownInText(up, needsUpdate);
650 needsUpdate |= cur.beforeDispatchCursor().inMathed();
652 // if the cursor cannot be moved up or down do not remove
653 // the selection right now, but wait for the next dispatch.
655 needsUpdate |= cur.selHandle(select);
656 cur.upDownInText(up, needsUpdate);
663 case LFUN_PARAGRAPH_UP:
664 case LFUN_PARAGRAPH_UP_SELECT:
665 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
666 needsUpdate |= cursorUpParagraph(cur);
669 case LFUN_PARAGRAPH_DOWN:
670 case LFUN_PARAGRAPH_DOWN_SELECT:
671 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
672 needsUpdate |= cursorDownParagraph(cur);
675 case LFUN_LINE_BEGIN:
676 case LFUN_LINE_BEGIN_SELECT:
677 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_BEGIN_SELECT);
678 needsUpdate |= tm.cursorHome(cur);
682 case LFUN_LINE_END_SELECT:
683 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
684 needsUpdate |= tm.cursorEnd(cur);
687 case LFUN_WORD_RIGHT:
688 case LFUN_WORD_RIGHT_SELECT:
689 if (lyxrc.visual_cursor) {
690 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_RIGHT_SELECT);
691 needsUpdate |= cursorVisRightOneWord(cur);
692 if (!needsUpdate && oldTopSlice == cur.top()
693 && cur.boundary() == oldBoundary) {
695 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
698 if (reverseDirectionNeeded(cur)) {
699 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
700 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
702 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
703 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
710 case LFUN_WORD_FORWARD:
711 case LFUN_WORD_FORWARD_SELECT:
712 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT);
713 needsUpdate |= cursorForwardOneWord(cur);
717 case LFUN_WORD_LEFT_SELECT:
718 if (lyxrc.visual_cursor) {
719 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_LEFT_SELECT);
720 needsUpdate |= cursorVisLeftOneWord(cur);
721 if (!needsUpdate && oldTopSlice == cur.top()
722 && cur.boundary() == oldBoundary) {
724 cmd = FuncRequest(LFUN_FINISHED_LEFT);
727 if (reverseDirectionNeeded(cur)) {
728 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
729 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
731 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
732 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
739 case LFUN_WORD_BACKWARD:
740 case LFUN_WORD_BACKWARD_SELECT:
741 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT);
742 needsUpdate |= cursorBackwardOneWord(cur);
745 case LFUN_WORD_SELECT: {
746 selectWord(cur, WHOLE_WORD);
747 finishChange(cur, true);
751 case LFUN_NEWLINE_INSERT: {
752 InsetNewlineParams inp;
753 docstring arg = cmd.argument();
754 // this avoids a double undo
755 // FIXME: should not be needed, ideally
756 if (!cur.selection())
758 cap::replaceSelection(cur);
759 if (arg == "linebreak")
760 inp.kind = InsetNewlineParams::LINEBREAK;
762 inp.kind = InsetNewlineParams::NEWLINE;
763 cur.insert(new InsetNewline(inp));
765 moveCursor(cur, false);
769 case LFUN_CHAR_DELETE_FORWARD:
770 if (!cur.selection()) {
771 if (cur.pos() == cur.paragraph().size())
772 // Par boundary, force full-screen update
773 singleParUpdate = false;
774 needsUpdate |= erase(cur);
776 // It is possible to make it a lot faster still
777 // just comment out the line below...
779 cutSelection(cur, true, false);
780 singleParUpdate = false;
782 moveCursor(cur, false);
785 case LFUN_CHAR_DELETE_BACKWARD:
786 if (!cur.selection()) {
787 if (bv->getIntl().getTransManager().backspace()) {
788 // Par boundary, full-screen update
790 singleParUpdate = false;
791 needsUpdate |= backspace(cur);
793 // It is possible to make it a lot faster still
794 // just comment out the line below...
797 cutSelection(cur, true, false);
798 singleParUpdate = false;
802 case LFUN_BREAK_PARAGRAPH:
803 cap::replaceSelection(cur);
804 breakParagraph(cur, cmd.argument() == "inverse");
809 // With the creation of LFUN_PARAGRAPH_PARAMS, this is now redundant,
810 // as its duties can be performed there. Should it be removed??
811 // FIXME For now, it can just dispatch LFUN_PARAGRAPH_PARAMS...
812 case LFUN_PARAGRAPH_SPACING: {
813 Paragraph & par = cur.paragraph();
814 Spacing::Space cur_spacing = par.params().spacing().getSpace();
815 string cur_value = "1.0";
816 if (cur_spacing == Spacing::Other)
817 cur_value = par.params().spacing().getValueAsString();
819 istringstream is(to_utf8(cmd.argument()));
822 Spacing::Space new_spacing = cur_spacing;
823 string new_value = cur_value;
825 lyxerr << "Missing argument to `paragraph-spacing'"
827 } else if (tmp == "single") {
828 new_spacing = Spacing::Single;
829 } else if (tmp == "onehalf") {
830 new_spacing = Spacing::Onehalf;
831 } else if (tmp == "double") {
832 new_spacing = Spacing::Double;
833 } else if (tmp == "other") {
834 new_spacing = Spacing::Other;
835 string tmpval = "0.0";
837 lyxerr << "new_value = " << tmpval << endl;
840 } else if (tmp == "default") {
841 new_spacing = Spacing::Default;
843 lyxerr << to_utf8(_("Unknown spacing argument: "))
844 << to_utf8(cmd.argument()) << endl;
846 if (cur_spacing != new_spacing || cur_value != new_value)
847 par.params().spacing(Spacing(new_spacing, new_value));
851 case LFUN_INSET_INSERT: {
853 Inset * inset = createInset(bv->buffer(), cmd);
855 // FIXME (Abdel 01/02/2006):
856 // What follows would be a partial fix for bug 2154:
857 // http://bugzilla.lyx.org/show_bug.cgi?id=2154
858 // This automatically put the label inset _after_ a
859 // numbered section. It should be possible to extend the mechanism
860 // to any kind of LateX environement.
861 // The correct way to fix that bug would be at LateX generation.
862 // I'll let the code here for reference as it could be used for some
863 // other feature like "automatic labelling".
865 Paragraph & par = pars_[cur.pit()];
866 if (inset->lyxCode() == LABEL_CODE
867 && par.layout().labeltype == LABEL_COUNTER) {
868 // Go to the end of the paragraph
869 // Warning: Because of Change-Tracking, the last
870 // position is 'size()' and not 'size()-1':
871 cur.pos() = par.size();
872 // Insert a new paragraph
873 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
878 cutSelection(cur, true, false);
885 case LFUN_INSET_DISSOLVE:
886 needsUpdate |= dissolveInset(cur);
889 case LFUN_INSET_SETTINGS: {
890 Inset & inset = cur.inset();
891 if (cmd.getArg(0) == insetName(inset.lyxCode())) {
892 // This inset dialog has been explicitely requested.
893 inset.showInsetDialog(bv);
896 // else, if there is an inset at the cursor, access this
897 Inset * next_inset = cur.nextInset();
899 next_inset->showInsetDialog(bv);
902 // if not then access the underlying inset.
903 inset.showInsetDialog(bv);
907 case LFUN_SET_GRAPHICS_GROUP: {
908 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
914 string id = to_utf8(cmd.argument());
915 string grp = graphics::getGroupParams(bv->buffer(), id);
916 InsetGraphicsParams tmp, inspar = ins->getParams();
919 inspar.groupId = to_utf8(cmd.argument());
921 InsetGraphics::string2params(grp, bv->buffer(), tmp);
922 tmp.filename = inspar.filename;
926 ins->setParams(inspar);
929 case LFUN_SPACE_INSERT:
930 if (cur.paragraph().layout().free_spacing)
931 insertChar(cur, ' ');
933 doInsertInset(cur, this, cmd, false, false);
936 moveCursor(cur, false);
939 case LFUN_SPECIALCHAR_INSERT: {
940 string const name = to_utf8(cmd.argument());
941 if (name == "hyphenation")
942 specialChar(cur, InsetSpecialChar::HYPHENATION);
943 else if (name == "ligature-break")
944 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
945 else if (name == "slash")
946 specialChar(cur, InsetSpecialChar::SLASH);
947 else if (name == "nobreakdash")
948 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
949 else if (name == "dots")
950 specialChar(cur, InsetSpecialChar::LDOTS);
951 else if (name == "end-of-sentence")
952 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
953 else if (name == "menu-separator")
954 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
955 else if (name.empty())
956 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
958 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
962 case LFUN_WORD_UPCASE:
963 changeCase(cur, text_uppercase);
966 case LFUN_WORD_LOWCASE:
967 changeCase(cur, text_lowercase);
970 case LFUN_WORD_CAPITALIZE:
971 changeCase(cur, text_capitalization);
974 case LFUN_CHARS_TRANSPOSE:
979 cur.message(_("Paste"));
980 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), /**/);
981 cap::replaceSelection(cur);
984 string const arg = to_utf8(cmd.argument());
986 if (theClipboard().isInternal())
987 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
988 else if (theClipboard().hasGraphicsContents())
989 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
991 pasteClipboardText(cur, bv->buffer().errorList("Paste"));
992 } else if (isStrUnsignedInt(arg)) {
993 // we have a numerical argument
994 pasteFromStack(cur, bv->buffer().errorList("Paste"),
995 convert<unsigned int>(arg));
997 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
999 type = Clipboard::PdfGraphicsType;
1000 else if (arg == "png")
1001 type = Clipboard::PngGraphicsType;
1002 else if (arg == "jpeg")
1003 type = Clipboard::JpegGraphicsType;
1004 else if (arg == "linkback")
1005 type = Clipboard::LinkBackGraphicsType;
1007 LASSERT(false, /**/);
1009 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1012 bv->buffer().errors("Paste");
1013 cur.clearSelection(); // bug 393
1019 cutSelection(cur, true, true);
1020 cur.message(_("Cut"));
1025 cur.message(_("Copy"));
1028 case LFUN_SERVER_GET_XY:
1029 cur.message(from_utf8(
1030 convert<string>(tm.cursorX(cur.top(), cur.boundary()))
1031 + ' ' + convert<string>(tm.cursorY(cur.top(), cur.boundary()))));
1034 case LFUN_SERVER_SET_XY: {
1037 istringstream is(to_utf8(cmd.argument()));
1040 lyxerr << "SETXY: Could not parse coordinates in '"
1041 << to_utf8(cmd.argument()) << endl;
1043 tm.setCursorFromCoordinates(cur, x, y);
1047 case LFUN_SERVER_GET_LAYOUT:
1048 cur.message(cur.paragraph().layout().name());
1052 docstring layout = cmd.argument();
1053 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1055 Paragraph const & para = cur.paragraph();
1056 docstring const old_layout = para.layout().name();
1057 DocumentClass const & tclass = bv->buffer().params().documentClass();
1060 layout = tclass.defaultLayoutName();
1062 if (para.forcePlainLayout())
1063 // in this case only the empty layout is allowed
1064 layout = tclass.plainLayoutName();
1065 else if (para.usePlainLayout()) {
1066 // in this case, default layout maps to empty layout
1067 if (layout == tclass.defaultLayoutName())
1068 layout = tclass.plainLayoutName();
1070 // otherwise, the empty layout maps to the default
1071 if (layout == tclass.plainLayoutName())
1072 layout = tclass.defaultLayoutName();
1075 bool hasLayout = tclass.hasLayout(layout);
1077 // If the entry is obsolete, use the new one instead.
1079 docstring const & obs = tclass[layout].obsoleted_by();
1085 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1086 from_utf8(N_(" not known")));
1090 bool change_layout = (old_layout != layout);
1092 if (!change_layout && cur.selection() &&
1093 cur.selBegin().pit() != cur.selEnd().pit())
1095 pit_type spit = cur.selBegin().pit();
1096 pit_type epit = cur.selEnd().pit() + 1;
1097 while (spit != epit) {
1098 if (pars_[spit].layout().name() != old_layout) {
1099 change_layout = true;
1107 setLayout(cur, layout);
1112 case LFUN_CLIPBOARD_PASTE:
1113 cur.clearSelection();
1114 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1115 cmd.argument() == "paragraph");
1116 bv->buffer().errors("Paste");
1119 case LFUN_PRIMARY_SELECTION_PASTE:
1120 pasteString(cur, theSelection().get(),
1121 cmd.argument() == "paragraph");
1124 case LFUN_UNICODE_INSERT: {
1125 if (cmd.argument().empty())
1127 docstring hexstring = cmd.argument();
1128 if (isHex(hexstring)) {
1129 char_type c = hexToInt(hexstring);
1130 if (c >= 32 && c < 0x10ffff) {
1131 lyxerr << "Inserting c: " << c << endl;
1132 docstring s = docstring(1, c);
1133 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1139 case LFUN_QUOTE_INSERT: {
1140 Paragraph & par = cur.paragraph();
1141 pos_type pos = cur.pos();
1142 BufferParams const & bufparams = bv->buffer().params();
1143 Layout const & style = par.layout();
1144 if (!style.pass_thru
1145 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1146 // this avoids a double undo
1147 // FIXME: should not be needed, ideally
1148 if (!cur.selection())
1150 cap::replaceSelection(cur);
1155 else if (cur.prevInset() && cur.prevInset()->isSpace())
1158 c = par.getChar(pos - 1);
1159 string arg = to_utf8(cmd.argument());
1160 cur.insert(new InsetQuotes(bv->buffer(), c, (arg == "single")
1161 ? InsetQuotes::SingleQuotes : InsetQuotes::DoubleQuotes));
1165 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1169 case LFUN_DATE_INSERT: {
1170 string const format = cmd.argument().empty()
1171 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1172 string const time = formatted_time(current_time(), format);
1173 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1177 case LFUN_MOUSE_TRIPLE:
1178 if (cmd.button() == mouse_button::button1) {
1187 case LFUN_MOUSE_DOUBLE:
1188 if (cmd.button() == mouse_button::button1) {
1189 selectWord(cur, WHOLE_WORD_STRICT);
1194 // Single-click on work area
1195 case LFUN_MOUSE_PRESS:
1196 // We are not marking a selection with the keyboard in any case.
1197 cur.bv().cursor().setMark(false);
1198 switch (cmd.button()) {
1199 case mouse_button::button1:
1201 if (!bv->mouseSetCursor(cur, cmd.argument() == "region-select"))
1202 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1205 case mouse_button::button2:
1206 // Middle mouse pasting.
1207 bv->mouseSetCursor(cur);
1208 if (!cap::selection()) {
1209 // There is no local selection in the current buffer, so try to
1210 // paste primary selection instead.
1211 lyx::dispatch(FuncRequest(LFUN_PRIMARY_SELECTION_PASTE,
1213 // Nothing else to do.
1217 // Copy the selection buffer to the clipboard stack, because we want it
1218 // to appear in the "Edit->Paste recent" menu.
1219 cap::copySelectionToStack();
1220 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1221 cur.updateFlags(Update::Force | Update::FitCursor);
1222 bv->buffer().errors("Paste");
1223 bv->buffer().markDirty();
1224 bv->cursor().finishUndo();
1227 case mouse_button::button3: {
1228 Cursor const & bvcur = cur.bv().cursor();
1229 // Don't do anything if we right-click a
1230 // selection, a context menu will popup.
1231 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1232 && cur < bvcur.selectionEnd()) {
1236 if (!bv->mouseSetCursor(cur, false))
1237 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1243 } // switch (cmd.button())
1246 case LFUN_MOUSE_MOTION: {
1247 // Mouse motion with right or middle mouse do nothing for now.
1248 if (cmd.button() != mouse_button::button1) {
1252 // ignore motions deeper nested than the real anchor
1253 Cursor & bvcur = cur.bv().cursor();
1254 if (!bvcur.anchor_.hasPart(cur)) {
1258 CursorSlice old = bvcur.top();
1260 int const wh = bv->workHeight();
1261 int const y = max(0, min(wh - 1, cmd.y));
1263 tm.setCursorFromCoordinates(cur, cmd.x, y);
1264 cur.setTargetX(cmd.x);
1266 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1268 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1269 // This is to allow jumping over large insets
1270 if (cur.top() == old) {
1272 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1274 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1276 // We continue with our existing selection or start a new one, so don't
1277 // reset the anchor.
1278 bvcur.setCursor(cur);
1279 bvcur.setSelection(true);
1280 if (cur.top() == old) {
1281 // We didn't move one iota, so no need to update the screen.
1282 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1289 case LFUN_MOUSE_RELEASE:
1290 switch (cmd.button()) {
1291 case mouse_button::button1:
1292 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1293 // If there is a new selection, update persistent selection;
1294 // otherwise, single click does not clear persistent selection
1296 if (cur.selection()) {
1297 // Finish selection. If double click,
1298 // cur is moved to the end of word by
1299 // selectWord but bvcur is current
1301 cur.bv().cursor().setSelection();
1302 // We might have removed an empty but drawn selection
1303 // (probably a margin)
1304 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1307 // FIXME: We could try to handle drag and drop of selection here.
1310 case mouse_button::button2:
1311 // Middle mouse pasting is handled at mouse press time,
1312 // see LFUN_MOUSE_PRESS.
1316 case mouse_button::button3:
1317 // Cursor was set at LFUN_MOUSE_PRESS time.
1318 // FIXME: If there is a selection we could try to handle a special
1319 // drag & drop context menu.
1323 case mouse_button::none:
1324 case mouse_button::button4:
1325 case mouse_button::button5:
1327 } // switch (cmd.button())
1331 case LFUN_SELF_INSERT: {
1332 if (cmd.argument().empty())
1335 // Automatically delete the currently selected
1336 // text and replace it with what is being
1337 // typed in now. Depends on lyxrc settings
1338 // "auto_region_delete", which defaults to
1341 if (lyxrc.auto_region_delete && cur.selection())
1342 cutSelection(cur, false, false);
1344 cur.clearSelection();
1346 docstring::const_iterator cit = cmd.argument().begin();
1347 docstring::const_iterator const end = cmd.argument().end();
1348 for (; cit != end; ++cit)
1349 bv->translateAndInsert(*cit, this, cur);
1352 moveCursor(cur, false);
1356 case LFUN_HYPERLINK_INSERT: {
1357 InsetCommandParams p(HYPERLINK_CODE);
1359 if (cur.selection()) {
1360 content = cur.selectionAsString(false);
1361 cutSelection(cur, true, false);
1363 p["target"] = (cmd.argument().empty()) ?
1364 content : cmd.argument();
1365 string const data = InsetCommand::params2string("href", p);
1366 if (p["target"].empty()) {
1367 bv->showDialog("href", data);
1369 FuncRequest fr(LFUN_INSET_INSERT, data);
1375 case LFUN_LABEL_INSERT: {
1376 InsetCommandParams p(LABEL_CODE);
1377 // Try to generate a valid label
1378 p["name"] = (cmd.argument().empty()) ?
1379 cur.getPossibleLabel() :
1381 string const data = InsetCommand::params2string("label", p);
1383 if (cmd.argument().empty()) {
1384 bv->showDialog("label", data);
1386 FuncRequest fr(LFUN_INSET_INSERT, data);
1392 case LFUN_INFO_INSERT: {
1394 if (cmd.argument().empty() && cur.selection()) {
1395 // if command argument is empty use current selection as parameter.
1396 docstring ds = cur.selectionAsString(false);
1397 cutSelection(cur, true, false);
1398 FuncRequest cmd0(cmd, ds);
1399 inset = createInset(cur.bv().buffer(), cmd0);
1401 inset = createInset(cur.bv().buffer(), cmd);
1405 insertInset(cur, inset);
1409 case LFUN_CAPTION_INSERT:
1410 case LFUN_FOOTNOTE_INSERT:
1411 case LFUN_NOTE_INSERT:
1412 case LFUN_FLEX_INSERT:
1413 case LFUN_BOX_INSERT:
1414 case LFUN_BRANCH_INSERT:
1415 case LFUN_ERT_INSERT:
1416 case LFUN_LISTING_INSERT:
1417 case LFUN_MARGINALNOTE_INSERT:
1418 case LFUN_OPTIONAL_INSERT:
1419 case LFUN_INDEX_INSERT:
1420 // Open the inset, and move the current selection
1422 doInsertInset(cur, this, cmd, true, true);
1424 // Some insets are numbered, others are shown in the outline pane so
1425 // let's update the labels and the toc backend.
1426 bv->buffer().updateLabels();
1429 case LFUN_TABULAR_INSERT:
1430 // if there were no arguments, just open the dialog
1431 if (doInsertInset(cur, this, cmd, false, true))
1434 bv->showDialog("tabularcreate");
1438 case LFUN_FLOAT_INSERT:
1439 case LFUN_FLOAT_WIDE_INSERT:
1440 case LFUN_WRAP_INSERT: {
1441 // will some text be moved into the inset?
1442 bool content = cur.selection();
1444 doInsertInset(cur, this, cmd, true, true);
1447 // If some text is moved into the inset, doInsertInset
1448 // puts the cursor outside the inset. To insert the
1449 // caption we put it back into the inset.
1453 ParagraphList & pars = cur.text()->paragraphs();
1455 DocumentClass const & tclass = bv->buffer().params().documentClass();
1457 // add a separate paragraph for the caption inset
1458 pars.push_back(Paragraph());
1459 pars.back().setInsetOwner(&pars[0].inInset());
1460 pars.back().setPlainOrDefaultLayout(tclass);
1461 int cap_pit = pars.size() - 1;
1463 // if an empty inset was created, we create an additional empty
1464 // paragraph at the bottom so that the user can choose where to put
1465 // the graphics (or table).
1467 pars.push_back(Paragraph());
1468 pars.back().setInsetOwner(&pars[0].inInset());
1469 pars.back().setPlainOrDefaultLayout(tclass);
1472 // reposition the cursor to the caption
1473 cur.pit() = cap_pit;
1475 // FIXME: This Text/Cursor dispatch handling is a mess!
1476 // We cannot use Cursor::dispatch here it needs access to up to
1478 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1479 doInsertInset(cur, cur.text(), cmd_caption, true, false);
1480 bv->buffer().updateLabels();
1481 cur.updateFlags(Update::Force);
1482 // FIXME: When leaving the Float (or Wrap) inset we should
1483 // delete any empty paragraph left above or below the
1488 case LFUN_NOMENCL_INSERT: {
1489 InsetCommandParams p(NOMENCL_CODE);
1490 if (cmd.argument().empty())
1491 p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
1493 p["symbol"] = cmd.argument();
1494 string const data = InsetCommand::params2string("nomenclature", p);
1495 bv->showDialog("nomenclature", data);
1499 case LFUN_INDEX_PRINT:
1500 case LFUN_NOMENCL_PRINT:
1501 case LFUN_TOC_INSERT:
1502 case LFUN_LINE_INSERT:
1503 case LFUN_NEWPAGE_INSERT:
1505 doInsertInset(cur, this, cmd, false, false);
1509 case LFUN_DEPTH_DECREMENT:
1510 changeDepth(cur, DEC_DEPTH);
1513 case LFUN_DEPTH_INCREMENT:
1514 changeDepth(cur, INC_DEPTH);
1517 case LFUN_MATH_DISPLAY:
1518 mathDispatch(cur, cmd, true);
1521 case LFUN_REGEXP_MODE:
1522 regexpDispatch(cur, cmd);
1525 case LFUN_MATH_MODE:
1526 if (cmd.argument() == "on")
1527 // don't pass "on" as argument
1528 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1530 mathDispatch(cur, cmd, false);
1533 case LFUN_MATH_MACRO:
1534 if (cmd.argument().empty())
1535 cur.errorMessage(from_utf8(N_("Missing argument")));
1537 string s = to_utf8(cmd.argument());
1538 string const s1 = token(s, ' ', 1);
1539 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1540 string const s2 = token(s, ' ', 2);
1541 MacroType type = MacroTypeNewcommand;
1543 type = MacroTypeDef;
1544 MathMacroTemplate * inset = new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, false, type);
1545 inset->setBuffer(bv->buffer());
1546 insertInset(cur, inset);
1548 // enter macro inset and select the name
1550 cur.top().pos() = cur.top().lastpos();
1552 cur.setSelection(true);
1553 cur.top().pos() = 0;
1557 // passthrough hat and underscore outside mathed:
1558 case LFUN_MATH_SUBSCRIPT:
1559 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1561 case LFUN_MATH_SUPERSCRIPT:
1562 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1565 case LFUN_MATH_INSERT:
1566 case LFUN_MATH_MATRIX:
1567 case LFUN_MATH_DELIM:
1568 case LFUN_MATH_BIGDELIM: {
1570 cap::replaceSelection(cur);
1571 cur.insert(new InsetMathHull(hullSimple));
1572 checkAndActivateInset(cur, true);
1573 LASSERT(cur.inMathed(), /**/);
1578 case LFUN_FONT_EMPH: {
1579 Font font(ignore_font, ignore_language);
1580 font.fontInfo().setEmph(FONT_TOGGLE);
1581 toggleAndShow(cur, this, font);
1585 case LFUN_FONT_BOLD:
1586 case LFUN_FONT_BOLDSYMBOL: {
1587 Font font(ignore_font, ignore_language);
1588 font.fontInfo().setSeries(BOLD_SERIES);
1589 toggleAndShow(cur, this, font);
1593 case LFUN_FONT_NOUN: {
1594 Font font(ignore_font, ignore_language);
1595 font.fontInfo().setNoun(FONT_TOGGLE);
1596 toggleAndShow(cur, this, font);
1600 case LFUN_FONT_TYPEWRITER: {
1601 Font font(ignore_font, ignore_language);
1602 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1603 toggleAndShow(cur, this, font);
1607 case LFUN_FONT_SANS: {
1608 Font font(ignore_font, ignore_language);
1609 font.fontInfo().setFamily(SANS_FAMILY);
1610 toggleAndShow(cur, this, font);
1614 case LFUN_FONT_ROMAN: {
1615 Font font(ignore_font, ignore_language);
1616 font.fontInfo().setFamily(ROMAN_FAMILY);
1617 toggleAndShow(cur, this, font);
1621 case LFUN_FONT_DEFAULT: {
1622 Font font(inherit_font, ignore_language);
1623 toggleAndShow(cur, this, font);
1627 case LFUN_FONT_UNDERLINE: {
1628 Font font(ignore_font, ignore_language);
1629 font.fontInfo().setUnderbar(FONT_TOGGLE);
1630 toggleAndShow(cur, this, font);
1634 case LFUN_FONT_SIZE: {
1635 Font font(ignore_font, ignore_language);
1636 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1637 toggleAndShow(cur, this, font);
1641 case LFUN_LANGUAGE: {
1642 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1645 Font font(ignore_font, lang);
1646 toggleAndShow(cur, this, font);
1650 case LFUN_TEXTSTYLE_APPLY:
1651 toggleAndShow(cur, this, freefont, toggleall);
1652 cur.message(_("Character set"));
1655 // Set the freefont using the contents of \param data dispatched from
1656 // the frontends and apply it at the current cursor location.
1657 case LFUN_TEXTSTYLE_UPDATE: {
1660 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1663 toggleAndShow(cur, this, freefont, toggleall);
1664 cur.message(_("Character set"));
1666 lyxerr << "Argument not ok";
1671 case LFUN_FINISHED_LEFT:
1672 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
1673 // We're leaving an inset, going left. If the inset is LTR, we're
1674 // leaving from the front, so we should not move (remain at --- but
1675 // not in --- the inset). If the inset is RTL, move left, without
1676 // entering the inset itself; i.e., move to after the inset.
1677 if (cur.paragraph().getFontSettings(
1678 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1679 cursorVisLeft(cur, true);
1682 case LFUN_FINISHED_RIGHT:
1683 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
1684 // We're leaving an inset, going right. If the inset is RTL, we're
1685 // leaving from the front, so we should not move (remain at --- but
1686 // not in --- the inset). If the inset is LTR, move right, without
1687 // entering the inset itself; i.e., move to after the inset.
1688 if (!cur.paragraph().getFontSettings(
1689 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1690 cursorVisRight(cur, true);
1693 case LFUN_FINISHED_BACKWARD:
1694 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
1697 case LFUN_FINISHED_FORWARD:
1698 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
1700 cur.setCurrentFont();
1703 case LFUN_LAYOUT_PARAGRAPH: {
1705 params2string(cur.paragraph(), data);
1706 data = "show\n" + data;
1707 bv->showDialog("paragraph", data);
1711 case LFUN_PARAGRAPH_UPDATE: {
1713 params2string(cur.paragraph(), data);
1715 // Will the paragraph accept changes from the dialog?
1717 cur.inset().allowParagraphCustomization(cur.idx());
1719 data = "update " + convert<string>(accept) + '\n' + data;
1720 bv->updateDialog("paragraph", data);
1724 case LFUN_ACCENT_UMLAUT:
1725 case LFUN_ACCENT_CIRCUMFLEX:
1726 case LFUN_ACCENT_GRAVE:
1727 case LFUN_ACCENT_ACUTE:
1728 case LFUN_ACCENT_TILDE:
1729 case LFUN_ACCENT_CEDILLA:
1730 case LFUN_ACCENT_MACRON:
1731 case LFUN_ACCENT_DOT:
1732 case LFUN_ACCENT_UNDERDOT:
1733 case LFUN_ACCENT_UNDERBAR:
1734 case LFUN_ACCENT_CARON:
1735 case LFUN_ACCENT_BREVE:
1736 case LFUN_ACCENT_TIE:
1737 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1738 case LFUN_ACCENT_CIRCLE:
1739 case LFUN_ACCENT_OGONEK:
1740 theLyXFunc().handleKeyFunc(cmd.action);
1741 if (!cmd.argument().empty())
1742 // FIXME: Are all these characters encoded in one byte in utf8?
1743 bv->translateAndInsert(cmd.argument()[0], this, cur);
1746 case LFUN_FLOAT_LIST_INSERT: {
1747 DocumentClass const & tclass = bv->buffer().params().documentClass();
1748 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1750 if (cur.selection())
1751 cutSelection(cur, true, false);
1752 breakParagraph(cur);
1754 if (cur.lastpos() != 0) {
1755 cursorBackward(cur);
1756 breakParagraph(cur);
1759 docstring const laystr = cur.inset().usePlainLayout() ?
1760 tclass.plainLayoutName() :
1761 tclass.defaultLayoutName();
1762 setLayout(cur, laystr);
1763 ParagraphParameters p;
1764 // FIXME If this call were replaced with one to clearParagraphParams(),
1765 // then we could get rid of this method altogether.
1766 setParagraphs(cur, p);
1767 // FIXME This should be simplified when InsetFloatList takes a
1768 // Buffer in its constructor.
1769 InsetFloatList * ifl = new InsetFloatList(to_utf8(cmd.argument()));
1770 ifl->setBuffer(bv->buffer());
1771 insertInset(cur, ifl);
1774 lyxerr << "Non-existent float type: "
1775 << to_utf8(cmd.argument()) << endl;
1780 case LFUN_CHANGE_ACCEPT: {
1781 acceptOrRejectChanges(cur, ACCEPT);
1785 case LFUN_CHANGE_REJECT: {
1786 acceptOrRejectChanges(cur, REJECT);
1790 case LFUN_THESAURUS_ENTRY: {
1791 docstring arg = cmd.argument();
1793 arg = cur.selectionAsString(false);
1795 if (arg.size() > 100 || arg.empty()) {
1796 // Get word or selection
1797 selectWordWhenUnderCursor(cur, WHOLE_WORD);
1798 arg = cur.selectionAsString(false);
1799 arg += " lang=" + from_ascii(cur.getFont().language()->lang());
1802 bv->showDialog("thesaurus", to_utf8(arg));
1806 case LFUN_PARAGRAPH_PARAMS_APPLY: {
1807 // Given data, an encoding of the ParagraphParameters
1808 // generated in the Paragraph dialog, this function sets
1809 // the current paragraph, or currently selected paragraphs,
1811 // NOTE: This function overrides all existing settings.
1812 setParagraphs(cur, cmd.argument());
1813 cur.message(_("Paragraph layout set"));
1817 case LFUN_PARAGRAPH_PARAMS: {
1818 // Given data, an encoding of the ParagraphParameters as we'd
1819 // find them in a LyX file, this function modifies the current paragraph,
1820 // or currently selected paragraphs.
1821 // NOTE: This function only modifies, and does not override, existing
1823 setParagraphs(cur, cmd.argument(), true);
1824 cur.message(_("Paragraph layout set"));
1829 if (cur.selection()) {
1830 cur.setSelection(false);
1833 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
1834 // correct, but I'm not 100% sure -- dov, 071019
1835 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1839 case LFUN_OUTLINE_UP:
1840 outline(OutlineUp, cur);
1841 setCursor(cur, cur.pit(), 0);
1842 cur.buffer()->updateLabels();
1846 case LFUN_OUTLINE_DOWN:
1847 outline(OutlineDown, cur);
1848 setCursor(cur, cur.pit(), 0);
1849 cur.buffer()->updateLabels();
1853 case LFUN_OUTLINE_IN:
1854 outline(OutlineIn, cur);
1855 cur.buffer()->updateLabels();
1859 case LFUN_OUTLINE_OUT:
1860 outline(OutlineOut, cur);
1861 cur.buffer()->updateLabels();
1866 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
1871 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
1873 // FIXME: The cursor flag is reset two lines below
1874 // so we need to check here if some of the LFUN did touch that.
1875 // for now only Text::erase() and Text::backspace() do that.
1876 // The plan is to verify all the LFUNs and then to remove this
1877 // singleParUpdate boolean altogether.
1878 if (cur.result().update() & Update::Force) {
1879 singleParUpdate = false;
1883 // FIXME: the following code should go in favor of fine grained
1884 // update flag treatment.
1885 if (singleParUpdate) {
1886 // Inserting characters does not change par height in general. So, try
1887 // to update _only_ this paragraph. BufferView will detect if a full
1888 // metrics update is needed anyway.
1889 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1894 && &oldTopSlice.inset() == &cur.inset()
1895 && oldTopSlice.idx() == cur.idx()
1896 && !sel // sel is a backup of cur.selection() at the biginning of the function.
1897 && !cur.selection())
1898 // FIXME: it would be better if we could just do this
1900 //if (cur.result().update() != Update::FitCursor)
1903 // But some LFUNs do not set Update::FitCursor when needed, so we
1904 // do it for all. This is not very harmfull as FitCursor will provoke
1905 // a full redraw only if needed but still, a proper review of all LFUN
1906 // should be done and this needsUpdate boolean can then be removed.
1907 cur.updateFlags(Update::FitCursor);
1909 cur.updateFlags(Update::Force | Update::FitCursor);
1913 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
1914 FuncStatus & flag) const
1916 LASSERT(cur.text() == this, /**/);
1918 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
1920 InsetCode code = NO_CODE;
1922 switch (cmd.action) {
1924 case LFUN_DEPTH_DECREMENT:
1925 enable = changeDepthAllowed(cur, DEC_DEPTH);
1928 case LFUN_DEPTH_INCREMENT:
1929 enable = changeDepthAllowed(cur, INC_DEPTH);
1933 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1936 case LFUN_DIALOG_SHOW_NEW_INSET:
1937 if (cmd.argument() == "bibitem")
1938 code = BIBITEM_CODE;
1939 else if (cmd.argument() == "bibtex")
1941 else if (cmd.argument() == "box")
1943 else if (cmd.argument() == "branch")
1945 else if (cmd.argument() == "citation")
1947 else if (cmd.argument() == "ert")
1949 else if (cmd.argument() == "external")
1950 code = EXTERNAL_CODE;
1951 else if (cmd.argument() == "float")
1953 else if (cmd.argument() == "graphics")
1954 code = GRAPHICS_CODE;
1955 else if (cmd.argument() == "href")
1956 code = HYPERLINK_CODE;
1957 else if (cmd.argument() == "include")
1958 code = INCLUDE_CODE;
1959 else if (cmd.argument() == "index")
1961 else if (cmd.argument() == "nomenclature")
1962 code = NOMENCL_CODE;
1963 else if (cmd.argument() == "label")
1965 else if (cmd.argument() == "note")
1967 else if (cmd.argument() == "ref")
1969 else if (cmd.argument() == "space")
1971 else if (cmd.argument() == "toc")
1973 else if (cmd.argument() == "vspace")
1975 else if (cmd.argument() == "wrap")
1977 else if (cmd.argument() == "listings")
1978 code = LISTINGS_CODE;
1981 case LFUN_ERT_INSERT:
1984 case LFUN_LISTING_INSERT:
1985 code = LISTINGS_CODE;
1986 // not allowed in description items
1987 enable = !inDescriptionItem(cur);
1989 case LFUN_FOOTNOTE_INSERT:
1992 case LFUN_TABULAR_INSERT:
1993 code = TABULAR_CODE;
1995 case LFUN_MARGINALNOTE_INSERT:
1998 case LFUN_FLOAT_INSERT:
1999 case LFUN_FLOAT_WIDE_INSERT:
2001 // not allowed in description items
2002 enable = !inDescriptionItem(cur);
2004 case LFUN_WRAP_INSERT:
2006 // not allowed in description items
2007 enable = !inDescriptionItem(cur);
2009 case LFUN_FLOAT_LIST_INSERT:
2010 code = FLOAT_LIST_CODE;
2012 case LFUN_CAPTION_INSERT:
2013 code = CAPTION_CODE;
2014 // not allowed in description items
2015 enable = !inDescriptionItem(cur);
2017 case LFUN_NOTE_INSERT:
2019 // in commands (sections etc.) and description items,
2020 // only Notes are allowed
2021 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
2022 (!cur.paragraph().layout().isCommand()
2023 && !inDescriptionItem(cur)));
2025 case LFUN_FLEX_INSERT: {
2027 string s = cmd.getArg(0);
2029 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2030 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2031 il.lyxtype() != InsetLayout::CUSTOM &&
2032 il.lyxtype() != InsetLayout::ELEMENT &&
2033 il.lyxtype ()!= InsetLayout::STANDARD)
2037 case LFUN_BOX_INSERT:
2040 case LFUN_BRANCH_INSERT:
2042 if (cur.buffer()->masterBuffer()->params().branchlist().empty())
2045 case LFUN_LABEL_INSERT:
2048 case LFUN_INFO_INSERT:
2051 case LFUN_OPTIONAL_INSERT:
2053 enable = cur.paragraph().insetList().count(OPTARG_CODE)
2054 < cur.paragraph().layout().optionalargs;
2056 case LFUN_INDEX_INSERT:
2059 case LFUN_INDEX_PRINT:
2060 code = INDEX_PRINT_CODE;
2062 case LFUN_NOMENCL_INSERT:
2063 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2067 code = NOMENCL_CODE;
2069 case LFUN_NOMENCL_PRINT:
2070 code = NOMENCL_PRINT_CODE;
2072 case LFUN_TOC_INSERT:
2075 case LFUN_HYPERLINK_INSERT:
2076 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2080 code = HYPERLINK_CODE;
2082 case LFUN_QUOTE_INSERT:
2083 // always allow this, since we will inset a raw quote
2084 // if an inset is not allowed.
2086 case LFUN_SPECIALCHAR_INSERT:
2087 code = SPECIALCHAR_CODE;
2089 case LFUN_SPACE_INSERT:
2090 // slight hack: we know this is allowed in math mode
2095 case LFUN_INSET_MODIFY:
2096 // We need to disable this, because we may get called for a
2098 // InsetTabular::getStatus() -> InsetText::getStatus()
2099 // and we don't handle LFUN_INSET_MODIFY.
2103 case LFUN_FONT_EMPH:
2104 flag.setOnOff(fontinfo.emph() == FONT_ON);
2107 case LFUN_FONT_NOUN:
2108 flag.setOnOff(fontinfo.noun() == FONT_ON);
2111 case LFUN_FONT_BOLD:
2112 case LFUN_FONT_BOLDSYMBOL:
2113 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2116 case LFUN_FONT_SANS:
2117 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2120 case LFUN_FONT_ROMAN:
2121 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2124 case LFUN_FONT_TYPEWRITER:
2125 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2130 enable = cur.selection();
2134 if (cmd.argument().empty()) {
2135 if (theClipboard().isInternal())
2136 enable = cap::numberOfSelections() > 0;
2138 enable = !theClipboard().empty();
2142 // we have an argument
2143 string const arg = to_utf8(cmd.argument());
2144 if (isStrUnsignedInt(arg)) {
2145 // it's a number and therefore means the internal stack
2146 unsigned int n = convert<unsigned int>(arg);
2147 enable = cap::numberOfSelections() > n;
2151 // explicit graphics type?
2152 if ((arg == "pdf" && theClipboard().hasGraphicsContents(Clipboard::PdfGraphicsType))
2153 || (arg == "png" && theClipboard().hasGraphicsContents(Clipboard::PngGraphicsType))
2154 || (arg == "jpeg" && theClipboard().hasGraphicsContents(Clipboard::JpegGraphicsType))
2155 || (arg == "linkback" && theClipboard().hasGraphicsContents(Clipboard::LinkBackGraphicsType))) {
2165 case LFUN_CLIPBOARD_PASTE:
2166 enable = !theClipboard().empty();
2169 case LFUN_PRIMARY_SELECTION_PASTE:
2170 enable = cur.selection() || !theSelection().empty();
2173 case LFUN_PARAGRAPH_MOVE_UP:
2174 enable = cur.pit() > 0 && !cur.selection();
2177 case LFUN_PARAGRAPH_MOVE_DOWN:
2178 enable = cur.pit() < cur.lastpit() && !cur.selection();
2181 case LFUN_INSET_DISSOLVE:
2182 if (!cmd.argument().empty()) {
2183 InsetLayout const & il = cur.inset().getLayout(cur.buffer()->params());
2184 InsetLayout::InsetLyXType const type =
2185 translateLyXType(to_utf8(cmd.argument()));
2186 enable = cur.inset().lyxCode() == FLEX_CODE
2187 && il.lyxtype() == type;
2189 enable = !isMainText(cur.bv().buffer())
2190 && cur.inset().nargs() == 1;
2194 case LFUN_CHANGE_ACCEPT:
2195 case LFUN_CHANGE_REJECT:
2196 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
2197 // In principle, these LFUNs should only be enabled if there
2198 // is a change at the current position/in the current selection.
2199 // However, without proper optimizations, this will inevitably
2200 // result in unacceptable performance - just imagine a user who
2201 // wants to select the complete content of a long document.
2205 case LFUN_OUTLINE_UP:
2206 case LFUN_OUTLINE_DOWN:
2207 case LFUN_OUTLINE_IN:
2208 case LFUN_OUTLINE_OUT:
2209 // FIXME: LyX is not ready for outlining within inset.
2210 enable = isMainText(cur.bv().buffer())
2211 && cur.paragraph().layout().toclevel != Layout::NOT_IN_TOC;
2214 case LFUN_NEWLINE_INSERT:
2215 // LaTeX restrictions (labels or empty par)
2216 enable = (cur.pos() > cur.paragraph().beginOfBody());
2219 case LFUN_SET_GRAPHICS_GROUP: {
2220 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
2224 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
2228 case LFUN_NEWPAGE_INSERT:
2229 // not allowed in description items
2230 enable = !inDescriptionItem(cur);
2233 case LFUN_WORD_DELETE_FORWARD:
2234 case LFUN_WORD_DELETE_BACKWARD:
2235 case LFUN_LINE_DELETE:
2236 case LFUN_WORD_FORWARD:
2237 case LFUN_WORD_BACKWARD:
2238 case LFUN_WORD_RIGHT:
2239 case LFUN_WORD_LEFT:
2240 case LFUN_CHAR_FORWARD:
2241 case LFUN_CHAR_FORWARD_SELECT:
2242 case LFUN_CHAR_BACKWARD:
2243 case LFUN_CHAR_BACKWARD_SELECT:
2244 case LFUN_CHAR_LEFT:
2245 case LFUN_CHAR_LEFT_SELECT:
2246 case LFUN_CHAR_RIGHT:
2247 case LFUN_CHAR_RIGHT_SELECT:
2249 case LFUN_UP_SELECT:
2251 case LFUN_DOWN_SELECT:
2252 case LFUN_PARAGRAPH_UP_SELECT:
2253 case LFUN_PARAGRAPH_DOWN_SELECT:
2254 case LFUN_LINE_BEGIN_SELECT:
2255 case LFUN_LINE_END_SELECT:
2256 case LFUN_WORD_FORWARD_SELECT:
2257 case LFUN_WORD_BACKWARD_SELECT:
2258 case LFUN_WORD_RIGHT_SELECT:
2259 case LFUN_WORD_LEFT_SELECT:
2260 case LFUN_WORD_SELECT:
2261 case LFUN_PARAGRAPH_UP:
2262 case LFUN_PARAGRAPH_DOWN:
2263 case LFUN_LINE_BEGIN:
2265 case LFUN_CHAR_DELETE_FORWARD:
2266 case LFUN_CHAR_DELETE_BACKWARD:
2267 case LFUN_BREAK_PARAGRAPH:
2268 case LFUN_PARAGRAPH_SPACING:
2269 case LFUN_INSET_INSERT:
2270 case LFUN_WORD_UPCASE:
2271 case LFUN_WORD_LOWCASE:
2272 case LFUN_WORD_CAPITALIZE:
2273 case LFUN_CHARS_TRANSPOSE:
2274 case LFUN_SERVER_GET_XY:
2275 case LFUN_SERVER_SET_XY:
2276 case LFUN_SERVER_GET_LAYOUT:
2278 case LFUN_DATE_INSERT:
2279 case LFUN_SELF_INSERT:
2280 case LFUN_LINE_INSERT:
2281 case LFUN_MATH_DISPLAY:
2282 case LFUN_MATH_MODE:
2283 case LFUN_MATH_MACRO:
2284 case LFUN_MATH_MATRIX:
2285 case LFUN_MATH_DELIM:
2286 case LFUN_MATH_BIGDELIM:
2287 case LFUN_MATH_INSERT:
2288 case LFUN_MATH_SUBSCRIPT:
2289 case LFUN_MATH_SUPERSCRIPT:
2290 case LFUN_FONT_DEFAULT:
2291 case LFUN_FONT_UNDERLINE:
2292 case LFUN_FONT_SIZE:
2294 case LFUN_TEXTSTYLE_APPLY:
2295 case LFUN_TEXTSTYLE_UPDATE:
2296 case LFUN_LAYOUT_PARAGRAPH:
2297 case LFUN_PARAGRAPH_UPDATE:
2298 case LFUN_ACCENT_UMLAUT:
2299 case LFUN_ACCENT_CIRCUMFLEX:
2300 case LFUN_ACCENT_GRAVE:
2301 case LFUN_ACCENT_ACUTE:
2302 case LFUN_ACCENT_TILDE:
2303 case LFUN_ACCENT_CEDILLA:
2304 case LFUN_ACCENT_MACRON:
2305 case LFUN_ACCENT_DOT:
2306 case LFUN_ACCENT_UNDERDOT:
2307 case LFUN_ACCENT_UNDERBAR:
2308 case LFUN_ACCENT_CARON:
2309 case LFUN_ACCENT_BREVE:
2310 case LFUN_ACCENT_TIE:
2311 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2312 case LFUN_ACCENT_CIRCLE:
2313 case LFUN_ACCENT_OGONEK:
2314 case LFUN_THESAURUS_ENTRY:
2315 case LFUN_PARAGRAPH_PARAMS_APPLY:
2316 case LFUN_PARAGRAPH_PARAMS:
2318 case LFUN_BUFFER_END:
2319 case LFUN_BUFFER_BEGIN:
2320 case LFUN_BUFFER_BEGIN_SELECT:
2321 case LFUN_BUFFER_END_SELECT:
2322 case LFUN_UNICODE_INSERT:
2323 // these are handled in our dispatch()
2332 && (cur.empty() || !cur.inset().insetAllowed(code)))
2335 flag.setEnabled(enable);
2340 void Text::pasteString(Cursor & cur, docstring const & clip,
2343 cur.clearSelection();
2344 if (!clip.empty()) {
2347 insertStringAsParagraphs(cur, clip);
2349 insertStringAsLines(cur, clip);
2354 // FIXME: an item inset would make things much easier.
2355 bool Text::inDescriptionItem(Cursor & cur) const
2357 Paragraph & par = cur.paragraph();
2358 pos_type const pos = cur.pos();
2359 pos_type const body_pos = par.beginOfBody();
2361 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
2362 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
2363 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
2366 return (pos < body_pos
2368 && (pos == 0 || par.getChar(pos - 1) != ' ')));