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;
86 extern string current_layout;
92 LyXFont freefont(LyXFont::ALL_IGNORE);
93 bool toggleall = false;
96 void toggleAndShow(LCursor & cur, LyXText * text,
97 LyXFont const & font, bool toggleall = true)
99 text->toggleFree(cur, font, toggleall);
101 if (font.language() != ignore_language ||
102 font.number() != LyXFont::IGNORE) {
103 Paragraph & par = cur.paragraph();
104 text->bidi.computeTables(par, cur.buffer(), cur.textRow());
105 if (cur.boundary() !=
106 text->bidi.isBoundary(cur.buffer(), par,
108 text->real_current_font))
109 text->setCursor(cur, cur.pit(), cur.pos(),
110 false, !cur.boundary());
115 void moveCursor(LCursor & cur, bool selecting)
117 if (selecting || cur.mark())
119 if (!cur.selection())
120 cur.bv().haveSelection(false);
121 cur.bv().switchKeyMap();
125 void finishChange(LCursor & cur, bool selecting)
128 moveCursor(cur, selecting);
132 void mathDispatch(LCursor & cur, FuncRequest const & cmd, bool display)
135 string sel = cur.selectionAsString(false);
136 //lyxerr << "selection is: '" << sel << "'" << endl;
139 const int old_pos = cur.pos();
140 cur.insert(new MathHullInset("simple"));
141 BOOST_ASSERT(old_pos == cur.pos());
142 cur.nextInset()->edit(cur, true);
143 // don't do that also for LFUN_MATH_MODE
144 // unless you want end up with always changing
145 // to mathrm when opening an inlined inset --
146 // I really hate "LyXfunc overloading"...
148 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
149 // Avoid an unnecessary undo step if cmd.argument
151 if (!cmd.argument.empty())
152 cur.dispatch(FuncRequest(LFUN_INSERT_MATH,
155 // create a macro if we see "\\newcommand"
156 // somewhere, and an ordinary formula
158 cutSelection(cur, true, true);
159 if (sel.find("\\newcommand") == string::npos
160 && sel.find("\\def") == string::npos)
162 cur.insert(new MathHullInset("simple"));
163 cur.dispatch(FuncRequest(LFUN_RIGHT));
164 cur.dispatch(FuncRequest(LFUN_INSERT_MATH, sel));
166 istringstream is(sel);
167 cur.insert(new MathMacroTemplate(is));
170 cur.message(N_("Math editor mode"));
179 string const freefont2string()
182 if (font2string(freefont, toggleall, data))
189 bool LyXText::cursorPrevious(LCursor & cur)
191 pos_type cpos = cur.pos();
192 lyx::pit_type cpar = cur.pit();
194 int x = cur.x_target();
196 bool updated = setCursorFromCoordinates(cur, x, 0);
199 updated |= cursorUp(cur);
201 if (cpar == cur.pit() && cpos == cur.pos()) {
202 // we have a row which is taller than the workarea. The
203 // simplest solution is to move to the previous row instead.
204 updated |= cursorUp(cur);
207 cur.bv().updateScrollbar();
213 bool LyXText::cursorNext(LCursor & cur)
215 pos_type cpos = cur.pos();
216 lyx::pit_type cpar = cur.pit();
218 int x = cur.x_target();
219 bool updated = setCursorFromCoordinates(cur, x, cur.bv().workHeight() - 1);
222 updated |= cursorDown(cur);
224 if (cpar == cur.pit() && cpos == cur.pos()) {
225 // we have a row which is taller than the workarea. The
226 // simplest solution is to move to the next row instead.
227 updated |= cursorDown(cur);
230 cur.bv().updateScrollbar();
238 void specialChar(LCursor & cur, InsetSpecialChar::Kind kind)
240 lyx::cap::replaceSelection(cur);
241 cur.insert(new InsetSpecialChar(kind));
246 bool doInsertInset(LCursor & cur, LyXText * text,
247 FuncRequest const & cmd, bool edit, bool pastesel)
249 InsetBase * inset = createInset(&cur.bv(), cmd);
255 if (cur.selection()) {
256 cur.bv().owner()->dispatch(FuncRequest(LFUN_CUT));
259 text->insertInset(cur, inset);
262 inset->edit(cur, true);
264 if (gotsel && pastesel)
265 cur.bv().owner()->dispatch(FuncRequest(LFUN_PASTE));
270 void update(LCursor & cur)
272 //we don't call update(true, false) directly to save a metrics call
273 if (cur.bv().fitCursor())
274 cur.bv().update(Update::Force);
281 void LyXText::number(LCursor & cur)
283 LyXFont font(LyXFont::ALL_IGNORE);
284 font.setNumber(LyXFont::TOGGLE);
285 toggleAndShow(cur, this, font);
289 bool LyXText::isRTL(Paragraph const & par) const
291 return par.isRightToLeftPar(bv()->buffer()->params());
295 void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
297 lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
299 BOOST_ASSERT(cur.text() == this);
300 BufferView * bv = &cur.bv();
301 CursorSlice oldTopSlice = cur.top();
302 bool oldBoundary = cur.boundary();
303 bool sel = cur.selection();
304 // Signals that, even if needsUpdate == false, an update of the
305 // cursor paragraph is required
306 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
307 LyXAction::SingleParUpdate);
308 // Signals that a full-screen update is required
309 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
310 LyXAction::NoUpdate) || singleParUpdate);
311 // Remember the old paragraph metric
312 Dimension olddim = cur.paragraph().dim();
314 switch (cmd.action) {
316 case LFUN_APPENDIX: {
317 Paragraph & par = cur.paragraph();
318 bool start = !par.params().startOfAppendix();
321 #warning The code below only makes sense at top level.
322 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
324 // ensure that we have only one start_of_appendix in this document
325 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
326 if (pars_[tmp].params().startOfAppendix()) {
328 pars_[tmp].params().startOfAppendix(false);
334 par.params().startOfAppendix(start);
336 // we can set the refreshing parameters now
337 updateCounters(cur.buffer());
341 case LFUN_DELETE_WORD_FORWARD:
342 cur.clearSelection();
343 deleteWordForward(cur);
344 finishChange(cur, false);
347 case LFUN_DELETE_WORD_BACKWARD:
348 cur.clearSelection();
349 deleteWordBackward(cur);
350 finishChange(cur, false);
353 case LFUN_DELETE_LINE_FORWARD:
354 cur.clearSelection();
355 deleteLineForward(cur);
356 finishChange(cur, false);
361 cur.clearSelection();
362 if (isRTL(cur.paragraph()))
363 needsUpdate = cursorLeftOneWord(cur);
365 needsUpdate = cursorRightOneWord(cur);
366 finishChange(cur, false);
371 cur.clearSelection();
372 if (isRTL(cur.paragraph()))
373 needsUpdate = cursorRightOneWord(cur);
375 needsUpdate = cursorLeftOneWord(cur);
376 finishChange(cur, false);
379 case LFUN_BEGINNINGBUF:
380 if (cur.depth() == 1) {
382 cur.clearSelection();
384 finishChange(cur, false);
390 case LFUN_BEGINNINGBUFSEL:
391 if (cur.depth() == 1) {
392 if (!cur.selection())
395 finishChange(cur, true);
402 if (cur.depth() == 1) {
404 cur.clearSelection();
406 finishChange(cur, false);
413 if (cur.depth() == 1) {
414 if (!cur.selection())
417 finishChange(cur, true);
425 //lyxerr << BOOST_CURRENT_FUNCTION
426 // << " LFUN_RIGHT[SEL]:\n" << cur << endl;
427 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
428 if (isRTL(cur.paragraph()))
429 needsUpdate = cursorLeft(cur);
431 needsUpdate = cursorRight(cur);
433 if (!needsUpdate && oldTopSlice == cur.top()
434 && cur.boundary() == oldBoundary) {
436 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
442 //lyxerr << "handle LFUN_LEFT[SEL]:\n" << cur << endl;
443 cur.selHandle(cmd.action == LFUN_LEFTSEL);
444 if (isRTL(cur.paragraph()))
445 needsUpdate = cursorRight(cur);
447 needsUpdate = cursorLeft(cur);
449 if (!needsUpdate && oldTopSlice == cur.top()
450 && cur.boundary() == oldBoundary) {
452 cmd = FuncRequest(LFUN_FINISHED_LEFT);
459 //lyxerr << "handle LFUN_UP[SEL]:\n" << cur << endl;
460 cur.selHandle(cmd.action == LFUN_UPSEL);
462 needsUpdate = cursorUp(cur);
463 if (!needsUpdate && oldTopSlice == cur.top()
464 && cur.boundary() == oldBoundary) {
466 cmd = FuncRequest(LFUN_FINISHED_UP);
473 //lyxerr << "handle LFUN_DOWN[SEL]:\n" << cur << endl;
474 cur.selHandle(cmd.action == LFUN_DOWNSEL);
475 needsUpdate = cursorDown(cur);
476 if (!needsUpdate && oldTopSlice == cur.top() &&
477 cur.boundary() == oldBoundary)
480 cmd = FuncRequest(LFUN_FINISHED_DOWN);
484 case LFUN_UP_PARAGRAPH:
486 cur.clearSelection();
487 needsUpdate = cursorUpParagraph(cur);
488 finishChange(cur, false);
491 case LFUN_UP_PARAGRAPHSEL:
492 if (!cur.selection())
494 cursorUpParagraph(cur);
495 finishChange(cur, true);
498 case LFUN_DOWN_PARAGRAPH:
500 cur.clearSelection();
501 needsUpdate = cursorDownParagraph(cur);
502 finishChange(cur, false);
505 case LFUN_DOWN_PARAGRAPHSEL:
506 if (!cur.selection())
508 cursorDownParagraph(cur);
509 finishChange(cur, true);
514 if (!cur.selection())
517 finishChange(cur, true);
522 if (!cur.selection())
525 finishChange(cur, true);
530 if (!cur.selection())
533 finishChange(cur, true);
538 if (!cur.selection())
541 finishChange(cur, true);
544 case LFUN_WORDRIGHTSEL:
545 if (!cur.selection())
547 if (isRTL(cur.paragraph()))
548 cursorLeftOneWord(cur);
550 cursorRightOneWord(cur);
551 finishChange(cur, true);
554 case LFUN_WORDLEFTSEL:
555 if (!cur.selection())
557 if (isRTL(cur.paragraph()))
558 cursorRightOneWord(cur);
560 cursorLeftOneWord(cur);
561 finishChange(cur, true);
565 selectWord(cur, lyx::WHOLE_WORD);
566 finishChange(cur, true);
573 cur.clearSelection();
574 finishChange(cur, false);
575 if (cur.pit() == 0 && cur.textRow().pos() == 0) {
577 cmd = FuncRequest(LFUN_FINISHED_UP);
579 needsUpdate = cursorPrevious(cur);
586 cur.clearSelection();
587 finishChange(cur, false);
588 if (cur.pit() == cur.lastpit()
589 && cur.textRow().endpos() == cur.lastpos()) {
591 cmd = FuncRequest(LFUN_FINISHED_DOWN);
593 needsUpdate = cursorNext(cur);
599 cur.clearSelection();
601 finishChange(cur, false);
606 cur.clearSelection();
608 finishChange(cur, false);
611 case LFUN_BREAKLINE: {
612 // Not allowed by LaTeX (labels or empty par)
613 if (cur.pos() > cur.paragraph().beginOfBody()) {
614 lyx::cap::replaceSelection(cur);
615 cur.insert(new InsetNewline);
617 moveCursor(cur, false);
623 if (!cur.selection()) {
626 // It is possible to make it a lot faster still
627 // just comment out the line below...
629 cutSelection(cur, true, false);
631 moveCursor(cur, false);
634 case LFUN_DELETE_SKIP:
635 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
636 if (!cur.selection()) {
637 if (cur.pos() == cur.lastpos()) {
644 cutSelection(cur, true, false);
650 if (!cur.selection()) {
651 if (bv->owner()->getIntl().getTransManager().backspace()) {
654 // It is possible to make it a lot faster still
655 // just comment out the line below...
658 cutSelection(cur, true, false);
663 case LFUN_BACKSPACE_SKIP:
664 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
665 if (!cur.selection()) {
669 //CursorSlice cur = cursor();
673 cutSelection(cur, true, false);
677 case LFUN_BREAKPARAGRAPH:
678 lyx::cap::replaceSelection(cur);
679 breakParagraph(cur, 0);
684 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
685 lyx::cap::replaceSelection(cur);
686 breakParagraph(cur, 1);
691 case LFUN_BREAKPARAGRAPH_SKIP: {
692 // When at the beginning of a paragraph, remove
693 // indentation. Otherwise, do the same as LFUN_BREAKPARAGRAPH.
694 lyx::cap::replaceSelection(cur);
696 cur.paragraph().params().labelWidthString(string());
698 breakParagraph(cur, 0);
704 case LFUN_PARAGRAPH_SPACING: {
705 Paragraph & par = cur.paragraph();
706 Spacing::Space cur_spacing = par.params().spacing().getSpace();
707 string cur_value = "1.0";
708 if (cur_spacing == Spacing::Other)
709 cur_value = par.params().spacing().getValueAsString();
711 istringstream is(cmd.argument);
714 Spacing::Space new_spacing = cur_spacing;
715 string new_value = cur_value;
717 lyxerr << "Missing argument to `paragraph-spacing'"
719 } else if (tmp == "single") {
720 new_spacing = Spacing::Single;
721 } else if (tmp == "onehalf") {
722 new_spacing = Spacing::Onehalf;
723 } else if (tmp == "double") {
724 new_spacing = Spacing::Double;
725 } else if (tmp == "other") {
726 new_spacing = Spacing::Other;
727 string tmpval = "0.0";
729 lyxerr << "new_value = " << tmpval << endl;
732 } else if (tmp == "default") {
733 new_spacing = Spacing::Default;
735 lyxerr << _("Unknown spacing argument: ")
736 << cmd.argument << endl;
738 if (cur_spacing != new_spacing || cur_value != new_value)
739 par.params().spacing(Spacing(new_spacing, new_value));
743 case LFUN_INSET_INSERT: {
745 InsetBase * inset = createInset(bv, cmd);
747 insertInset(cur, inset);
753 case LFUN_INSET_SETTINGS:
754 cur.inset().showInsetDialog(bv);
757 case LFUN_NEXT_INSET_TOGGLE: {
758 InsetBase * inset = cur.nextInset();
759 // this is the real function we want to invoke
760 cmd = FuncRequest(LFUN_INSET_TOGGLE);
762 // if there is an inset at cursor, see whether it
765 LCursor tmpcur = cur;
766 tmpcur.pushLeft(*inset);
767 inset->dispatch(tmpcur, cmd);
768 if (tmpcur.result().dispatched()) {
769 cur.clearSelection();
773 // if it did not work, try the underlying inset.
774 if (!cur.result().dispatched())
775 cur.inset().dispatch(cur, cmd);
779 case LFUN_SPACE_INSERT:
780 if (cur.paragraph().layout()->free_spacing)
781 insertChar(cur, ' ');
783 doInsertInset(cur, this, cmd, false, false);
786 moveCursor(cur, false);
789 case LFUN_HYPHENATION:
790 specialChar(cur, InsetSpecialChar::HYPHENATION);
793 case LFUN_LIGATURE_BREAK:
794 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
798 specialChar(cur, InsetSpecialChar::LDOTS);
801 case LFUN_END_OF_SENTENCE:
802 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
805 case LFUN_MENU_SEPARATOR:
806 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
809 case LFUN_UPCASE_WORD:
810 changeCase(cur, LyXText::text_uppercase);
813 case LFUN_LOWCASE_WORD:
814 changeCase(cur, LyXText::text_lowercase);
817 case LFUN_CAPITALIZE_WORD:
818 changeCase(cur, LyXText::text_capitalization);
821 case LFUN_TRANSPOSE_CHARS:
826 cur.message(_("Paste"));
827 lyx::cap::replaceSelection(cur);
829 #warning FIXME Check if the arg is in the domain of available selections.
831 if (isStrUnsignedInt(cmd.argument))
832 pasteSelection(cur, convert<unsigned int>(cmd.argument));
834 pasteSelection(cur, 0);
835 cur.clearSelection(); // bug 393
841 cutSelection(cur, true, true);
842 cur.message(_("Cut"));
847 cur.message(_("Copy"));
851 cur.message(convert<string>(cursorX(cur.top(), cur.boundary())) + ' '
852 + convert<string>(cursorY(cur.top(), cur.boundary())));
858 istringstream is(cmd.argument);
861 lyxerr << "SETXY: Could not parse coordinates in '"
862 << cmd.argument << std::endl;
864 setCursorFromCoordinates(cur, x, y);
869 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
871 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
878 cur.message(cur.paragraph().layout()->name());
882 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
883 << cmd.argument << endl;
885 // This is not the good solution to the empty argument
886 // problem, but it will hopefully suffice for 1.2.0.
887 // The correct solution would be to augument the
888 // function list/array with information about what
889 // functions needs arguments and their type.
890 if (cmd.argument.empty()) {
891 cur.errorMessage(_("LyX function 'layout' needs an argument."));
895 // Derive layout number from given argument (string)
896 // and current buffer's textclass (number)
897 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
898 bool hasLayout = tclass.hasLayout(cmd.argument);
899 string layout = cmd.argument;
901 // If the entry is obsolete, use the new one instead.
903 string const & obs = tclass[layout]->obsoleted_by();
909 cur.errorMessage(string(N_("Layout ")) + cmd.argument +
914 bool change_layout = (current_layout != layout);
916 if (!change_layout && cur.selection() &&
917 cur.selBegin().pit() != cur.selEnd().pit())
919 pit_type spit = cur.selBegin().pit();
920 pit_type epit = cur.selEnd().pit() + 1;
921 while (spit != epit) {
922 if (pars_[spit].layout()->name() != current_layout) {
923 change_layout = true;
931 current_layout = layout;
932 setLayout(cur, layout);
933 bv->owner()->setLayout(layout);
939 case LFUN_PASTESELECTION: {
940 cur.clearSelection();
941 string const clip = bv->getClipboard();
944 if (cmd.argument == "paragraph")
945 insertStringAsParagraphs(cur, clip);
947 insertStringAsLines(cur, clip);
953 lyx::cap::replaceSelection(cur);
954 Paragraph & par = cur.paragraph();
955 lyx::pos_type pos = cur.pos();
959 else if (cur.prevInset() && cur.prevInset()->isSpace())
962 c = par.getChar(pos - 1);
964 LyXLayout_ptr const & style = par.layout();
966 BufferParams const & bufparams = bv->buffer()->params();
967 if (!style->pass_thru
968 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
969 string arg = cmd.argument;
971 cur.insert(new InsetQuotes(c,
972 bufparams.quotes_language,
973 InsetQuotes::SingleQ));
975 cur.insert(new InsetQuotes(c,
976 bufparams.quotes_language,
977 InsetQuotes::DoubleQ));
981 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
985 case LFUN_DATE_INSERT:
986 if (cmd.argument.empty())
987 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT,
988 lyx::formatted_time(lyx::current_time())));
990 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT,
991 lyx::formatted_time(lyx::current_time(), cmd.argument)));
994 case LFUN_MOUSE_TRIPLE:
995 if (cmd.button() == mouse_button::button1) {
1001 bv->haveSelection(cur.selection());
1005 case LFUN_MOUSE_DOUBLE:
1006 if (cmd.button() == mouse_button::button1) {
1007 selectWord(cur, lyx::WHOLE_WORD_STRICT);
1009 bv->haveSelection(cur.selection());
1013 // Single-click on work area
1014 case LFUN_MOUSE_PRESS: {
1015 // Right click on a footnote flag opens float menu
1016 if (cmd.button() == mouse_button::button3) {
1017 cur.clearSelection();
1021 // Middle button press pastes if we have a selection
1022 // We do this here as if the selection was inside an inset
1023 // it could get cleared on the unlocking of the inset so
1024 // we have to check this first
1025 bool paste_internally = false;
1026 if (cmd.button() == mouse_button::button2 && cur.selection()) {
1027 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1028 paste_internally = true;
1031 // Clear the selection
1032 cur.clearSelection();
1034 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1039 // Has the cursor just left the inset?
1040 if (bv->cursor().inMathed() && !cur.inMathed())
1041 bv->cursor().inset().notifyCursorLeaves(bv->cursor());
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 // Prevents language turds in new lyxtexts under non-english
1134 BufferParams const & bufparams = cur.buffer().params();
1135 Language const * lang = cur.paragraph().getParLanguage(bufparams);
1136 current_font.setLanguage(lang);
1137 real_current_font.setLanguage(lang);
1139 string::const_iterator cit = cmd.argument.begin();
1140 string::const_iterator end = cmd.argument.end();
1141 for (; cit != end; ++cit)
1142 bv->owner()->getIntl().getTransManager().
1143 TranslateAndInsert(*cit, this);
1146 moveCursor(cur, false);
1147 bv->updateScrollbar();
1152 InsetCommandParams p("url");
1153 string const data = InsetCommandMailer::params2string("url", p);
1154 bv->owner()->getDialogs().show("url", data, 0);
1158 case LFUN_HTMLURL: {
1159 InsetCommandParams p("htmlurl");
1160 string const data = InsetCommandMailer::params2string("url", p);
1161 bv->owner()->getDialogs().show("url", data, 0);
1165 case LFUN_INSERT_LABEL: {
1166 // Try to generate a valid label
1167 string const contents = cmd.argument.empty() ?
1168 cur.getPossibleLabel() : cmd.argument;
1170 InsetCommandParams p("label", contents);
1171 string const data = InsetCommandMailer::params2string("label", p);
1173 if (cmd.argument.empty()) {
1174 bv->owner()->getDialogs().show("label", data, 0);
1176 FuncRequest fr(LFUN_INSET_INSERT, data);
1184 case LFUN_INSET_LIST:
1185 case LFUN_INSET_THEOREM:
1186 case LFUN_INSET_CAPTION:
1188 case LFUN_INSERT_NOTE:
1189 case LFUN_INSERT_CHARSTYLE:
1190 case LFUN_INSERT_BOX:
1191 case LFUN_INSERT_BRANCH:
1192 case LFUN_INSERT_BIBITEM:
1193 case LFUN_INSET_ERT:
1194 case LFUN_INSET_FOOTNOTE:
1195 case LFUN_INSET_MARGINAL:
1196 case LFUN_INSET_OPTARG:
1197 case LFUN_ENVIRONMENT_INSERT:
1198 // Open the inset, and move the current selection
1200 doInsertInset(cur, this, cmd, true, true);
1204 case LFUN_TABULAR_INSERT:
1205 // if there were no arguments, just open the dialog
1206 if (doInsertInset(cur, this, cmd, false, true))
1209 bv->owner()->getDialogs().show("tabularcreate");
1213 case LFUN_INSET_FLOAT:
1214 case LFUN_INSET_WIDE_FLOAT:
1215 case LFUN_INSET_WRAP:
1216 doInsertInset(cur, this, cmd, true, true);
1218 // FIXME: the "Caption" name should not be hardcoded,
1219 // but given by the float definition.
1220 cur.dispatch(FuncRequest(LFUN_LAYOUT, "Caption"));
1223 case LFUN_INDEX_INSERT: {
1224 InsetBase * inset = createInset(&cur.bv(), cmd);
1229 cur.clearSelection();
1230 insertInset(cur, inset);
1231 inset->edit(cur, true);
1236 case LFUN_INDEX_PRINT:
1237 case LFUN_TOC_INSERT:
1239 case LFUN_INSERT_LINE:
1240 case LFUN_INSERT_PAGEBREAK:
1242 doInsertInset(cur, this, cmd, false, false);
1246 case LFUN_DEPTH_MIN:
1247 changeDepth(cur, DEC_DEPTH);
1250 case LFUN_DEPTH_PLUS:
1251 changeDepth(cur, INC_DEPTH);
1254 case LFUN_MATH_DISPLAY:
1255 mathDispatch(cur, cmd, true);
1258 case LFUN_MATH_IMPORT_SELECTION:
1259 case LFUN_MATH_MODE:
1260 if (cmd.argument == "on")
1261 // don't pass "on" as argument
1262 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1264 mathDispatch(cur, cmd, false);
1267 case LFUN_MATH_MACRO:
1268 if (cmd.argument.empty())
1269 cur.errorMessage(N_("Missing argument"));
1271 string s = cmd.argument;
1272 string const s1 = token(s, ' ', 1);
1273 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1274 string const s2 = token(s, ' ', 2);
1275 string const type = s2.empty() ? "newcommand" : s2;
1276 cur.insert(new MathMacroTemplate(token(s, ' ', 0), nargs, type));
1277 //cur.nextInset()->edit(cur, true);
1281 // passthrough hat and underscore outside mathed:
1282 case LFUN_SUBSCRIPT:
1283 mathDispatch(cur, FuncRequest(LFUN_SELFINSERT, "_"), false);
1285 case LFUN_SUPERSCRIPT:
1286 mathDispatch(cur, FuncRequest(LFUN_SELFINSERT, "^"), false);
1289 case LFUN_INSERT_MATH:
1290 case LFUN_INSERT_MATRIX:
1291 case LFUN_MATH_DELIM: {
1292 cur.insert(new MathHullInset("simple"));
1293 cur.dispatch(FuncRequest(LFUN_RIGHT));
1299 LyXFont font(LyXFont::ALL_IGNORE);
1300 font.setEmph(LyXFont::TOGGLE);
1301 toggleAndShow(cur, this, font);
1306 LyXFont font(LyXFont::ALL_IGNORE);
1307 font.setSeries(LyXFont::BOLD_SERIES);
1308 toggleAndShow(cur, this, font);
1313 LyXFont font(LyXFont::ALL_IGNORE);
1314 font.setNoun(LyXFont::TOGGLE);
1315 toggleAndShow(cur, this, font);
1320 LyXFont font(LyXFont::ALL_IGNORE);
1321 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
1322 toggleAndShow(cur, this, font);
1327 LyXFont font(LyXFont::ALL_IGNORE);
1328 font.setFamily(LyXFont::SANS_FAMILY);
1329 toggleAndShow(cur, this, font);
1334 LyXFont font(LyXFont::ALL_IGNORE);
1335 font.setFamily(LyXFont::ROMAN_FAMILY);
1336 toggleAndShow(cur, this, font);
1340 case LFUN_DEFAULT: {
1341 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
1342 toggleAndShow(cur, this, font);
1346 case LFUN_UNDERLINE: {
1347 LyXFont font(LyXFont::ALL_IGNORE);
1348 font.setUnderbar(LyXFont::TOGGLE);
1349 toggleAndShow(cur, this, font);
1353 case LFUN_FONT_SIZE: {
1354 LyXFont font(LyXFont::ALL_IGNORE);
1355 font.setLyXSize(cmd.argument);
1356 toggleAndShow(cur, this, font);
1360 case LFUN_LANGUAGE: {
1361 Language const * lang = languages.getLanguage(cmd.argument);
1364 LyXFont font(LyXFont::ALL_IGNORE);
1365 font.setLanguage(lang);
1366 toggleAndShow(cur, this, font);
1371 case LFUN_FREEFONT_APPLY:
1372 toggleAndShow(cur, this, freefont, toggleall);
1373 cur.message(_("Character set"));
1376 // Set the freefont using the contents of \param data dispatched from
1377 // the frontends and apply it at the current cursor location.
1378 case LFUN_FREEFONT_UPDATE: {
1381 if (bv_funcs::string2font(cmd.argument, font, toggle)) {
1384 toggleAndShow(cur, this, freefont, toggleall);
1385 cur.message(_("Character set"));
1390 case LFUN_FINISHED_LEFT:
1391 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1394 case LFUN_FINISHED_RIGHT:
1395 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1399 case LFUN_FINISHED_UP:
1400 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_UP:\n" << cur << endl;
1404 case LFUN_FINISHED_DOWN:
1405 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_DOWN:\n" << cur << endl;
1409 case LFUN_LAYOUT_PARAGRAPH: {
1411 params2string(cur.paragraph(), data);
1412 data = "show\n" + data;
1413 bv->owner()->getDialogs().show("paragraph", data);
1417 case LFUN_PARAGRAPH_UPDATE: {
1418 if (!bv->owner()->getDialogs().visible("paragraph"))
1421 params2string(cur.paragraph(), data);
1423 // Will the paragraph accept changes from the dialog?
1424 bool const accept = !cur.inset().forceDefaultParagraphs(cur.idx());
1426 data = "update " + convert<string>(accept) + '\n' + data;
1427 bv->owner()->getDialogs().update("paragraph", data);
1432 case LFUN_CIRCUMFLEX:
1442 case LFUN_SPECIAL_CARON:
1445 case LFUN_HUNG_UMLAUT:
1448 bv->owner()->getLyXFunc().handleKeyFunc(cmd.action);
1449 if (!cmd.argument.empty())
1450 bv->owner()->getIntl().getTransManager()
1451 .TranslateAndInsert(cmd.argument[0], this);
1454 case LFUN_FLOAT_LIST: {
1455 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1456 if (tclass.floats().typeExist(cmd.argument)) {
1457 // not quite sure if we want this...
1459 cur.clearSelection();
1460 breakParagraph(cur);
1462 if (cur.lastpos() != 0) {
1464 breakParagraph(cur);
1467 setLayout(cur, tclass.defaultLayoutName());
1468 setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1469 insertInset(cur, new InsetFloatList(cmd.argument));
1472 lyxerr << "Non-existent float type: "
1473 << cmd.argument << endl;
1478 case LFUN_ACCEPT_CHANGE: {
1483 case LFUN_REJECT_CHANGE: {
1488 case LFUN_THESAURUS_ENTRY: {
1489 string arg = cmd.argument;
1491 arg = cur.selectionAsString(false);
1493 if (arg.size() > 100 || arg.empty()) {
1494 // Get word or selection
1495 selectWordWhenUnderCursor(cur, lyx::WHOLE_WORD);
1496 arg = cur.selectionAsString(false);
1499 bv->owner()->getDialogs().show("thesaurus", arg);
1503 case LFUN_PARAGRAPH_APPLY: {
1504 // Given data, an encoding of the ParagraphParameters
1505 // generated in the Paragraph dialog, this function sets
1506 // the current paragraph appropriately.
1507 istringstream is(cmd.argument);
1510 ParagraphParameters params;
1515 params.labelWidthString(),
1517 cur.message(_("Paragraph layout set"));
1521 case LFUN_INSET_DIALOG_SHOW: {
1522 InsetBase * inset = cur.nextInset();
1524 FuncRequest fr(LFUN_INSET_DIALOG_SHOW);
1525 inset->dispatch(cur, fr);
1531 if (cur.selection()) {
1532 cur.selection() = false;
1535 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1540 lyxerr[Debug::ACTION]
1541 << BOOST_CURRENT_FUNCTION
1542 << ": Command " << cmd
1543 << " not DISPATCHED by LyXText" << endl;
1548 if (singleParUpdate)
1549 // Inserting characters does not change par height
1550 if (cur.paragraph().dim().asc == olddim.asc
1551 && cur.paragraph().dim().des == olddim.des) {
1552 // if so, update _only_ this paragraph
1553 cur.bv().update(Update::SinglePar | Update::Force);
1557 && &oldTopSlice.inset() == &cur.inset()
1558 && oldTopSlice.idx() == cur.idx()
1560 && !cur.selection())
1567 bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
1568 FuncStatus & flag) const
1570 BOOST_ASSERT(cur.text() == this);
1572 LyXFont const & font = real_current_font;
1574 InsetBase::Code code = InsetBase::NO_CODE;
1576 switch (cmd.action) {
1578 case LFUN_DEPTH_MIN:
1579 enable = changeDepthAllowed(cur, DEC_DEPTH);
1582 case LFUN_DEPTH_PLUS:
1583 enable = changeDepthAllowed(cur, INC_DEPTH);
1587 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1590 case LFUN_INSERT_BIBITEM:
1591 enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO);
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 (cur.buffer().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 (cur.buffer().getMasterBuffer()->params().branchlist().empty())
1682 case LFUN_INSERT_LABEL:
1683 code = InsetBase::LABEL_CODE;
1685 case LFUN_INSET_OPTARG:
1686 code = InsetBase::OPTARG_CODE;
1687 enable = numberOfOptArgs(cur.paragraph())
1688 < cur.paragraph().layout()->optionalargs;
1690 case LFUN_ENVIRONMENT_INSERT:
1691 code = InsetBase::BOX_CODE;
1693 case LFUN_INDEX_INSERT:
1694 code = InsetBase::INDEX_CODE;
1696 case LFUN_INDEX_PRINT:
1697 code = InsetBase::INDEX_PRINT_CODE;
1699 case LFUN_TOC_INSERT:
1700 code = InsetBase::TOC_CODE;
1704 code = InsetBase::URL_CODE;
1707 // always allow this, since we will inset a raw quote
1708 // if an inset is not allowed.
1710 case LFUN_HYPHENATION:
1711 case LFUN_LIGATURE_BREAK:
1713 case LFUN_MENU_SEPARATOR:
1715 case LFUN_END_OF_SENTENCE:
1716 code = InsetBase::SPECIALCHAR_CODE;
1718 case LFUN_SPACE_INSERT:
1719 // slight hack: we know this is allowed in math mode
1721 code = InsetBase::SPACE_CODE;
1724 #ifdef WITH_WARNINGS
1725 #warning This LFUN is not used anymore and should be nuked (JMarc 29/10/2005)
1728 case LFUN_INSET_DIALOG_SHOW: {
1729 InsetBase * inset = cur.nextInset();
1732 code = inset->lyxCode();
1733 if (!(code == InsetBase::INCLUDE_CODE
1734 || code == InsetBase::BIBTEX_CODE
1735 || code == InsetBase::FLOAT_LIST_CODE
1736 || code == InsetBase::TOC_CODE))
1743 case LFUN_INSET_MODIFY:
1744 // We need to disable this, because we may get called for a
1746 // InsetTabular::getStatus() -> InsetText::getStatus()
1747 // and we don't handle LFUN_INSET_MODIFY.
1752 flag.setOnOff(font.emph() == LyXFont::ON);
1756 flag.setOnOff(font.noun() == LyXFont::ON);
1760 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1764 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1768 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1772 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1777 enable = cur.selection();
1781 enable = lyx::cap::numberOfSelections() > 0;
1784 case LFUN_DELETE_WORD_FORWARD:
1785 case LFUN_DELETE_WORD_BACKWARD:
1786 case LFUN_DELETE_LINE_FORWARD:
1787 case LFUN_WORDRIGHT:
1797 case LFUN_UP_PARAGRAPHSEL:
1798 case LFUN_DOWN_PARAGRAPHSEL:
1803 case LFUN_WORDRIGHTSEL:
1804 case LFUN_WORDLEFTSEL:
1806 case LFUN_UP_PARAGRAPH:
1807 case LFUN_DOWN_PARAGRAPH:
1812 case LFUN_BREAKLINE:
1814 case LFUN_DELETE_SKIP:
1815 case LFUN_BACKSPACE:
1816 case LFUN_BACKSPACE_SKIP:
1817 case LFUN_BREAKPARAGRAPH:
1818 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1819 case LFUN_BREAKPARAGRAPH_SKIP:
1820 case LFUN_PARAGRAPH_SPACING:
1821 case LFUN_INSET_INSERT:
1822 case LFUN_NEXT_INSET_TOGGLE:
1823 case LFUN_UPCASE_WORD:
1824 case LFUN_LOWCASE_WORD:
1825 case LFUN_CAPITALIZE_WORD:
1826 case LFUN_TRANSPOSE_CHARS:
1830 case LFUN_GETLAYOUT:
1832 case LFUN_PASTESELECTION:
1833 case LFUN_DATE_INSERT:
1834 case LFUN_SELFINSERT:
1835 case LFUN_INSERT_LINE:
1836 case LFUN_INSERT_PAGEBREAK:
1837 case LFUN_MATH_DISPLAY:
1838 case LFUN_MATH_IMPORT_SELECTION:
1839 case LFUN_MATH_MODE:
1840 case LFUN_MATH_MACRO:
1841 case LFUN_INSERT_MATH:
1842 case LFUN_INSERT_MATRIX:
1843 case LFUN_MATH_DELIM:
1844 case LFUN_SUBSCRIPT:
1845 case LFUN_SUPERSCRIPT:
1847 case LFUN_UNDERLINE:
1848 case LFUN_FONT_SIZE:
1850 case LFUN_FREEFONT_APPLY:
1851 case LFUN_FREEFONT_UPDATE:
1852 case LFUN_LAYOUT_PARAGRAPH:
1853 case LFUN_PARAGRAPH_UPDATE:
1855 case LFUN_CIRCUMFLEX:
1865 case LFUN_SPECIAL_CARON:
1868 case LFUN_HUNG_UMLAUT:
1871 case LFUN_ACCEPT_CHANGE:
1872 case LFUN_REJECT_CHANGE:
1873 case LFUN_THESAURUS_ENTRY:
1874 case LFUN_PARAGRAPH_APPLY:
1877 case LFUN_BEGINNINGBUF:
1878 case LFUN_BEGINNINGBUFSEL:
1879 case LFUN_ENDBUFSEL:
1880 // these are handled in our dispatch()
1888 if (code != InsetBase::NO_CODE
1889 && (cur.empty() || !cur.inset().insetAllowed(code)))
1892 flag.enabled(enable);