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:
526 if (cur.selection()) {
527 cutSelection(cur, true, false);
529 deleteWordBackward(cur);
530 finishChange(cur, false);
533 case LFUN_LINE_DELETE:
534 if (cur.selection()) {
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);
549 cur.updateFlags(Update::FitCursor);
552 case LFUN_BUFFER_END:
553 case LFUN_BUFFER_END_SELECT:
554 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_END_SELECT);
555 if (cur.depth() == 1) {
556 needsUpdate |= cursorBottom(cur);
560 cur.updateFlags(Update::FitCursor);
563 case LFUN_CHAR_FORWARD:
564 case LFUN_CHAR_FORWARD_SELECT:
565 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
566 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
567 needsUpdate |= cursorForward(cur);
569 if (!needsUpdate && oldTopSlice == cur.top()
570 && cur.boundary() == oldBoundary) {
572 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
576 case LFUN_CHAR_BACKWARD:
577 case LFUN_CHAR_BACKWARD_SELECT:
578 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
579 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
580 needsUpdate |= cursorBackward(cur);
582 if (!needsUpdate && oldTopSlice == cur.top()
583 && cur.boundary() == oldBoundary) {
585 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
590 case LFUN_CHAR_LEFT_SELECT:
591 if (lyxrc.visual_cursor) {
592 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_LEFT_SELECT);
593 needsUpdate |= cursorVisLeft(cur);
594 if (!needsUpdate && oldTopSlice == cur.top()
595 && cur.boundary() == oldBoundary) {
597 cmd = FuncRequest(LFUN_FINISHED_LEFT);
600 if (reverseDirectionNeeded(cur)) {
601 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
602 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
604 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
605 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
612 case LFUN_CHAR_RIGHT:
613 case LFUN_CHAR_RIGHT_SELECT:
614 if (lyxrc.visual_cursor) {
615 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_RIGHT_SELECT);
616 needsUpdate |= cursorVisRight(cur);
617 if (!needsUpdate && oldTopSlice == cur.top()
618 && cur.boundary() == oldBoundary) {
620 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
623 if (reverseDirectionNeeded(cur)) {
624 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
625 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
627 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
628 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
637 case LFUN_DOWN_SELECT:
640 // stop/start the selection
641 bool select = cmd.action == LFUN_DOWN_SELECT ||
642 cmd.action == LFUN_UP_SELECT;
643 cur.selHandle(select);
645 // move cursor up/down
646 bool up = cmd.action == LFUN_UP_SELECT || cmd.action == LFUN_UP;
647 bool const successful = cur.upDownInText(up, needsUpdate);
649 // redraw if you leave mathed (for the decorations)
650 needsUpdate |= cur.beforeDispatchCursor().inMathed();
657 case LFUN_PARAGRAPH_UP:
658 case LFUN_PARAGRAPH_UP_SELECT:
659 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
660 needsUpdate |= cursorUpParagraph(cur);
663 case LFUN_PARAGRAPH_DOWN:
664 case LFUN_PARAGRAPH_DOWN_SELECT:
665 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
666 needsUpdate |= cursorDownParagraph(cur);
669 case LFUN_LINE_BEGIN:
670 case LFUN_LINE_BEGIN_SELECT:
671 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_BEGIN_SELECT);
672 needsUpdate |= tm.cursorHome(cur);
676 case LFUN_LINE_END_SELECT:
677 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
678 needsUpdate |= tm.cursorEnd(cur);
681 case LFUN_WORD_RIGHT:
682 case LFUN_WORD_RIGHT_SELECT:
683 if (lyxrc.visual_cursor) {
684 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_RIGHT_SELECT);
685 needsUpdate |= cursorVisRightOneWord(cur);
686 if (!needsUpdate && oldTopSlice == cur.top()
687 && cur.boundary() == oldBoundary) {
689 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
692 if (reverseDirectionNeeded(cur)) {
693 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
694 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
696 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
697 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
704 case LFUN_WORD_FORWARD:
705 case LFUN_WORD_FORWARD_SELECT:
706 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT);
707 needsUpdate |= cursorForwardOneWord(cur);
711 case LFUN_WORD_LEFT_SELECT:
712 if (lyxrc.visual_cursor) {
713 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_LEFT_SELECT);
714 needsUpdate |= cursorVisLeftOneWord(cur);
715 if (!needsUpdate && oldTopSlice == cur.top()
716 && cur.boundary() == oldBoundary) {
718 cmd = FuncRequest(LFUN_FINISHED_LEFT);
721 if (reverseDirectionNeeded(cur)) {
722 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
723 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
725 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
726 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
733 case LFUN_WORD_BACKWARD:
734 case LFUN_WORD_BACKWARD_SELECT:
735 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT);
736 needsUpdate |= cursorBackwardOneWord(cur);
739 case LFUN_WORD_SELECT: {
740 selectWord(cur, WHOLE_WORD);
741 finishChange(cur, true);
745 case LFUN_NEWLINE_INSERT: {
746 InsetNewlineParams inp;
747 docstring arg = cmd.argument();
748 // this avoids a double undo
749 // FIXME: should not be needed, ideally
750 if (!cur.selection())
752 cap::replaceSelection(cur);
753 if (arg == "linebreak")
754 inp.kind = InsetNewlineParams::LINEBREAK;
756 inp.kind = InsetNewlineParams::NEWLINE;
757 cur.insert(new InsetNewline(inp));
759 moveCursor(cur, false);
763 case LFUN_CHAR_DELETE_FORWARD:
764 if (!cur.selection()) {
765 if (cur.pos() == cur.paragraph().size())
766 // Par boundary, force full-screen update
767 singleParUpdate = false;
768 needsUpdate |= erase(cur);
770 // It is possible to make it a lot faster still
771 // just comment out the line below...
773 cutSelection(cur, true, false);
774 singleParUpdate = false;
776 moveCursor(cur, false);
779 case LFUN_CHAR_DELETE_BACKWARD:
780 if (!cur.selection()) {
781 if (bv->getIntl().getTransManager().backspace()) {
782 // Par boundary, full-screen update
784 singleParUpdate = false;
785 needsUpdate |= backspace(cur);
787 // It is possible to make it a lot faster still
788 // just comment out the line below...
791 cutSelection(cur, true, false);
792 singleParUpdate = false;
796 case LFUN_BREAK_PARAGRAPH:
797 cap::replaceSelection(cur);
798 breakParagraph(cur, cmd.argument() == "inverse");
803 // With the creation of LFUN_PARAGRAPH_PARAMS, this is now redundant,
804 // as its duties can be performed there. Should it be removed??
805 // FIXME For now, it can just dispatch LFUN_PARAGRAPH_PARAMS...
806 case LFUN_PARAGRAPH_SPACING: {
807 Paragraph & par = cur.paragraph();
808 Spacing::Space cur_spacing = par.params().spacing().getSpace();
809 string cur_value = "1.0";
810 if (cur_spacing == Spacing::Other)
811 cur_value = par.params().spacing().getValueAsString();
813 istringstream is(to_utf8(cmd.argument()));
816 Spacing::Space new_spacing = cur_spacing;
817 string new_value = cur_value;
819 lyxerr << "Missing argument to `paragraph-spacing'"
821 } else if (tmp == "single") {
822 new_spacing = Spacing::Single;
823 } else if (tmp == "onehalf") {
824 new_spacing = Spacing::Onehalf;
825 } else if (tmp == "double") {
826 new_spacing = Spacing::Double;
827 } else if (tmp == "other") {
828 new_spacing = Spacing::Other;
829 string tmpval = "0.0";
831 lyxerr << "new_value = " << tmpval << endl;
834 } else if (tmp == "default") {
835 new_spacing = Spacing::Default;
837 lyxerr << to_utf8(_("Unknown spacing argument: "))
838 << to_utf8(cmd.argument()) << endl;
840 if (cur_spacing != new_spacing || cur_value != new_value)
841 par.params().spacing(Spacing(new_spacing, new_value));
845 case LFUN_INSET_INSERT: {
847 Inset * inset = createInset(bv->buffer(), cmd);
849 // FIXME (Abdel 01/02/2006):
850 // What follows would be a partial fix for bug 2154:
851 // http://bugzilla.lyx.org/show_bug.cgi?id=2154
852 // This automatically put the label inset _after_ a
853 // numbered section. It should be possible to extend the mechanism
854 // to any kind of LateX environement.
855 // The correct way to fix that bug would be at LateX generation.
856 // I'll let the code here for reference as it could be used for some
857 // other feature like "automatic labelling".
859 Paragraph & par = pars_[cur.pit()];
860 if (inset->lyxCode() == LABEL_CODE
861 && par.layout().labeltype == LABEL_COUNTER) {
862 // Go to the end of the paragraph
863 // Warning: Because of Change-Tracking, the last
864 // position is 'size()' and not 'size()-1':
865 cur.pos() = par.size();
866 // Insert a new paragraph
867 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
872 cutSelection(cur, true, false);
879 case LFUN_INSET_DISSOLVE:
880 needsUpdate |= dissolveInset(cur);
883 case LFUN_INSET_SETTINGS: {
884 Inset & inset = cur.inset();
885 if (cmd.getArg(0) == insetName(inset.lyxCode())) {
886 // This inset dialog has been explicitely requested.
887 inset.showInsetDialog(bv);
890 // else, if there is an inset at the cursor, access this
891 Inset * next_inset = cur.nextInset();
893 next_inset->showInsetDialog(bv);
896 // if not then access the underlying inset.
897 inset.showInsetDialog(bv);
901 case LFUN_SET_GRAPHICS_GROUP: {
902 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
908 string id = to_utf8(cmd.argument());
909 string grp = graphics::getGroupParams(bv->buffer(), id);
910 InsetGraphicsParams tmp, inspar = ins->getParams();
913 inspar.groupId = to_utf8(cmd.argument());
915 InsetGraphics::string2params(grp, bv->buffer(), tmp);
916 tmp.filename = inspar.filename;
920 ins->setParams(inspar);
923 case LFUN_SPACE_INSERT:
924 if (cur.paragraph().layout().free_spacing)
925 insertChar(cur, ' ');
927 doInsertInset(cur, this, cmd, false, false);
930 moveCursor(cur, false);
933 case LFUN_SPECIALCHAR_INSERT: {
934 string const name = to_utf8(cmd.argument());
935 if (name == "hyphenation")
936 specialChar(cur, InsetSpecialChar::HYPHENATION);
937 else if (name == "ligature-break")
938 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
939 else if (name == "slash")
940 specialChar(cur, InsetSpecialChar::SLASH);
941 else if (name == "nobreakdash")
942 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
943 else if (name == "dots")
944 specialChar(cur, InsetSpecialChar::LDOTS);
945 else if (name == "end-of-sentence")
946 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
947 else if (name == "menu-separator")
948 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
949 else if (name.empty())
950 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
952 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
956 case LFUN_WORD_UPCASE:
957 changeCase(cur, text_uppercase);
960 case LFUN_WORD_LOWCASE:
961 changeCase(cur, text_lowercase);
964 case LFUN_WORD_CAPITALIZE:
965 changeCase(cur, text_capitalization);
968 case LFUN_CHARS_TRANSPOSE:
973 cur.message(_("Paste"));
974 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), /**/);
975 cap::replaceSelection(cur);
978 string const arg = to_utf8(cmd.argument());
980 if (theClipboard().isInternal())
981 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
982 else if (theClipboard().hasGraphicsContents())
983 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
985 pasteClipboardText(cur, bv->buffer().errorList("Paste"));
986 } else if (isStrUnsignedInt(arg)) {
987 // we have a numerical argument
988 pasteFromStack(cur, bv->buffer().errorList("Paste"),
989 convert<unsigned int>(arg));
991 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
993 type = Clipboard::PdfGraphicsType;
994 else if (arg == "png")
995 type = Clipboard::PngGraphicsType;
996 else if (arg == "jpeg")
997 type = Clipboard::JpegGraphicsType;
998 else if (arg == "linkback")
999 type = Clipboard::LinkBackGraphicsType;
1001 LASSERT(false, /**/);
1003 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1006 bv->buffer().errors("Paste");
1007 cur.clearSelection(); // bug 393
1013 cutSelection(cur, true, true);
1014 cur.message(_("Cut"));
1019 cur.message(_("Copy"));
1022 case LFUN_SERVER_GET_XY:
1023 cur.message(from_utf8(
1024 convert<string>(tm.cursorX(cur.top(), cur.boundary()))
1025 + ' ' + convert<string>(tm.cursorY(cur.top(), cur.boundary()))));
1028 case LFUN_SERVER_SET_XY: {
1031 istringstream is(to_utf8(cmd.argument()));
1034 lyxerr << "SETXY: Could not parse coordinates in '"
1035 << to_utf8(cmd.argument()) << endl;
1037 tm.setCursorFromCoordinates(cur, x, y);
1041 case LFUN_SERVER_GET_LAYOUT:
1042 cur.message(cur.paragraph().layout().name());
1046 docstring layout = cmd.argument();
1047 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1049 Paragraph const & para = cur.paragraph();
1050 docstring const old_layout = para.layout().name();
1051 DocumentClass const & tclass = bv->buffer().params().documentClass();
1054 layout = tclass.defaultLayoutName();
1056 if (para.forcePlainLayout())
1057 // in this case only the empty layout is allowed
1058 layout = tclass.plainLayoutName();
1059 else if (para.usePlainLayout()) {
1060 // in this case, default layout maps to empty layout
1061 if (layout == tclass.defaultLayoutName())
1062 layout = tclass.plainLayoutName();
1064 // otherwise, the empty layout maps to the default
1065 if (layout == tclass.plainLayoutName())
1066 layout = tclass.defaultLayoutName();
1069 bool hasLayout = tclass.hasLayout(layout);
1071 // If the entry is obsolete, use the new one instead.
1073 docstring const & obs = tclass[layout].obsoleted_by();
1079 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1080 from_utf8(N_(" not known")));
1084 bool change_layout = (old_layout != layout);
1086 if (!change_layout && cur.selection() &&
1087 cur.selBegin().pit() != cur.selEnd().pit())
1089 pit_type spit = cur.selBegin().pit();
1090 pit_type epit = cur.selEnd().pit() + 1;
1091 while (spit != epit) {
1092 if (pars_[spit].layout().name() != old_layout) {
1093 change_layout = true;
1101 setLayout(cur, layout);
1106 case LFUN_CLIPBOARD_PASTE:
1107 cur.clearSelection();
1108 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1109 cmd.argument() == "paragraph");
1110 bv->buffer().errors("Paste");
1113 case LFUN_PRIMARY_SELECTION_PASTE:
1114 pasteString(cur, theSelection().get(),
1115 cmd.argument() == "paragraph");
1118 case LFUN_UNICODE_INSERT: {
1119 if (cmd.argument().empty())
1121 docstring hexstring = cmd.argument();
1122 if (isHex(hexstring)) {
1123 char_type c = hexToInt(hexstring);
1124 if (c >= 32 && c < 0x10ffff) {
1125 lyxerr << "Inserting c: " << c << endl;
1126 docstring s = docstring(1, c);
1127 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1133 case LFUN_QUOTE_INSERT: {
1134 Paragraph & par = cur.paragraph();
1135 pos_type pos = cur.pos();
1136 BufferParams const & bufparams = bv->buffer().params();
1137 Layout const & style = par.layout();
1138 if (!style.pass_thru
1139 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1140 // this avoids a double undo
1141 // FIXME: should not be needed, ideally
1142 if (!cur.selection())
1144 cap::replaceSelection(cur);
1149 else if (cur.prevInset() && cur.prevInset()->isSpace())
1152 c = par.getChar(pos - 1);
1153 string arg = to_utf8(cmd.argument());
1154 cur.insert(new InsetQuotes(bv->buffer(), c, (arg == "single")
1155 ? InsetQuotes::SingleQuotes : InsetQuotes::DoubleQuotes));
1159 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1163 case LFUN_DATE_INSERT: {
1164 string const format = cmd.argument().empty()
1165 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1166 string const time = formatted_time(current_time(), format);
1167 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1171 case LFUN_MOUSE_TRIPLE:
1172 if (cmd.button() == mouse_button::button1) {
1181 case LFUN_MOUSE_DOUBLE:
1182 if (cmd.button() == mouse_button::button1) {
1183 selectWord(cur, WHOLE_WORD_STRICT);
1188 // Single-click on work area
1189 case LFUN_MOUSE_PRESS:
1190 // We are not marking a selection with the keyboard in any case.
1191 cur.bv().cursor().setMark(false);
1192 switch (cmd.button()) {
1193 case mouse_button::button1:
1195 if (!bv->mouseSetCursor(cur, cmd.argument() == "region-select"))
1196 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1199 case mouse_button::button2:
1200 // Middle mouse pasting.
1201 bv->mouseSetCursor(cur);
1202 if (!cap::selection()) {
1203 // There is no local selection in the current buffer, so try to
1204 // paste primary selection instead.
1205 lyx::dispatch(FuncRequest(LFUN_PRIMARY_SELECTION_PASTE,
1207 // Nothing else to do.
1211 // Copy the selection buffer to the clipboard stack, because we want it
1212 // to appear in the "Edit->Paste recent" menu.
1213 cap::copySelectionToStack();
1214 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1215 cur.updateFlags(Update::Force | Update::FitCursor);
1216 bv->buffer().errors("Paste");
1217 bv->buffer().markDirty();
1218 bv->cursor().finishUndo();
1221 case mouse_button::button3: {
1222 Cursor const & bvcur = cur.bv().cursor();
1223 // Don't do anything if we right-click a
1224 // selection, a context menu will popup.
1225 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1226 && cur < bvcur.selectionEnd()) {
1230 if (!bv->mouseSetCursor(cur, false))
1231 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1237 } // switch (cmd.button())
1240 case LFUN_MOUSE_MOTION: {
1241 // Mouse motion with right or middle mouse do nothing for now.
1242 if (cmd.button() != mouse_button::button1) {
1246 // ignore motions deeper nested than the real anchor
1247 Cursor & bvcur = cur.bv().cursor();
1248 if (!bvcur.anchor_.hasPart(cur)) {
1252 CursorSlice old = bvcur.top();
1254 int const wh = bv->workHeight();
1255 int const y = max(0, min(wh - 1, cmd.y));
1257 tm.setCursorFromCoordinates(cur, cmd.x, y);
1258 cur.setTargetX(cmd.x);
1260 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1262 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1263 // This is to allow jumping over large insets
1264 if (cur.top() == old) {
1266 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1268 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1270 // We continue with our existing selection or start a new one, so don't
1271 // reset the anchor.
1272 bvcur.setCursor(cur);
1273 bvcur.setSelection(true);
1274 if (cur.top() == old) {
1275 // We didn't move one iota, so no need to update the screen.
1276 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1283 case LFUN_MOUSE_RELEASE:
1284 switch (cmd.button()) {
1285 case mouse_button::button1:
1286 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1287 // If there is a new selection, update persistent selection;
1288 // otherwise, single click does not clear persistent selection
1290 if (cur.selection()) {
1291 // Finish selection. If double click,
1292 // cur is moved to the end of word by
1293 // selectWord but bvcur is current
1295 cur.bv().cursor().setSelection();
1296 // We might have removed an empty but drawn selection
1297 // (probably a margin)
1298 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1301 // FIXME: We could try to handle drag and drop of selection here.
1304 case mouse_button::button2:
1305 // Middle mouse pasting is handled at mouse press time,
1306 // see LFUN_MOUSE_PRESS.
1310 case mouse_button::button3:
1311 // Cursor was set at LFUN_MOUSE_PRESS time.
1312 // FIXME: If there is a selection we could try to handle a special
1313 // drag & drop context menu.
1317 case mouse_button::none:
1318 case mouse_button::button4:
1319 case mouse_button::button5:
1321 } // switch (cmd.button())
1325 case LFUN_SELF_INSERT: {
1326 if (cmd.argument().empty())
1329 // Automatically delete the currently selected
1330 // text and replace it with what is being
1331 // typed in now. Depends on lyxrc settings
1332 // "auto_region_delete", which defaults to
1335 if (lyxrc.auto_region_delete && cur.selection())
1336 cutSelection(cur, false, false);
1338 cur.clearSelection();
1340 docstring::const_iterator cit = cmd.argument().begin();
1341 docstring::const_iterator const end = cmd.argument().end();
1342 for (; cit != end; ++cit)
1343 bv->translateAndInsert(*cit, this, cur);
1346 moveCursor(cur, false);
1350 case LFUN_HYPERLINK_INSERT: {
1351 InsetCommandParams p(HYPERLINK_CODE);
1353 if (cur.selection()) {
1354 content = cur.selectionAsString(false);
1355 cutSelection(cur, true, false);
1357 p["target"] = (cmd.argument().empty()) ?
1358 content : cmd.argument();
1359 string const data = InsetCommand::params2string("href", p);
1360 if (p["target"].empty()) {
1361 bv->showDialog("href", data);
1363 FuncRequest fr(LFUN_INSET_INSERT, data);
1369 case LFUN_LABEL_INSERT: {
1370 InsetCommandParams p(LABEL_CODE);
1371 // Try to generate a valid label
1372 p["name"] = (cmd.argument().empty()) ?
1373 cur.getPossibleLabel() :
1375 string const data = InsetCommand::params2string("label", p);
1377 if (cmd.argument().empty()) {
1378 bv->showDialog("label", data);
1380 FuncRequest fr(LFUN_INSET_INSERT, data);
1386 case LFUN_INFO_INSERT: {
1388 if (cmd.argument().empty() && cur.selection()) {
1389 // if command argument is empty use current selection as parameter.
1390 docstring ds = cur.selectionAsString(false);
1391 cutSelection(cur, true, false);
1392 FuncRequest cmd0(cmd, ds);
1393 inset = createInset(cur.bv().buffer(), cmd0);
1395 inset = createInset(cur.bv().buffer(), cmd);
1399 insertInset(cur, inset);
1403 case LFUN_CAPTION_INSERT:
1404 case LFUN_FOOTNOTE_INSERT:
1405 case LFUN_NOTE_INSERT:
1406 case LFUN_FLEX_INSERT:
1407 case LFUN_BOX_INSERT:
1408 case LFUN_BRANCH_INSERT:
1409 case LFUN_ERT_INSERT:
1410 case LFUN_LISTING_INSERT:
1411 case LFUN_MARGINALNOTE_INSERT:
1412 case LFUN_OPTIONAL_INSERT:
1413 case LFUN_INDEX_INSERT:
1414 // Open the inset, and move the current selection
1416 doInsertInset(cur, this, cmd, true, true);
1418 // Some insets are numbered, others are shown in the outline pane so
1419 // let's update the labels and the toc backend.
1420 bv->buffer().updateLabels();
1423 case LFUN_TABULAR_INSERT:
1424 // if there were no arguments, just open the dialog
1425 if (doInsertInset(cur, this, cmd, false, true))
1428 bv->showDialog("tabularcreate");
1432 case LFUN_FLOAT_INSERT:
1433 case LFUN_FLOAT_WIDE_INSERT:
1434 case LFUN_WRAP_INSERT: {
1435 // will some text be moved into the inset?
1436 bool content = cur.selection();
1438 doInsertInset(cur, this, cmd, true, true);
1441 // If some text is moved into the inset, doInsertInset
1442 // puts the cursor outside the inset. To insert the
1443 // caption we put it back into the inset.
1447 ParagraphList & pars = cur.text()->paragraphs();
1449 DocumentClass const & tclass = bv->buffer().params().documentClass();
1451 // add a separate paragraph for the caption inset
1452 pars.push_back(Paragraph());
1453 pars.back().setInsetOwner(&pars[0].inInset());
1454 pars.back().setPlainOrDefaultLayout(tclass);
1455 int cap_pit = pars.size() - 1;
1457 // if an empty inset was created, we create an additional empty
1458 // paragraph at the bottom so that the user can choose where to put
1459 // the graphics (or table).
1461 pars.push_back(Paragraph());
1462 pars.back().setInsetOwner(&pars[0].inInset());
1463 pars.back().setPlainOrDefaultLayout(tclass);
1466 // reposition the cursor to the caption
1467 cur.pit() = cap_pit;
1469 // FIXME: This Text/Cursor dispatch handling is a mess!
1470 // We cannot use Cursor::dispatch here it needs access to up to
1472 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1473 doInsertInset(cur, cur.text(), cmd_caption, true, false);
1474 bv->buffer().updateLabels();
1475 cur.updateFlags(Update::Force);
1476 // FIXME: When leaving the Float (or Wrap) inset we should
1477 // delete any empty paragraph left above or below the
1482 case LFUN_NOMENCL_INSERT: {
1483 InsetCommandParams p(NOMENCL_CODE);
1484 if (cmd.argument().empty())
1485 p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
1487 p["symbol"] = cmd.argument();
1488 string const data = InsetCommand::params2string("nomenclature", p);
1489 bv->showDialog("nomenclature", data);
1493 case LFUN_INDEX_PRINT:
1494 case LFUN_NOMENCL_PRINT:
1495 case LFUN_TOC_INSERT:
1496 case LFUN_LINE_INSERT:
1497 case LFUN_NEWPAGE_INSERT:
1499 doInsertInset(cur, this, cmd, false, false);
1503 case LFUN_DEPTH_DECREMENT:
1504 changeDepth(cur, DEC_DEPTH);
1507 case LFUN_DEPTH_INCREMENT:
1508 changeDepth(cur, INC_DEPTH);
1511 case LFUN_MATH_DISPLAY:
1512 mathDispatch(cur, cmd, true);
1515 case LFUN_REGEXP_MODE:
1516 regexpDispatch(cur, cmd);
1519 case LFUN_MATH_MODE:
1520 if (cmd.argument() == "on")
1521 // don't pass "on" as argument
1522 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1524 mathDispatch(cur, cmd, false);
1527 case LFUN_MATH_MACRO:
1528 if (cmd.argument().empty())
1529 cur.errorMessage(from_utf8(N_("Missing argument")));
1531 string s = to_utf8(cmd.argument());
1532 string const s1 = token(s, ' ', 1);
1533 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1534 string const s2 = token(s, ' ', 2);
1535 MacroType type = MacroTypeNewcommand;
1537 type = MacroTypeDef;
1538 MathMacroTemplate * inset = new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, false, type);
1539 inset->setBuffer(bv->buffer());
1540 insertInset(cur, inset);
1542 // enter macro inset and select the name
1544 cur.top().pos() = cur.top().lastpos();
1546 cur.setSelection(true);
1547 cur.top().pos() = 0;
1551 // passthrough hat and underscore outside mathed:
1552 case LFUN_MATH_SUBSCRIPT:
1553 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1555 case LFUN_MATH_SUPERSCRIPT:
1556 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1559 case LFUN_MATH_INSERT:
1560 case LFUN_MATH_MATRIX:
1561 case LFUN_MATH_DELIM:
1562 case LFUN_MATH_BIGDELIM: {
1564 cap::replaceSelection(cur);
1565 cur.insert(new InsetMathHull(hullSimple));
1566 checkAndActivateInset(cur, true);
1567 LASSERT(cur.inMathed(), /**/);
1572 case LFUN_FONT_EMPH: {
1573 Font font(ignore_font, ignore_language);
1574 font.fontInfo().setEmph(FONT_TOGGLE);
1575 toggleAndShow(cur, this, font);
1579 case LFUN_FONT_BOLD:
1580 case LFUN_FONT_BOLDSYMBOL: {
1581 Font font(ignore_font, ignore_language);
1582 font.fontInfo().setSeries(BOLD_SERIES);
1583 toggleAndShow(cur, this, font);
1587 case LFUN_FONT_NOUN: {
1588 Font font(ignore_font, ignore_language);
1589 font.fontInfo().setNoun(FONT_TOGGLE);
1590 toggleAndShow(cur, this, font);
1594 case LFUN_FONT_TYPEWRITER: {
1595 Font font(ignore_font, ignore_language);
1596 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1597 toggleAndShow(cur, this, font);
1601 case LFUN_FONT_SANS: {
1602 Font font(ignore_font, ignore_language);
1603 font.fontInfo().setFamily(SANS_FAMILY);
1604 toggleAndShow(cur, this, font);
1608 case LFUN_FONT_ROMAN: {
1609 Font font(ignore_font, ignore_language);
1610 font.fontInfo().setFamily(ROMAN_FAMILY);
1611 toggleAndShow(cur, this, font);
1615 case LFUN_FONT_DEFAULT: {
1616 Font font(inherit_font, ignore_language);
1617 toggleAndShow(cur, this, font);
1621 case LFUN_FONT_UNDERLINE: {
1622 Font font(ignore_font, ignore_language);
1623 font.fontInfo().setUnderbar(FONT_TOGGLE);
1624 toggleAndShow(cur, this, font);
1628 case LFUN_FONT_SIZE: {
1629 Font font(ignore_font, ignore_language);
1630 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1631 toggleAndShow(cur, this, font);
1635 case LFUN_LANGUAGE: {
1636 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1639 Font font(ignore_font, lang);
1640 toggleAndShow(cur, this, font);
1644 case LFUN_TEXTSTYLE_APPLY:
1645 toggleAndShow(cur, this, freefont, toggleall);
1646 cur.message(_("Character set"));
1649 // Set the freefont using the contents of \param data dispatched from
1650 // the frontends and apply it at the current cursor location.
1651 case LFUN_TEXTSTYLE_UPDATE: {
1654 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1657 toggleAndShow(cur, this, freefont, toggleall);
1658 cur.message(_("Character set"));
1660 lyxerr << "Argument not ok";
1665 case LFUN_FINISHED_LEFT:
1666 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
1667 // We're leaving an inset, going left. If the inset is LTR, we're
1668 // leaving from the front, so we should not move (remain at --- but
1669 // not in --- the inset). If the inset is RTL, move left, without
1670 // entering the inset itself; i.e., move to after the inset.
1671 if (cur.paragraph().getFontSettings(
1672 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1673 cursorVisLeft(cur, true);
1676 case LFUN_FINISHED_RIGHT:
1677 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
1678 // We're leaving an inset, going right. If the inset is RTL, we're
1679 // leaving from the front, so we should not move (remain at --- but
1680 // not in --- the inset). If the inset is LTR, move right, without
1681 // entering the inset itself; i.e., move to after the inset.
1682 if (!cur.paragraph().getFontSettings(
1683 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1684 cursorVisRight(cur, true);
1687 case LFUN_FINISHED_BACKWARD:
1688 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
1691 case LFUN_FINISHED_FORWARD:
1692 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
1694 cur.setCurrentFont();
1697 case LFUN_LAYOUT_PARAGRAPH: {
1699 params2string(cur.paragraph(), data);
1700 data = "show\n" + data;
1701 bv->showDialog("paragraph", data);
1705 case LFUN_PARAGRAPH_UPDATE: {
1707 params2string(cur.paragraph(), data);
1709 // Will the paragraph accept changes from the dialog?
1711 cur.inset().allowParagraphCustomization(cur.idx());
1713 data = "update " + convert<string>(accept) + '\n' + data;
1714 bv->updateDialog("paragraph", data);
1718 case LFUN_ACCENT_UMLAUT:
1719 case LFUN_ACCENT_CIRCUMFLEX:
1720 case LFUN_ACCENT_GRAVE:
1721 case LFUN_ACCENT_ACUTE:
1722 case LFUN_ACCENT_TILDE:
1723 case LFUN_ACCENT_CEDILLA:
1724 case LFUN_ACCENT_MACRON:
1725 case LFUN_ACCENT_DOT:
1726 case LFUN_ACCENT_UNDERDOT:
1727 case LFUN_ACCENT_UNDERBAR:
1728 case LFUN_ACCENT_CARON:
1729 case LFUN_ACCENT_BREVE:
1730 case LFUN_ACCENT_TIE:
1731 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1732 case LFUN_ACCENT_CIRCLE:
1733 case LFUN_ACCENT_OGONEK:
1734 theLyXFunc().handleKeyFunc(cmd.action);
1735 if (!cmd.argument().empty())
1736 // FIXME: Are all these characters encoded in one byte in utf8?
1737 bv->translateAndInsert(cmd.argument()[0], this, cur);
1740 case LFUN_FLOAT_LIST_INSERT: {
1741 DocumentClass const & tclass = bv->buffer().params().documentClass();
1742 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1744 if (cur.selection())
1745 cutSelection(cur, true, false);
1746 breakParagraph(cur);
1748 if (cur.lastpos() != 0) {
1749 cursorBackward(cur);
1750 breakParagraph(cur);
1753 docstring const laystr = cur.inset().usePlainLayout() ?
1754 tclass.plainLayoutName() :
1755 tclass.defaultLayoutName();
1756 setLayout(cur, laystr);
1757 ParagraphParameters p;
1758 // FIXME If this call were replaced with one to clearParagraphParams(),
1759 // then we could get rid of this method altogether.
1760 setParagraphs(cur, p);
1761 // FIXME This should be simplified when InsetFloatList takes a
1762 // Buffer in its constructor.
1763 InsetFloatList * ifl = new InsetFloatList(to_utf8(cmd.argument()));
1764 ifl->setBuffer(bv->buffer());
1765 insertInset(cur, ifl);
1768 lyxerr << "Non-existent float type: "
1769 << to_utf8(cmd.argument()) << endl;
1774 case LFUN_CHANGE_ACCEPT: {
1775 acceptOrRejectChanges(cur, ACCEPT);
1779 case LFUN_CHANGE_REJECT: {
1780 acceptOrRejectChanges(cur, REJECT);
1784 case LFUN_THESAURUS_ENTRY: {
1785 docstring arg = cmd.argument();
1787 arg = cur.selectionAsString(false);
1789 if (arg.size() > 100 || arg.empty()) {
1790 // Get word or selection
1791 selectWordWhenUnderCursor(cur, WHOLE_WORD);
1792 arg = cur.selectionAsString(false);
1795 bv->showDialog("thesaurus", to_utf8(arg));
1799 case LFUN_PARAGRAPH_PARAMS_APPLY: {
1800 // Given data, an encoding of the ParagraphParameters
1801 // generated in the Paragraph dialog, this function sets
1802 // the current paragraph, or currently selected paragraphs,
1804 // NOTE: This function overrides all existing settings.
1805 setParagraphs(cur, cmd.argument());
1806 cur.message(_("Paragraph layout set"));
1810 case LFUN_PARAGRAPH_PARAMS: {
1811 // Given data, an encoding of the ParagraphParameters as we'd
1812 // find them in a LyX file, this function modifies the current paragraph,
1813 // or currently selected paragraphs.
1814 // NOTE: This function only modifies, and does not override, existing
1816 setParagraphs(cur, cmd.argument(), true);
1817 cur.message(_("Paragraph layout set"));
1822 if (cur.selection()) {
1823 cur.setSelection(false);
1826 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
1827 // correct, but I'm not 100% sure -- dov, 071019
1828 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1832 case LFUN_OUTLINE_UP:
1833 outline(OutlineUp, cur);
1834 setCursor(cur, cur.pit(), 0);
1835 cur.buffer().updateLabels();
1839 case LFUN_OUTLINE_DOWN:
1840 outline(OutlineDown, cur);
1841 setCursor(cur, cur.pit(), 0);
1842 cur.buffer().updateLabels();
1846 case LFUN_OUTLINE_IN:
1847 outline(OutlineIn, cur);
1848 cur.buffer().updateLabels();
1852 case LFUN_OUTLINE_OUT:
1853 outline(OutlineOut, cur);
1854 cur.buffer().updateLabels();
1859 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
1864 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
1866 // FIXME: The cursor flag is reset two lines below
1867 // so we need to check here if some of the LFUN did touch that.
1868 // for now only Text::erase() and Text::backspace() do that.
1869 // The plan is to verify all the LFUNs and then to remove this
1870 // singleParUpdate boolean altogether.
1871 if (cur.result().update() & Update::Force) {
1872 singleParUpdate = false;
1876 // FIXME: the following code should go in favor of fine grained
1877 // update flag treatment.
1878 if (singleParUpdate) {
1879 // Inserting characters does not change par height in general. So, try
1880 // to update _only_ this paragraph. BufferView will detect if a full
1881 // metrics update is needed anyway.
1882 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1887 && &oldTopSlice.inset() == &cur.inset()
1888 && oldTopSlice.idx() == cur.idx()
1889 && !sel // sel is a backup of cur.selection() at the biginning of the function.
1890 && !cur.selection())
1891 // FIXME: it would be better if we could just do this
1893 //if (cur.result().update() != Update::FitCursor)
1896 // But some LFUNs do not set Update::FitCursor when needed, so we
1897 // do it for all. This is not very harmfull as FitCursor will provoke
1898 // a full redraw only if needed but still, a proper review of all LFUN
1899 // should be done and this needsUpdate boolean can then be removed.
1900 cur.updateFlags(Update::FitCursor);
1902 cur.updateFlags(Update::Force | Update::FitCursor);
1906 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
1907 FuncStatus & flag) const
1909 LASSERT(cur.text() == this, /**/);
1911 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
1913 InsetCode code = NO_CODE;
1915 switch (cmd.action) {
1917 case LFUN_DEPTH_DECREMENT:
1918 enable = changeDepthAllowed(cur, DEC_DEPTH);
1921 case LFUN_DEPTH_INCREMENT:
1922 enable = changeDepthAllowed(cur, INC_DEPTH);
1926 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1929 case LFUN_DIALOG_SHOW_NEW_INSET:
1930 if (cmd.argument() == "bibitem")
1931 code = BIBITEM_CODE;
1932 else if (cmd.argument() == "bibtex")
1934 else if (cmd.argument() == "box")
1936 else if (cmd.argument() == "branch")
1938 else if (cmd.argument() == "citation")
1940 else if (cmd.argument() == "ert")
1942 else if (cmd.argument() == "external")
1943 code = EXTERNAL_CODE;
1944 else if (cmd.argument() == "float")
1946 else if (cmd.argument() == "graphics")
1947 code = GRAPHICS_CODE;
1948 else if (cmd.argument() == "href")
1949 code = HYPERLINK_CODE;
1950 else if (cmd.argument() == "include")
1951 code = INCLUDE_CODE;
1952 else if (cmd.argument() == "index")
1954 else if (cmd.argument() == "nomenclature")
1955 code = NOMENCL_CODE;
1956 else if (cmd.argument() == "label")
1958 else if (cmd.argument() == "note")
1960 else if (cmd.argument() == "ref")
1962 else if (cmd.argument() == "space")
1964 else if (cmd.argument() == "toc")
1966 else if (cmd.argument() == "vspace")
1968 else if (cmd.argument() == "wrap")
1970 else if (cmd.argument() == "listings")
1971 code = LISTINGS_CODE;
1974 case LFUN_ERT_INSERT:
1977 case LFUN_LISTING_INSERT:
1978 code = LISTINGS_CODE;
1979 // not allowed in description items
1980 enable = !inDescriptionItem(cur);
1982 case LFUN_FOOTNOTE_INSERT:
1985 case LFUN_TABULAR_INSERT:
1986 code = TABULAR_CODE;
1988 case LFUN_MARGINALNOTE_INSERT:
1991 case LFUN_FLOAT_INSERT:
1992 case LFUN_FLOAT_WIDE_INSERT:
1994 // not allowed in description items
1995 enable = !inDescriptionItem(cur);
1997 case LFUN_WRAP_INSERT:
1999 // not allowed in description items
2000 enable = !inDescriptionItem(cur);
2002 case LFUN_FLOAT_LIST_INSERT:
2003 code = FLOAT_LIST_CODE;
2005 case LFUN_CAPTION_INSERT:
2006 code = CAPTION_CODE;
2007 // not allowed in description items
2008 enable = !inDescriptionItem(cur);
2010 case LFUN_NOTE_INSERT:
2012 // in commands (sections etc.) and description items,
2013 // only Notes are allowed
2014 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
2015 (!cur.paragraph().layout().isCommand()
2016 && !inDescriptionItem(cur)));
2018 case LFUN_FLEX_INSERT: {
2020 string s = cmd.getArg(0);
2022 cur.buffer().params().documentClass().insetLayout(from_utf8(s));
2023 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2024 il.lyxtype() != InsetLayout::CUSTOM &&
2025 il.lyxtype() != InsetLayout::ELEMENT &&
2026 il.lyxtype ()!= InsetLayout::STANDARD)
2030 case LFUN_BOX_INSERT:
2033 case LFUN_BRANCH_INSERT:
2035 if (cur.buffer().masterBuffer()->params().branchlist().empty())
2038 case LFUN_LABEL_INSERT:
2041 case LFUN_INFO_INSERT:
2044 case LFUN_OPTIONAL_INSERT:
2046 enable = cur.paragraph().insetList().count(OPTARG_CODE)
2047 < cur.paragraph().layout().optionalargs;
2049 case LFUN_INDEX_INSERT:
2052 case LFUN_INDEX_PRINT:
2053 code = INDEX_PRINT_CODE;
2055 case LFUN_NOMENCL_INSERT:
2056 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2060 code = NOMENCL_CODE;
2062 case LFUN_NOMENCL_PRINT:
2063 code = NOMENCL_PRINT_CODE;
2065 case LFUN_TOC_INSERT:
2068 case LFUN_HYPERLINK_INSERT:
2069 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2073 code = HYPERLINK_CODE;
2075 case LFUN_QUOTE_INSERT:
2076 // always allow this, since we will inset a raw quote
2077 // if an inset is not allowed.
2079 case LFUN_SPECIALCHAR_INSERT:
2080 code = SPECIALCHAR_CODE;
2082 case LFUN_SPACE_INSERT:
2083 // slight hack: we know this is allowed in math mode
2088 case LFUN_INSET_MODIFY:
2089 // We need to disable this, because we may get called for a
2091 // InsetTabular::getStatus() -> InsetText::getStatus()
2092 // and we don't handle LFUN_INSET_MODIFY.
2096 case LFUN_FONT_EMPH:
2097 flag.setOnOff(fontinfo.emph() == FONT_ON);
2100 case LFUN_FONT_NOUN:
2101 flag.setOnOff(fontinfo.noun() == FONT_ON);
2104 case LFUN_FONT_BOLD:
2105 case LFUN_FONT_BOLDSYMBOL:
2106 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2109 case LFUN_FONT_SANS:
2110 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2113 case LFUN_FONT_ROMAN:
2114 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2117 case LFUN_FONT_TYPEWRITER:
2118 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2123 enable = cur.selection();
2127 if (cmd.argument().empty()) {
2128 if (theClipboard().isInternal())
2129 enable = cap::numberOfSelections() > 0;
2131 enable = !theClipboard().empty();
2135 // we have an argument
2136 string const arg = to_utf8(cmd.argument());
2137 if (isStrUnsignedInt(arg)) {
2138 // it's a number and therefore means the internal stack
2139 unsigned int n = convert<unsigned int>(arg);
2140 enable = cap::numberOfSelections() > n;
2144 // explicit graphics type?
2145 if ((arg == "pdf" && theClipboard().hasGraphicsContents(Clipboard::PdfGraphicsType))
2146 || (arg == "png" && theClipboard().hasGraphicsContents(Clipboard::PngGraphicsType))
2147 || (arg == "jpeg" && theClipboard().hasGraphicsContents(Clipboard::JpegGraphicsType))
2148 || (arg == "linkback" && theClipboard().hasGraphicsContents(Clipboard::LinkBackGraphicsType))) {
2158 case LFUN_CLIPBOARD_PASTE:
2159 enable = !theClipboard().empty();
2162 case LFUN_PRIMARY_SELECTION_PASTE:
2163 enable = cur.selection() || !theSelection().empty();
2166 case LFUN_PARAGRAPH_MOVE_UP:
2167 enable = cur.pit() > 0 && !cur.selection();
2170 case LFUN_PARAGRAPH_MOVE_DOWN:
2171 enable = cur.pit() < cur.lastpit() && !cur.selection();
2174 case LFUN_INSET_DISSOLVE:
2175 if (!cmd.argument().empty()) {
2176 InsetLayout const & il = cur.inset().getLayout(cur.buffer().params());
2177 InsetLayout::InsetLyXType const type =
2178 translateLyXType(to_utf8(cmd.argument()));
2179 enable = cur.inset().lyxCode() == FLEX_CODE
2180 && il.lyxtype() == type;
2182 enable = !isMainText(cur.bv().buffer())
2183 && cur.inset().nargs() == 1;
2187 case LFUN_CHANGE_ACCEPT:
2188 case LFUN_CHANGE_REJECT:
2189 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
2190 // In principle, these LFUNs should only be enabled if there
2191 // is a change at the current position/in the current selection.
2192 // However, without proper optimizations, this will inevitably
2193 // result in unacceptable performance - just imagine a user who
2194 // wants to select the complete content of a long document.
2198 case LFUN_OUTLINE_UP:
2199 case LFUN_OUTLINE_DOWN:
2200 case LFUN_OUTLINE_IN:
2201 case LFUN_OUTLINE_OUT:
2202 // FIXME: LyX is not ready for outlining within inset.
2203 enable = isMainText(cur.bv().buffer())
2204 && cur.paragraph().layout().toclevel != Layout::NOT_IN_TOC;
2207 case LFUN_NEWLINE_INSERT:
2208 // LaTeX restrictions (labels or empty par)
2209 enable = (cur.pos() > cur.paragraph().beginOfBody());
2212 case LFUN_SET_GRAPHICS_GROUP: {
2213 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
2217 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
2221 case LFUN_NEWPAGE_INSERT:
2222 // not allowed in description items
2223 enable = !inDescriptionItem(cur);
2226 case LFUN_WORD_DELETE_FORWARD:
2227 case LFUN_WORD_DELETE_BACKWARD:
2228 case LFUN_LINE_DELETE:
2229 case LFUN_WORD_FORWARD:
2230 case LFUN_WORD_BACKWARD:
2231 case LFUN_WORD_RIGHT:
2232 case LFUN_WORD_LEFT:
2233 case LFUN_CHAR_FORWARD:
2234 case LFUN_CHAR_FORWARD_SELECT:
2235 case LFUN_CHAR_BACKWARD:
2236 case LFUN_CHAR_BACKWARD_SELECT:
2237 case LFUN_CHAR_LEFT:
2238 case LFUN_CHAR_LEFT_SELECT:
2239 case LFUN_CHAR_RIGHT:
2240 case LFUN_CHAR_RIGHT_SELECT:
2242 case LFUN_UP_SELECT:
2244 case LFUN_DOWN_SELECT:
2245 case LFUN_PARAGRAPH_UP_SELECT:
2246 case LFUN_PARAGRAPH_DOWN_SELECT:
2247 case LFUN_LINE_BEGIN_SELECT:
2248 case LFUN_LINE_END_SELECT:
2249 case LFUN_WORD_FORWARD_SELECT:
2250 case LFUN_WORD_BACKWARD_SELECT:
2251 case LFUN_WORD_RIGHT_SELECT:
2252 case LFUN_WORD_LEFT_SELECT:
2253 case LFUN_WORD_SELECT:
2254 case LFUN_PARAGRAPH_UP:
2255 case LFUN_PARAGRAPH_DOWN:
2256 case LFUN_LINE_BEGIN:
2258 case LFUN_CHAR_DELETE_FORWARD:
2259 case LFUN_CHAR_DELETE_BACKWARD:
2260 case LFUN_BREAK_PARAGRAPH:
2261 case LFUN_PARAGRAPH_SPACING:
2262 case LFUN_INSET_INSERT:
2263 case LFUN_WORD_UPCASE:
2264 case LFUN_WORD_LOWCASE:
2265 case LFUN_WORD_CAPITALIZE:
2266 case LFUN_CHARS_TRANSPOSE:
2267 case LFUN_SERVER_GET_XY:
2268 case LFUN_SERVER_SET_XY:
2269 case LFUN_SERVER_GET_LAYOUT:
2271 case LFUN_DATE_INSERT:
2272 case LFUN_SELF_INSERT:
2273 case LFUN_LINE_INSERT:
2274 case LFUN_MATH_DISPLAY:
2275 case LFUN_MATH_MODE:
2276 case LFUN_MATH_MACRO:
2277 case LFUN_MATH_MATRIX:
2278 case LFUN_MATH_DELIM:
2279 case LFUN_MATH_BIGDELIM:
2280 case LFUN_MATH_INSERT:
2281 case LFUN_MATH_SUBSCRIPT:
2282 case LFUN_MATH_SUPERSCRIPT:
2283 case LFUN_FONT_DEFAULT:
2284 case LFUN_FONT_UNDERLINE:
2285 case LFUN_FONT_SIZE:
2287 case LFUN_TEXTSTYLE_APPLY:
2288 case LFUN_TEXTSTYLE_UPDATE:
2289 case LFUN_LAYOUT_PARAGRAPH:
2290 case LFUN_PARAGRAPH_UPDATE:
2291 case LFUN_ACCENT_UMLAUT:
2292 case LFUN_ACCENT_CIRCUMFLEX:
2293 case LFUN_ACCENT_GRAVE:
2294 case LFUN_ACCENT_ACUTE:
2295 case LFUN_ACCENT_TILDE:
2296 case LFUN_ACCENT_CEDILLA:
2297 case LFUN_ACCENT_MACRON:
2298 case LFUN_ACCENT_DOT:
2299 case LFUN_ACCENT_UNDERDOT:
2300 case LFUN_ACCENT_UNDERBAR:
2301 case LFUN_ACCENT_CARON:
2302 case LFUN_ACCENT_BREVE:
2303 case LFUN_ACCENT_TIE:
2304 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2305 case LFUN_ACCENT_CIRCLE:
2306 case LFUN_ACCENT_OGONEK:
2307 case LFUN_THESAURUS_ENTRY:
2308 case LFUN_PARAGRAPH_PARAMS_APPLY:
2309 case LFUN_PARAGRAPH_PARAMS:
2311 case LFUN_BUFFER_END:
2312 case LFUN_BUFFER_BEGIN:
2313 case LFUN_BUFFER_BEGIN_SELECT:
2314 case LFUN_BUFFER_END_SELECT:
2315 case LFUN_UNICODE_INSERT:
2316 // these are handled in our dispatch()
2325 && (cur.empty() || !cur.inset().insetAllowed(code)))
2328 flag.setEnabled(enable);
2333 void Text::pasteString(Cursor & cur, docstring const & clip,
2336 cur.clearSelection();
2337 if (!clip.empty()) {
2340 insertStringAsParagraphs(cur, clip);
2342 insertStringAsLines(cur, clip);
2347 // FIXME: an item inset would make things much easier.
2348 bool Text::inDescriptionItem(Cursor & cur) const
2350 Paragraph & par = cur.paragraph();
2351 pos_type const pos = cur.pos();
2352 pos_type const body_pos = par.beginOfBody();
2354 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
2355 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
2356 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
2359 return (pos < body_pos
2361 && (pos == 0 || par.getChar(pos - 1) != ' ')));