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;
138 // It may happen that sel is empty but there is a selection
139 replaceSelection(cur);
142 const int old_pos = cur.pos();
143 cur.insert(new MathHullInset("simple"));
144 BOOST_ASSERT(old_pos == cur.pos());
145 cur.nextInset()->edit(cur, true);
146 // don't do that also for LFUN_MATH_MODE
147 // unless you want end up with always changing
148 // to mathrm when opening an inlined inset --
149 // I really hate "LyXfunc overloading"...
151 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
152 // Avoid an unnecessary undo step if cmd.argument
154 if (!cmd.argument.empty())
155 cur.dispatch(FuncRequest(LFUN_INSERT_MATH,
158 // create a macro if we see "\\newcommand"
159 // somewhere, and an ordinary formula
161 if (sel.find("\\newcommand") == string::npos
162 && sel.find("\\def") == string::npos)
164 cur.insert(new MathHullInset("simple"));
165 cur.dispatch(FuncRequest(LFUN_RIGHT));
166 cur.dispatch(FuncRequest(LFUN_INSERT_MATH, sel));
168 istringstream is(sel);
169 cur.insert(new MathMacroTemplate(is));
172 cur.message(N_("Math editor mode"));
181 string const freefont2string()
184 if (font2string(freefont, toggleall, data))
191 bool LyXText::cursorPrevious(LCursor & cur)
193 pos_type cpos = cur.pos();
194 lyx::pit_type cpar = cur.pit();
196 int x = cur.x_target();
198 bool updated = setCursorFromCoordinates(cur, x, 0);
201 updated |= cursorUp(cur);
203 if (cpar == cur.pit() && cpos == cur.pos()) {
204 // we have a row which is taller than the workarea. The
205 // simplest solution is to move to the previous row instead.
206 updated |= cursorUp(cur);
209 cur.bv().updateScrollbar();
215 bool LyXText::cursorNext(LCursor & cur)
217 pos_type cpos = cur.pos();
218 lyx::pit_type cpar = cur.pit();
220 int x = cur.x_target();
221 bool updated = setCursorFromCoordinates(cur, x, cur.bv().workHeight() - 1);
224 updated |= cursorDown(cur);
226 if (cpar == cur.pit() && cpos == cur.pos()) {
227 // we have a row which is taller than the workarea. The
228 // simplest solution is to move to the next row instead.
229 updated |= cursorDown(cur);
232 cur.bv().updateScrollbar();
240 void specialChar(LCursor & cur, InsetSpecialChar::Kind kind)
242 lyx::cap::replaceSelection(cur);
243 cur.insert(new InsetSpecialChar(kind));
248 bool doInsertInset(LCursor & cur, LyXText * text,
249 FuncRequest const & cmd, bool edit, bool pastesel)
251 InsetBase * inset = createInset(&cur.bv(), cmd);
257 if (cur.selection()) {
258 cur.bv().owner()->dispatch(FuncRequest(LFUN_CUT));
261 text->insertInset(cur, inset);
264 inset->edit(cur, true);
266 if (gotsel && pastesel)
267 cur.bv().owner()->dispatch(FuncRequest(LFUN_PASTE));
272 void update(LCursor & cur)
274 //we don't call update(true, false) directly to save a metrics call
275 if (cur.bv().fitCursor())
276 cur.bv().update(Update::Force);
283 void LyXText::number(LCursor & cur)
285 LyXFont font(LyXFont::ALL_IGNORE);
286 font.setNumber(LyXFont::TOGGLE);
287 toggleAndShow(cur, this, font);
291 bool LyXText::isRTL(Paragraph const & par) const
293 return par.isRightToLeftPar(bv()->buffer()->params());
297 void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
299 lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
301 BOOST_ASSERT(cur.text() == this);
302 BufferView * bv = &cur.bv();
303 CursorSlice oldTopSlice = cur.top();
304 bool oldBoundary = cur.boundary();
305 bool sel = cur.selection();
306 // Signals that, even if needsUpdate == false, an update of the
307 // cursor paragraph is required
308 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
309 LyXAction::SingleParUpdate);
310 // Signals that a full-screen update is required
311 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
312 LyXAction::NoUpdate) || singleParUpdate);
313 // Remember the old paragraph metric (_outer_ paragraph!)
314 Dimension olddim = cur.bottom().paragraph().dim();
316 switch (cmd.action) {
318 case LFUN_APPENDIX: {
319 Paragraph & par = cur.paragraph();
320 bool start = !par.params().startOfAppendix();
323 #warning The code below only makes sense at top level.
324 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
326 // ensure that we have only one start_of_appendix in this document
327 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
328 if (pars_[tmp].params().startOfAppendix()) {
330 pars_[tmp].params().startOfAppendix(false);
336 par.params().startOfAppendix(start);
338 // we can set the refreshing parameters now
339 updateCounters(cur.buffer());
343 case LFUN_DELETE_WORD_FORWARD:
344 cur.clearSelection();
345 deleteWordForward(cur);
346 finishChange(cur, false);
349 case LFUN_DELETE_WORD_BACKWARD:
350 cur.clearSelection();
351 deleteWordBackward(cur);
352 finishChange(cur, false);
355 case LFUN_DELETE_LINE_FORWARD:
356 cur.clearSelection();
357 deleteLineForward(cur);
358 finishChange(cur, false);
363 cur.clearSelection();
364 if (isRTL(cur.paragraph()))
365 needsUpdate = cursorLeftOneWord(cur);
367 needsUpdate = cursorRightOneWord(cur);
368 finishChange(cur, false);
373 cur.clearSelection();
374 if (isRTL(cur.paragraph()))
375 needsUpdate = cursorRightOneWord(cur);
377 needsUpdate = cursorLeftOneWord(cur);
378 finishChange(cur, false);
381 case LFUN_BEGINNINGBUF:
382 if (cur.depth() == 1) {
384 cur.clearSelection();
386 finishChange(cur, false);
392 case LFUN_BEGINNINGBUFSEL:
393 if (cur.depth() == 1) {
394 if (!cur.selection())
397 finishChange(cur, true);
404 if (cur.depth() == 1) {
406 cur.clearSelection();
408 finishChange(cur, false);
415 if (cur.depth() == 1) {
416 if (!cur.selection())
419 finishChange(cur, true);
427 //lyxerr << BOOST_CURRENT_FUNCTION
428 // << " LFUN_RIGHT[SEL]:\n" << cur << endl;
429 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
430 if (isRTL(cur.paragraph()))
431 needsUpdate = cursorLeft(cur);
433 needsUpdate = cursorRight(cur);
435 if (!needsUpdate && oldTopSlice == cur.top()
436 && cur.boundary() == oldBoundary) {
438 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
444 //lyxerr << "handle LFUN_LEFT[SEL]:\n" << cur << endl;
445 cur.selHandle(cmd.action == LFUN_LEFTSEL);
446 if (isRTL(cur.paragraph()))
447 needsUpdate = cursorRight(cur);
449 needsUpdate = cursorLeft(cur);
451 if (!needsUpdate && oldTopSlice == cur.top()
452 && cur.boundary() == oldBoundary) {
454 cmd = FuncRequest(LFUN_FINISHED_LEFT);
461 //lyxerr << "handle LFUN_UP[SEL]:\n" << cur << endl;
462 cur.selHandle(cmd.action == LFUN_UPSEL);
464 needsUpdate = cursorUp(cur);
465 if (!needsUpdate && oldTopSlice == cur.top()
466 && cur.boundary() == oldBoundary) {
468 cmd = FuncRequest(LFUN_FINISHED_UP);
475 //lyxerr << "handle LFUN_DOWN[SEL]:\n" << cur << endl;
476 cur.selHandle(cmd.action == LFUN_DOWNSEL);
477 needsUpdate = cursorDown(cur);
478 if (!needsUpdate && oldTopSlice == cur.top() &&
479 cur.boundary() == oldBoundary)
482 cmd = FuncRequest(LFUN_FINISHED_DOWN);
486 case LFUN_UP_PARAGRAPH:
488 cur.clearSelection();
489 needsUpdate = cursorUpParagraph(cur);
490 finishChange(cur, false);
493 case LFUN_UP_PARAGRAPHSEL:
494 if (!cur.selection())
496 cursorUpParagraph(cur);
497 finishChange(cur, true);
500 case LFUN_DOWN_PARAGRAPH:
502 cur.clearSelection();
503 needsUpdate = cursorDownParagraph(cur);
504 finishChange(cur, false);
507 case LFUN_DOWN_PARAGRAPHSEL:
508 if (!cur.selection())
510 cursorDownParagraph(cur);
511 finishChange(cur, true);
516 if (!cur.selection())
519 finishChange(cur, true);
524 if (!cur.selection())
527 finishChange(cur, true);
532 if (!cur.selection())
535 finishChange(cur, true);
540 if (!cur.selection())
543 finishChange(cur, true);
546 case LFUN_WORDRIGHTSEL:
547 if (!cur.selection())
549 if (isRTL(cur.paragraph()))
550 cursorLeftOneWord(cur);
552 cursorRightOneWord(cur);
553 finishChange(cur, true);
556 case LFUN_WORDLEFTSEL:
557 if (!cur.selection())
559 if (isRTL(cur.paragraph()))
560 cursorRightOneWord(cur);
562 cursorLeftOneWord(cur);
563 finishChange(cur, true);
567 selectWord(cur, lyx::WHOLE_WORD);
568 finishChange(cur, true);
575 cur.clearSelection();
576 finishChange(cur, false);
577 if (cur.pit() == 0 && cur.textRow().pos() == 0) {
579 cmd = FuncRequest(LFUN_FINISHED_UP);
581 needsUpdate = cursorPrevious(cur);
588 cur.clearSelection();
589 finishChange(cur, false);
590 if (cur.pit() == cur.lastpit()
591 && cur.textRow().endpos() == cur.lastpos()) {
593 cmd = FuncRequest(LFUN_FINISHED_DOWN);
595 needsUpdate = cursorNext(cur);
601 cur.clearSelection();
603 finishChange(cur, false);
608 cur.clearSelection();
610 finishChange(cur, false);
613 case LFUN_BREAKLINE: {
614 // Not allowed by LaTeX (labels or empty par)
615 if (cur.pos() > cur.paragraph().beginOfBody()) {
616 lyx::cap::replaceSelection(cur);
617 cur.insert(new InsetNewline);
619 moveCursor(cur, false);
625 if (!cur.selection()) {
626 needsUpdate = Delete(cur);
628 // It is possible to make it a lot faster still
629 // just comment out the line below...
631 cutSelection(cur, true, false);
633 moveCursor(cur, false);
636 case LFUN_DELETE_SKIP:
637 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
638 if (!cur.selection()) {
639 if (cur.pos() == cur.lastpos()) {
646 cutSelection(cur, true, false);
652 if (!cur.selection()) {
653 if (bv->owner()->getIntl().getTransManager().backspace()) {
654 needsUpdate = backspace(cur);
656 // It is possible to make it a lot faster still
657 // just comment out the line below...
660 cutSelection(cur, true, false);
665 case LFUN_BACKSPACE_SKIP:
666 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
667 if (!cur.selection()) {
671 //CursorSlice cur = cursor();
675 cutSelection(cur, true, false);
679 case LFUN_BREAKPARAGRAPH:
680 lyx::cap::replaceSelection(cur);
681 breakParagraph(cur, 0);
686 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
687 lyx::cap::replaceSelection(cur);
688 breakParagraph(cur, 1);
693 case LFUN_BREAKPARAGRAPH_SKIP: {
694 // When at the beginning of a paragraph, remove
695 // indentation. Otherwise, do the same as LFUN_BREAKPARAGRAPH.
696 lyx::cap::replaceSelection(cur);
698 cur.paragraph().params().labelWidthString(string());
700 breakParagraph(cur, 0);
706 case LFUN_PARAGRAPH_SPACING: {
707 Paragraph & par = cur.paragraph();
708 Spacing::Space cur_spacing = par.params().spacing().getSpace();
709 string cur_value = "1.0";
710 if (cur_spacing == Spacing::Other)
711 cur_value = par.params().spacing().getValueAsString();
713 istringstream is(cmd.argument);
716 Spacing::Space new_spacing = cur_spacing;
717 string new_value = cur_value;
719 lyxerr << "Missing argument to `paragraph-spacing'"
721 } else if (tmp == "single") {
722 new_spacing = Spacing::Single;
723 } else if (tmp == "onehalf") {
724 new_spacing = Spacing::Onehalf;
725 } else if (tmp == "double") {
726 new_spacing = Spacing::Double;
727 } else if (tmp == "other") {
728 new_spacing = Spacing::Other;
729 string tmpval = "0.0";
731 lyxerr << "new_value = " << tmpval << endl;
734 } else if (tmp == "default") {
735 new_spacing = Spacing::Default;
737 lyxerr << _("Unknown spacing argument: ")
738 << cmd.argument << endl;
740 if (cur_spacing != new_spacing || cur_value != new_value)
741 par.params().spacing(Spacing(new_spacing, new_value));
745 case LFUN_INSET_INSERT: {
747 InsetBase * inset = createInset(bv, cmd);
749 insertInset(cur, inset);
755 case LFUN_INSET_SETTINGS:
756 cur.inset().showInsetDialog(bv);
759 case LFUN_NEXT_INSET_TOGGLE: {
760 InsetBase * inset = cur.nextInset();
761 // this is the real function we want to invoke
762 cmd = FuncRequest(LFUN_INSET_TOGGLE);
764 // if there is an inset at cursor, see whether it
767 LCursor tmpcur = cur;
768 tmpcur.pushLeft(*inset);
769 inset->dispatch(tmpcur, cmd);
770 if (tmpcur.result().dispatched()) {
771 cur.clearSelection();
775 // if it did not work, try the underlying inset.
776 if (!cur.result().dispatched())
777 cur.inset().dispatch(cur, cmd);
781 case LFUN_SPACE_INSERT:
782 if (cur.paragraph().layout()->free_spacing)
783 insertChar(cur, ' ');
785 doInsertInset(cur, this, cmd, false, false);
788 moveCursor(cur, false);
791 case LFUN_HYPHENATION:
792 specialChar(cur, InsetSpecialChar::HYPHENATION);
795 case LFUN_LIGATURE_BREAK:
796 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
800 specialChar(cur, InsetSpecialChar::LDOTS);
803 case LFUN_END_OF_SENTENCE:
804 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
807 case LFUN_MENU_SEPARATOR:
808 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
811 case LFUN_UPCASE_WORD:
812 changeCase(cur, LyXText::text_uppercase);
815 case LFUN_LOWCASE_WORD:
816 changeCase(cur, LyXText::text_lowercase);
819 case LFUN_CAPITALIZE_WORD:
820 changeCase(cur, LyXText::text_capitalization);
823 case LFUN_TRANSPOSE_CHARS:
828 cur.message(_("Paste"));
829 lyx::cap::replaceSelection(cur);
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 bv->mouseSetCursor(cur);
1032 // Insert primary selection with middle mouse
1033 // if there is a local selection in the current buffer,
1035 if (cmd.button() == mouse_button::button2) {
1036 if (paste_internally)
1037 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1039 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1045 case LFUN_MOUSE_MOTION: {
1046 // Only use motion with button 1
1047 //if (cmd.button() != mouse_button::button1)
1050 // ignore motions deeper nested than the real anchor
1051 LCursor & bvcur = cur.bv().cursor();
1052 if (bvcur.anchor_.hasPart(cur)) {
1053 CursorSlice old = bvcur.top();
1055 int const wh = bv->workHeight();
1056 int const y = std::max(0, std::min(wh - 1, cmd.y));
1058 setCursorFromCoordinates(cur, cmd.x, y);
1059 cur.x_target() = cmd.x;
1064 // This is to allow jumping over large insets
1065 if (cur.top() == old) {
1072 if (cur.top() == old)
1075 // don't set anchor_
1076 bvcur.setCursor(cur);
1077 bvcur.selection() = true;
1078 //lyxerr << "MOTION: " << bv->cursor() << endl;
1086 case LFUN_MOUSE_RELEASE: {
1087 if (cmd.button() == mouse_button::button2)
1091 if (cmd.button() == mouse_button::button1)
1092 bv->haveSelection(cur.selection());
1095 bv->owner()->updateMenubar();
1096 bv->owner()->updateToolbars();
1100 case LFUN_SELFINSERT: {
1101 if (cmd.argument.empty())
1104 // Automatically delete the currently selected
1105 // text and replace it with what is being
1106 // typed in now. Depends on lyxrc settings
1107 // "auto_region_delete", which defaults to
1110 if (lyxrc.auto_region_delete) {
1111 if (cur.selection())
1112 cutSelection(cur, false, false);
1113 bv->haveSelection(false);
1116 cur.clearSelection();
1117 LyXFont const old_font = real_current_font;
1119 string::const_iterator cit = cmd.argument.begin();
1120 string::const_iterator end = cmd.argument.end();
1121 for (; cit != end; ++cit)
1122 bv->owner()->getIntl().getTransManager().
1123 TranslateAndInsert(*cit, this);
1126 moveCursor(cur, false);
1127 bv->updateScrollbar();
1132 InsetCommandParams p("url");
1133 string const data = InsetCommandMailer::params2string("url", p);
1134 bv->owner()->getDialogs().show("url", data, 0);
1138 case LFUN_HTMLURL: {
1139 InsetCommandParams p("htmlurl");
1140 string const data = InsetCommandMailer::params2string("url", p);
1141 bv->owner()->getDialogs().show("url", data, 0);
1145 case LFUN_INSERT_LABEL: {
1146 // Try to generate a valid label
1147 string const contents = cmd.argument.empty() ?
1148 cur.getPossibleLabel() : cmd.argument;
1150 InsetCommandParams p("label", contents);
1151 string const data = InsetCommandMailer::params2string("label", p);
1153 if (cmd.argument.empty()) {
1154 bv->owner()->getDialogs().show("label", data, 0);
1156 FuncRequest fr(LFUN_INSET_INSERT, data);
1164 case LFUN_INSET_LIST:
1165 case LFUN_INSET_THEOREM:
1166 case LFUN_INSET_CAPTION:
1168 case LFUN_INSERT_NOTE:
1169 case LFUN_INSERT_CHARSTYLE:
1170 case LFUN_INSERT_BOX:
1171 case LFUN_INSERT_BRANCH:
1172 case LFUN_INSERT_BIBITEM:
1173 case LFUN_INSET_ERT:
1174 case LFUN_INSET_FOOTNOTE:
1175 case LFUN_INSET_MARGINAL:
1176 case LFUN_INSET_OPTARG:
1177 case LFUN_ENVIRONMENT_INSERT:
1178 // Open the inset, and move the current selection
1180 doInsertInset(cur, this, cmd, true, true);
1184 case LFUN_TABULAR_INSERT:
1185 // if there were no arguments, just open the dialog
1186 if (doInsertInset(cur, this, cmd, false, true))
1189 bv->owner()->getDialogs().show("tabularcreate");
1193 case LFUN_INSET_FLOAT:
1194 case LFUN_INSET_WIDE_FLOAT:
1195 case LFUN_INSET_WRAP:
1196 doInsertInset(cur, this, cmd, true, true);
1198 // FIXME: the "Caption" name should not be hardcoded,
1199 // but given by the float definition.
1200 cur.dispatch(FuncRequest(LFUN_LAYOUT, "Caption"));
1203 case LFUN_INDEX_INSERT: {
1204 InsetBase * inset = createInset(&cur.bv(), cmd);
1209 cur.clearSelection();
1210 insertInset(cur, inset);
1211 inset->edit(cur, true);
1216 case LFUN_INDEX_PRINT:
1217 case LFUN_TOC_INSERT:
1219 case LFUN_INSERT_LINE:
1220 case LFUN_INSERT_PAGEBREAK:
1222 doInsertInset(cur, this, cmd, false, false);
1226 case LFUN_DEPTH_MIN:
1227 changeDepth(cur, DEC_DEPTH);
1230 case LFUN_DEPTH_PLUS:
1231 changeDepth(cur, INC_DEPTH);
1234 case LFUN_MATH_DISPLAY:
1235 mathDispatch(cur, cmd, true);
1238 case LFUN_MATH_IMPORT_SELECTION:
1239 case LFUN_MATH_MODE:
1240 if (cmd.argument == "on")
1241 // don't pass "on" as argument
1242 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1244 mathDispatch(cur, cmd, false);
1247 case LFUN_MATH_MACRO:
1248 if (cmd.argument.empty())
1249 cur.errorMessage(N_("Missing argument"));
1251 string s = cmd.argument;
1252 string const s1 = token(s, ' ', 1);
1253 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1254 string const s2 = token(s, ' ', 2);
1255 string const type = s2.empty() ? "newcommand" : s2;
1256 cur.insert(new MathMacroTemplate(token(s, ' ', 0), nargs, type));
1257 //cur.nextInset()->edit(cur, true);
1261 // passthrough hat and underscore outside mathed:
1262 case LFUN_SUBSCRIPT:
1263 mathDispatch(cur, FuncRequest(LFUN_SELFINSERT, "_"), false);
1265 case LFUN_SUPERSCRIPT:
1266 mathDispatch(cur, FuncRequest(LFUN_SELFINSERT, "^"), false);
1269 case LFUN_INSERT_MATH:
1270 case LFUN_INSERT_MATRIX:
1271 case LFUN_MATH_DELIM: {
1272 cur.insert(new MathHullInset("simple"));
1273 cur.dispatch(FuncRequest(LFUN_RIGHT));
1279 LyXFont font(LyXFont::ALL_IGNORE);
1280 font.setEmph(LyXFont::TOGGLE);
1281 toggleAndShow(cur, this, font);
1286 LyXFont font(LyXFont::ALL_IGNORE);
1287 font.setSeries(LyXFont::BOLD_SERIES);
1288 toggleAndShow(cur, this, font);
1293 LyXFont font(LyXFont::ALL_IGNORE);
1294 font.setNoun(LyXFont::TOGGLE);
1295 toggleAndShow(cur, this, font);
1300 LyXFont font(LyXFont::ALL_IGNORE);
1301 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
1302 toggleAndShow(cur, this, font);
1307 LyXFont font(LyXFont::ALL_IGNORE);
1308 font.setFamily(LyXFont::SANS_FAMILY);
1309 toggleAndShow(cur, this, font);
1314 LyXFont font(LyXFont::ALL_IGNORE);
1315 font.setFamily(LyXFont::ROMAN_FAMILY);
1316 toggleAndShow(cur, this, font);
1320 case LFUN_DEFAULT: {
1321 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
1322 toggleAndShow(cur, this, font);
1326 case LFUN_UNDERLINE: {
1327 LyXFont font(LyXFont::ALL_IGNORE);
1328 font.setUnderbar(LyXFont::TOGGLE);
1329 toggleAndShow(cur, this, font);
1333 case LFUN_FONT_SIZE: {
1334 LyXFont font(LyXFont::ALL_IGNORE);
1335 font.setLyXSize(cmd.argument);
1336 toggleAndShow(cur, this, font);
1340 case LFUN_LANGUAGE: {
1341 Language const * lang = languages.getLanguage(cmd.argument);
1344 LyXFont font(LyXFont::ALL_IGNORE);
1345 font.setLanguage(lang);
1346 toggleAndShow(cur, this, font);
1351 case LFUN_FREEFONT_APPLY:
1352 toggleAndShow(cur, this, freefont, toggleall);
1353 cur.message(_("Character set"));
1356 // Set the freefont using the contents of \param data dispatched from
1357 // the frontends and apply it at the current cursor location.
1358 case LFUN_FREEFONT_UPDATE: {
1361 if (bv_funcs::string2font(cmd.argument, font, toggle)) {
1364 toggleAndShow(cur, this, freefont, toggleall);
1365 cur.message(_("Character set"));
1370 case LFUN_FINISHED_LEFT:
1371 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1374 case LFUN_FINISHED_RIGHT:
1375 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1379 case LFUN_FINISHED_UP:
1380 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_UP:\n" << cur << endl;
1384 case LFUN_FINISHED_DOWN:
1385 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_DOWN:\n" << cur << endl;
1389 case LFUN_LAYOUT_PARAGRAPH: {
1391 params2string(cur.paragraph(), data);
1392 data = "show\n" + data;
1393 bv->owner()->getDialogs().show("paragraph", data);
1397 case LFUN_PARAGRAPH_UPDATE: {
1398 if (!bv->owner()->getDialogs().visible("paragraph"))
1401 params2string(cur.paragraph(), data);
1403 // Will the paragraph accept changes from the dialog?
1404 bool const accept = !cur.inset().forceDefaultParagraphs(cur.idx());
1406 data = "update " + convert<string>(accept) + '\n' + data;
1407 bv->owner()->getDialogs().update("paragraph", data);
1412 case LFUN_CIRCUMFLEX:
1422 case LFUN_SPECIAL_CARON:
1425 case LFUN_HUNG_UMLAUT:
1428 bv->owner()->getLyXFunc().handleKeyFunc(cmd.action);
1429 if (!cmd.argument.empty())
1430 bv->owner()->getIntl().getTransManager()
1431 .TranslateAndInsert(cmd.argument[0], this);
1434 case LFUN_FLOAT_LIST: {
1435 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1436 if (tclass.floats().typeExist(cmd.argument)) {
1437 // not quite sure if we want this...
1439 cur.clearSelection();
1440 breakParagraph(cur);
1442 if (cur.lastpos() != 0) {
1444 breakParagraph(cur);
1447 setLayout(cur, tclass.defaultLayoutName());
1448 setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1449 insertInset(cur, new InsetFloatList(cmd.argument));
1452 lyxerr << "Non-existent float type: "
1453 << cmd.argument << endl;
1458 case LFUN_ACCEPT_CHANGE: {
1463 case LFUN_REJECT_CHANGE: {
1468 case LFUN_THESAURUS_ENTRY: {
1469 string arg = cmd.argument;
1471 arg = cur.selectionAsString(false);
1473 if (arg.size() > 100 || arg.empty()) {
1474 // Get word or selection
1475 selectWordWhenUnderCursor(cur, lyx::WHOLE_WORD);
1476 arg = cur.selectionAsString(false);
1479 bv->owner()->getDialogs().show("thesaurus", arg);
1483 case LFUN_PARAGRAPH_APPLY: {
1484 // Given data, an encoding of the ParagraphParameters
1485 // generated in the Paragraph dialog, this function sets
1486 // the current paragraph appropriately.
1487 istringstream is(cmd.argument);
1490 ParagraphParameters params;
1495 params.labelWidthString(),
1497 cur.message(_("Paragraph layout set"));
1501 case LFUN_INSET_DIALOG_SHOW: {
1502 InsetBase * inset = cur.nextInset();
1504 FuncRequest fr(LFUN_INSET_DIALOG_SHOW);
1505 inset->dispatch(cur, fr);
1511 if (cur.selection()) {
1512 cur.selection() = false;
1515 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1520 lyxerr[Debug::ACTION]
1521 << BOOST_CURRENT_FUNCTION
1522 << ": Command " << cmd
1523 << " not DISPATCHED by LyXText" << endl;
1528 if (singleParUpdate)
1529 // Inserting characters does not change par height
1530 if (cur.bottom().paragraph().dim().height()
1531 == olddim.height()) {
1532 // if so, update _only_ this paragraph
1533 cur.bv().update(Update::SinglePar | Update::Force);
1537 && &oldTopSlice.inset() == &cur.inset()
1538 && oldTopSlice.idx() == cur.idx()
1540 && !cur.selection())
1547 bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
1548 FuncStatus & flag) const
1550 BOOST_ASSERT(cur.text() == this);
1552 LyXFont const & font = real_current_font;
1554 InsetBase::Code code = InsetBase::NO_CODE;
1556 switch (cmd.action) {
1558 case LFUN_DEPTH_MIN:
1559 enable = changeDepthAllowed(cur, DEC_DEPTH);
1562 case LFUN_DEPTH_PLUS:
1563 enable = changeDepthAllowed(cur, INC_DEPTH);
1567 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1570 case LFUN_INSERT_BIBITEM:
1571 enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO);
1574 case LFUN_DIALOG_SHOW_NEW_INSET:
1575 if (cmd.argument == "bibitem")
1576 code = InsetBase::BIBITEM_CODE;
1577 else if (cmd.argument == "bibtex")
1578 code = InsetBase::BIBTEX_CODE;
1579 else if (cmd.argument == "box")
1580 code = InsetBase::BOX_CODE;
1581 else if (cmd.argument == "branch")
1582 code = InsetBase::BRANCH_CODE;
1583 else if (cmd.argument == "citation")
1584 code = InsetBase::CITE_CODE;
1585 else if (cmd.argument == "ert")
1586 code = InsetBase::ERT_CODE;
1587 else if (cmd.argument == "external")
1588 code = InsetBase::EXTERNAL_CODE;
1589 else if (cmd.argument == "float")
1590 code = InsetBase::FLOAT_CODE;
1591 else if (cmd.argument == "graphics")
1592 code = InsetBase::GRAPHICS_CODE;
1593 else if (cmd.argument == "include")
1594 code = InsetBase::INCLUDE_CODE;
1595 else if (cmd.argument == "index")
1596 code = InsetBase::INDEX_CODE;
1597 else if (cmd.argument == "label")
1598 code = InsetBase::LABEL_CODE;
1599 else if (cmd.argument == "note")
1600 code = InsetBase::NOTE_CODE;
1601 else if (cmd.argument == "ref")
1602 code = InsetBase::REF_CODE;
1603 else if (cmd.argument == "toc")
1604 code = InsetBase::TOC_CODE;
1605 else if (cmd.argument == "url")
1606 code = InsetBase::URL_CODE;
1607 else if (cmd.argument == "vspace")
1608 code = InsetBase::VSPACE_CODE;
1609 else if (cmd.argument == "wrap")
1610 code = InsetBase::WRAP_CODE;
1613 case LFUN_INSET_ERT:
1614 code = InsetBase::ERT_CODE;
1616 case LFUN_INSET_FOOTNOTE:
1617 code = InsetBase::FOOT_CODE;
1619 case LFUN_TABULAR_INSERT:
1620 code = InsetBase::TABULAR_CODE;
1622 case LFUN_INSET_MARGINAL:
1623 code = InsetBase::MARGIN_CODE;
1625 case LFUN_INSET_FLOAT:
1626 case LFUN_INSET_WIDE_FLOAT:
1627 code = InsetBase::FLOAT_CODE;
1629 case LFUN_INSET_WRAP:
1630 code = InsetBase::WRAP_CODE;
1632 case LFUN_FLOAT_LIST:
1633 code = InsetBase::FLOAT_LIST_CODE;
1636 case LFUN_INSET_LIST:
1637 code = InsetBase::LIST_CODE;
1639 case LFUN_INSET_THEOREM:
1640 code = InsetBase::THEOREM_CODE;
1643 case LFUN_INSET_CAPTION:
1644 code = InsetBase::CAPTION_CODE;
1646 case LFUN_INSERT_NOTE:
1647 code = InsetBase::NOTE_CODE;
1649 case LFUN_INSERT_CHARSTYLE:
1650 code = InsetBase::CHARSTYLE_CODE;
1651 if (cur.buffer().params().getLyXTextClass().charstyles().empty())
1654 case LFUN_INSERT_BOX:
1655 code = InsetBase::BOX_CODE;
1657 case LFUN_INSERT_BRANCH:
1658 code = InsetBase::BRANCH_CODE;
1659 if (cur.buffer().getMasterBuffer()->params().branchlist().empty())
1662 case LFUN_INSERT_LABEL:
1663 code = InsetBase::LABEL_CODE;
1665 case LFUN_INSET_OPTARG:
1666 code = InsetBase::OPTARG_CODE;
1667 enable = numberOfOptArgs(cur.paragraph())
1668 < cur.paragraph().layout()->optionalargs;
1670 case LFUN_ENVIRONMENT_INSERT:
1671 code = InsetBase::BOX_CODE;
1673 case LFUN_INDEX_INSERT:
1674 code = InsetBase::INDEX_CODE;
1676 case LFUN_INDEX_PRINT:
1677 code = InsetBase::INDEX_PRINT_CODE;
1679 case LFUN_TOC_INSERT:
1680 code = InsetBase::TOC_CODE;
1684 code = InsetBase::URL_CODE;
1687 // always allow this, since we will inset a raw quote
1688 // if an inset is not allowed.
1690 case LFUN_HYPHENATION:
1691 case LFUN_LIGATURE_BREAK:
1693 case LFUN_MENU_SEPARATOR:
1695 case LFUN_END_OF_SENTENCE:
1696 code = InsetBase::SPECIALCHAR_CODE;
1698 case LFUN_SPACE_INSERT:
1699 // slight hack: we know this is allowed in math mode
1701 code = InsetBase::SPACE_CODE;
1704 #ifdef WITH_WARNINGS
1705 #warning This LFUN is not used anymore and should be nuked (JMarc 29/10/2005)
1708 case LFUN_INSET_DIALOG_SHOW: {
1709 InsetBase * inset = cur.nextInset();
1712 code = inset->lyxCode();
1713 if (!(code == InsetBase::INCLUDE_CODE
1714 || code == InsetBase::BIBTEX_CODE
1715 || code == InsetBase::FLOAT_LIST_CODE
1716 || code == InsetBase::TOC_CODE))
1723 case LFUN_INSET_MODIFY:
1724 // We need to disable this, because we may get called for a
1726 // InsetTabular::getStatus() -> InsetText::getStatus()
1727 // and we don't handle LFUN_INSET_MODIFY.
1732 flag.setOnOff(font.emph() == LyXFont::ON);
1736 flag.setOnOff(font.noun() == LyXFont::ON);
1740 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1744 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1748 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1752 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1757 enable = cur.selection();
1761 enable = lyx::cap::numberOfSelections() > 0;
1764 case LFUN_DELETE_WORD_FORWARD:
1765 case LFUN_DELETE_WORD_BACKWARD:
1766 case LFUN_DELETE_LINE_FORWARD:
1767 case LFUN_WORDRIGHT:
1777 case LFUN_UP_PARAGRAPHSEL:
1778 case LFUN_DOWN_PARAGRAPHSEL:
1783 case LFUN_WORDRIGHTSEL:
1784 case LFUN_WORDLEFTSEL:
1786 case LFUN_UP_PARAGRAPH:
1787 case LFUN_DOWN_PARAGRAPH:
1792 case LFUN_BREAKLINE:
1794 case LFUN_DELETE_SKIP:
1795 case LFUN_BACKSPACE:
1796 case LFUN_BACKSPACE_SKIP:
1797 case LFUN_BREAKPARAGRAPH:
1798 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1799 case LFUN_BREAKPARAGRAPH_SKIP:
1800 case LFUN_PARAGRAPH_SPACING:
1801 case LFUN_INSET_INSERT:
1802 case LFUN_NEXT_INSET_TOGGLE:
1803 case LFUN_UPCASE_WORD:
1804 case LFUN_LOWCASE_WORD:
1805 case LFUN_CAPITALIZE_WORD:
1806 case LFUN_TRANSPOSE_CHARS:
1810 case LFUN_GETLAYOUT:
1812 case LFUN_PASTESELECTION:
1813 case LFUN_DATE_INSERT:
1814 case LFUN_SELFINSERT:
1815 case LFUN_INSERT_LINE:
1816 case LFUN_INSERT_PAGEBREAK:
1817 case LFUN_MATH_DISPLAY:
1818 case LFUN_MATH_IMPORT_SELECTION:
1819 case LFUN_MATH_MODE:
1820 case LFUN_MATH_MACRO:
1821 case LFUN_INSERT_MATH:
1822 case LFUN_INSERT_MATRIX:
1823 case LFUN_MATH_DELIM:
1824 case LFUN_SUBSCRIPT:
1825 case LFUN_SUPERSCRIPT:
1827 case LFUN_UNDERLINE:
1828 case LFUN_FONT_SIZE:
1830 case LFUN_FREEFONT_APPLY:
1831 case LFUN_FREEFONT_UPDATE:
1832 case LFUN_LAYOUT_PARAGRAPH:
1833 case LFUN_PARAGRAPH_UPDATE:
1835 case LFUN_CIRCUMFLEX:
1845 case LFUN_SPECIAL_CARON:
1848 case LFUN_HUNG_UMLAUT:
1851 case LFUN_ACCEPT_CHANGE:
1852 case LFUN_REJECT_CHANGE:
1853 case LFUN_THESAURUS_ENTRY:
1854 case LFUN_PARAGRAPH_APPLY:
1857 case LFUN_BEGINNINGBUF:
1858 case LFUN_BEGINNINGBUFSEL:
1859 case LFUN_ENDBUFSEL:
1860 // these are handled in our dispatch()
1868 if (code != InsetBase::NO_CODE
1869 && (cur.empty() || !cur.inset().insetAllowed(code)))
1872 flag.enabled(enable);