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"
48 #include "frontends/Dialogs.h"
49 #include "frontends/LyXView.h"
51 #include "insets/insetcommand.h"
52 #include "insets/insetfloatlist.h"
53 #include "insets/insetnewline.h"
54 #include "insets/insetquotes.h"
55 #include "insets/insetspecialchar.h"
56 #include "insets/insettext.h"
58 #include "support/lstrings.h"
59 #include "support/lyxlib.h"
60 #include "support/convert.h"
61 #include "support/lyxtime.h"
63 #include "mathed/math_hullinset.h"
64 #include "mathed/math_macrotemplate.h"
66 #include <boost/current_function.hpp>
73 using lyx::cap::copySelection;
74 using lyx::cap::cutSelection;
75 using lyx::cap::pasteSelection;
76 using lyx::cap::replaceSelection;
78 using lyx::support::isStrUnsignedInt;
79 using lyx::support::token;
83 using std::istringstream;
84 using std::ostringstream;
87 extern string current_layout;
93 LyXFont freefont(LyXFont::ALL_IGNORE);
94 bool toggleall = false;
97 void toggleAndShow(LCursor & cur, LyXText * text,
98 LyXFont const & font, bool toggleall = true)
100 text->toggleFree(cur, font, toggleall);
102 if (font.language() != ignore_language ||
103 font.number() != LyXFont::IGNORE) {
104 Paragraph & par = cur.paragraph();
105 text->bidi.computeTables(par, cur.buffer(), cur.textRow());
106 if (cur.boundary() !=
107 text->bidi.isBoundary(cur.buffer(), par,
109 text->real_current_font))
110 text->setCursor(cur, cur.pit(), cur.pos(),
111 false, !cur.boundary());
116 void moveCursor(LCursor & cur, bool selecting)
118 if (selecting || cur.mark())
120 if (!cur.selection())
121 cur.bv().haveSelection(false);
122 cur.bv().switchKeyMap();
126 void finishChange(LCursor & cur, bool selecting)
129 moveCursor(cur, selecting);
133 void mathDispatch(LCursor & cur, FuncRequest const & cmd, bool display)
136 string sel = cur.selectionAsString(false);
137 //lyxerr << "selection is: '" << sel << "'" << endl;
139 // It may happen that sel is empty but there is a selection
140 replaceSelection(cur);
143 const int old_pos = cur.pos();
144 cur.insert(new MathHullInset("simple"));
145 BOOST_ASSERT(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_INSERT_MATH,
159 // create a macro if we see "\\newcommand"
160 // somewhere, and an ordinary formula
162 istringstream is(sel);
163 if (sel.find("\\newcommand") == string::npos
164 && sel.find("\\def") == string::npos)
166 MathHullInset * formula = new MathHullInset;
169 formula->read(cur.buffer(), lex);
170 if (formula->getType() == "none")
171 // Don't create pseudo formulas if
172 // delimiters are left out
173 formula->mutate("simple");
176 cur.insert(new MathMacroTemplate(is));
178 cur.message(N_("Math editor mode"));
187 string const freefont2string()
190 if (font2string(freefont, toggleall, data))
197 bool LyXText::cursorPrevious(LCursor & cur)
199 pos_type cpos = cur.pos();
200 lyx::pit_type cpar = cur.pit();
202 int x = cur.x_target();
204 bool updated = setCursorFromCoordinates(cur, x, 0);
207 updated |= cursorUp(cur);
209 if (cpar == cur.pit() && cpos == cur.pos()) {
210 // we have a row which is taller than the workarea. The
211 // simplest solution is to move to the previous row instead.
212 updated |= cursorUp(cur);
215 cur.bv().updateScrollbar();
221 bool LyXText::cursorNext(LCursor & cur)
223 pos_type cpos = cur.pos();
224 lyx::pit_type cpar = cur.pit();
226 int x = cur.x_target();
227 bool updated = setCursorFromCoordinates(cur, x, cur.bv().workHeight() - 1);
230 updated |= cursorDown(cur);
232 if (cpar == cur.pit() && cpos == cur.pos()) {
233 // we have a row which is taller than the workarea. The
234 // simplest solution is to move to the next row instead.
235 updated |= cursorDown(cur);
238 cur.bv().updateScrollbar();
246 void specialChar(LCursor & cur, InsetSpecialChar::Kind kind)
248 lyx::cap::replaceSelection(cur);
249 cur.insert(new InsetSpecialChar(kind));
254 bool doInsertInset(LCursor & cur, LyXText * text,
255 FuncRequest const & cmd, bool edit, bool pastesel)
257 InsetBase * inset = createInset(&cur.bv(), cmd);
263 if (cur.selection()) {
264 cur.bv().owner()->dispatch(FuncRequest(LFUN_CUT));
267 text->insertInset(cur, inset);
270 inset->edit(cur, true);
272 if (gotsel && pastesel)
273 cur.bv().owner()->dispatch(FuncRequest(LFUN_PASTE));
278 void update(LCursor & cur)
280 //we don't call update(true, false) directly to save a metrics call
281 if (cur.bv().fitCursor())
282 cur.bv().update(Update::Force);
289 void LyXText::number(LCursor & cur)
291 LyXFont font(LyXFont::ALL_IGNORE);
292 font.setNumber(LyXFont::TOGGLE);
293 toggleAndShow(cur, this, font);
297 bool LyXText::isRTL(Paragraph const & par) const
299 return par.isRightToLeftPar(bv()->buffer()->params());
303 void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
305 lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
307 BOOST_ASSERT(cur.text() == this);
308 BufferView * bv = &cur.bv();
309 CursorSlice oldTopSlice = cur.top();
310 bool oldBoundary = cur.boundary();
311 bool sel = cur.selection();
312 // Signals that, even if needsUpdate == false, an update of the
313 // cursor paragraph is required
314 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
315 LyXAction::SingleParUpdate);
316 // Signals that a full-screen update is required
317 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
318 LyXAction::NoUpdate) || singleParUpdate);
319 // Remember the old paragraph metric (_outer_ paragraph!)
320 Dimension olddim = cur.bottom().paragraph().dim();
322 switch (cmd.action) {
324 case LFUN_APPENDIX: {
325 Paragraph & par = cur.paragraph();
326 bool start = !par.params().startOfAppendix();
329 #warning The code below only makes sense at top level.
330 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
332 // ensure that we have only one start_of_appendix in this document
333 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
334 if (pars_[tmp].params().startOfAppendix()) {
336 pars_[tmp].params().startOfAppendix(false);
342 par.params().startOfAppendix(start);
344 // we can set the refreshing parameters now
345 updateCounters(cur.buffer());
349 case LFUN_DELETE_WORD_FORWARD:
350 cur.clearSelection();
351 deleteWordForward(cur);
352 finishChange(cur, false);
355 case LFUN_DELETE_WORD_BACKWARD:
356 cur.clearSelection();
357 deleteWordBackward(cur);
358 finishChange(cur, false);
361 case LFUN_DELETE_LINE_FORWARD:
362 cur.clearSelection();
363 deleteLineForward(cur);
364 finishChange(cur, false);
369 cur.clearSelection();
370 if (isRTL(cur.paragraph()))
371 needsUpdate = cursorLeftOneWord(cur);
373 needsUpdate = cursorRightOneWord(cur);
374 finishChange(cur, false);
379 cur.clearSelection();
380 if (isRTL(cur.paragraph()))
381 needsUpdate = cursorRightOneWord(cur);
383 needsUpdate = cursorLeftOneWord(cur);
384 finishChange(cur, false);
387 case LFUN_BEGINNINGBUF:
388 if (cur.depth() == 1) {
390 cur.clearSelection();
391 needsUpdate = cursorTop(cur);
392 finishChange(cur, false);
398 case LFUN_BEGINNINGBUFSEL:
399 if (cur.depth() == 1) {
400 if (!cur.selection())
402 needsUpdate = cursorTop(cur);
403 finishChange(cur, true);
410 if (cur.depth() == 1) {
412 cur.clearSelection();
413 needsUpdate = cursorBottom(cur);
414 finishChange(cur, false);
421 if (cur.depth() == 1) {
422 if (!cur.selection())
424 needsUpdate = cursorBottom(cur);
425 finishChange(cur, true);
433 //lyxerr << BOOST_CURRENT_FUNCTION
434 // << " LFUN_RIGHT[SEL]:\n" << cur << endl;
435 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
436 if (isRTL(cur.paragraph()))
437 needsUpdate = cursorLeft(cur);
439 needsUpdate = cursorRight(cur);
441 if (!needsUpdate && oldTopSlice == cur.top()
442 && cur.boundary() == oldBoundary) {
444 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
450 //lyxerr << "handle LFUN_LEFT[SEL]:\n" << cur << endl;
451 cur.selHandle(cmd.action == LFUN_LEFTSEL);
452 if (isRTL(cur.paragraph()))
453 needsUpdate = cursorRight(cur);
455 needsUpdate = cursorLeft(cur);
457 if (!needsUpdate && oldTopSlice == cur.top()
458 && cur.boundary() == oldBoundary) {
460 cmd = FuncRequest(LFUN_FINISHED_LEFT);
467 //lyxerr << "handle LFUN_UP[SEL]:\n" << cur << endl;
468 cur.selHandle(cmd.action == LFUN_UPSEL);
470 needsUpdate = cursorUp(cur);
471 if (!needsUpdate && oldTopSlice == cur.top()
472 && cur.boundary() == oldBoundary) {
474 cmd = FuncRequest(LFUN_FINISHED_UP);
481 //lyxerr << "handle LFUN_DOWN[SEL]:\n" << cur << endl;
482 cur.selHandle(cmd.action == LFUN_DOWNSEL);
483 needsUpdate = cursorDown(cur);
484 if (!needsUpdate && oldTopSlice == cur.top() &&
485 cur.boundary() == oldBoundary)
488 cmd = FuncRequest(LFUN_FINISHED_DOWN);
492 case LFUN_UP_PARAGRAPH:
494 cur.clearSelection();
495 needsUpdate = cursorUpParagraph(cur);
496 finishChange(cur, false);
499 case LFUN_UP_PARAGRAPHSEL:
500 if (!cur.selection())
502 cursorUpParagraph(cur);
503 finishChange(cur, true);
506 case LFUN_DOWN_PARAGRAPH:
508 cur.clearSelection();
509 needsUpdate = cursorDownParagraph(cur);
510 finishChange(cur, false);
513 case LFUN_DOWN_PARAGRAPHSEL:
514 if (!cur.selection())
516 cursorDownParagraph(cur);
517 finishChange(cur, true);
522 if (!cur.selection())
524 needsUpdate = cursorPrevious(cur);
525 finishChange(cur, true);
530 if (!cur.selection())
532 needsUpdate = cursorNext(cur);
533 finishChange(cur, true);
538 if (!cur.selection())
540 needsUpdate = cursorHome(cur);
541 finishChange(cur, true);
546 if (!cur.selection())
548 needsUpdate = cursorEnd(cur);
549 finishChange(cur, true);
552 case LFUN_WORDRIGHTSEL:
553 if (!cur.selection())
555 if (isRTL(cur.paragraph()))
556 cursorLeftOneWord(cur);
558 cursorRightOneWord(cur);
559 finishChange(cur, true);
562 case LFUN_WORDLEFTSEL:
563 if (!cur.selection())
565 if (isRTL(cur.paragraph()))
566 cursorRightOneWord(cur);
568 cursorLeftOneWord(cur);
569 finishChange(cur, true);
573 selectWord(cur, lyx::WHOLE_WORD);
574 finishChange(cur, true);
581 cur.clearSelection();
582 finishChange(cur, false);
583 if (cur.pit() == 0 && cur.textRow().pos() == 0) {
585 cmd = FuncRequest(LFUN_FINISHED_UP);
587 needsUpdate = cursorPrevious(cur);
594 cur.clearSelection();
595 finishChange(cur, false);
596 if (cur.pit() == cur.lastpit()
597 && cur.textRow().endpos() == cur.lastpos()) {
599 cmd = FuncRequest(LFUN_FINISHED_DOWN);
601 needsUpdate = cursorNext(cur);
607 cur.clearSelection();
608 needsUpdate = cursorHome(cur);
609 finishChange(cur, false);
614 cur.clearSelection();
615 needsUpdate = cursorEnd(cur);
616 finishChange(cur, false);
619 case LFUN_BREAKLINE: {
620 // Not allowed by LaTeX (labels or empty par)
621 if (cur.pos() > cur.paragraph().beginOfBody()) {
622 lyx::cap::replaceSelection(cur);
623 cur.insert(new InsetNewline);
625 moveCursor(cur, false);
631 if (!cur.selection()) {
632 if (cur.pos() == cur.paragraph().size())
633 // Par boundary, force full-screen update
634 singleParUpdate = false;
635 needsUpdate = erase(cur);
637 // It is possible to make it a lot faster still
638 // just comment out the line below...
640 cutSelection(cur, true, false);
641 singleParUpdate = false;
643 moveCursor(cur, false);
646 case LFUN_DELETE_SKIP:
647 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
648 if (!cur.selection()) {
649 if (cur.pos() == cur.lastpos()) {
656 cutSelection(cur, true, false);
662 if (!cur.selection()) {
663 if (bv->owner()->getIntl().getTransManager().backspace()) {
664 // Par boundary, full-screen update
666 singleParUpdate = false;
667 needsUpdate = backspace(cur);
669 // It is possible to make it a lot faster still
670 // just comment out the line below...
673 cutSelection(cur, true, false);
674 singleParUpdate = false;
679 case LFUN_BACKSPACE_SKIP:
680 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
681 if (!cur.selection()) {
685 //CursorSlice cur = cursor();
689 cutSelection(cur, true, false);
693 case LFUN_BREAKPARAGRAPH:
694 lyx::cap::replaceSelection(cur);
695 breakParagraph(cur, 0);
700 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
701 lyx::cap::replaceSelection(cur);
702 breakParagraph(cur, 1);
707 case LFUN_BREAKPARAGRAPH_SKIP: {
708 // When at the beginning of a paragraph, remove
709 // indentation. Otherwise, do the same as LFUN_BREAKPARAGRAPH.
710 lyx::cap::replaceSelection(cur);
712 cur.paragraph().params().labelWidthString(string());
714 breakParagraph(cur, 0);
720 case LFUN_PARAGRAPH_SPACING: {
721 Paragraph & par = cur.paragraph();
722 Spacing::Space cur_spacing = par.params().spacing().getSpace();
723 string cur_value = "1.0";
724 if (cur_spacing == Spacing::Other)
725 cur_value = par.params().spacing().getValueAsString();
727 istringstream is(cmd.argument);
730 Spacing::Space new_spacing = cur_spacing;
731 string new_value = cur_value;
733 lyxerr << "Missing argument to `paragraph-spacing'"
735 } else if (tmp == "single") {
736 new_spacing = Spacing::Single;
737 } else if (tmp == "onehalf") {
738 new_spacing = Spacing::Onehalf;
739 } else if (tmp == "double") {
740 new_spacing = Spacing::Double;
741 } else if (tmp == "other") {
742 new_spacing = Spacing::Other;
743 string tmpval = "0.0";
745 lyxerr << "new_value = " << tmpval << endl;
748 } else if (tmp == "default") {
749 new_spacing = Spacing::Default;
751 lyxerr << _("Unknown spacing argument: ")
752 << cmd.argument << endl;
754 if (cur_spacing != new_spacing || cur_value != new_value)
755 par.params().spacing(Spacing(new_spacing, new_value));
759 case LFUN_INSET_INSERT: {
761 InsetBase * inset = createInset(bv, cmd);
763 insertInset(cur, inset);
769 case LFUN_INSET_SETTINGS:
770 cur.inset().showInsetDialog(bv);
773 case LFUN_NEXT_INSET_TOGGLE: {
774 InsetBase * inset = cur.nextInset();
775 // this is the real function we want to invoke
776 cmd = FuncRequest(LFUN_INSET_TOGGLE);
778 // if there is an inset at cursor, see whether it
781 LCursor tmpcur = cur;
782 tmpcur.pushLeft(*inset);
783 inset->dispatch(tmpcur, cmd);
784 if (tmpcur.result().dispatched()) {
785 cur.clearSelection();
789 // if it did not work, try the underlying inset.
790 if (!cur.result().dispatched())
791 cur.inset().dispatch(cur, cmd);
795 case LFUN_SPACE_INSERT:
796 if (cur.paragraph().layout()->free_spacing)
797 insertChar(cur, ' ');
799 doInsertInset(cur, this, cmd, false, false);
802 moveCursor(cur, false);
805 case LFUN_HYPHENATION:
806 specialChar(cur, InsetSpecialChar::HYPHENATION);
809 case LFUN_LIGATURE_BREAK:
810 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
814 specialChar(cur, InsetSpecialChar::LDOTS);
817 case LFUN_END_OF_SENTENCE:
818 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
821 case LFUN_MENU_SEPARATOR:
822 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
825 case LFUN_UPCASE_WORD:
826 changeCase(cur, LyXText::text_uppercase);
829 case LFUN_LOWCASE_WORD:
830 changeCase(cur, LyXText::text_lowercase);
833 case LFUN_CAPITALIZE_WORD:
834 changeCase(cur, LyXText::text_capitalization);
837 case LFUN_TRANSPOSE_CHARS:
842 cur.message(_("Paste"));
843 lyx::cap::replaceSelection(cur);
844 if (isStrUnsignedInt(cmd.argument))
845 pasteSelection(cur, convert<unsigned int>(cmd.argument));
847 pasteSelection(cur, 0);
848 cur.clearSelection(); // bug 393
854 cutSelection(cur, true, true);
855 cur.message(_("Cut"));
860 cur.message(_("Copy"));
864 cur.message(convert<string>(cursorX(cur.top(), cur.boundary())) + ' '
865 + convert<string>(cursorY(cur.top(), cur.boundary())));
871 istringstream is(cmd.argument);
874 lyxerr << "SETXY: Could not parse coordinates in '"
875 << cmd.argument << std::endl;
877 setCursorFromCoordinates(cur, x, y);
882 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
884 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
891 cur.message(cur.paragraph().layout()->name());
895 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
896 << cmd.argument << endl;
898 // This is not the good solution to the empty argument
899 // problem, but it will hopefully suffice for 1.2.0.
900 // The correct solution would be to augument the
901 // function list/array with information about what
902 // functions needs arguments and their type.
903 if (cmd.argument.empty()) {
904 cur.errorMessage(_("LyX function 'layout' needs an argument."));
908 // Derive layout number from given argument (string)
909 // and current buffer's textclass (number)
910 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
911 bool hasLayout = tclass.hasLayout(cmd.argument);
912 string layout = cmd.argument;
914 // If the entry is obsolete, use the new one instead.
916 string const & obs = tclass[layout]->obsoleted_by();
922 cur.errorMessage(string(N_("Layout ")) + cmd.argument +
927 bool change_layout = (current_layout != layout);
929 if (!change_layout && cur.selection() &&
930 cur.selBegin().pit() != cur.selEnd().pit())
932 pit_type spit = cur.selBegin().pit();
933 pit_type epit = cur.selEnd().pit() + 1;
934 while (spit != epit) {
935 if (pars_[spit].layout()->name() != current_layout) {
936 change_layout = true;
944 current_layout = layout;
945 setLayout(cur, layout);
946 bv->owner()->setLayout(layout);
952 case LFUN_PASTESELECTION: {
953 cur.clearSelection();
954 string const clip = bv->getClipboard();
957 if (cmd.argument == "paragraph")
958 insertStringAsParagraphs(cur, clip);
960 insertStringAsLines(cur, clip);
966 lyx::cap::replaceSelection(cur);
967 Paragraph & par = cur.paragraph();
968 lyx::pos_type pos = cur.pos();
972 else if (cur.prevInset() && cur.prevInset()->isSpace())
975 c = par.getChar(pos - 1);
977 LyXLayout_ptr const & style = par.layout();
979 BufferParams const & bufparams = bv->buffer()->params();
980 if (!style->pass_thru
981 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
982 string arg = cmd.argument;
984 cur.insert(new InsetQuotes(c,
985 bufparams.quotes_language,
986 InsetQuotes::SingleQ));
988 cur.insert(new InsetQuotes(c,
989 bufparams.quotes_language,
990 InsetQuotes::DoubleQ));
994 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
998 case LFUN_DATE_INSERT:
999 if (cmd.argument.empty())
1000 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT,
1001 lyx::formatted_time(lyx::current_time())));
1003 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT,
1004 lyx::formatted_time(lyx::current_time(), cmd.argument)));
1007 case LFUN_MOUSE_TRIPLE:
1008 if (cmd.button() == mouse_button::button1) {
1014 bv->haveSelection(cur.selection());
1018 case LFUN_MOUSE_DOUBLE:
1019 if (cmd.button() == mouse_button::button1) {
1020 selectWord(cur, lyx::WHOLE_WORD_STRICT);
1022 bv->haveSelection(cur.selection());
1026 // Single-click on work area
1027 case LFUN_MOUSE_PRESS: {
1028 // Right click on a footnote flag opens float menu
1029 if (cmd.button() == mouse_button::button3) {
1030 cur.clearSelection();
1034 // Middle button press pastes if we have a selection
1035 // We do this here as if the selection was inside an inset
1036 // it could get cleared on the unlocking of the inset so
1037 // we have to check this first
1038 bool paste_internally = false;
1039 if (cmd.button() == mouse_button::button2 && cur.selection()) {
1040 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1041 paste_internally = true;
1044 bv->mouseSetCursor(cur);
1046 // Insert primary selection with middle mouse
1047 // if there is a local selection in the current buffer,
1049 if (cmd.button() == mouse_button::button2) {
1050 if (paste_internally)
1051 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1053 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1059 case LFUN_MOUSE_MOTION: {
1060 // Only use motion with button 1
1061 //if (cmd.button() != mouse_button::button1)
1064 // ignore motions deeper nested than the real anchor
1065 LCursor & bvcur = cur.bv().cursor();
1066 if (bvcur.anchor_.hasPart(cur)) {
1067 CursorSlice old = bvcur.top();
1069 int const wh = bv->workHeight();
1070 int const y = std::max(0, std::min(wh - 1, cmd.y));
1072 setCursorFromCoordinates(cur, cmd.x, y);
1073 cur.x_target() = cmd.x;
1078 // This is to allow jumping over large insets
1079 if (cur.top() == old) {
1086 if (cur.top() == old)
1089 // don't set anchor_
1090 bvcur.setCursor(cur);
1091 bvcur.selection() = true;
1092 //lyxerr << "MOTION: " << bv->cursor() << endl;
1100 case LFUN_MOUSE_RELEASE: {
1101 if (cmd.button() == mouse_button::button2)
1105 if (cmd.button() == mouse_button::button1)
1106 bv->haveSelection(cur.selection());
1109 bv->owner()->updateMenubar();
1110 bv->owner()->updateToolbars();
1114 case LFUN_SELFINSERT: {
1115 if (cmd.argument.empty())
1118 // Automatically delete the currently selected
1119 // text and replace it with what is being
1120 // typed in now. Depends on lyxrc settings
1121 // "auto_region_delete", which defaults to
1124 if (lyxrc.auto_region_delete) {
1125 if (cur.selection())
1126 cutSelection(cur, false, false);
1127 bv->haveSelection(false);
1130 cur.clearSelection();
1131 LyXFont const old_font = real_current_font;
1133 string::const_iterator cit = cmd.argument.begin();
1134 string::const_iterator end = cmd.argument.end();
1135 for (; cit != end; ++cit)
1136 bv->owner()->getIntl().getTransManager().
1137 translateAndInsert(*cit, this);
1140 moveCursor(cur, false);
1141 bv->updateScrollbar();
1146 InsetCommandParams p("url");
1147 string const data = InsetCommandMailer::params2string("url", p);
1148 bv->owner()->getDialogs().show("url", data, 0);
1152 case LFUN_HTMLURL: {
1153 InsetCommandParams p("htmlurl");
1154 string const data = InsetCommandMailer::params2string("url", p);
1155 bv->owner()->getDialogs().show("url", data, 0);
1159 case LFUN_INSERT_LABEL: {
1160 // Try to generate a valid label
1161 string const contents = cmd.argument.empty() ?
1162 cur.getPossibleLabel() : cmd.argument;
1164 InsetCommandParams p("label", contents);
1165 string const data = InsetCommandMailer::params2string("label", p);
1167 if (cmd.argument.empty()) {
1168 bv->owner()->getDialogs().show("label", data, 0);
1170 FuncRequest fr(LFUN_INSET_INSERT, data);
1178 case LFUN_INSET_LIST:
1179 case LFUN_INSET_THEOREM:
1180 case LFUN_INSET_CAPTION:
1182 case LFUN_INSERT_NOTE:
1183 case LFUN_INSERT_CHARSTYLE:
1184 case LFUN_INSERT_BOX:
1185 case LFUN_INSERT_BRANCH:
1186 case LFUN_INSERT_BIBITEM:
1187 case LFUN_INSET_ERT:
1188 case LFUN_INSET_FOOTNOTE:
1189 case LFUN_INSET_MARGINAL:
1190 case LFUN_INSET_OPTARG:
1191 case LFUN_ENVIRONMENT_INSERT:
1192 // Open the inset, and move the current selection
1194 doInsertInset(cur, this, cmd, true, true);
1198 case LFUN_TABULAR_INSERT:
1199 // if there were no arguments, just open the dialog
1200 if (doInsertInset(cur, this, cmd, false, true))
1203 bv->owner()->getDialogs().show("tabularcreate");
1207 case LFUN_INSET_FLOAT:
1208 case LFUN_INSET_WIDE_FLOAT:
1209 case LFUN_INSET_WRAP:
1210 doInsertInset(cur, this, cmd, true, true);
1212 // FIXME: the "Caption" name should not be hardcoded,
1213 // but given by the float definition.
1214 cur.dispatch(FuncRequest(LFUN_LAYOUT, "Caption"));
1217 case LFUN_INDEX_INSERT: {
1218 InsetBase * inset = createInset(&cur.bv(), cmd);
1223 cur.clearSelection();
1224 insertInset(cur, inset);
1225 inset->edit(cur, true);
1230 case LFUN_INDEX_PRINT:
1231 case LFUN_TOC_INSERT:
1233 case LFUN_INSERT_LINE:
1234 case LFUN_INSERT_PAGEBREAK:
1236 doInsertInset(cur, this, cmd, false, false);
1240 case LFUN_DEPTH_MIN:
1241 changeDepth(cur, DEC_DEPTH);
1244 case LFUN_DEPTH_PLUS:
1245 changeDepth(cur, INC_DEPTH);
1248 case LFUN_MATH_DISPLAY:
1249 mathDispatch(cur, cmd, true);
1252 case LFUN_MATH_IMPORT_SELECTION:
1253 case LFUN_MATH_MODE:
1254 if (cmd.argument == "on")
1255 // don't pass "on" as argument
1256 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1258 mathDispatch(cur, cmd, false);
1261 case LFUN_MATH_MACRO:
1262 if (cmd.argument.empty())
1263 cur.errorMessage(N_("Missing argument"));
1265 string s = cmd.argument;
1266 string const s1 = token(s, ' ', 1);
1267 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1268 string const s2 = token(s, ' ', 2);
1269 string const type = s2.empty() ? "newcommand" : s2;
1270 cur.insert(new MathMacroTemplate(token(s, ' ', 0), nargs, type));
1271 //cur.nextInset()->edit(cur, true);
1275 // passthrough hat and underscore outside mathed:
1276 case LFUN_SUBSCRIPT:
1277 mathDispatch(cur, FuncRequest(LFUN_SELFINSERT, "_"), false);
1279 case LFUN_SUPERSCRIPT:
1280 mathDispatch(cur, FuncRequest(LFUN_SELFINSERT, "^"), false);
1283 case LFUN_INSERT_MATH:
1284 case LFUN_INSERT_MATRIX:
1285 case LFUN_MATH_DELIM: {
1286 cur.insert(new MathHullInset("simple"));
1287 cur.dispatch(FuncRequest(LFUN_RIGHT));
1293 LyXFont font(LyXFont::ALL_IGNORE);
1294 font.setEmph(LyXFont::TOGGLE);
1295 toggleAndShow(cur, this, font);
1300 LyXFont font(LyXFont::ALL_IGNORE);
1301 font.setSeries(LyXFont::BOLD_SERIES);
1302 toggleAndShow(cur, this, font);
1307 LyXFont font(LyXFont::ALL_IGNORE);
1308 font.setNoun(LyXFont::TOGGLE);
1309 toggleAndShow(cur, this, font);
1314 LyXFont font(LyXFont::ALL_IGNORE);
1315 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
1316 toggleAndShow(cur, this, font);
1321 LyXFont font(LyXFont::ALL_IGNORE);
1322 font.setFamily(LyXFont::SANS_FAMILY);
1323 toggleAndShow(cur, this, font);
1328 LyXFont font(LyXFont::ALL_IGNORE);
1329 font.setFamily(LyXFont::ROMAN_FAMILY);
1330 toggleAndShow(cur, this, font);
1334 case LFUN_DEFAULT: {
1335 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
1336 toggleAndShow(cur, this, font);
1340 case LFUN_UNDERLINE: {
1341 LyXFont font(LyXFont::ALL_IGNORE);
1342 font.setUnderbar(LyXFont::TOGGLE);
1343 toggleAndShow(cur, this, font);
1347 case LFUN_FONT_SIZE: {
1348 LyXFont font(LyXFont::ALL_IGNORE);
1349 font.setLyXSize(cmd.argument);
1350 toggleAndShow(cur, this, font);
1354 case LFUN_LANGUAGE: {
1355 Language const * lang = languages.getLanguage(cmd.argument);
1358 LyXFont font(LyXFont::ALL_IGNORE);
1359 font.setLanguage(lang);
1360 toggleAndShow(cur, this, font);
1365 case LFUN_FREEFONT_APPLY:
1366 toggleAndShow(cur, this, freefont, toggleall);
1367 cur.message(_("Character set"));
1370 // Set the freefont using the contents of \param data dispatched from
1371 // the frontends and apply it at the current cursor location.
1372 case LFUN_FREEFONT_UPDATE: {
1375 if (bv_funcs::string2font(cmd.argument, font, toggle)) {
1378 toggleAndShow(cur, this, freefont, toggleall);
1379 cur.message(_("Character set"));
1384 case LFUN_FINISHED_LEFT:
1385 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1388 case LFUN_FINISHED_RIGHT:
1389 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1393 case LFUN_FINISHED_UP:
1394 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_UP:\n" << cur << endl;
1398 case LFUN_FINISHED_DOWN:
1399 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_DOWN:\n" << cur << endl;
1403 case LFUN_LAYOUT_PARAGRAPH: {
1405 params2string(cur.paragraph(), data);
1406 data = "show\n" + data;
1407 bv->owner()->getDialogs().show("paragraph", data);
1411 case LFUN_PARAGRAPH_UPDATE: {
1412 if (!bv->owner()->getDialogs().visible("paragraph"))
1415 params2string(cur.paragraph(), data);
1417 // Will the paragraph accept changes from the dialog?
1418 bool const accept = !cur.inset().forceDefaultParagraphs(cur.idx());
1420 data = "update " + convert<string>(accept) + '\n' + data;
1421 bv->owner()->getDialogs().update("paragraph", data);
1426 case LFUN_CIRCUMFLEX:
1436 case LFUN_SPECIAL_CARON:
1439 case LFUN_HUNG_UMLAUT:
1442 bv->owner()->getLyXFunc().handleKeyFunc(cmd.action);
1443 if (!cmd.argument.empty())
1444 bv->owner()->getIntl().getTransManager()
1445 .translateAndInsert(cmd.argument[0], this);
1448 case LFUN_FLOAT_LIST: {
1449 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1450 if (tclass.floats().typeExist(cmd.argument)) {
1451 // not quite sure if we want this...
1453 cur.clearSelection();
1454 breakParagraph(cur);
1456 if (cur.lastpos() != 0) {
1458 breakParagraph(cur);
1461 setLayout(cur, tclass.defaultLayoutName());
1462 setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1463 insertInset(cur, new InsetFloatList(cmd.argument));
1466 lyxerr << "Non-existent float type: "
1467 << cmd.argument << endl;
1472 case LFUN_ACCEPT_CHANGE: {
1477 case LFUN_REJECT_CHANGE: {
1482 case LFUN_THESAURUS_ENTRY: {
1483 string arg = cmd.argument;
1485 arg = cur.selectionAsString(false);
1487 if (arg.size() > 100 || arg.empty()) {
1488 // Get word or selection
1489 selectWordWhenUnderCursor(cur, lyx::WHOLE_WORD);
1490 arg = cur.selectionAsString(false);
1493 bv->owner()->getDialogs().show("thesaurus", arg);
1497 case LFUN_PARAGRAPH_APPLY: {
1498 // Given data, an encoding of the ParagraphParameters
1499 // generated in the Paragraph dialog, this function sets
1500 // the current paragraph appropriately.
1501 istringstream is(cmd.argument);
1504 ParagraphParameters params;
1509 params.labelWidthString(),
1511 cur.message(_("Paragraph layout set"));
1515 case LFUN_INSET_DIALOG_SHOW: {
1516 InsetBase * inset = cur.nextInset();
1518 FuncRequest fr(LFUN_INSET_DIALOG_SHOW);
1519 inset->dispatch(cur, fr);
1525 if (cur.selection()) {
1526 cur.selection() = false;
1529 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1534 lyxerr[Debug::ACTION]
1535 << BOOST_CURRENT_FUNCTION
1536 << ": Command " << cmd
1537 << " not DISPATCHED by LyXText" << endl;
1542 if (singleParUpdate)
1543 // Inserting characters does not change par height
1544 if (cur.bottom().paragraph().dim().height()
1545 == olddim.height()) {
1546 // if so, update _only_ this paragraph
1547 cur.bv().update(Update::SinglePar |
1549 Update::MultiParSel);
1555 && &oldTopSlice.inset() == &cur.inset()
1556 && oldTopSlice.idx() == cur.idx()
1558 && !cur.selection())
1565 bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
1566 FuncStatus & flag) const
1568 BOOST_ASSERT(cur.text() == this);
1570 LyXFont const & font = real_current_font;
1572 InsetBase::Code code = InsetBase::NO_CODE;
1574 switch (cmd.action) {
1576 case LFUN_DEPTH_MIN:
1577 enable = changeDepthAllowed(cur, DEC_DEPTH);
1580 case LFUN_DEPTH_PLUS:
1581 enable = changeDepthAllowed(cur, INC_DEPTH);
1585 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1588 case LFUN_INSERT_BIBITEM:
1589 enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO);
1592 case LFUN_DIALOG_SHOW_NEW_INSET:
1593 if (cmd.argument == "bibitem")
1594 code = InsetBase::BIBITEM_CODE;
1595 else if (cmd.argument == "bibtex")
1596 code = InsetBase::BIBTEX_CODE;
1597 else if (cmd.argument == "box")
1598 code = InsetBase::BOX_CODE;
1599 else if (cmd.argument == "branch")
1600 code = InsetBase::BRANCH_CODE;
1601 else if (cmd.argument == "citation")
1602 code = InsetBase::CITE_CODE;
1603 else if (cmd.argument == "ert")
1604 code = InsetBase::ERT_CODE;
1605 else if (cmd.argument == "external")
1606 code = InsetBase::EXTERNAL_CODE;
1607 else if (cmd.argument == "float")
1608 code = InsetBase::FLOAT_CODE;
1609 else if (cmd.argument == "graphics")
1610 code = InsetBase::GRAPHICS_CODE;
1611 else if (cmd.argument == "include")
1612 code = InsetBase::INCLUDE_CODE;
1613 else if (cmd.argument == "index")
1614 code = InsetBase::INDEX_CODE;
1615 else if (cmd.argument == "label")
1616 code = InsetBase::LABEL_CODE;
1617 else if (cmd.argument == "note")
1618 code = InsetBase::NOTE_CODE;
1619 else if (cmd.argument == "ref")
1620 code = InsetBase::REF_CODE;
1621 else if (cmd.argument == "toc")
1622 code = InsetBase::TOC_CODE;
1623 else if (cmd.argument == "url")
1624 code = InsetBase::URL_CODE;
1625 else if (cmd.argument == "vspace")
1626 code = InsetBase::VSPACE_CODE;
1627 else if (cmd.argument == "wrap")
1628 code = InsetBase::WRAP_CODE;
1631 case LFUN_INSET_ERT:
1632 code = InsetBase::ERT_CODE;
1634 case LFUN_INSET_FOOTNOTE:
1635 code = InsetBase::FOOT_CODE;
1637 case LFUN_TABULAR_INSERT:
1638 code = InsetBase::TABULAR_CODE;
1640 case LFUN_INSET_MARGINAL:
1641 code = InsetBase::MARGIN_CODE;
1643 case LFUN_INSET_FLOAT:
1644 case LFUN_INSET_WIDE_FLOAT:
1645 code = InsetBase::FLOAT_CODE;
1647 case LFUN_INSET_WRAP:
1648 code = InsetBase::WRAP_CODE;
1650 case LFUN_FLOAT_LIST:
1651 code = InsetBase::FLOAT_LIST_CODE;
1654 case LFUN_INSET_LIST:
1655 code = InsetBase::LIST_CODE;
1657 case LFUN_INSET_THEOREM:
1658 code = InsetBase::THEOREM_CODE;
1661 case LFUN_INSET_CAPTION:
1662 code = InsetBase::CAPTION_CODE;
1664 case LFUN_INSERT_NOTE:
1665 code = InsetBase::NOTE_CODE;
1667 case LFUN_INSERT_CHARSTYLE:
1668 code = InsetBase::CHARSTYLE_CODE;
1669 if (cur.buffer().params().getLyXTextClass().charstyles().empty())
1672 case LFUN_INSERT_BOX:
1673 code = InsetBase::BOX_CODE;
1675 case LFUN_INSERT_BRANCH:
1676 code = InsetBase::BRANCH_CODE;
1677 if (cur.buffer().getMasterBuffer()->params().branchlist().empty())
1680 case LFUN_INSERT_LABEL:
1681 code = InsetBase::LABEL_CODE;
1683 case LFUN_INSET_OPTARG:
1684 code = InsetBase::OPTARG_CODE;
1685 enable = numberOfOptArgs(cur.paragraph())
1686 < cur.paragraph().layout()->optionalargs;
1688 case LFUN_ENVIRONMENT_INSERT:
1689 code = InsetBase::BOX_CODE;
1691 case LFUN_INDEX_INSERT:
1692 code = InsetBase::INDEX_CODE;
1694 case LFUN_INDEX_PRINT:
1695 code = InsetBase::INDEX_PRINT_CODE;
1697 case LFUN_TOC_INSERT:
1698 code = InsetBase::TOC_CODE;
1702 code = InsetBase::URL_CODE;
1705 // always allow this, since we will inset a raw quote
1706 // if an inset is not allowed.
1708 case LFUN_HYPHENATION:
1709 case LFUN_LIGATURE_BREAK:
1711 case LFUN_MENU_SEPARATOR:
1713 case LFUN_END_OF_SENTENCE:
1714 code = InsetBase::SPECIALCHAR_CODE;
1716 case LFUN_SPACE_INSERT:
1717 // slight hack: we know this is allowed in math mode
1719 code = InsetBase::SPACE_CODE;
1722 #ifdef WITH_WARNINGS
1723 #warning This LFUN is not used anymore and should be nuked (JMarc 29/10/2005)
1726 case LFUN_INSET_DIALOG_SHOW: {
1727 InsetBase * inset = cur.nextInset();
1730 code = inset->lyxCode();
1731 if (!(code == InsetBase::INCLUDE_CODE
1732 || code == InsetBase::BIBTEX_CODE
1733 || code == InsetBase::FLOAT_LIST_CODE
1734 || code == InsetBase::TOC_CODE))
1741 case LFUN_INSET_MODIFY:
1742 // We need to disable this, because we may get called for a
1744 // InsetTabular::getStatus() -> InsetText::getStatus()
1745 // and we don't handle LFUN_INSET_MODIFY.
1750 flag.setOnOff(font.emph() == LyXFont::ON);
1754 flag.setOnOff(font.noun() == LyXFont::ON);
1758 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1762 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1766 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1770 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1775 enable = cur.selection();
1779 enable = lyx::cap::numberOfSelections() > 0;
1782 case LFUN_DELETE_WORD_FORWARD:
1783 case LFUN_DELETE_WORD_BACKWARD:
1784 case LFUN_DELETE_LINE_FORWARD:
1785 case LFUN_WORDRIGHT:
1795 case LFUN_UP_PARAGRAPHSEL:
1796 case LFUN_DOWN_PARAGRAPHSEL:
1801 case LFUN_WORDRIGHTSEL:
1802 case LFUN_WORDLEFTSEL:
1804 case LFUN_UP_PARAGRAPH:
1805 case LFUN_DOWN_PARAGRAPH:
1810 case LFUN_BREAKLINE:
1812 case LFUN_DELETE_SKIP:
1813 case LFUN_BACKSPACE:
1814 case LFUN_BACKSPACE_SKIP:
1815 case LFUN_BREAKPARAGRAPH:
1816 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1817 case LFUN_BREAKPARAGRAPH_SKIP:
1818 case LFUN_PARAGRAPH_SPACING:
1819 case LFUN_INSET_INSERT:
1820 case LFUN_NEXT_INSET_TOGGLE:
1821 case LFUN_UPCASE_WORD:
1822 case LFUN_LOWCASE_WORD:
1823 case LFUN_CAPITALIZE_WORD:
1824 case LFUN_TRANSPOSE_CHARS:
1828 case LFUN_GETLAYOUT:
1830 case LFUN_PASTESELECTION:
1831 case LFUN_DATE_INSERT:
1832 case LFUN_SELFINSERT:
1833 case LFUN_INSERT_LINE:
1834 case LFUN_INSERT_PAGEBREAK:
1835 case LFUN_MATH_DISPLAY:
1836 case LFUN_MATH_IMPORT_SELECTION:
1837 case LFUN_MATH_MODE:
1838 case LFUN_MATH_MACRO:
1839 case LFUN_INSERT_MATH:
1840 case LFUN_INSERT_MATRIX:
1841 case LFUN_MATH_DELIM:
1842 case LFUN_SUBSCRIPT:
1843 case LFUN_SUPERSCRIPT:
1845 case LFUN_UNDERLINE:
1846 case LFUN_FONT_SIZE:
1848 case LFUN_FREEFONT_APPLY:
1849 case LFUN_FREEFONT_UPDATE:
1850 case LFUN_LAYOUT_PARAGRAPH:
1851 case LFUN_PARAGRAPH_UPDATE:
1853 case LFUN_CIRCUMFLEX:
1863 case LFUN_SPECIAL_CARON:
1866 case LFUN_HUNG_UMLAUT:
1869 case LFUN_ACCEPT_CHANGE:
1870 case LFUN_REJECT_CHANGE:
1871 case LFUN_THESAURUS_ENTRY:
1872 case LFUN_PARAGRAPH_APPLY:
1875 case LFUN_BEGINNINGBUF:
1876 case LFUN_BEGINNINGBUFSEL:
1877 case LFUN_ENDBUFSEL:
1878 // these are handled in our dispatch()
1886 if (code != InsetBase::NO_CODE
1887 && (cur.empty() || !cur.inset().insetAllowed(code)))
1890 flag.enabled(enable);