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);
140 BOOST_ASSERT(old_pos == cur.pos());
141 cur.nextInset()->edit(cur, true);
142 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
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 cur.dispatch(FuncRequest(LFUN_INSERT_MATH, cmd.argument));
151 // create a macro if we see "\\newcommand"
152 // somewhere, and an ordinary formula
154 cutSelection(cur, true, true);
155 if (sel.find("\\newcommand") == string::npos
156 && sel.find("\\def") == string::npos)
158 cur.insert(new MathHullInset);
159 cur.dispatch(FuncRequest(LFUN_RIGHT));
160 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
161 cur.dispatch(FuncRequest(LFUN_INSERT_MATH, sel));
163 istringstream is(sel);
164 cur.insert(new MathMacroTemplate(is));
167 cur.message(N_("Math editor mode"));
176 string const freefont2string()
179 if (font2string(freefont, toggleall, data))
186 bool LyXText::cursorPrevious(LCursor & cur)
188 pos_type cpos = cur.pos();
189 lyx::pit_type cpar = cur.pit();
191 int x = cur.x_target();
193 setCursorFromCoordinates(cur, x, 0);
194 bool updated = cursorUp(cur);
196 if (cpar == cur.pit() && cpos == cur.pos()) {
197 // we have a row which is taller than the workarea. The
198 // simplest solution is to move to the previous row instead.
199 updated |= cursorUp(cur);
202 cur.bv().updateScrollbar();
208 bool LyXText::cursorNext(LCursor & cur)
210 pos_type cpos = cur.pos();
211 lyx::pit_type cpar = cur.pit();
213 int x = cur.x_target();
214 setCursorFromCoordinates(cur, x, cur.bv().workHeight() - 1);
215 bool updated = cursorDown(cur);
217 if (cpar == cur.pit() && cpos == cur.pos()) {
218 // we have a row which is taller than the workarea. The
219 // simplest solution is to move to the next row instead.
220 updated |= cursorDown(cur);
223 cur.bv().updateScrollbar();
231 void specialChar(LCursor & cur, InsetSpecialChar::Kind kind)
233 lyx::cap::replaceSelection(cur);
234 cur.insert(new InsetSpecialChar(kind));
239 bool doInsertInset(LCursor & cur, LyXText * text,
240 FuncRequest const & cmd, bool edit, bool pastesel)
242 InsetBase * inset = createInset(&cur.bv(), cmd);
248 if (cur.selection()) {
249 cur.bv().owner()->dispatch(FuncRequest(LFUN_CUT));
252 text->insertInset(cur, inset);
255 inset->edit(cur, true);
257 if (gotsel && pastesel)
258 cur.bv().owner()->dispatch(FuncRequest(LFUN_PASTE));
263 void update(LCursor & cur)
265 //we don't call update(true, false) directly to save a metrics call
266 if (cur.bv().fitCursor())
267 cur.bv().update(Update::Force);
274 void LyXText::number(LCursor & cur)
276 LyXFont font(LyXFont::ALL_IGNORE);
277 font.setNumber(LyXFont::TOGGLE);
278 toggleAndShow(cur, this, font);
282 bool LyXText::isRTL(Paragraph const & par) const
284 return par.isRightToLeftPar(bv()->buffer()->params());
288 void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
290 lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
292 BOOST_ASSERT(cur.text() == this);
293 BufferView * bv = &cur.bv();
294 CursorSlice oldTopSlice = cur.top();
295 bool oldBoundary = cur.boundary();
296 bool sel = cur.selection();
297 bool needsUpdate = !lyxaction.funcHasFlag(cmd.action, LyXAction::NoUpdate);
299 switch (cmd.action) {
301 case LFUN_APPENDIX: {
302 Paragraph & par = cur.paragraph();
303 bool start = !par.params().startOfAppendix();
306 #warning The code below only makes sense at top level.
307 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
309 // ensure that we have only one start_of_appendix in this document
310 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
311 if (pars_[tmp].params().startOfAppendix()) {
313 pars_[tmp].params().startOfAppendix(false);
319 par.params().startOfAppendix(start);
321 // we can set the refreshing parameters now
322 updateCounters(cur.buffer());
326 case LFUN_DELETE_WORD_FORWARD:
327 cur.clearSelection();
328 deleteWordForward(cur);
329 finishChange(cur, false);
332 case LFUN_DELETE_WORD_BACKWARD:
333 cur.clearSelection();
334 deleteWordBackward(cur);
335 finishChange(cur, false);
338 case LFUN_DELETE_LINE_FORWARD:
339 cur.clearSelection();
340 deleteLineForward(cur);
341 finishChange(cur, false);
346 cur.clearSelection();
347 if (isRTL(cur.paragraph()))
348 needsUpdate = cursorLeftOneWord(cur);
350 needsUpdate = cursorRightOneWord(cur);
351 finishChange(cur, false);
356 cur.clearSelection();
357 if (isRTL(cur.paragraph()))
358 needsUpdate = cursorRightOneWord(cur);
360 needsUpdate = cursorLeftOneWord(cur);
361 finishChange(cur, false);
364 case LFUN_BEGINNINGBUF:
365 if (cur.depth() == 1) {
367 cur.clearSelection();
369 finishChange(cur, false);
375 case LFUN_BEGINNINGBUFSEL:
376 if (cur.depth() == 1) {
377 if (!cur.selection())
380 finishChange(cur, true);
387 if (cur.depth() == 1) {
389 cur.clearSelection();
391 finishChange(cur, false);
398 if (cur.depth() == 1) {
399 if (!cur.selection())
402 finishChange(cur, true);
410 //lyxerr << BOOST_CURRENT_FUNCTION
411 // << " LFUN_RIGHT[SEL]:\n" << cur << endl;
412 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
413 if (isRTL(cur.paragraph()))
414 needsUpdate = cursorLeft(cur);
416 needsUpdate = cursorRight(cur);
418 if (!needsUpdate && oldTopSlice == cur.top()
419 && cur.boundary() == oldBoundary) {
421 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
427 //lyxerr << "handle LFUN_LEFT[SEL]:\n" << cur << endl;
428 cur.selHandle(cmd.action == LFUN_LEFTSEL);
429 if (isRTL(cur.paragraph()))
430 needsUpdate = cursorRight(cur);
432 needsUpdate = cursorLeft(cur);
434 if (!needsUpdate && oldTopSlice == cur.top()
435 && cur.boundary() == oldBoundary) {
437 cmd = FuncRequest(LFUN_FINISHED_LEFT);
444 //lyxerr << "handle LFUN_UP[SEL]:\n" << cur << endl;
445 cur.selHandle(cmd.action == LFUN_UPSEL);
447 needsUpdate = cursorUp(cur);
448 if (!needsUpdate && oldTopSlice == cur.top()
449 && cur.boundary() == oldBoundary) {
451 cmd = FuncRequest(LFUN_FINISHED_UP);
458 //lyxerr << "handle LFUN_DOWN[SEL]:\n" << cur << endl;
459 cur.selHandle(cmd.action == LFUN_DOWNSEL);
460 needsUpdate = cursorDown(cur);
461 if (!needsUpdate && oldTopSlice == cur.top() &&
462 cur.boundary() == oldBoundary)
465 cmd = FuncRequest(LFUN_FINISHED_DOWN);
469 case LFUN_UP_PARAGRAPH:
471 cur.clearSelection();
472 needsUpdate = cursorUpParagraph(cur);
473 finishChange(cur, false);
476 case LFUN_UP_PARAGRAPHSEL:
477 if (!cur.selection())
479 cursorUpParagraph(cur);
480 finishChange(cur, true);
483 case LFUN_DOWN_PARAGRAPH:
485 cur.clearSelection();
486 needsUpdate = cursorDownParagraph(cur);
487 finishChange(cur, false);
490 case LFUN_DOWN_PARAGRAPHSEL:
491 if (!cur.selection())
493 cursorDownParagraph(cur);
494 finishChange(cur, true);
499 if (!cur.selection())
502 finishChange(cur, true);
507 if (!cur.selection())
510 finishChange(cur, true);
515 if (!cur.selection())
518 finishChange(cur, true);
523 if (!cur.selection())
526 finishChange(cur, true);
529 case LFUN_WORDRIGHTSEL:
530 if (!cur.selection())
532 if (isRTL(cur.paragraph()))
533 cursorLeftOneWord(cur);
535 cursorRightOneWord(cur);
536 finishChange(cur, true);
539 case LFUN_WORDLEFTSEL:
540 if (!cur.selection())
542 if (isRTL(cur.paragraph()))
543 cursorRightOneWord(cur);
545 cursorLeftOneWord(cur);
546 finishChange(cur, true);
550 selectWord(cur, lyx::WHOLE_WORD);
551 finishChange(cur, true);
558 cur.clearSelection();
559 finishChange(cur, false);
560 if (cur.pit() == 0 && cur.textRow().pos() == 0) {
562 cmd = FuncRequest(LFUN_FINISHED_UP);
564 needsUpdate = cursorPrevious(cur);
571 cur.clearSelection();
572 finishChange(cur, false);
573 if (cur.pit() == cur.lastpit()
574 && cur.textRow().endpos() == cur.lastpos()) {
576 cmd = FuncRequest(LFUN_FINISHED_DOWN);
578 needsUpdate = cursorNext(cur);
584 cur.clearSelection();
586 finishChange(cur, false);
591 cur.clearSelection();
593 finishChange(cur, false);
596 case LFUN_BREAKLINE: {
597 // Not allowed by LaTeX (labels or empty par)
598 if (cur.pos() > cur.paragraph().beginOfBody()) {
599 lyx::cap::replaceSelection(cur);
600 cur.insert(new InsetNewline);
602 moveCursor(cur, false);
608 if (!cur.selection()) {
611 // It is possible to make it a lot faster still
612 // just comment out the line below...
614 cutSelection(cur, true, false);
616 moveCursor(cur, false);
619 case LFUN_DELETE_SKIP:
620 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
621 if (!cur.selection()) {
622 if (cur.pos() == cur.lastpos()) {
629 cutSelection(cur, true, false);
635 if (!cur.selection()) {
636 if (bv->owner()->getIntl().getTransManager().backspace()) {
639 // It is possible to make it a lot faster still
640 // just comment out the line below...
643 cutSelection(cur, true, false);
648 case LFUN_BACKSPACE_SKIP:
649 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
650 if (!cur.selection()) {
654 //CursorSlice cur = cursor();
658 cutSelection(cur, true, false);
662 case LFUN_BREAKPARAGRAPH:
663 lyx::cap::replaceSelection(cur);
664 breakParagraph(cur, 0);
669 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
670 lyx::cap::replaceSelection(cur);
671 breakParagraph(cur, 1);
676 case LFUN_BREAKPARAGRAPH_SKIP: {
677 // When at the beginning of a paragraph, remove
678 // indentation. Otherwise, do the same as LFUN_BREAKPARAGRAPH.
679 lyx::cap::replaceSelection(cur);
681 cur.paragraph().params().labelWidthString(string());
683 breakParagraph(cur, 0);
689 case LFUN_PARAGRAPH_SPACING: {
690 Paragraph & par = cur.paragraph();
691 Spacing::Space cur_spacing = par.params().spacing().getSpace();
692 string cur_value = "1.0";
693 if (cur_spacing == Spacing::Other)
694 cur_value = par.params().spacing().getValueAsString();
696 istringstream is(cmd.argument);
699 Spacing::Space new_spacing = cur_spacing;
700 string new_value = cur_value;
702 lyxerr << "Missing argument to `paragraph-spacing'"
704 } else if (tmp == "single") {
705 new_spacing = Spacing::Single;
706 } else if (tmp == "onehalf") {
707 new_spacing = Spacing::Onehalf;
708 } else if (tmp == "double") {
709 new_spacing = Spacing::Double;
710 } else if (tmp == "other") {
711 new_spacing = Spacing::Other;
712 string tmpval = "0.0";
714 lyxerr << "new_value = " << tmpval << endl;
717 } else if (tmp == "default") {
718 new_spacing = Spacing::Default;
720 lyxerr << _("Unknown spacing argument: ")
721 << cmd.argument << endl;
723 if (cur_spacing != new_spacing || cur_value != new_value)
724 par.params().spacing(Spacing(new_spacing, new_value));
728 case LFUN_INSET_INSERT: {
730 InsetBase * inset = createInset(bv, cmd);
732 insertInset(cur, inset);
738 case LFUN_INSET_SETTINGS:
739 cur.inset().showInsetDialog(bv);
742 case LFUN_NEXT_INSET_TOGGLE: {
743 InsetBase * inset = cur.nextInset();
745 cur.clearSelection();
746 FuncRequest fr = cmd;
747 fr.action = LFUN_INSET_TOGGLE;
748 inset->dispatch(cur, fr);
753 case LFUN_KEYMAP_TOGGLE:
754 cur.clearSelection();
758 case LFUN_SPACE_INSERT:
759 if (cur.paragraph().layout()->free_spacing)
760 insertChar(cur, ' ');
762 doInsertInset(cur, this, cmd, false, false);
765 moveCursor(cur, false);
768 case LFUN_HYPHENATION:
769 specialChar(cur, InsetSpecialChar::HYPHENATION);
772 case LFUN_LIGATURE_BREAK:
773 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
777 specialChar(cur, InsetSpecialChar::LDOTS);
780 case LFUN_END_OF_SENTENCE:
781 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
784 case LFUN_MENU_SEPARATOR:
785 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
788 case LFUN_UPCASE_WORD:
789 changeCase(cur, LyXText::text_uppercase);
792 case LFUN_LOWCASE_WORD:
793 changeCase(cur, LyXText::text_lowercase);
796 case LFUN_CAPITALIZE_WORD:
797 changeCase(cur, LyXText::text_capitalization);
800 case LFUN_TRANSPOSE_CHARS:
805 cur.message(_("Paste"));
806 lyx::cap::replaceSelection(cur);
808 #warning FIXME Check if the arg is in the domain of available selections.
810 if (isStrUnsignedInt(cmd.argument))
811 pasteSelection(cur, convert<unsigned int>(cmd.argument));
813 pasteSelection(cur, 0);
814 cur.clearSelection(); // bug 393
820 cutSelection(cur, true, true);
821 cur.message(_("Cut"));
826 cur.message(_("Copy"));
830 cur.message(convert<string>(cursorX(cur.top(), cur.boundary())) + ' '
831 + convert<string>(cursorY(cur.top(), cur.boundary())));
837 istringstream is(cmd.argument);
840 lyxerr << "SETXY: Could not parse coordinates in '"
841 << cmd.argument << std::endl;
843 setCursorFromCoordinates(cur, x, y);
848 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
850 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
857 cur.message(cur.paragraph().layout()->name());
861 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
862 << cmd.argument << endl;
864 // This is not the good solution to the empty argument
865 // problem, but it will hopefully suffice for 1.2.0.
866 // The correct solution would be to augument the
867 // function list/array with information about what
868 // functions needs arguments and their type.
869 if (cmd.argument.empty()) {
870 cur.errorMessage(_("LyX function 'layout' needs an argument."));
874 // Derive layout number from given argument (string)
875 // and current buffer's textclass (number)
876 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
877 bool hasLayout = tclass.hasLayout(cmd.argument);
878 string layout = cmd.argument;
880 // If the entry is obsolete, use the new one instead.
882 string const & obs = tclass[layout]->obsoleted_by();
888 cur.errorMessage(string(N_("Layout ")) + cmd.argument +
893 bool change_layout = (current_layout != layout);
895 if (!change_layout && cur.selection() &&
896 cur.selBegin().pit() != cur.selEnd().pit())
898 pit_type spit = cur.selBegin().pit();
899 pit_type epit = cur.selEnd().pit() + 1;
900 while (spit != epit) {
901 if (pars_[spit].layout()->name() != current_layout) {
902 change_layout = true;
910 current_layout = layout;
911 setLayout(cur, layout);
912 bv->owner()->setLayout(layout);
918 case LFUN_PASTESELECTION: {
919 cur.clearSelection();
920 string const clip = bv->getClipboard();
922 if (cmd.argument == "paragraph")
923 insertStringAsParagraphs(cur, clip);
925 insertStringAsLines(cur, clip);
931 lyx::cap::replaceSelection(cur);
932 Paragraph & par = cur.paragraph();
933 lyx::pos_type pos = cur.pos();
937 else if (cur.prevInset() && cur.prevInset()->isSpace())
940 c = par.getChar(pos - 1);
942 LyXLayout_ptr const & style = par.layout();
944 BufferParams const & bufparams = bv->buffer()->params();
945 if (!style->pass_thru
946 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
947 string arg = cmd.argument;
949 cur.insert(new InsetQuotes(c,
950 bufparams.quotes_language,
951 InsetQuotes::SingleQ));
952 else if (arg == "double")
953 cur.insert(new InsetQuotes(c,
954 bufparams.quotes_language,
955 InsetQuotes::DoubleQ));
957 cur.insert(new InsetQuotes(c, bufparams));
961 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
965 case LFUN_DATE_INSERT:
966 if (cmd.argument.empty())
967 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT,
968 lyx::formatted_time(lyx::current_time())));
970 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT,
971 lyx::formatted_time(lyx::current_time(), cmd.argument)));
974 case LFUN_MOUSE_TRIPLE:
975 if (cmd.button() == mouse_button::button1) {
981 bv->haveSelection(cur.selection());
985 case LFUN_MOUSE_DOUBLE:
986 if (cmd.button() == mouse_button::button1) {
987 selectWord(cur, lyx::WHOLE_WORD_STRICT);
989 bv->haveSelection(cur.selection());
993 // Single-click on work area
994 case LFUN_MOUSE_PRESS: {
995 // Right click on a footnote flag opens float menu
996 if (cmd.button() == mouse_button::button3) {
997 cur.clearSelection();
1001 // Middle button press pastes if we have a selection
1002 // We do this here as if the selection was inside an inset
1003 // it could get cleared on the unlocking of the inset so
1004 // we have to check this first
1005 bool paste_internally = false;
1006 if (cmd.button() == mouse_button::button2 && cur.selection()) {
1007 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1008 paste_internally = true;
1011 // Clear the selection
1012 cur.clearSelection();
1014 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1019 // Has the cursor just left the inset?
1020 if (bv->cursor().inMathed() && !cur.inMathed())
1021 bv->cursor().inset().notifyCursorLeaves(bv->cursor());
1026 // Insert primary selection with middle mouse
1027 // if there is a local selection in the current buffer,
1029 if (cmd.button() == mouse_button::button2) {
1030 if (paste_internally)
1031 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1033 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1039 case LFUN_MOUSE_MOTION: {
1040 // Only use motion with button 1
1041 //if (cmd.button() != mouse_button::button1)
1044 // ignore motions deeper nested than the real anchor
1045 LCursor & bvcur = cur.bv().cursor();
1046 if (bvcur.anchor_.hasPart(cur)) {
1047 CursorSlice old = bvcur.top();
1049 int const wh = bv->workHeight();
1050 int const y = std::max(0, std::min(wh - 1, cmd.y));
1052 setCursorFromCoordinates(cur, cmd.x, y);
1053 cur.x_target() = cmd.x;
1058 // This is to allow jumping over large insets
1059 if (cur.top() == old) {
1066 if (cur.top() == old)
1069 // don't set anchor_
1070 bvcur.setCursor(cur);
1071 bvcur.selection() = true;
1072 //lyxerr << "MOTION: " << bv->cursor() << endl;
1080 case LFUN_MOUSE_RELEASE: {
1081 if (cmd.button() == mouse_button::button2)
1085 if (cmd.button() == mouse_button::button1)
1086 bv->haveSelection(cur.selection());
1089 bv->owner()->updateMenubar();
1090 bv->owner()->updateToolbars();
1094 case LFUN_SELFINSERT: {
1095 if (cmd.argument.empty())
1098 // Automatically delete the currently selected
1099 // text and replace it with what is being
1100 // typed in now. Depends on lyxrc settings
1101 // "auto_region_delete", which defaults to
1104 if (lyxrc.auto_region_delete) {
1105 if (cur.selection())
1106 cutSelection(cur, false, false);
1107 bv->haveSelection(false);
1110 cur.clearSelection();
1111 LyXFont const old_font = real_current_font;
1113 // Prevents language turds in new lyxtexts under non-english
1114 BufferParams const & bufparams = cur.buffer().params();
1115 Language const * lang = cur.paragraph().getParLanguage(bufparams);
1116 current_font.setLanguage(lang);
1117 real_current_font.setLanguage(lang);
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);
1128 needsUpdate = redoParagraph(cur.pit());
1130 // update only this paragraph
1131 cur.bv().update(Update::SinglePar | Update::Force);
1134 bv->updateScrollbar();
1139 InsetCommandParams p("url");
1140 string const data = InsetCommandMailer::params2string("url", p);
1141 bv->owner()->getDialogs().show("url", data, 0);
1145 case LFUN_HTMLURL: {
1146 InsetCommandParams p("htmlurl");
1147 string const data = InsetCommandMailer::params2string("url", p);
1148 bv->owner()->getDialogs().show("url", data, 0);
1152 case LFUN_INSERT_LABEL: {
1153 // Try to generate a valid label
1154 string const contents = cmd.argument.empty() ?
1155 cur.getPossibleLabel() : cmd.argument;
1157 InsetCommandParams p("label", contents);
1158 string const data = InsetCommandMailer::params2string("label", p);
1160 if (cmd.argument.empty()) {
1161 bv->owner()->getDialogs().show("label", data, 0);
1163 FuncRequest fr(LFUN_INSET_INSERT, data);
1171 case LFUN_INSET_LIST:
1172 case LFUN_INSET_THEOREM:
1173 case LFUN_INSET_CAPTION:
1175 case LFUN_INSERT_NOTE:
1176 case LFUN_INSERT_CHARSTYLE:
1177 case LFUN_INSERT_BOX:
1178 case LFUN_INSERT_BRANCH:
1179 case LFUN_INSERT_BIBITEM:
1180 case LFUN_INSET_ERT:
1181 case LFUN_INSET_FOOTNOTE:
1182 case LFUN_INSET_MARGINAL:
1183 case LFUN_INSET_OPTARG:
1184 case LFUN_ENVIRONMENT_INSERT:
1185 // Open the inset, and move the current selection
1187 doInsertInset(cur, this, cmd, true, true);
1191 case LFUN_TABULAR_INSERT:
1192 // if there were no arguments, just open the dialog
1193 if (doInsertInset(cur, this, cmd, false, true))
1196 bv->owner()->getDialogs().show("tabularcreate");
1200 case LFUN_INSET_FLOAT:
1201 case LFUN_INSET_WIDE_FLOAT:
1202 case LFUN_INSET_WRAP:
1203 doInsertInset(cur, this, cmd, true, true);
1205 // FIXME: the "Caption" name should not be hardcoded,
1206 // but given by the float definition.
1207 cur.dispatch(FuncRequest(LFUN_LAYOUT, "Caption"));
1210 case LFUN_INDEX_INSERT:
1211 // Just open the inset
1212 doInsertInset(cur, this, cmd, true, false);
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, s2));
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);
1273 cur.dispatch(FuncRequest(LFUN_RIGHT));
1274 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
1280 LyXFont font(LyXFont::ALL_IGNORE);
1281 font.setEmph(LyXFont::TOGGLE);
1282 toggleAndShow(cur, this, font);
1287 LyXFont font(LyXFont::ALL_IGNORE);
1288 font.setSeries(LyXFont::BOLD_SERIES);
1289 toggleAndShow(cur, this, font);
1294 LyXFont font(LyXFont::ALL_IGNORE);
1295 font.setNoun(LyXFont::TOGGLE);
1296 toggleAndShow(cur, this, font);
1301 LyXFont font(LyXFont::ALL_IGNORE);
1302 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
1303 toggleAndShow(cur, this, font);
1308 LyXFont font(LyXFont::ALL_IGNORE);
1309 font.setFamily(LyXFont::SANS_FAMILY);
1310 toggleAndShow(cur, this, font);
1315 LyXFont font(LyXFont::ALL_IGNORE);
1316 font.setFamily(LyXFont::ROMAN_FAMILY);
1317 toggleAndShow(cur, this, font);
1321 case LFUN_DEFAULT: {
1322 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
1323 toggleAndShow(cur, this, font);
1327 case LFUN_UNDERLINE: {
1328 LyXFont font(LyXFont::ALL_IGNORE);
1329 font.setUnderbar(LyXFont::TOGGLE);
1330 toggleAndShow(cur, this, font);
1334 case LFUN_FONT_SIZE: {
1335 LyXFont font(LyXFont::ALL_IGNORE);
1336 font.setLyXSize(cmd.argument);
1337 toggleAndShow(cur, this, font);
1341 case LFUN_LANGUAGE: {
1342 Language const * lang = languages.getLanguage(cmd.argument);
1345 LyXFont font(LyXFont::ALL_IGNORE);
1346 font.setLanguage(lang);
1347 toggleAndShow(cur, this, font);
1352 case LFUN_FREEFONT_APPLY:
1353 toggleAndShow(cur, this, freefont, toggleall);
1354 cur.message(_("Character set"));
1357 // Set the freefont using the contents of \param data dispatched from
1358 // the frontends and apply it at the current cursor location.
1359 case LFUN_FREEFONT_UPDATE: {
1362 if (bv_funcs::string2font(cmd.argument, font, toggle)) {
1365 toggleAndShow(cur, this, freefont, toggleall);
1366 cur.message(_("Character set"));
1371 case LFUN_FINISHED_LEFT:
1372 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1375 case LFUN_FINISHED_RIGHT:
1376 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1380 case LFUN_FINISHED_UP:
1381 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_UP:\n" << cur << endl;
1385 case LFUN_FINISHED_DOWN:
1386 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_DOWN:\n" << cur << endl;
1390 case LFUN_LAYOUT_PARAGRAPH: {
1392 params2string(cur.paragraph(), data);
1393 data = "show\n" + data;
1394 bv->owner()->getDialogs().show("paragraph", data);
1398 case LFUN_PARAGRAPH_UPDATE: {
1399 if (!bv->owner()->getDialogs().visible("paragraph"))
1402 params2string(cur.paragraph(), data);
1404 // Will the paragraph accept changes from the dialog?
1405 InsetBase & inset = cur.inset();
1406 bool const accept = !inset.forceDefaultParagraphs(&inset);
1408 data = "update " + convert<string>(accept) + '\n' + data;
1409 bv->owner()->getDialogs().update("paragraph", data);
1414 case LFUN_CIRCUMFLEX:
1424 case LFUN_SPECIAL_CARON:
1427 case LFUN_HUNG_UMLAUT:
1430 bv->owner()->getLyXFunc().handleKeyFunc(cmd.action);
1431 if (!cmd.argument.empty())
1432 bv->owner()->getIntl().getTransManager()
1433 .TranslateAndInsert(cmd.argument[0], this);
1436 case LFUN_FLOAT_LIST: {
1437 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1438 if (tclass.floats().typeExist(cmd.argument)) {
1439 // not quite sure if we want this...
1441 cur.clearSelection();
1442 breakParagraph(cur);
1444 if (cur.lastpos() != 0) {
1446 breakParagraph(cur);
1449 setLayout(cur, tclass.defaultLayoutName());
1450 setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1451 insertInset(cur, new InsetFloatList(cmd.argument));
1454 lyxerr << "Non-existent float type: "
1455 << cmd.argument << endl;
1460 case LFUN_ACCEPT_CHANGE: {
1465 case LFUN_REJECT_CHANGE: {
1470 case LFUN_THESAURUS_ENTRY: {
1471 string arg = cmd.argument;
1473 arg = cur.selectionAsString(false);
1475 if (arg.size() > 100 || arg.empty()) {
1476 // Get word or selection
1477 selectWordWhenUnderCursor(cur, lyx::WHOLE_WORD);
1478 arg = cur.selectionAsString(false);
1481 bv->owner()->getDialogs().show("thesaurus", arg);
1485 case LFUN_PARAGRAPH_APPLY: {
1486 // Given data, an encoding of the ParagraphParameters
1487 // generated in the Paragraph dialog, this function sets
1488 // the current paragraph appropriately.
1489 istringstream is(cmd.argument);
1492 ParagraphParameters params;
1497 params.labelWidthString(),
1499 cur.message(_("Paragraph layout set"));
1503 case LFUN_INSET_DIALOG_SHOW: {
1504 InsetBase * inset = cur.nextInset();
1506 FuncRequest fr(LFUN_INSET_DIALOG_SHOW);
1507 inset->dispatch(cur, fr);
1513 if (cur.selection()) {
1514 cur.selection() = false;
1517 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1522 lyxerr << BOOST_CURRENT_FUNCTION
1523 << ": Command " << cmd << " not DISPATCHED by LyXText" << endl;
1529 && &oldTopSlice.inset() == &cur.inset()
1530 && oldTopSlice.idx() == cur.idx()
1532 && !cur.selection())
1539 bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
1540 FuncStatus & flag) const
1542 BOOST_ASSERT(cur.text() == this);
1543 LyXFont const & font = real_current_font;
1546 switch (cmd.action) {
1548 case LFUN_DEPTH_MIN:
1549 enable = changeDepthAllowed(cur, DEC_DEPTH);
1552 case LFUN_DEPTH_PLUS:
1553 enable = changeDepthAllowed(cur, INC_DEPTH);
1556 case LFUN_INSET_OPTARG:
1557 enable = numberOfOptArgs(cur.paragraph())
1558 < cur.paragraph().layout()->optionalargs;
1562 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1565 case LFUN_INSERT_BIBITEM:
1566 enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO);
1570 // the functions which insert insets
1571 InsetBase::Code code = InsetBase::NO_CODE;
1572 switch (cmd.action) {
1573 case LFUN_DIALOG_SHOW_NEW_INSET:
1574 if (cmd.argument == "bibitem")
1575 code = InsetBase::BIBITEM_CODE;
1576 else if (cmd.argument == "bibtex")
1577 code = InsetBase::BIBTEX_CODE;
1578 else if (cmd.argument == "box")
1579 code = InsetBase::BOX_CODE;
1580 else if (cmd.argument == "branch")
1581 code = InsetBase::BRANCH_CODE;
1582 else if (cmd.argument == "citation")
1583 code = InsetBase::CITE_CODE;
1584 else if (cmd.argument == "ert")
1585 code = InsetBase::ERT_CODE;
1586 else if (cmd.argument == "external")
1587 code = InsetBase::EXTERNAL_CODE;
1588 else if (cmd.argument == "float")
1589 code = InsetBase::FLOAT_CODE;
1590 else if (cmd.argument == "graphics")
1591 code = InsetBase::GRAPHICS_CODE;
1592 else if (cmd.argument == "include")
1593 code = InsetBase::INCLUDE_CODE;
1594 else if (cmd.argument == "index")
1595 code = InsetBase::INDEX_CODE;
1596 else if (cmd.argument == "label")
1597 code = InsetBase::LABEL_CODE;
1598 else if (cmd.argument == "note")
1599 code = InsetBase::NOTE_CODE;
1600 else if (cmd.argument == "ref")
1601 code = InsetBase::REF_CODE;
1602 else if (cmd.argument == "toc")
1603 code = InsetBase::TOC_CODE;
1604 else if (cmd.argument == "url")
1605 code = InsetBase::URL_CODE;
1606 else if (cmd.argument == "vspace")
1607 code = InsetBase::VSPACE_CODE;
1608 else if (cmd.argument == "wrap")
1609 code = InsetBase::WRAP_CODE;
1612 case LFUN_INSET_ERT:
1613 code = InsetBase::ERT_CODE;
1615 case LFUN_INSET_FOOTNOTE:
1616 code = InsetBase::FOOT_CODE;
1618 case LFUN_TABULAR_INSERT:
1619 code = InsetBase::TABULAR_CODE;
1621 case LFUN_INSET_MARGINAL:
1622 code = InsetBase::MARGIN_CODE;
1624 case LFUN_INSET_FLOAT:
1625 case LFUN_INSET_WIDE_FLOAT:
1626 code = InsetBase::FLOAT_CODE;
1628 case LFUN_INSET_WRAP:
1629 code = InsetBase::WRAP_CODE;
1631 case LFUN_FLOAT_LIST:
1632 code = InsetBase::FLOAT_LIST_CODE;
1635 case LFUN_INSET_LIST:
1636 code = InsetBase::LIST_CODE;
1638 case LFUN_INSET_THEOREM:
1639 code = InsetBase::THEOREM_CODE;
1642 case LFUN_INSET_CAPTION:
1643 code = InsetBase::CAPTION_CODE;
1645 case LFUN_INSERT_NOTE:
1646 code = InsetBase::NOTE_CODE;
1648 case LFUN_INSERT_CHARSTYLE:
1649 code = InsetBase::CHARSTYLE_CODE;
1650 if (buf->params().getLyXTextClass().charstyles().empty())
1653 case LFUN_INSERT_BOX:
1654 code = InsetBase::BOX_CODE;
1656 case LFUN_INSERT_BRANCH:
1657 code = InsetBase::BRANCH_CODE;
1658 if (buf->params().branchlist().empty())
1661 case LFUN_INSERT_LABEL:
1662 code = InsetBase::LABEL_CODE;
1664 case LFUN_INSET_OPTARG:
1665 code = InsetBase::OPTARG_CODE;
1667 case LFUN_ENVIRONMENT_INSERT:
1668 code = InsetBase::BOX_CODE;
1670 case LFUN_INDEX_INSERT:
1671 code = InsetBase::INDEX_CODE;
1673 case LFUN_INDEX_PRINT:
1674 code = InsetBase::INDEX_PRINT_CODE;
1676 case LFUN_TOC_INSERT:
1677 code = InsetBase::TOC_CODE;
1681 code = InsetBase::URL_CODE;
1684 // always allow this, since we will inset a raw quote
1685 // if an inset is not allowed.
1687 case LFUN_HYPHENATION:
1688 case LFUN_LIGATURE_BREAK:
1690 case LFUN_MENU_SEPARATOR:
1692 case LFUN_END_OF_SENTENCE:
1693 code = InsetBase::SPECIALCHAR_CODE;
1695 case LFUN_SPACE_INSERT:
1696 // slight hack: we know this is allowed in math mode
1698 code = InsetBase::SPACE_CODE;
1700 case LFUN_INSET_DIALOG_SHOW: {
1701 InsetBase * inset = cur.nextInset();
1704 code = inset->lyxCode();
1705 if (!(code == InsetBase::INCLUDE_CODE
1706 || code == InsetBase::BIBTEX_CODE
1707 || code == InsetBase::FLOAT_LIST_CODE
1708 || code == InsetBase::TOC_CODE))
1717 if (code != InsetBase::NO_CODE
1718 && (cur.empty() || !cur.inset().insetAllowed(code)))
1723 case LFUN_DIALOG_SHOW_NEW_INSET:
1724 case LFUN_INSET_ERT:
1725 case LFUN_INSERT_BOX:
1726 case LFUN_INSERT_BRANCH:
1727 case LFUN_ENVIRONMENT_INSERT:
1728 case LFUN_INDEX_INSERT:
1729 case LFUN_INDEX_PRINT:
1730 case LFUN_TOC_INSERT:
1734 case LFUN_HYPHENATION:
1735 case LFUN_LIGATURE_BREAK:
1737 case LFUN_MENU_SEPARATOR:
1739 case LFUN_END_OF_SENTENCE:
1740 case LFUN_SPACE_INSERT:
1741 case LFUN_INSET_DIALOG_SHOW:
1744 case LFUN_INSET_MODIFY:
1745 // We need to disable this, because we may get called for a
1747 // InsetTabular::getStatus() -> InsetText::getStatus()
1748 // and we don't handle LFUN_INSET_MODIFY.
1753 flag.setOnOff(font.emph() == LyXFont::ON);
1757 flag.setOnOff(font.noun() == LyXFont::ON);
1761 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1765 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1769 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1773 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1776 case LFUN_DELETE_WORD_FORWARD:
1777 case LFUN_DELETE_WORD_BACKWARD:
1778 case LFUN_DELETE_LINE_FORWARD:
1779 case LFUN_WORDRIGHT:
1789 case LFUN_UP_PARAGRAPHSEL:
1790 case LFUN_DOWN_PARAGRAPHSEL:
1795 case LFUN_WORDRIGHTSEL:
1796 case LFUN_WORDLEFTSEL:
1798 case LFUN_UP_PARAGRAPH:
1799 case LFUN_DOWN_PARAGRAPH:
1804 case LFUN_BREAKLINE:
1806 case LFUN_DELETE_SKIP:
1807 case LFUN_BACKSPACE:
1808 case LFUN_BACKSPACE_SKIP:
1809 case LFUN_BREAKPARAGRAPH:
1810 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1811 case LFUN_BREAKPARAGRAPH_SKIP:
1812 case LFUN_PARAGRAPH_SPACING:
1813 case LFUN_INSET_INSERT:
1814 case LFUN_NEXT_INSET_TOGGLE:
1815 case LFUN_UPCASE_WORD:
1816 case LFUN_LOWCASE_WORD:
1817 case LFUN_CAPITALIZE_WORD:
1818 case LFUN_TRANSPOSE_CHARS:
1825 case LFUN_GETLAYOUT:
1827 case LFUN_PASTESELECTION:
1828 case LFUN_DATE_INSERT:
1829 case LFUN_SELFINSERT:
1830 case LFUN_INSERT_LABEL:
1831 case LFUN_INSERT_NOTE:
1832 case LFUN_INSERT_CHARSTYLE:
1833 case LFUN_INSET_FLOAT:
1834 case LFUN_INSET_FOOTNOTE:
1835 case LFUN_INSET_MARGINAL:
1836 case LFUN_INSET_WIDE_FLOAT:
1837 case LFUN_INSET_WRAP:
1838 case LFUN_TABULAR_INSERT:
1839 case LFUN_INSERT_LINE:
1840 case LFUN_INSERT_PAGEBREAK:
1841 case LFUN_MATH_DISPLAY:
1842 case LFUN_MATH_IMPORT_SELECTION:
1843 case LFUN_MATH_MODE:
1844 case LFUN_MATH_MACRO:
1845 case LFUN_INSERT_MATH:
1846 case LFUN_INSERT_MATRIX:
1847 case LFUN_MATH_DELIM:
1848 case LFUN_SUBSCRIPT:
1849 case LFUN_SUPERSCRIPT:
1851 case LFUN_UNDERLINE:
1852 case LFUN_FONT_SIZE:
1854 case LFUN_FREEFONT_APPLY:
1855 case LFUN_FREEFONT_UPDATE:
1856 case LFUN_LAYOUT_PARAGRAPH:
1857 case LFUN_PARAGRAPH_UPDATE:
1859 case LFUN_CIRCUMFLEX:
1869 case LFUN_SPECIAL_CARON:
1872 case LFUN_HUNG_UMLAUT:
1875 case LFUN_FLOAT_LIST:
1876 case LFUN_ACCEPT_CHANGE:
1877 case LFUN_REJECT_CHANGE:
1878 case LFUN_THESAURUS_ENTRY:
1879 case LFUN_PARAGRAPH_APPLY:
1881 case LFUN_KEYMAP_TOGGLE:
1883 case LFUN_BEGINNINGBUF:
1884 case LFUN_BEGINNINGBUFSEL:
1885 case LFUN_ENDBUFSEL:
1886 // these are handled in our dispatch()
1893 flag.enabled(enable);