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 "FloatList.h"
21 #include "FuncStatus.h"
23 #include "buffer_funcs.h"
24 #include "bufferparams.h"
25 #include "BufferView.h"
27 #include "coordcache.h"
28 #include "CutAndPaste.h"
30 #include "dispatchresult.h"
32 #include "funcrequest.h"
36 #include "LyXAction.h"
41 #include "paragraph.h"
42 #include "paragraph_funcs.h"
43 #include "ParagraphParameters.h"
47 #include "frontends/Dialogs.h"
48 #include "frontends/LyXView.h"
50 #include "insets/insetcommand.h"
51 #include "insets/insetfloatlist.h"
52 #include "insets/insetnewline.h"
53 #include "insets/insetquotes.h"
54 #include "insets/insetspecialchar.h"
55 #include "insets/insettext.h"
57 #include "support/lstrings.h"
58 #include "support/lyxlib.h"
59 #include "support/convert.h"
60 #include "support/lyxtime.h"
62 #include "mathed/math_hullinset.h"
63 #include "mathed/math_macrotemplate.h"
65 #include <boost/current_function.hpp>
72 using lyx::cap::copySelection;
73 using lyx::cap::cutSelection;
74 using lyx::cap::pasteSelection;
75 using lyx::cap::replaceSelection;
77 using lyx::support::isStrUnsignedInt;
78 using lyx::support::token;
82 using std::istringstream;
85 extern string current_layout;
91 LyXFont freefont(LyXFont::ALL_IGNORE);
92 bool toggleall = false;
95 void toggleAndShow(LCursor & cur, LyXText * text,
96 LyXFont const & font, bool toggleall = true)
98 text->toggleFree(cur, font, toggleall);
100 if (font.language() != ignore_language ||
101 font.number() != LyXFont::IGNORE) {
102 Paragraph & par = cur.paragraph();
103 text->bidi.computeTables(par, cur.buffer(), cur.textRow());
104 if (cur.boundary() !=
105 text->bidi.isBoundary(cur.buffer(), par,
107 text->real_current_font))
108 text->setCursor(cur, cur.pit(), cur.pos(),
109 false, !cur.boundary());
114 void moveCursor(LCursor & cur, bool selecting)
116 if (selecting || cur.mark())
118 if (!cur.selection())
119 cur.bv().haveSelection(false);
120 cur.bv().switchKeyMap();
124 void finishChange(LCursor & cur, bool selecting)
127 moveCursor(cur, selecting);
131 void mathDispatch(LCursor & cur, FuncRequest const & cmd, bool display)
134 string sel = cur.selectionAsString(false);
135 //lyxerr << "selection is: '" << sel << "'" << endl;
138 const int old_pos = cur.pos();
139 cur.insert(new MathHullInset("simple"));
140 BOOST_ASSERT(old_pos == cur.pos());
141 cur.nextInset()->edit(cur, true);
142 // don't do that also for LFUN_MATH_MODE
143 // unless you want end up with always changing
144 // to mathrm when opening an inlined inset --
145 // I really hate "LyXfunc overloading"...
147 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
148 // Avoid an unnecessary undo step if cmd.argument
150 if (!cmd.argument.empty())
151 cur.dispatch(FuncRequest(LFUN_INSERT_MATH,
154 // create a macro if we see "\\newcommand"
155 // somewhere, and an ordinary formula
157 cutSelection(cur, true, true);
158 if (sel.find("\\newcommand") == string::npos
159 && sel.find("\\def") == string::npos)
161 cur.insert(new MathHullInset("simple"));
162 cur.dispatch(FuncRequest(LFUN_RIGHT));
163 cur.dispatch(FuncRequest(LFUN_INSERT_MATH, sel));
165 istringstream is(sel);
166 cur.insert(new MathMacroTemplate(is));
169 cur.message(N_("Math editor mode"));
178 string const freefont2string()
181 if (font2string(freefont, toggleall, data))
188 bool LyXText::cursorPrevious(LCursor & cur)
190 pos_type cpos = cur.pos();
191 lyx::pit_type cpar = cur.pit();
193 int x = cur.x_target();
195 bool updated = setCursorFromCoordinates(cur, x, 0);
198 updated |= cursorUp(cur);
200 if (cpar == cur.pit() && cpos == cur.pos()) {
201 // we have a row which is taller than the workarea. The
202 // simplest solution is to move to the previous row instead.
203 updated |= cursorUp(cur);
206 cur.bv().updateScrollbar();
212 bool LyXText::cursorNext(LCursor & cur)
214 pos_type cpos = cur.pos();
215 lyx::pit_type cpar = cur.pit();
217 int x = cur.x_target();
218 bool updated = setCursorFromCoordinates(cur, x, cur.bv().workHeight() - 1);
221 updated |= cursorDown(cur);
223 if (cpar == cur.pit() && cpos == cur.pos()) {
224 // we have a row which is taller than the workarea. The
225 // simplest solution is to move to the next row instead.
226 updated |= cursorDown(cur);
229 cur.bv().updateScrollbar();
237 void specialChar(LCursor & cur, InsetSpecialChar::Kind kind)
239 lyx::cap::replaceSelection(cur);
240 cur.insert(new InsetSpecialChar(kind));
245 bool doInsertInset(LCursor & cur, LyXText * text,
246 FuncRequest const & cmd, bool edit, bool pastesel)
248 InsetBase * inset = createInset(&cur.bv(), cmd);
254 if (cur.selection()) {
255 cur.bv().owner()->dispatch(FuncRequest(LFUN_CUT));
258 text->insertInset(cur, inset);
261 inset->edit(cur, true);
263 if (gotsel && pastesel)
264 cur.bv().owner()->dispatch(FuncRequest(LFUN_PASTE));
269 void update(LCursor & cur)
271 //we don't call update(true, false) directly to save a metrics call
272 if (cur.bv().fitCursor())
273 cur.bv().update(Update::Force);
280 void LyXText::number(LCursor & cur)
282 LyXFont font(LyXFont::ALL_IGNORE);
283 font.setNumber(LyXFont::TOGGLE);
284 toggleAndShow(cur, this, font);
288 bool LyXText::isRTL(Paragraph const & par) const
290 return par.isRightToLeftPar(bv()->buffer()->params());
294 void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
296 lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
298 BOOST_ASSERT(cur.text() == this);
299 BufferView * bv = &cur.bv();
300 CursorSlice oldTopSlice = cur.top();
301 bool oldBoundary = cur.boundary();
302 bool sel = cur.selection();
303 // Signals that, even if needsUpdate == false, an update of the
304 // cursor paragraph is required
305 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
306 LyXAction::SingleParUpdate);
307 // Signals that a full-screen update is required
308 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
309 LyXAction::NoUpdate) || singleParUpdate);
310 // Remember the old paragraph metric
311 Dimension olddim = cur.paragraph().dim();
313 switch (cmd.action) {
315 case LFUN_APPENDIX: {
316 Paragraph & par = cur.paragraph();
317 bool start = !par.params().startOfAppendix();
320 #warning The code below only makes sense at top level.
321 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
323 // ensure that we have only one start_of_appendix in this document
324 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
325 if (pars_[tmp].params().startOfAppendix()) {
327 pars_[tmp].params().startOfAppendix(false);
333 par.params().startOfAppendix(start);
335 // we can set the refreshing parameters now
336 updateCounters(cur.buffer());
340 case LFUN_DELETE_WORD_FORWARD:
341 cur.clearSelection();
342 deleteWordForward(cur);
343 finishChange(cur, false);
346 case LFUN_DELETE_WORD_BACKWARD:
347 cur.clearSelection();
348 deleteWordBackward(cur);
349 finishChange(cur, false);
352 case LFUN_DELETE_LINE_FORWARD:
353 cur.clearSelection();
354 deleteLineForward(cur);
355 finishChange(cur, false);
360 cur.clearSelection();
361 if (isRTL(cur.paragraph()))
362 needsUpdate = cursorLeftOneWord(cur);
364 needsUpdate = cursorRightOneWord(cur);
365 finishChange(cur, false);
370 cur.clearSelection();
371 if (isRTL(cur.paragraph()))
372 needsUpdate = cursorRightOneWord(cur);
374 needsUpdate = cursorLeftOneWord(cur);
375 finishChange(cur, false);
378 case LFUN_BEGINNINGBUF:
379 if (cur.depth() == 1) {
381 cur.clearSelection();
383 finishChange(cur, false);
389 case LFUN_BEGINNINGBUFSEL:
390 if (cur.depth() == 1) {
391 if (!cur.selection())
394 finishChange(cur, true);
401 if (cur.depth() == 1) {
403 cur.clearSelection();
405 finishChange(cur, false);
412 if (cur.depth() == 1) {
413 if (!cur.selection())
416 finishChange(cur, true);
424 //lyxerr << BOOST_CURRENT_FUNCTION
425 // << " LFUN_RIGHT[SEL]:\n" << cur << endl;
426 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
427 if (isRTL(cur.paragraph()))
428 needsUpdate = cursorLeft(cur);
430 needsUpdate = cursorRight(cur);
432 if (!needsUpdate && oldTopSlice == cur.top()
433 && cur.boundary() == oldBoundary) {
435 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
441 //lyxerr << "handle LFUN_LEFT[SEL]:\n" << cur << endl;
442 cur.selHandle(cmd.action == LFUN_LEFTSEL);
443 if (isRTL(cur.paragraph()))
444 needsUpdate = cursorRight(cur);
446 needsUpdate = cursorLeft(cur);
448 if (!needsUpdate && oldTopSlice == cur.top()
449 && cur.boundary() == oldBoundary) {
451 cmd = FuncRequest(LFUN_FINISHED_LEFT);
458 //lyxerr << "handle LFUN_UP[SEL]:\n" << cur << endl;
459 cur.selHandle(cmd.action == LFUN_UPSEL);
461 needsUpdate = cursorUp(cur);
462 if (!needsUpdate && oldTopSlice == cur.top()
463 && cur.boundary() == oldBoundary) {
465 cmd = FuncRequest(LFUN_FINISHED_UP);
472 //lyxerr << "handle LFUN_DOWN[SEL]:\n" << cur << endl;
473 cur.selHandle(cmd.action == LFUN_DOWNSEL);
474 needsUpdate = cursorDown(cur);
475 if (!needsUpdate && oldTopSlice == cur.top() &&
476 cur.boundary() == oldBoundary)
479 cmd = FuncRequest(LFUN_FINISHED_DOWN);
483 case LFUN_UP_PARAGRAPH:
485 cur.clearSelection();
486 needsUpdate = cursorUpParagraph(cur);
487 finishChange(cur, false);
490 case LFUN_UP_PARAGRAPHSEL:
491 if (!cur.selection())
493 cursorUpParagraph(cur);
494 finishChange(cur, true);
497 case LFUN_DOWN_PARAGRAPH:
499 cur.clearSelection();
500 needsUpdate = cursorDownParagraph(cur);
501 finishChange(cur, false);
504 case LFUN_DOWN_PARAGRAPHSEL:
505 if (!cur.selection())
507 cursorDownParagraph(cur);
508 finishChange(cur, true);
513 if (!cur.selection())
516 finishChange(cur, true);
521 if (!cur.selection())
524 finishChange(cur, true);
529 if (!cur.selection())
532 finishChange(cur, true);
537 if (!cur.selection())
540 finishChange(cur, true);
543 case LFUN_WORDRIGHTSEL:
544 if (!cur.selection())
546 if (isRTL(cur.paragraph()))
547 cursorLeftOneWord(cur);
549 cursorRightOneWord(cur);
550 finishChange(cur, true);
553 case LFUN_WORDLEFTSEL:
554 if (!cur.selection())
556 if (isRTL(cur.paragraph()))
557 cursorRightOneWord(cur);
559 cursorLeftOneWord(cur);
560 finishChange(cur, true);
564 selectWord(cur, lyx::WHOLE_WORD);
565 finishChange(cur, true);
572 cur.clearSelection();
573 finishChange(cur, false);
574 if (cur.pit() == 0 && cur.textRow().pos() == 0) {
576 cmd = FuncRequest(LFUN_FINISHED_UP);
578 needsUpdate = cursorPrevious(cur);
585 cur.clearSelection();
586 finishChange(cur, false);
587 if (cur.pit() == cur.lastpit()
588 && cur.textRow().endpos() == cur.lastpos()) {
590 cmd = FuncRequest(LFUN_FINISHED_DOWN);
592 needsUpdate = cursorNext(cur);
598 cur.clearSelection();
600 finishChange(cur, false);
605 cur.clearSelection();
607 finishChange(cur, false);
610 case LFUN_BREAKLINE: {
611 // Not allowed by LaTeX (labels or empty par)
612 if (cur.pos() > cur.paragraph().beginOfBody()) {
613 lyx::cap::replaceSelection(cur);
614 cur.insert(new InsetNewline);
616 moveCursor(cur, false);
622 if (!cur.selection()) {
625 // It is possible to make it a lot faster still
626 // just comment out the line below...
628 cutSelection(cur, true, false);
630 moveCursor(cur, false);
633 case LFUN_DELETE_SKIP:
634 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
635 if (!cur.selection()) {
636 if (cur.pos() == cur.lastpos()) {
643 cutSelection(cur, true, false);
649 if (!cur.selection()) {
650 if (bv->owner()->getIntl().getTransManager().backspace()) {
653 // It is possible to make it a lot faster still
654 // just comment out the line below...
657 cutSelection(cur, true, false);
662 case LFUN_BACKSPACE_SKIP:
663 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
664 if (!cur.selection()) {
668 //CursorSlice cur = cursor();
672 cutSelection(cur, true, false);
676 case LFUN_BREAKPARAGRAPH:
677 lyx::cap::replaceSelection(cur);
678 breakParagraph(cur, 0);
683 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
684 lyx::cap::replaceSelection(cur);
685 breakParagraph(cur, 1);
690 case LFUN_BREAKPARAGRAPH_SKIP: {
691 // When at the beginning of a paragraph, remove
692 // indentation. Otherwise, do the same as LFUN_BREAKPARAGRAPH.
693 lyx::cap::replaceSelection(cur);
695 cur.paragraph().params().labelWidthString(string());
697 breakParagraph(cur, 0);
703 case LFUN_PARAGRAPH_SPACING: {
704 Paragraph & par = cur.paragraph();
705 Spacing::Space cur_spacing = par.params().spacing().getSpace();
706 string cur_value = "1.0";
707 if (cur_spacing == Spacing::Other)
708 cur_value = par.params().spacing().getValueAsString();
710 istringstream is(cmd.argument);
713 Spacing::Space new_spacing = cur_spacing;
714 string new_value = cur_value;
716 lyxerr << "Missing argument to `paragraph-spacing'"
718 } else if (tmp == "single") {
719 new_spacing = Spacing::Single;
720 } else if (tmp == "onehalf") {
721 new_spacing = Spacing::Onehalf;
722 } else if (tmp == "double") {
723 new_spacing = Spacing::Double;
724 } else if (tmp == "other") {
725 new_spacing = Spacing::Other;
726 string tmpval = "0.0";
728 lyxerr << "new_value = " << tmpval << endl;
731 } else if (tmp == "default") {
732 new_spacing = Spacing::Default;
734 lyxerr << _("Unknown spacing argument: ")
735 << cmd.argument << endl;
737 if (cur_spacing != new_spacing || cur_value != new_value)
738 par.params().spacing(Spacing(new_spacing, new_value));
742 case LFUN_INSET_INSERT: {
744 InsetBase * inset = createInset(bv, cmd);
746 insertInset(cur, inset);
752 case LFUN_INSET_SETTINGS:
753 cur.inset().showInsetDialog(bv);
756 case LFUN_NEXT_INSET_TOGGLE: {
757 InsetBase * inset = cur.nextInset();
758 // this is the real function we want to invoke
759 cmd = FuncRequest(LFUN_INSET_TOGGLE);
761 // if there is an inset at cursor, see whether it
764 LCursor tmpcur = cur;
765 tmpcur.pushLeft(*inset);
766 inset->dispatch(tmpcur, cmd);
767 if (tmpcur.result().dispatched()) {
768 cur.clearSelection();
772 // if it did not work, try the underlying inset.
773 if (!cur.result().dispatched())
774 cur.inset().dispatch(cur, cmd);
778 case LFUN_SPACE_INSERT:
779 if (cur.paragraph().layout()->free_spacing)
780 insertChar(cur, ' ');
782 doInsertInset(cur, this, cmd, false, false);
785 moveCursor(cur, false);
788 case LFUN_HYPHENATION:
789 specialChar(cur, InsetSpecialChar::HYPHENATION);
792 case LFUN_LIGATURE_BREAK:
793 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
797 specialChar(cur, InsetSpecialChar::LDOTS);
800 case LFUN_END_OF_SENTENCE:
801 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
804 case LFUN_MENU_SEPARATOR:
805 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
808 case LFUN_UPCASE_WORD:
809 changeCase(cur, LyXText::text_uppercase);
812 case LFUN_LOWCASE_WORD:
813 changeCase(cur, LyXText::text_lowercase);
816 case LFUN_CAPITALIZE_WORD:
817 changeCase(cur, LyXText::text_capitalization);
820 case LFUN_TRANSPOSE_CHARS:
825 cur.message(_("Paste"));
826 lyx::cap::replaceSelection(cur);
828 #warning FIXME Check if the arg is in the domain of available selections.
830 if (isStrUnsignedInt(cmd.argument))
831 pasteSelection(cur, convert<unsigned int>(cmd.argument));
833 pasteSelection(cur, 0);
834 cur.clearSelection(); // bug 393
840 cutSelection(cur, true, true);
841 cur.message(_("Cut"));
846 cur.message(_("Copy"));
850 cur.message(convert<string>(cursorX(cur.top(), cur.boundary())) + ' '
851 + convert<string>(cursorY(cur.top(), cur.boundary())));
857 istringstream is(cmd.argument);
860 lyxerr << "SETXY: Could not parse coordinates in '"
861 << cmd.argument << std::endl;
863 setCursorFromCoordinates(cur, x, y);
868 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
870 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
877 cur.message(cur.paragraph().layout()->name());
881 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
882 << cmd.argument << endl;
884 // This is not the good solution to the empty argument
885 // problem, but it will hopefully suffice for 1.2.0.
886 // The correct solution would be to augument the
887 // function list/array with information about what
888 // functions needs arguments and their type.
889 if (cmd.argument.empty()) {
890 cur.errorMessage(_("LyX function 'layout' needs an argument."));
894 // Derive layout number from given argument (string)
895 // and current buffer's textclass (number)
896 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
897 bool hasLayout = tclass.hasLayout(cmd.argument);
898 string layout = cmd.argument;
900 // If the entry is obsolete, use the new one instead.
902 string const & obs = tclass[layout]->obsoleted_by();
908 cur.errorMessage(string(N_("Layout ")) + cmd.argument +
913 bool change_layout = (current_layout != layout);
915 if (!change_layout && cur.selection() &&
916 cur.selBegin().pit() != cur.selEnd().pit())
918 pit_type spit = cur.selBegin().pit();
919 pit_type epit = cur.selEnd().pit() + 1;
920 while (spit != epit) {
921 if (pars_[spit].layout()->name() != current_layout) {
922 change_layout = true;
930 current_layout = layout;
931 setLayout(cur, layout);
932 bv->owner()->setLayout(layout);
938 case LFUN_PASTESELECTION: {
939 cur.clearSelection();
940 string const clip = bv->getClipboard();
943 if (cmd.argument == "paragraph")
944 insertStringAsParagraphs(cur, clip);
946 insertStringAsLines(cur, clip);
952 lyx::cap::replaceSelection(cur);
953 Paragraph & par = cur.paragraph();
954 lyx::pos_type pos = cur.pos();
958 else if (cur.prevInset() && cur.prevInset()->isSpace())
961 c = par.getChar(pos - 1);
963 LyXLayout_ptr const & style = par.layout();
965 BufferParams const & bufparams = bv->buffer()->params();
966 if (!style->pass_thru
967 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
968 string arg = cmd.argument;
970 cur.insert(new InsetQuotes(c,
971 bufparams.quotes_language,
972 InsetQuotes::SingleQ));
974 cur.insert(new InsetQuotes(c,
975 bufparams.quotes_language,
976 InsetQuotes::DoubleQ));
980 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
984 case LFUN_DATE_INSERT:
985 if (cmd.argument.empty())
986 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT,
987 lyx::formatted_time(lyx::current_time())));
989 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT,
990 lyx::formatted_time(lyx::current_time(), cmd.argument)));
993 case LFUN_MOUSE_TRIPLE:
994 if (cmd.button() == mouse_button::button1) {
1000 bv->haveSelection(cur.selection());
1004 case LFUN_MOUSE_DOUBLE:
1005 if (cmd.button() == mouse_button::button1) {
1006 selectWord(cur, lyx::WHOLE_WORD_STRICT);
1008 bv->haveSelection(cur.selection());
1012 // Single-click on work area
1013 case LFUN_MOUSE_PRESS: {
1014 // Right click on a footnote flag opens float menu
1015 if (cmd.button() == mouse_button::button3) {
1016 cur.clearSelection();
1020 // Middle button press pastes if we have a selection
1021 // We do this here as if the selection was inside an inset
1022 // it could get cleared on the unlocking of the inset so
1023 // we have to check this first
1024 bool paste_internally = false;
1025 if (cmd.button() == mouse_button::button2 && cur.selection()) {
1026 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1027 paste_internally = true;
1030 // Clear the selection
1031 cur.clearSelection();
1033 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1038 // Has the cursor just left the inset?
1039 if (bv->cursor().inMathed() && !cur.inMathed())
1040 bv->cursor().inset().notifyCursorLeaves(bv->cursor());
1045 // Insert primary selection with middle mouse
1046 // if there is a local selection in the current buffer,
1048 if (cmd.button() == mouse_button::button2) {
1049 if (paste_internally)
1050 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1052 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1058 case LFUN_MOUSE_MOTION: {
1059 // Only use motion with button 1
1060 //if (cmd.button() != mouse_button::button1)
1063 // ignore motions deeper nested than the real anchor
1064 LCursor & bvcur = cur.bv().cursor();
1065 if (bvcur.anchor_.hasPart(cur)) {
1066 CursorSlice old = bvcur.top();
1068 int const wh = bv->workHeight();
1069 int const y = std::max(0, std::min(wh - 1, cmd.y));
1071 setCursorFromCoordinates(cur, cmd.x, y);
1072 cur.x_target() = cmd.x;
1077 // This is to allow jumping over large insets
1078 if (cur.top() == old) {
1085 if (cur.top() == old)
1088 // don't set anchor_
1089 bvcur.setCursor(cur);
1090 bvcur.selection() = true;
1091 //lyxerr << "MOTION: " << bv->cursor() << endl;
1099 case LFUN_MOUSE_RELEASE: {
1100 if (cmd.button() == mouse_button::button2)
1104 if (cmd.button() == mouse_button::button1)
1105 bv->haveSelection(cur.selection());
1108 bv->owner()->updateMenubar();
1109 bv->owner()->updateToolbars();
1113 case LFUN_SELFINSERT: {
1114 if (cmd.argument.empty())
1117 // Automatically delete the currently selected
1118 // text and replace it with what is being
1119 // typed in now. Depends on lyxrc settings
1120 // "auto_region_delete", which defaults to
1123 if (lyxrc.auto_region_delete) {
1124 if (cur.selection())
1125 cutSelection(cur, false, false);
1126 bv->haveSelection(false);
1129 cur.clearSelection();
1130 LyXFont const old_font = real_current_font;
1132 // Prevents language turds in new lyxtexts under non-english
1133 BufferParams const & bufparams = cur.buffer().params();
1134 Language const * lang = cur.paragraph().getParLanguage(bufparams);
1135 current_font.setLanguage(lang);
1136 real_current_font.setLanguage(lang);
1138 string::const_iterator cit = cmd.argument.begin();
1139 string::const_iterator end = cmd.argument.end();
1140 for (; cit != end; ++cit)
1141 bv->owner()->getIntl().getTransManager().
1142 TranslateAndInsert(*cit, this);
1145 moveCursor(cur, false);
1146 bv->updateScrollbar();
1151 InsetCommandParams p("url");
1152 string const data = InsetCommandMailer::params2string("url", p);
1153 bv->owner()->getDialogs().show("url", data, 0);
1157 case LFUN_HTMLURL: {
1158 InsetCommandParams p("htmlurl");
1159 string const data = InsetCommandMailer::params2string("url", p);
1160 bv->owner()->getDialogs().show("url", data, 0);
1164 case LFUN_INSERT_LABEL: {
1165 // Try to generate a valid label
1166 string const contents = cmd.argument.empty() ?
1167 cur.getPossibleLabel() : cmd.argument;
1169 InsetCommandParams p("label", contents);
1170 string const data = InsetCommandMailer::params2string("label", p);
1172 if (cmd.argument.empty()) {
1173 bv->owner()->getDialogs().show("label", data, 0);
1175 FuncRequest fr(LFUN_INSET_INSERT, data);
1183 case LFUN_INSET_LIST:
1184 case LFUN_INSET_THEOREM:
1185 case LFUN_INSET_CAPTION:
1187 case LFUN_INSERT_NOTE:
1188 case LFUN_INSERT_CHARSTYLE:
1189 case LFUN_INSERT_BOX:
1190 case LFUN_INSERT_BRANCH:
1191 case LFUN_INSERT_BIBITEM:
1192 case LFUN_INSET_ERT:
1193 case LFUN_INSET_FOOTNOTE:
1194 case LFUN_INSET_MARGINAL:
1195 case LFUN_INSET_OPTARG:
1196 case LFUN_ENVIRONMENT_INSERT:
1197 // Open the inset, and move the current selection
1199 doInsertInset(cur, this, cmd, true, true);
1203 case LFUN_TABULAR_INSERT:
1204 // if there were no arguments, just open the dialog
1205 if (doInsertInset(cur, this, cmd, false, true))
1208 bv->owner()->getDialogs().show("tabularcreate");
1212 case LFUN_INSET_FLOAT:
1213 case LFUN_INSET_WIDE_FLOAT:
1214 case LFUN_INSET_WRAP:
1215 doInsertInset(cur, this, cmd, true, true);
1217 // FIXME: the "Caption" name should not be hardcoded,
1218 // but given by the float definition.
1219 cur.dispatch(FuncRequest(LFUN_LAYOUT, "Caption"));
1222 case LFUN_INDEX_INSERT:
1223 // Just open the inset
1224 doInsertInset(cur, this, cmd, true, false);
1228 case LFUN_INDEX_PRINT:
1229 case LFUN_TOC_INSERT:
1231 case LFUN_INSERT_LINE:
1232 case LFUN_INSERT_PAGEBREAK:
1234 doInsertInset(cur, this, cmd, false, false);
1238 case LFUN_DEPTH_MIN:
1239 changeDepth(cur, DEC_DEPTH);
1242 case LFUN_DEPTH_PLUS:
1243 changeDepth(cur, INC_DEPTH);
1246 case LFUN_MATH_DISPLAY:
1247 mathDispatch(cur, cmd, true);
1250 case LFUN_MATH_IMPORT_SELECTION:
1251 case LFUN_MATH_MODE:
1252 if (cmd.argument == "on")
1253 // don't pass "on" as argument
1254 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1256 mathDispatch(cur, cmd, false);
1259 case LFUN_MATH_MACRO:
1260 if (cmd.argument.empty())
1261 cur.errorMessage(N_("Missing argument"));
1263 string s = cmd.argument;
1264 string const s1 = token(s, ' ', 1);
1265 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1266 string const s2 = token(s, ' ', 2);
1267 string const type = s2.empty() ? "newcommand" : s2;
1268 cur.insert(new MathMacroTemplate(token(s, ' ', 0), nargs, type));
1269 //cur.nextInset()->edit(cur, true);
1273 // passthrough hat and underscore outside mathed:
1274 case LFUN_SUBSCRIPT:
1275 mathDispatch(cur, FuncRequest(LFUN_SELFINSERT, "_"), false);
1277 case LFUN_SUPERSCRIPT:
1278 mathDispatch(cur, FuncRequest(LFUN_SELFINSERT, "^"), false);
1281 case LFUN_INSERT_MATH:
1282 case LFUN_INSERT_MATRIX:
1283 case LFUN_MATH_DELIM: {
1284 cur.insert(new MathHullInset("simple"));
1285 cur.dispatch(FuncRequest(LFUN_RIGHT));
1291 LyXFont font(LyXFont::ALL_IGNORE);
1292 font.setEmph(LyXFont::TOGGLE);
1293 toggleAndShow(cur, this, font);
1298 LyXFont font(LyXFont::ALL_IGNORE);
1299 font.setSeries(LyXFont::BOLD_SERIES);
1300 toggleAndShow(cur, this, font);
1305 LyXFont font(LyXFont::ALL_IGNORE);
1306 font.setNoun(LyXFont::TOGGLE);
1307 toggleAndShow(cur, this, font);
1312 LyXFont font(LyXFont::ALL_IGNORE);
1313 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
1314 toggleAndShow(cur, this, font);
1319 LyXFont font(LyXFont::ALL_IGNORE);
1320 font.setFamily(LyXFont::SANS_FAMILY);
1321 toggleAndShow(cur, this, font);
1326 LyXFont font(LyXFont::ALL_IGNORE);
1327 font.setFamily(LyXFont::ROMAN_FAMILY);
1328 toggleAndShow(cur, this, font);
1332 case LFUN_DEFAULT: {
1333 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
1334 toggleAndShow(cur, this, font);
1338 case LFUN_UNDERLINE: {
1339 LyXFont font(LyXFont::ALL_IGNORE);
1340 font.setUnderbar(LyXFont::TOGGLE);
1341 toggleAndShow(cur, this, font);
1345 case LFUN_FONT_SIZE: {
1346 LyXFont font(LyXFont::ALL_IGNORE);
1347 font.setLyXSize(cmd.argument);
1348 toggleAndShow(cur, this, font);
1352 case LFUN_LANGUAGE: {
1353 Language const * lang = languages.getLanguage(cmd.argument);
1356 LyXFont font(LyXFont::ALL_IGNORE);
1357 font.setLanguage(lang);
1358 toggleAndShow(cur, this, font);
1363 case LFUN_FREEFONT_APPLY:
1364 toggleAndShow(cur, this, freefont, toggleall);
1365 cur.message(_("Character set"));
1368 // Set the freefont using the contents of \param data dispatched from
1369 // the frontends and apply it at the current cursor location.
1370 case LFUN_FREEFONT_UPDATE: {
1373 if (bv_funcs::string2font(cmd.argument, font, toggle)) {
1376 toggleAndShow(cur, this, freefont, toggleall);
1377 cur.message(_("Character set"));
1382 case LFUN_FINISHED_LEFT:
1383 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1386 case LFUN_FINISHED_RIGHT:
1387 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1391 case LFUN_FINISHED_UP:
1392 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_UP:\n" << cur << endl;
1396 case LFUN_FINISHED_DOWN:
1397 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_DOWN:\n" << cur << endl;
1401 case LFUN_LAYOUT_PARAGRAPH: {
1403 params2string(cur.paragraph(), data);
1404 data = "show\n" + data;
1405 bv->owner()->getDialogs().show("paragraph", data);
1409 case LFUN_PARAGRAPH_UPDATE: {
1410 if (!bv->owner()->getDialogs().visible("paragraph"))
1413 params2string(cur.paragraph(), data);
1415 // Will the paragraph accept changes from the dialog?
1416 InsetBase & inset = cur.inset();
1417 bool const accept = !inset.forceDefaultParagraphs(&inset);
1419 data = "update " + convert<string>(accept) + '\n' + data;
1420 bv->owner()->getDialogs().update("paragraph", data);
1425 case LFUN_CIRCUMFLEX:
1435 case LFUN_SPECIAL_CARON:
1438 case LFUN_HUNG_UMLAUT:
1441 bv->owner()->getLyXFunc().handleKeyFunc(cmd.action);
1442 if (!cmd.argument.empty())
1443 bv->owner()->getIntl().getTransManager()
1444 .TranslateAndInsert(cmd.argument[0], this);
1447 case LFUN_FLOAT_LIST: {
1448 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1449 if (tclass.floats().typeExist(cmd.argument)) {
1450 // not quite sure if we want this...
1452 cur.clearSelection();
1453 breakParagraph(cur);
1455 if (cur.lastpos() != 0) {
1457 breakParagraph(cur);
1460 setLayout(cur, tclass.defaultLayoutName());
1461 setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1462 insertInset(cur, new InsetFloatList(cmd.argument));
1465 lyxerr << "Non-existent float type: "
1466 << cmd.argument << endl;
1471 case LFUN_ACCEPT_CHANGE: {
1476 case LFUN_REJECT_CHANGE: {
1481 case LFUN_THESAURUS_ENTRY: {
1482 string arg = cmd.argument;
1484 arg = cur.selectionAsString(false);
1486 if (arg.size() > 100 || arg.empty()) {
1487 // Get word or selection
1488 selectWordWhenUnderCursor(cur, lyx::WHOLE_WORD);
1489 arg = cur.selectionAsString(false);
1492 bv->owner()->getDialogs().show("thesaurus", arg);
1496 case LFUN_PARAGRAPH_APPLY: {
1497 // Given data, an encoding of the ParagraphParameters
1498 // generated in the Paragraph dialog, this function sets
1499 // the current paragraph appropriately.
1500 istringstream is(cmd.argument);
1503 ParagraphParameters params;
1508 params.labelWidthString(),
1510 cur.message(_("Paragraph layout set"));
1514 case LFUN_INSET_DIALOG_SHOW: {
1515 InsetBase * inset = cur.nextInset();
1517 FuncRequest fr(LFUN_INSET_DIALOG_SHOW);
1518 inset->dispatch(cur, fr);
1524 if (cur.selection()) {
1525 cur.selection() = false;
1528 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1533 lyxerr[Debug::ACTION]
1534 << BOOST_CURRENT_FUNCTION
1535 << ": Command " << cmd
1536 << " not DISPATCHED by LyXText" << endl;
1541 if (singleParUpdate)
1542 // Inserting characters does not change par height
1543 if (cur.paragraph().dim().asc == olddim.asc
1544 && cur.paragraph().dim().des == olddim.des) {
1545 // if so, update _only_ this paragraph
1546 cur.bv().update(Update::SinglePar | Update::Force);
1550 && &oldTopSlice.inset() == &cur.inset()
1551 && oldTopSlice.idx() == cur.idx()
1553 && !cur.selection())
1560 bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
1561 FuncStatus & flag) const
1563 BOOST_ASSERT(cur.text() == this);
1564 LyXFont const & font = real_current_font;
1567 switch (cmd.action) {
1569 case LFUN_DEPTH_MIN:
1570 enable = changeDepthAllowed(cur, DEC_DEPTH);
1573 case LFUN_DEPTH_PLUS:
1574 enable = changeDepthAllowed(cur, INC_DEPTH);
1577 case LFUN_INSET_OPTARG:
1578 enable = numberOfOptArgs(cur.paragraph())
1579 < cur.paragraph().layout()->optionalargs;
1583 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1586 case LFUN_INSERT_BIBITEM:
1587 enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO);
1591 // the functions which insert insets
1592 InsetBase::Code code = InsetBase::NO_CODE;
1593 switch (cmd.action) {
1594 case LFUN_DIALOG_SHOW_NEW_INSET:
1595 if (cmd.argument == "bibitem")
1596 code = InsetBase::BIBITEM_CODE;
1597 else if (cmd.argument == "bibtex")
1598 code = InsetBase::BIBTEX_CODE;
1599 else if (cmd.argument == "box")
1600 code = InsetBase::BOX_CODE;
1601 else if (cmd.argument == "branch")
1602 code = InsetBase::BRANCH_CODE;
1603 else if (cmd.argument == "citation")
1604 code = InsetBase::CITE_CODE;
1605 else if (cmd.argument == "ert")
1606 code = InsetBase::ERT_CODE;
1607 else if (cmd.argument == "external")
1608 code = InsetBase::EXTERNAL_CODE;
1609 else if (cmd.argument == "float")
1610 code = InsetBase::FLOAT_CODE;
1611 else if (cmd.argument == "graphics")
1612 code = InsetBase::GRAPHICS_CODE;
1613 else if (cmd.argument == "include")
1614 code = InsetBase::INCLUDE_CODE;
1615 else if (cmd.argument == "index")
1616 code = InsetBase::INDEX_CODE;
1617 else if (cmd.argument == "label")
1618 code = InsetBase::LABEL_CODE;
1619 else if (cmd.argument == "note")
1620 code = InsetBase::NOTE_CODE;
1621 else if (cmd.argument == "ref")
1622 code = InsetBase::REF_CODE;
1623 else if (cmd.argument == "toc")
1624 code = InsetBase::TOC_CODE;
1625 else if (cmd.argument == "url")
1626 code = InsetBase::URL_CODE;
1627 else if (cmd.argument == "vspace")
1628 code = InsetBase::VSPACE_CODE;
1629 else if (cmd.argument == "wrap")
1630 code = InsetBase::WRAP_CODE;
1633 case LFUN_INSET_ERT:
1634 code = InsetBase::ERT_CODE;
1636 case LFUN_INSET_FOOTNOTE:
1637 code = InsetBase::FOOT_CODE;
1639 case LFUN_TABULAR_INSERT:
1640 code = InsetBase::TABULAR_CODE;
1642 case LFUN_INSET_MARGINAL:
1643 code = InsetBase::MARGIN_CODE;
1645 case LFUN_INSET_FLOAT:
1646 case LFUN_INSET_WIDE_FLOAT:
1647 code = InsetBase::FLOAT_CODE;
1649 case LFUN_INSET_WRAP:
1650 code = InsetBase::WRAP_CODE;
1652 case LFUN_FLOAT_LIST:
1653 code = InsetBase::FLOAT_LIST_CODE;
1656 case LFUN_INSET_LIST:
1657 code = InsetBase::LIST_CODE;
1659 case LFUN_INSET_THEOREM:
1660 code = InsetBase::THEOREM_CODE;
1663 case LFUN_INSET_CAPTION:
1664 code = InsetBase::CAPTION_CODE;
1666 case LFUN_INSERT_NOTE:
1667 code = InsetBase::NOTE_CODE;
1669 case LFUN_INSERT_CHARSTYLE:
1670 code = InsetBase::CHARSTYLE_CODE;
1671 if (buf->params().getLyXTextClass().charstyles().empty())
1674 case LFUN_INSERT_BOX:
1675 code = InsetBase::BOX_CODE;
1677 case LFUN_INSERT_BRANCH:
1678 code = InsetBase::BRANCH_CODE;
1679 if (buf->getMasterBuffer()->params().branchlist().empty())
1682 case LFUN_INSERT_LABEL:
1683 code = InsetBase::LABEL_CODE;
1685 case LFUN_INSET_OPTARG:
1686 code = InsetBase::OPTARG_CODE;
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;
1721 case LFUN_INSET_DIALOG_SHOW: {
1722 InsetBase * inset = cur.nextInset();
1725 code = inset->lyxCode();
1726 if (!(code == InsetBase::INCLUDE_CODE
1727 || code == InsetBase::BIBTEX_CODE
1728 || code == InsetBase::FLOAT_LIST_CODE
1729 || code == InsetBase::TOC_CODE))
1738 if (code != InsetBase::NO_CODE
1739 && (cur.empty() || !cur.inset().insetAllowed(code)))
1744 case LFUN_DIALOG_SHOW_NEW_INSET:
1745 case LFUN_INSET_ERT:
1746 case LFUN_INSERT_BOX:
1747 case LFUN_INSERT_BRANCH:
1748 case LFUN_ENVIRONMENT_INSERT:
1749 case LFUN_INDEX_INSERT:
1750 case LFUN_INDEX_PRINT:
1751 case LFUN_TOC_INSERT:
1755 case LFUN_HYPHENATION:
1756 case LFUN_LIGATURE_BREAK:
1758 case LFUN_MENU_SEPARATOR:
1760 case LFUN_END_OF_SENTENCE:
1761 case LFUN_SPACE_INSERT:
1762 case LFUN_INSET_DIALOG_SHOW:
1765 case LFUN_INSET_MODIFY:
1766 // We need to disable this, because we may get called for a
1768 // InsetTabular::getStatus() -> InsetText::getStatus()
1769 // and we don't handle LFUN_INSET_MODIFY.
1774 flag.setOnOff(font.emph() == LyXFont::ON);
1778 flag.setOnOff(font.noun() == LyXFont::ON);
1782 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1786 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1790 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1794 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1797 case LFUN_DELETE_WORD_FORWARD:
1798 case LFUN_DELETE_WORD_BACKWARD:
1799 case LFUN_DELETE_LINE_FORWARD:
1800 case LFUN_WORDRIGHT:
1810 case LFUN_UP_PARAGRAPHSEL:
1811 case LFUN_DOWN_PARAGRAPHSEL:
1816 case LFUN_WORDRIGHTSEL:
1817 case LFUN_WORDLEFTSEL:
1819 case LFUN_UP_PARAGRAPH:
1820 case LFUN_DOWN_PARAGRAPH:
1825 case LFUN_BREAKLINE:
1827 case LFUN_DELETE_SKIP:
1828 case LFUN_BACKSPACE:
1829 case LFUN_BACKSPACE_SKIP:
1830 case LFUN_BREAKPARAGRAPH:
1831 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1832 case LFUN_BREAKPARAGRAPH_SKIP:
1833 case LFUN_PARAGRAPH_SPACING:
1834 case LFUN_INSET_INSERT:
1835 case LFUN_NEXT_INSET_TOGGLE:
1836 case LFUN_UPCASE_WORD:
1837 case LFUN_LOWCASE_WORD:
1838 case LFUN_CAPITALIZE_WORD:
1839 case LFUN_TRANSPOSE_CHARS:
1846 case LFUN_GETLAYOUT:
1848 case LFUN_PASTESELECTION:
1849 case LFUN_DATE_INSERT:
1850 case LFUN_SELFINSERT:
1851 case LFUN_INSERT_LABEL:
1852 case LFUN_INSERT_NOTE:
1853 case LFUN_INSERT_CHARSTYLE:
1854 case LFUN_INSET_FLOAT:
1855 case LFUN_INSET_FOOTNOTE:
1856 case LFUN_INSET_MARGINAL:
1857 case LFUN_INSET_WIDE_FLOAT:
1858 case LFUN_INSET_WRAP:
1859 case LFUN_TABULAR_INSERT:
1860 case LFUN_INSERT_LINE:
1861 case LFUN_INSERT_PAGEBREAK:
1862 case LFUN_MATH_DISPLAY:
1863 case LFUN_MATH_IMPORT_SELECTION:
1864 case LFUN_MATH_MODE:
1865 case LFUN_MATH_MACRO:
1866 case LFUN_INSERT_MATH:
1867 case LFUN_INSERT_MATRIX:
1868 case LFUN_MATH_DELIM:
1869 case LFUN_SUBSCRIPT:
1870 case LFUN_SUPERSCRIPT:
1872 case LFUN_UNDERLINE:
1873 case LFUN_FONT_SIZE:
1875 case LFUN_FREEFONT_APPLY:
1876 case LFUN_FREEFONT_UPDATE:
1877 case LFUN_LAYOUT_PARAGRAPH:
1878 case LFUN_PARAGRAPH_UPDATE:
1880 case LFUN_CIRCUMFLEX:
1890 case LFUN_SPECIAL_CARON:
1893 case LFUN_HUNG_UMLAUT:
1896 case LFUN_FLOAT_LIST:
1897 case LFUN_ACCEPT_CHANGE:
1898 case LFUN_REJECT_CHANGE:
1899 case LFUN_THESAURUS_ENTRY:
1900 case LFUN_PARAGRAPH_APPLY:
1903 case LFUN_BEGINNINGBUF:
1904 case LFUN_BEGINNINGBUFSEL:
1905 case LFUN_ENDBUFSEL:
1906 // these are handled in our dispatch()
1913 flag.enabled(enable);