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.
20 #include "BranchList.h"
21 #include "FloatList.h"
22 #include "FuncStatus.h"
24 #include "buffer_funcs.h"
25 #include "bufferparams.h"
26 #include "BufferView.h"
28 #include "coordcache.h"
29 #include "CutAndPaste.h"
31 #include "dispatchresult.h"
33 #include "funcrequest.h"
37 #include "LyXAction.h"
42 #include "paragraph.h"
43 #include "paragraph_funcs.h"
44 #include "ParagraphParameters.h"
47 #include "pariterator.h"
49 #include "frontends/Dialogs.h"
50 #include "frontends/LyXView.h"
52 #include "insets/insetcommand.h"
53 #include "insets/insetfloatlist.h"
54 #include "insets/insetnewline.h"
55 #include "insets/insetquotes.h"
56 #include "insets/insetspecialchar.h"
57 #include "insets/insettext.h"
59 #include "support/lstrings.h"
60 #include "support/lyxlib.h"
61 #include "support/convert.h"
62 #include "support/lyxtime.h"
64 #include "mathed/math_hullinset.h"
65 #include "mathed/math_macrotemplate.h"
67 #include <boost/current_function.hpp>
74 using lyx::cap::copySelection;
75 using lyx::cap::cutSelection;
76 using lyx::cap::pasteSelection;
77 using lyx::cap::replaceSelection;
79 using lyx::support::isStrUnsignedInt;
80 using lyx::support::token;
84 using std::istringstream;
85 using std::ostringstream;
88 extern string current_layout;
94 LyXFont freefont(LyXFont::ALL_IGNORE);
95 bool toggleall = false;
98 void toggleAndShow(LCursor & cur, LyXText * text,
99 LyXFont const & font, bool toggleall = true)
101 text->toggleFree(cur, font, toggleall);
103 if (font.language() != ignore_language ||
104 font.number() != LyXFont::IGNORE) {
105 Paragraph & par = cur.paragraph();
106 text->bidi.computeTables(par, cur.buffer(), cur.textRow());
107 if (cur.boundary() !=
108 text->bidi.isBoundary(cur.buffer(), par,
110 text->real_current_font))
111 text->setCursor(cur, cur.pit(), cur.pos(),
112 false, !cur.boundary());
117 void moveCursor(LCursor & cur, bool selecting)
119 if (selecting || cur.mark())
121 if (!cur.selection())
122 cur.bv().haveSelection(false);
123 cur.bv().switchKeyMap();
127 void finishChange(LCursor & cur, bool selecting)
130 moveCursor(cur, selecting);
134 void mathDispatch(LCursor & cur, FuncRequest const & cmd, bool display)
137 string sel = cur.selectionAsString(false);
138 //lyxerr << "selection is: '" << sel << "'" << endl;
140 // It may happen that sel is empty but there is a selection
141 replaceSelection(cur);
144 const int old_pos = cur.pos();
145 cur.insert(new MathHullInset("simple"));
146 BOOST_ASSERT(old_pos == cur.pos());
147 cur.nextInset()->edit(cur, true);
148 // don't do that also for LFUN_MATH_MODE
149 // unless you want end up with always changing
150 // to mathrm when opening an inlined inset --
151 // I really hate "LyXfunc overloading"...
153 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
154 // Avoid an unnecessary undo step if cmd.argument
156 if (!cmd.argument.empty())
157 cur.dispatch(FuncRequest(LFUN_INSERT_MATH,
160 // create a macro if we see "\\newcommand"
161 // somewhere, and an ordinary formula
163 istringstream is(sel);
164 if (sel.find("\\newcommand") == string::npos
165 && sel.find("\\def") == string::npos)
167 MathHullInset * formula = new MathHullInset;
170 formula->read(cur.buffer(), lex);
171 if (formula->getType() == "none")
172 // Don't create pseudo formulas if
173 // delimiters are left out
174 formula->mutate("simple");
177 cur.insert(new MathMacroTemplate(is));
179 cur.message(N_("Math editor mode"));
188 string const freefont2string()
191 if (font2string(freefont, toggleall, data))
198 bool LyXText::cursorPrevious(LCursor & cur)
200 pos_type cpos = cur.pos();
201 lyx::pit_type cpar = cur.pit();
203 int x = cur.x_target();
205 bool updated = setCursorFromCoordinates(cur, x, 0);
208 updated |= cursorUp(cur);
210 if (cpar == cur.pit() && cpos == cur.pos()) {
211 // we have a row which is taller than the workarea. The
212 // simplest solution is to move to the previous row instead.
213 updated |= cursorUp(cur);
216 cur.bv().updateScrollbar();
222 bool LyXText::cursorNext(LCursor & cur)
224 pos_type cpos = cur.pos();
225 lyx::pit_type cpar = cur.pit();
227 int x = cur.x_target();
228 bool updated = setCursorFromCoordinates(cur, x, cur.bv().workHeight() - 1);
231 updated |= cursorDown(cur);
233 if (cpar == cur.pit() && cpos == cur.pos()) {
234 // we have a row which is taller than the workarea. The
235 // simplest solution is to move to the next row instead.
236 updated |= cursorDown(cur);
239 cur.bv().updateScrollbar();
247 void specialChar(LCursor & cur, InsetSpecialChar::Kind kind)
249 lyx::cap::replaceSelection(cur);
250 cur.insert(new InsetSpecialChar(kind));
255 bool doInsertInset(LCursor & cur, LyXText * text,
256 FuncRequest const & cmd, bool edit, bool pastesel)
258 InsetBase * inset = createInset(&cur.bv(), cmd);
264 if (cur.selection()) {
265 cur.bv().owner()->dispatch(FuncRequest(LFUN_CUT));
268 text->insertInset(cur, inset);
271 inset->edit(cur, true);
273 if (gotsel && pastesel)
274 cur.bv().owner()->dispatch(FuncRequest(LFUN_PASTE));
279 void update(LCursor & cur)
281 //we don't call update(true, false) directly to save a metrics call
282 if (cur.bv().fitCursor())
283 cur.bv().update(Update::Force);
290 void LyXText::number(LCursor & cur)
292 LyXFont font(LyXFont::ALL_IGNORE);
293 font.setNumber(LyXFont::TOGGLE);
294 toggleAndShow(cur, this, font);
298 bool LyXText::isRTL(Paragraph const & par) const
300 return par.isRightToLeftPar(bv()->buffer()->params());
304 void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
306 lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
308 BOOST_ASSERT(cur.text() == this);
309 BufferView * bv = &cur.bv();
310 CursorSlice oldTopSlice = cur.top();
311 bool oldBoundary = cur.boundary();
312 bool sel = cur.selection();
313 // Signals that, even if needsUpdate == false, an update of the
314 // cursor paragraph is required
315 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
316 LyXAction::SingleParUpdate);
317 // Signals that a full-screen update is required
318 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
319 LyXAction::NoUpdate) || singleParUpdate);
320 // Remember the old paragraph metric (_outer_ paragraph!)
321 Dimension olddim = cur.bottom().paragraph().dim();
323 switch (cmd.action) {
325 case LFUN_PARAGRAPH_MOVE_DOWN: {
326 pit_type const pit = cur.pit();
327 recUndo(pit, pit + 1);
329 std::swap(pars_[pit], pars_[pit + 1]);
331 ParIterator begin(cur);
333 ParIterator end = boost::next(ParIterator(cur));
334 updateLabels(cur.buffer(), begin, end);
340 case LFUN_PARAGRAPH_MOVE_UP: {
341 pit_type const pit = cur.pit();
342 recUndo(pit - 1, pit);
344 std::swap(pars_[pit], pars_[pit - 1]);
346 ParIterator end = boost::next(ParIterator(cur));
348 ParIterator begin(cur);
349 updateLabels(cur.buffer(), begin, end);
355 case LFUN_APPENDIX: {
356 Paragraph & par = cur.paragraph();
357 bool start = !par.params().startOfAppendix();
360 #warning The code below only makes sense at top level.
361 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
363 // ensure that we have only one start_of_appendix in this document
364 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
365 if (pars_[tmp].params().startOfAppendix()) {
367 pars_[tmp].params().startOfAppendix(false);
373 par.params().startOfAppendix(start);
375 // we can set the refreshing parameters now
376 updateLabels(cur.buffer());
380 case LFUN_DELETE_WORD_FORWARD:
381 cur.clearSelection();
382 deleteWordForward(cur);
383 finishChange(cur, false);
386 case LFUN_DELETE_WORD_BACKWARD:
387 cur.clearSelection();
388 deleteWordBackward(cur);
389 finishChange(cur, false);
392 case LFUN_DELETE_LINE_FORWARD:
393 cur.clearSelection();
394 deleteLineForward(cur);
395 finishChange(cur, false);
400 cur.clearSelection();
401 if (isRTL(cur.paragraph()))
402 needsUpdate = cursorLeftOneWord(cur);
404 needsUpdate = cursorRightOneWord(cur);
405 finishChange(cur, false);
410 cur.clearSelection();
411 if (isRTL(cur.paragraph()))
412 needsUpdate = cursorRightOneWord(cur);
414 needsUpdate = cursorLeftOneWord(cur);
415 finishChange(cur, false);
418 case LFUN_BEGINNINGBUF:
419 if (cur.depth() == 1) {
421 cur.clearSelection();
422 needsUpdate = cursorTop(cur);
423 finishChange(cur, false);
429 case LFUN_BEGINNINGBUFSEL:
430 if (cur.depth() == 1) {
431 if (!cur.selection())
433 needsUpdate = cursorTop(cur);
434 finishChange(cur, true);
441 if (cur.depth() == 1) {
443 cur.clearSelection();
444 needsUpdate = cursorBottom(cur);
445 finishChange(cur, false);
452 if (cur.depth() == 1) {
453 if (!cur.selection())
455 needsUpdate = cursorBottom(cur);
456 finishChange(cur, true);
464 //lyxerr << BOOST_CURRENT_FUNCTION
465 // << " LFUN_RIGHT[SEL]:\n" << cur << endl;
466 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
467 if (isRTL(cur.paragraph()))
468 needsUpdate = cursorLeft(cur);
470 needsUpdate = cursorRight(cur);
472 if (!needsUpdate && oldTopSlice == cur.top()
473 && cur.boundary() == oldBoundary) {
475 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
481 //lyxerr << "handle LFUN_LEFT[SEL]:\n" << cur << endl;
482 cur.selHandle(cmd.action == LFUN_LEFTSEL);
483 if (isRTL(cur.paragraph()))
484 needsUpdate = cursorRight(cur);
486 needsUpdate = cursorLeft(cur);
488 if (!needsUpdate && oldTopSlice == cur.top()
489 && cur.boundary() == oldBoundary) {
491 cmd = FuncRequest(LFUN_FINISHED_LEFT);
498 //lyxerr << "handle LFUN_UP[SEL]:\n" << cur << endl;
499 cur.selHandle(cmd.action == LFUN_UPSEL);
501 needsUpdate = cursorUp(cur);
502 if (!needsUpdate && oldTopSlice == cur.top()
503 && cur.boundary() == oldBoundary) {
505 cmd = FuncRequest(LFUN_FINISHED_UP);
512 //lyxerr << "handle LFUN_DOWN[SEL]:\n" << cur << endl;
513 cur.selHandle(cmd.action == LFUN_DOWNSEL);
514 needsUpdate = cursorDown(cur);
515 if (!needsUpdate && oldTopSlice == cur.top() &&
516 cur.boundary() == oldBoundary)
519 cmd = FuncRequest(LFUN_FINISHED_DOWN);
523 case LFUN_UP_PARAGRAPH:
525 cur.clearSelection();
526 needsUpdate = cursorUpParagraph(cur);
527 finishChange(cur, false);
530 case LFUN_UP_PARAGRAPHSEL:
531 if (!cur.selection())
533 cursorUpParagraph(cur);
534 finishChange(cur, true);
537 case LFUN_DOWN_PARAGRAPH:
539 cur.clearSelection();
540 needsUpdate = cursorDownParagraph(cur);
541 finishChange(cur, false);
544 case LFUN_DOWN_PARAGRAPHSEL:
545 if (!cur.selection())
547 cursorDownParagraph(cur);
548 finishChange(cur, true);
553 if (!cur.selection())
555 needsUpdate = cursorPrevious(cur);
556 finishChange(cur, true);
561 if (!cur.selection())
563 needsUpdate = cursorNext(cur);
564 finishChange(cur, true);
569 if (!cur.selection())
571 needsUpdate = cursorHome(cur);
572 finishChange(cur, true);
577 if (!cur.selection())
579 needsUpdate = cursorEnd(cur);
580 finishChange(cur, true);
583 case LFUN_WORDRIGHTSEL:
584 if (!cur.selection())
586 if (isRTL(cur.paragraph()))
587 cursorLeftOneWord(cur);
589 cursorRightOneWord(cur);
590 finishChange(cur, true);
593 case LFUN_WORDLEFTSEL:
594 if (!cur.selection())
596 if (isRTL(cur.paragraph()))
597 cursorRightOneWord(cur);
599 cursorLeftOneWord(cur);
600 finishChange(cur, true);
604 selectWord(cur, lyx::WHOLE_WORD);
605 finishChange(cur, true);
612 cur.clearSelection();
613 finishChange(cur, false);
614 if (cur.pit() == 0 && cur.textRow().pos() == 0) {
616 cmd = FuncRequest(LFUN_FINISHED_UP);
618 needsUpdate = cursorPrevious(cur);
625 cur.clearSelection();
626 finishChange(cur, false);
627 if (cur.pit() == cur.lastpit()
628 && cur.textRow().endpos() == cur.lastpos()) {
630 cmd = FuncRequest(LFUN_FINISHED_DOWN);
632 needsUpdate = cursorNext(cur);
638 cur.clearSelection();
639 needsUpdate = cursorHome(cur);
640 finishChange(cur, false);
645 cur.clearSelection();
646 needsUpdate = cursorEnd(cur);
647 finishChange(cur, false);
650 case LFUN_BREAKLINE: {
651 // Not allowed by LaTeX (labels or empty par)
652 if (cur.pos() > cur.paragraph().beginOfBody()) {
653 lyx::cap::replaceSelection(cur);
654 cur.insert(new InsetNewline);
656 moveCursor(cur, false);
662 if (!cur.selection()) {
663 if (cur.pos() == cur.paragraph().size())
664 // Par boundary, force full-screen update
665 singleParUpdate = false;
666 needsUpdate = erase(cur);
668 // It is possible to make it a lot faster still
669 // just comment out the line below...
671 cutSelection(cur, true, false);
672 singleParUpdate = false;
674 moveCursor(cur, false);
677 case LFUN_DELETE_SKIP:
678 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
679 if (!cur.selection()) {
680 if (cur.pos() == cur.lastpos()) {
687 cutSelection(cur, true, false);
693 if (!cur.selection()) {
694 if (bv->owner()->getIntl().getTransManager().backspace()) {
695 // Par boundary, full-screen update
697 singleParUpdate = false;
698 needsUpdate = backspace(cur);
700 // It is possible to make it a lot faster still
701 // just comment out the line below...
704 cutSelection(cur, true, false);
705 singleParUpdate = false;
710 case LFUN_BACKSPACE_SKIP:
711 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
712 if (!cur.selection()) {
716 //CursorSlice cur = cursor();
720 cutSelection(cur, true, false);
724 case LFUN_BREAKPARAGRAPH:
725 lyx::cap::replaceSelection(cur);
726 breakParagraph(cur, 0);
731 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
732 lyx::cap::replaceSelection(cur);
733 breakParagraph(cur, 1);
738 case LFUN_BREAKPARAGRAPH_SKIP: {
739 // When at the beginning of a paragraph, remove
740 // indentation. Otherwise, do the same as LFUN_BREAKPARAGRAPH.
741 lyx::cap::replaceSelection(cur);
743 cur.paragraph().params().labelWidthString(string());
745 breakParagraph(cur, 0);
751 case LFUN_PARAGRAPH_SPACING: {
752 Paragraph & par = cur.paragraph();
753 Spacing::Space cur_spacing = par.params().spacing().getSpace();
754 string cur_value = "1.0";
755 if (cur_spacing == Spacing::Other)
756 cur_value = par.params().spacing().getValueAsString();
758 istringstream is(cmd.argument);
761 Spacing::Space new_spacing = cur_spacing;
762 string new_value = cur_value;
764 lyxerr << "Missing argument to `paragraph-spacing'"
766 } else if (tmp == "single") {
767 new_spacing = Spacing::Single;
768 } else if (tmp == "onehalf") {
769 new_spacing = Spacing::Onehalf;
770 } else if (tmp == "double") {
771 new_spacing = Spacing::Double;
772 } else if (tmp == "other") {
773 new_spacing = Spacing::Other;
774 string tmpval = "0.0";
776 lyxerr << "new_value = " << tmpval << endl;
779 } else if (tmp == "default") {
780 new_spacing = Spacing::Default;
782 lyxerr << _("Unknown spacing argument: ")
783 << cmd.argument << endl;
785 if (cur_spacing != new_spacing || cur_value != new_value)
786 par.params().spacing(Spacing(new_spacing, new_value));
790 case LFUN_INSET_INSERT: {
792 InsetBase * inset = createInset(bv, cmd);
794 insertInset(cur, inset);
800 case LFUN_INSET_SETTINGS:
801 cur.inset().showInsetDialog(bv);
804 case LFUN_NEXT_INSET_TOGGLE: {
805 InsetBase * inset = cur.nextInset();
806 // this is the real function we want to invoke
807 cmd = FuncRequest(LFUN_INSET_TOGGLE);
809 // if there is an inset at cursor, see whether it
812 LCursor tmpcur = cur;
813 tmpcur.pushLeft(*inset);
814 inset->dispatch(tmpcur, cmd);
815 if (tmpcur.result().dispatched()) {
816 cur.clearSelection();
820 // if it did not work, try the underlying inset.
821 if (!cur.result().dispatched())
822 cur.inset().dispatch(cur, cmd);
826 case LFUN_SPACE_INSERT:
827 if (cur.paragraph().layout()->free_spacing)
828 insertChar(cur, ' ');
830 doInsertInset(cur, this, cmd, false, false);
833 moveCursor(cur, false);
836 case LFUN_HYPHENATION:
837 specialChar(cur, InsetSpecialChar::HYPHENATION);
840 case LFUN_LIGATURE_BREAK:
841 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
845 specialChar(cur, InsetSpecialChar::LDOTS);
848 case LFUN_END_OF_SENTENCE:
849 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
852 case LFUN_MENU_SEPARATOR:
853 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
856 case LFUN_UPCASE_WORD:
857 changeCase(cur, LyXText::text_uppercase);
860 case LFUN_LOWCASE_WORD:
861 changeCase(cur, LyXText::text_lowercase);
864 case LFUN_CAPITALIZE_WORD:
865 changeCase(cur, LyXText::text_capitalization);
868 case LFUN_TRANSPOSE_CHARS:
873 cur.message(_("Paste"));
874 lyx::cap::replaceSelection(cur);
875 if (isStrUnsignedInt(cmd.argument))
876 pasteSelection(cur, convert<unsigned int>(cmd.argument));
878 pasteSelection(cur, 0);
879 cur.clearSelection(); // bug 393
885 cutSelection(cur, true, true);
886 cur.message(_("Cut"));
891 cur.message(_("Copy"));
895 cur.message(convert<string>(cursorX(cur.top(), cur.boundary())) + ' '
896 + convert<string>(cursorY(cur.top(), cur.boundary())));
902 istringstream is(cmd.argument);
905 lyxerr << "SETXY: Could not parse coordinates in '"
906 << cmd.argument << std::endl;
908 setCursorFromCoordinates(cur, x, y);
913 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
915 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
922 cur.message(cur.paragraph().layout()->name());
926 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
927 << cmd.argument << endl;
929 // This is not the good solution to the empty argument
930 // problem, but it will hopefully suffice for 1.2.0.
931 // The correct solution would be to augument the
932 // function list/array with information about what
933 // functions needs arguments and their type.
934 if (cmd.argument.empty()) {
935 cur.errorMessage(_("LyX function 'layout' needs an argument."));
939 // Derive layout number from given argument (string)
940 // and current buffer's textclass (number)
941 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
942 bool hasLayout = tclass.hasLayout(cmd.argument);
943 string layout = cmd.argument;
945 // If the entry is obsolete, use the new one instead.
947 string const & obs = tclass[layout]->obsoleted_by();
953 cur.errorMessage(string(N_("Layout ")) + cmd.argument +
958 bool change_layout = (current_layout != layout);
960 if (!change_layout && cur.selection() &&
961 cur.selBegin().pit() != cur.selEnd().pit())
963 pit_type spit = cur.selBegin().pit();
964 pit_type epit = cur.selEnd().pit() + 1;
965 while (spit != epit) {
966 if (pars_[spit].layout()->name() != current_layout) {
967 change_layout = true;
975 current_layout = layout;
976 setLayout(cur, layout);
977 bv->owner()->setLayout(layout);
983 case LFUN_PASTESELECTION: {
984 cur.clearSelection();
985 string const clip = bv->getClipboard();
988 if (cmd.argument == "paragraph")
989 insertStringAsParagraphs(cur, clip);
991 insertStringAsLines(cur, clip);
997 lyx::cap::replaceSelection(cur);
998 Paragraph & par = cur.paragraph();
999 lyx::pos_type pos = cur.pos();
1003 else if (cur.prevInset() && cur.prevInset()->isSpace())
1006 c = par.getChar(pos - 1);
1008 LyXLayout_ptr const & style = par.layout();
1010 BufferParams const & bufparams = bv->buffer()->params();
1011 if (!style->pass_thru
1012 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1013 string arg = cmd.argument;
1014 if (arg == "single")
1015 cur.insert(new InsetQuotes(c,
1016 bufparams.quotes_language,
1017 InsetQuotes::SingleQ));
1019 cur.insert(new InsetQuotes(c,
1020 bufparams.quotes_language,
1021 InsetQuotes::DoubleQ));
1025 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1029 case LFUN_DATE_INSERT:
1030 if (cmd.argument.empty())
1031 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT,
1032 lyx::formatted_time(lyx::current_time())));
1034 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT,
1035 lyx::formatted_time(lyx::current_time(), cmd.argument)));
1038 case LFUN_MOUSE_TRIPLE:
1039 if (cmd.button() == mouse_button::button1) {
1045 bv->haveSelection(cur.selection());
1049 case LFUN_MOUSE_DOUBLE:
1050 if (cmd.button() == mouse_button::button1) {
1051 selectWord(cur, lyx::WHOLE_WORD_STRICT);
1053 bv->haveSelection(cur.selection());
1057 // Single-click on work area
1058 case LFUN_MOUSE_PRESS: {
1059 // Right click on a footnote flag opens float menu
1060 if (cmd.button() == mouse_button::button3)
1061 cur.clearSelection();
1063 // Middle button press pastes if we have a selection
1064 // We do this here as if the selection was inside an inset
1065 // it could get cleared on the unlocking of the inset so
1066 // we have to check this first
1067 bool paste_internally = false;
1068 if (cmd.button() == mouse_button::button2 && cur.selection()) {
1069 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1070 paste_internally = true;
1073 bv->mouseSetCursor(cur);
1075 // Insert primary selection with middle mouse
1076 // if there is a local selection in the current buffer,
1078 if (cmd.button() == mouse_button::button2) {
1079 if (paste_internally)
1080 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1082 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1088 case LFUN_MOUSE_MOTION: {
1089 // Only use motion with button 1
1090 //if (cmd.button() != mouse_button::button1)
1093 // ignore motions deeper nested than the real anchor
1094 LCursor & bvcur = cur.bv().cursor();
1095 if (bvcur.anchor_.hasPart(cur)) {
1096 CursorSlice old = bvcur.top();
1098 int const wh = bv->workHeight();
1099 int const y = std::max(0, std::min(wh - 1, cmd.y));
1101 setCursorFromCoordinates(cur, cmd.x, y);
1102 cur.x_target() = cmd.x;
1107 // This is to allow jumping over large insets
1108 if (cur.top() == old) {
1115 if (cur.top() == old)
1118 // don't set anchor_
1119 bvcur.setCursor(cur);
1120 bvcur.selection() = true;
1121 //lyxerr << "MOTION: " << bv->cursor() << endl;
1129 case LFUN_MOUSE_RELEASE: {
1130 if (cmd.button() == mouse_button::button2)
1134 if (cmd.button() == mouse_button::button1)
1135 bv->haveSelection(cur.selection());
1138 bv->owner()->updateMenubar();
1139 bv->owner()->updateToolbars();
1143 case LFUN_SELFINSERT: {
1144 if (cmd.argument.empty())
1147 // Automatically delete the currently selected
1148 // text and replace it with what is being
1149 // typed in now. Depends on lyxrc settings
1150 // "auto_region_delete", which defaults to
1153 if (lyxrc.auto_region_delete) {
1154 if (cur.selection())
1155 cutSelection(cur, false, false);
1156 bv->haveSelection(false);
1159 cur.clearSelection();
1160 LyXFont const old_font = real_current_font;
1162 string::const_iterator cit = cmd.argument.begin();
1163 string::const_iterator end = cmd.argument.end();
1164 for (; cit != end; ++cit)
1165 bv->owner()->getIntl().getTransManager().
1166 translateAndInsert(*cit, this);
1169 moveCursor(cur, false);
1170 bv->updateScrollbar();
1175 InsetCommandParams p("url");
1176 string const data = InsetCommandMailer::params2string("url", p);
1177 bv->owner()->getDialogs().show("url", data, 0);
1181 case LFUN_HTMLURL: {
1182 InsetCommandParams p("htmlurl");
1183 string const data = InsetCommandMailer::params2string("url", p);
1184 bv->owner()->getDialogs().show("url", data, 0);
1188 case LFUN_INSERT_LABEL: {
1189 // Try to generate a valid label
1190 string const contents = cmd.argument.empty() ?
1191 cur.getPossibleLabel() : cmd.argument;
1193 InsetCommandParams p("label", contents);
1194 string const data = InsetCommandMailer::params2string("label", p);
1196 if (cmd.argument.empty()) {
1197 bv->owner()->getDialogs().show("label", data, 0);
1199 FuncRequest fr(LFUN_INSET_INSERT, data);
1207 case LFUN_INSET_LIST:
1208 case LFUN_INSET_THEOREM:
1209 case LFUN_INSET_CAPTION:
1211 case LFUN_INSERT_NOTE:
1212 case LFUN_INSERT_CHARSTYLE:
1213 case LFUN_INSERT_BOX:
1214 case LFUN_INSERT_BRANCH:
1215 case LFUN_INSERT_BIBITEM:
1216 case LFUN_INSET_ERT:
1217 case LFUN_INSET_FOOTNOTE:
1218 case LFUN_INSET_MARGINAL:
1219 case LFUN_INSET_OPTARG:
1220 case LFUN_ENVIRONMENT_INSERT:
1221 // Open the inset, and move the current selection
1223 doInsertInset(cur, this, cmd, true, true);
1227 case LFUN_TABULAR_INSERT:
1228 // if there were no arguments, just open the dialog
1229 if (doInsertInset(cur, this, cmd, false, true))
1232 bv->owner()->getDialogs().show("tabularcreate");
1236 case LFUN_INSET_FLOAT:
1237 case LFUN_INSET_WIDE_FLOAT:
1238 case LFUN_INSET_WRAP:
1239 doInsertInset(cur, this, cmd, true, true);
1241 // FIXME: the "Caption" name should not be hardcoded,
1242 // but given by the float definition.
1243 cur.dispatch(FuncRequest(LFUN_LAYOUT, "Caption"));
1246 case LFUN_INDEX_INSERT: {
1247 InsetBase * inset = createInset(&cur.bv(), cmd);
1252 cur.clearSelection();
1253 insertInset(cur, inset);
1254 inset->edit(cur, true);
1259 case LFUN_INDEX_PRINT:
1260 case LFUN_TOC_INSERT:
1262 case LFUN_INSERT_LINE:
1263 case LFUN_INSERT_PAGEBREAK:
1265 doInsertInset(cur, this, cmd, false, false);
1269 case LFUN_DEPTH_MIN:
1270 changeDepth(cur, DEC_DEPTH);
1273 case LFUN_DEPTH_PLUS:
1274 changeDepth(cur, INC_DEPTH);
1277 case LFUN_MATH_DISPLAY:
1278 mathDispatch(cur, cmd, true);
1281 case LFUN_MATH_IMPORT_SELECTION:
1282 case LFUN_MATH_MODE:
1283 if (cmd.argument == "on")
1284 // don't pass "on" as argument
1285 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1287 mathDispatch(cur, cmd, false);
1290 case LFUN_MATH_MACRO:
1291 if (cmd.argument.empty())
1292 cur.errorMessage(N_("Missing argument"));
1294 string s = cmd.argument;
1295 string const s1 = token(s, ' ', 1);
1296 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1297 string const s2 = token(s, ' ', 2);
1298 string const type = s2.empty() ? "newcommand" : s2;
1299 cur.insert(new MathMacroTemplate(token(s, ' ', 0), nargs, type));
1300 //cur.nextInset()->edit(cur, true);
1304 // passthrough hat and underscore outside mathed:
1305 case LFUN_SUBSCRIPT:
1306 mathDispatch(cur, FuncRequest(LFUN_SELFINSERT, "_"), false);
1308 case LFUN_SUPERSCRIPT:
1309 mathDispatch(cur, FuncRequest(LFUN_SELFINSERT, "^"), false);
1312 case LFUN_INSERT_MATH:
1313 case LFUN_INSERT_MATRIX:
1314 case LFUN_MATH_DELIM: {
1315 cur.insert(new MathHullInset("simple"));
1316 cur.dispatch(FuncRequest(LFUN_RIGHT));
1322 LyXFont font(LyXFont::ALL_IGNORE);
1323 font.setEmph(LyXFont::TOGGLE);
1324 toggleAndShow(cur, this, font);
1329 LyXFont font(LyXFont::ALL_IGNORE);
1330 font.setSeries(LyXFont::BOLD_SERIES);
1331 toggleAndShow(cur, this, font);
1336 LyXFont font(LyXFont::ALL_IGNORE);
1337 font.setNoun(LyXFont::TOGGLE);
1338 toggleAndShow(cur, this, font);
1343 LyXFont font(LyXFont::ALL_IGNORE);
1344 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
1345 toggleAndShow(cur, this, font);
1350 LyXFont font(LyXFont::ALL_IGNORE);
1351 font.setFamily(LyXFont::SANS_FAMILY);
1352 toggleAndShow(cur, this, font);
1357 LyXFont font(LyXFont::ALL_IGNORE);
1358 font.setFamily(LyXFont::ROMAN_FAMILY);
1359 toggleAndShow(cur, this, font);
1363 case LFUN_DEFAULT: {
1364 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
1365 toggleAndShow(cur, this, font);
1369 case LFUN_UNDERLINE: {
1370 LyXFont font(LyXFont::ALL_IGNORE);
1371 font.setUnderbar(LyXFont::TOGGLE);
1372 toggleAndShow(cur, this, font);
1376 case LFUN_FONT_SIZE: {
1377 LyXFont font(LyXFont::ALL_IGNORE);
1378 font.setLyXSize(cmd.argument);
1379 toggleAndShow(cur, this, font);
1383 case LFUN_LANGUAGE: {
1384 Language const * lang = languages.getLanguage(cmd.argument);
1387 LyXFont font(LyXFont::ALL_IGNORE);
1388 font.setLanguage(lang);
1389 toggleAndShow(cur, this, font);
1394 case LFUN_FREEFONT_APPLY:
1395 toggleAndShow(cur, this, freefont, toggleall);
1396 cur.message(_("Character set"));
1399 // Set the freefont using the contents of \param data dispatched from
1400 // the frontends and apply it at the current cursor location.
1401 case LFUN_FREEFONT_UPDATE: {
1404 if (bv_funcs::string2font(cmd.argument, font, toggle)) {
1407 toggleAndShow(cur, this, freefont, toggleall);
1408 cur.message(_("Character set"));
1413 case LFUN_FINISHED_LEFT:
1414 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1417 case LFUN_FINISHED_RIGHT:
1418 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1422 case LFUN_FINISHED_UP:
1423 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_UP:\n" << cur << endl;
1427 case LFUN_FINISHED_DOWN:
1428 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_DOWN:\n" << cur << endl;
1432 case LFUN_LAYOUT_PARAGRAPH: {
1434 params2string(cur.paragraph(), data);
1435 data = "show\n" + data;
1436 bv->owner()->getDialogs().show("paragraph", data);
1440 case LFUN_PARAGRAPH_UPDATE: {
1441 if (!bv->owner()->getDialogs().visible("paragraph"))
1444 params2string(cur.paragraph(), data);
1446 // Will the paragraph accept changes from the dialog?
1447 bool const accept = !cur.inset().forceDefaultParagraphs(cur.idx());
1449 data = "update " + convert<string>(accept) + '\n' + data;
1450 bv->owner()->getDialogs().update("paragraph", data);
1455 case LFUN_CIRCUMFLEX:
1465 case LFUN_SPECIAL_CARON:
1468 case LFUN_HUNG_UMLAUT:
1471 bv->owner()->getLyXFunc().handleKeyFunc(cmd.action);
1472 if (!cmd.argument.empty())
1473 bv->owner()->getIntl().getTransManager()
1474 .translateAndInsert(cmd.argument[0], this);
1477 case LFUN_FLOAT_LIST: {
1478 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1479 if (tclass.floats().typeExist(cmd.argument)) {
1480 // not quite sure if we want this...
1482 cur.clearSelection();
1483 breakParagraph(cur);
1485 if (cur.lastpos() != 0) {
1487 breakParagraph(cur);
1490 setLayout(cur, tclass.defaultLayoutName());
1491 setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1492 insertInset(cur, new InsetFloatList(cmd.argument));
1495 lyxerr << "Non-existent float type: "
1496 << cmd.argument << endl;
1501 case LFUN_ACCEPT_CHANGE: {
1506 case LFUN_REJECT_CHANGE: {
1511 case LFUN_THESAURUS_ENTRY: {
1512 string arg = cmd.argument;
1514 arg = cur.selectionAsString(false);
1516 if (arg.size() > 100 || arg.empty()) {
1517 // Get word or selection
1518 selectWordWhenUnderCursor(cur, lyx::WHOLE_WORD);
1519 arg = cur.selectionAsString(false);
1522 bv->owner()->getDialogs().show("thesaurus", arg);
1526 case LFUN_PARAGRAPH_APPLY: {
1527 // Given data, an encoding of the ParagraphParameters
1528 // generated in the Paragraph dialog, this function sets
1529 // the current paragraph appropriately.
1530 istringstream is(cmd.argument);
1533 ParagraphParameters params;
1538 params.labelWidthString(),
1540 cur.message(_("Paragraph layout set"));
1544 case LFUN_INSET_DIALOG_SHOW: {
1545 InsetBase * inset = cur.nextInset();
1547 FuncRequest fr(LFUN_INSET_DIALOG_SHOW);
1548 inset->dispatch(cur, fr);
1554 if (cur.selection()) {
1555 cur.selection() = false;
1558 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1563 lyxerr[Debug::ACTION]
1564 << BOOST_CURRENT_FUNCTION
1565 << ": Command " << cmd
1566 << " not DISPATCHED by LyXText" << endl;
1571 if (singleParUpdate)
1572 // Inserting characters does not change par height
1573 if (cur.bottom().paragraph().dim().height()
1574 == olddim.height()) {
1575 // if so, update _only_ this paragraph
1576 cur.bv().update(Update::SinglePar |
1578 Update::MultiParSel);
1584 && &oldTopSlice.inset() == &cur.inset()
1585 && oldTopSlice.idx() == cur.idx()
1587 && !cur.selection())
1594 bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
1595 FuncStatus & flag) const
1597 BOOST_ASSERT(cur.text() == this);
1599 LyXFont const & font = real_current_font;
1601 InsetBase::Code code = InsetBase::NO_CODE;
1603 switch (cmd.action) {
1605 case LFUN_DEPTH_MIN:
1606 enable = changeDepthAllowed(cur, DEC_DEPTH);
1609 case LFUN_DEPTH_PLUS:
1610 enable = changeDepthAllowed(cur, INC_DEPTH);
1614 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1617 case LFUN_INSERT_BIBITEM:
1618 enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO);
1621 case LFUN_DIALOG_SHOW_NEW_INSET:
1622 if (cmd.argument == "bibitem")
1623 code = InsetBase::BIBITEM_CODE;
1624 else if (cmd.argument == "bibtex")
1625 code = InsetBase::BIBTEX_CODE;
1626 else if (cmd.argument == "box")
1627 code = InsetBase::BOX_CODE;
1628 else if (cmd.argument == "branch")
1629 code = InsetBase::BRANCH_CODE;
1630 else if (cmd.argument == "citation")
1631 code = InsetBase::CITE_CODE;
1632 else if (cmd.argument == "ert")
1633 code = InsetBase::ERT_CODE;
1634 else if (cmd.argument == "external")
1635 code = InsetBase::EXTERNAL_CODE;
1636 else if (cmd.argument == "float")
1637 code = InsetBase::FLOAT_CODE;
1638 else if (cmd.argument == "graphics")
1639 code = InsetBase::GRAPHICS_CODE;
1640 else if (cmd.argument == "include")
1641 code = InsetBase::INCLUDE_CODE;
1642 else if (cmd.argument == "index")
1643 code = InsetBase::INDEX_CODE;
1644 else if (cmd.argument == "label")
1645 code = InsetBase::LABEL_CODE;
1646 else if (cmd.argument == "note")
1647 code = InsetBase::NOTE_CODE;
1648 else if (cmd.argument == "ref")
1649 code = InsetBase::REF_CODE;
1650 else if (cmd.argument == "toc")
1651 code = InsetBase::TOC_CODE;
1652 else if (cmd.argument == "url")
1653 code = InsetBase::URL_CODE;
1654 else if (cmd.argument == "vspace")
1655 code = InsetBase::VSPACE_CODE;
1656 else if (cmd.argument == "wrap")
1657 code = InsetBase::WRAP_CODE;
1660 case LFUN_INSET_ERT:
1661 code = InsetBase::ERT_CODE;
1663 case LFUN_INSET_FOOTNOTE:
1664 code = InsetBase::FOOT_CODE;
1666 case LFUN_TABULAR_INSERT:
1667 code = InsetBase::TABULAR_CODE;
1669 case LFUN_INSET_MARGINAL:
1670 code = InsetBase::MARGIN_CODE;
1672 case LFUN_INSET_FLOAT:
1673 case LFUN_INSET_WIDE_FLOAT:
1674 code = InsetBase::FLOAT_CODE;
1676 case LFUN_INSET_WRAP:
1677 code = InsetBase::WRAP_CODE;
1679 case LFUN_FLOAT_LIST:
1680 code = InsetBase::FLOAT_LIST_CODE;
1683 case LFUN_INSET_LIST:
1684 code = InsetBase::LIST_CODE;
1686 case LFUN_INSET_THEOREM:
1687 code = InsetBase::THEOREM_CODE;
1690 case LFUN_INSET_CAPTION:
1691 code = InsetBase::CAPTION_CODE;
1693 case LFUN_INSERT_NOTE:
1694 code = InsetBase::NOTE_CODE;
1696 case LFUN_INSERT_CHARSTYLE:
1697 code = InsetBase::CHARSTYLE_CODE;
1698 if (cur.buffer().params().getLyXTextClass().charstyles().empty())
1701 case LFUN_INSERT_BOX:
1702 code = InsetBase::BOX_CODE;
1704 case LFUN_INSERT_BRANCH:
1705 code = InsetBase::BRANCH_CODE;
1706 if (cur.buffer().getMasterBuffer()->params().branchlist().empty())
1709 case LFUN_INSERT_LABEL:
1710 code = InsetBase::LABEL_CODE;
1712 case LFUN_INSET_OPTARG:
1713 code = InsetBase::OPTARG_CODE;
1714 enable = numberOfOptArgs(cur.paragraph())
1715 < cur.paragraph().layout()->optionalargs;
1717 case LFUN_ENVIRONMENT_INSERT:
1718 code = InsetBase::BOX_CODE;
1720 case LFUN_INDEX_INSERT:
1721 code = InsetBase::INDEX_CODE;
1723 case LFUN_INDEX_PRINT:
1724 code = InsetBase::INDEX_PRINT_CODE;
1726 case LFUN_TOC_INSERT:
1727 code = InsetBase::TOC_CODE;
1731 code = InsetBase::URL_CODE;
1734 // always allow this, since we will inset a raw quote
1735 // if an inset is not allowed.
1737 case LFUN_HYPHENATION:
1738 case LFUN_LIGATURE_BREAK:
1740 case LFUN_MENU_SEPARATOR:
1742 case LFUN_END_OF_SENTENCE:
1743 code = InsetBase::SPECIALCHAR_CODE;
1745 case LFUN_SPACE_INSERT:
1746 // slight hack: we know this is allowed in math mode
1748 code = InsetBase::SPACE_CODE;
1751 #ifdef WITH_WARNINGS
1752 #warning This LFUN is not used anymore and should be nuked (JMarc 29/10/2005)
1755 case LFUN_INSET_DIALOG_SHOW: {
1756 InsetBase * inset = cur.nextInset();
1759 code = inset->lyxCode();
1760 if (!(code == InsetBase::INCLUDE_CODE
1761 || code == InsetBase::BIBTEX_CODE
1762 || code == InsetBase::FLOAT_LIST_CODE
1763 || code == InsetBase::TOC_CODE))
1770 case LFUN_INSET_MODIFY:
1771 // We need to disable this, because we may get called for a
1773 // InsetTabular::getStatus() -> InsetText::getStatus()
1774 // and we don't handle LFUN_INSET_MODIFY.
1779 flag.setOnOff(font.emph() == LyXFont::ON);
1783 flag.setOnOff(font.noun() == LyXFont::ON);
1787 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1791 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1795 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1799 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1804 enable = cur.selection();
1808 enable = lyx::cap::numberOfSelections() > 0;
1811 case LFUN_PARAGRAPH_MOVE_UP: {
1812 enable = cur.pit() > 0 && !cur.selection();
1816 case LFUN_PARAGRAPH_MOVE_DOWN: {
1817 enable = cur.pit() < cur.lastpit() && !cur.selection();
1821 case LFUN_DELETE_WORD_FORWARD:
1822 case LFUN_DELETE_WORD_BACKWARD:
1823 case LFUN_DELETE_LINE_FORWARD:
1824 case LFUN_WORDRIGHT:
1834 case LFUN_UP_PARAGRAPHSEL:
1835 case LFUN_DOWN_PARAGRAPHSEL:
1840 case LFUN_WORDRIGHTSEL:
1841 case LFUN_WORDLEFTSEL:
1843 case LFUN_UP_PARAGRAPH:
1844 case LFUN_DOWN_PARAGRAPH:
1849 case LFUN_BREAKLINE:
1851 case LFUN_DELETE_SKIP:
1852 case LFUN_BACKSPACE:
1853 case LFUN_BACKSPACE_SKIP:
1854 case LFUN_BREAKPARAGRAPH:
1855 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1856 case LFUN_BREAKPARAGRAPH_SKIP:
1857 case LFUN_PARAGRAPH_SPACING:
1858 case LFUN_INSET_INSERT:
1859 case LFUN_NEXT_INSET_TOGGLE:
1860 case LFUN_UPCASE_WORD:
1861 case LFUN_LOWCASE_WORD:
1862 case LFUN_CAPITALIZE_WORD:
1863 case LFUN_TRANSPOSE_CHARS:
1867 case LFUN_GETLAYOUT:
1869 case LFUN_PASTESELECTION:
1870 case LFUN_DATE_INSERT:
1871 case LFUN_SELFINSERT:
1872 case LFUN_INSERT_LINE:
1873 case LFUN_INSERT_PAGEBREAK:
1874 case LFUN_MATH_DISPLAY:
1875 case LFUN_MATH_IMPORT_SELECTION:
1876 case LFUN_MATH_MODE:
1877 case LFUN_MATH_MACRO:
1878 case LFUN_INSERT_MATH:
1879 case LFUN_INSERT_MATRIX:
1880 case LFUN_MATH_DELIM:
1881 case LFUN_SUBSCRIPT:
1882 case LFUN_SUPERSCRIPT:
1884 case LFUN_UNDERLINE:
1885 case LFUN_FONT_SIZE:
1887 case LFUN_FREEFONT_APPLY:
1888 case LFUN_FREEFONT_UPDATE:
1889 case LFUN_LAYOUT_PARAGRAPH:
1890 case LFUN_PARAGRAPH_UPDATE:
1892 case LFUN_CIRCUMFLEX:
1902 case LFUN_SPECIAL_CARON:
1905 case LFUN_HUNG_UMLAUT:
1908 case LFUN_ACCEPT_CHANGE:
1909 case LFUN_REJECT_CHANGE:
1910 case LFUN_THESAURUS_ENTRY:
1911 case LFUN_PARAGRAPH_APPLY:
1914 case LFUN_BEGINNINGBUF:
1915 case LFUN_BEGINNINGBUFSEL:
1916 case LFUN_ENDBUFSEL:
1917 // these are handled in our dispatch()
1925 if (code != InsetBase::NO_CODE
1926 && (cur.empty() || !cur.inset().insetAllowed(code)))
1929 flag.enabled(enable);