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();
923 if (cmd.argument == "paragraph")
924 insertStringAsParagraphs(cur, clip);
926 insertStringAsLines(cur, clip);
932 lyx::cap::replaceSelection(cur);
933 Paragraph & par = cur.paragraph();
934 lyx::pos_type pos = cur.pos();
938 else if (cur.prevInset() && cur.prevInset()->isSpace())
941 c = par.getChar(pos - 1);
943 LyXLayout_ptr const & style = par.layout();
945 BufferParams const & bufparams = bv->buffer()->params();
946 if (!style->pass_thru
947 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
948 string arg = cmd.argument;
950 cur.insert(new InsetQuotes(c,
951 bufparams.quotes_language,
952 InsetQuotes::SingleQ));
953 else if (arg == "double")
954 cur.insert(new InsetQuotes(c,
955 bufparams.quotes_language,
956 InsetQuotes::DoubleQ));
958 cur.insert(new InsetQuotes(c, bufparams));
962 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
966 case LFUN_DATE_INSERT:
967 if (cmd.argument.empty())
968 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT,
969 lyx::formatted_time(lyx::current_time())));
971 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT,
972 lyx::formatted_time(lyx::current_time(), cmd.argument)));
975 case LFUN_MOUSE_TRIPLE:
976 if (cmd.button() == mouse_button::button1) {
982 bv->haveSelection(cur.selection());
986 case LFUN_MOUSE_DOUBLE:
987 if (cmd.button() == mouse_button::button1) {
988 selectWord(cur, lyx::WHOLE_WORD_STRICT);
990 bv->haveSelection(cur.selection());
994 // Single-click on work area
995 case LFUN_MOUSE_PRESS: {
996 // Right click on a footnote flag opens float menu
997 if (cmd.button() == mouse_button::button3) {
998 cur.clearSelection();
1002 // Middle button press pastes if we have a selection
1003 // We do this here as if the selection was inside an inset
1004 // it could get cleared on the unlocking of the inset so
1005 // we have to check this first
1006 bool paste_internally = false;
1007 if (cmd.button() == mouse_button::button2 && cur.selection()) {
1008 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1009 paste_internally = true;
1012 // Clear the selection
1013 cur.clearSelection();
1015 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1020 // Has the cursor just left the inset?
1021 if (bv->cursor().inMathed() && !cur.inMathed())
1022 bv->cursor().inset().notifyCursorLeaves(bv->cursor());
1027 // Insert primary selection with middle mouse
1028 // if there is a local selection in the current buffer,
1030 if (cmd.button() == mouse_button::button2) {
1031 if (paste_internally)
1032 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1034 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1040 case LFUN_MOUSE_MOTION: {
1041 // Only use motion with button 1
1042 //if (cmd.button() != mouse_button::button1)
1045 // ignore motions deeper nested than the real anchor
1046 LCursor & bvcur = cur.bv().cursor();
1047 if (bvcur.anchor_.hasPart(cur)) {
1048 CursorSlice old = bvcur.top();
1050 int const wh = bv->workHeight();
1051 int const y = std::max(0, std::min(wh - 1, cmd.y));
1053 setCursorFromCoordinates(cur, cmd.x, y);
1054 cur.x_target() = cmd.x;
1059 // This is to allow jumping over large insets
1060 if (cur.top() == old) {
1067 if (cur.top() == old)
1070 // don't set anchor_
1071 bvcur.setCursor(cur);
1072 bvcur.selection() = true;
1073 //lyxerr << "MOTION: " << bv->cursor() << endl;
1081 case LFUN_MOUSE_RELEASE: {
1082 if (cmd.button() == mouse_button::button2)
1086 if (cmd.button() == mouse_button::button1)
1087 bv->haveSelection(cur.selection());
1090 bv->owner()->updateMenubar();
1091 bv->owner()->updateToolbars();
1095 case LFUN_SELFINSERT: {
1096 if (cmd.argument.empty())
1099 // Automatically delete the currently selected
1100 // text and replace it with what is being
1101 // typed in now. Depends on lyxrc settings
1102 // "auto_region_delete", which defaults to
1105 if (lyxrc.auto_region_delete) {
1106 if (cur.selection())
1107 cutSelection(cur, false, false);
1108 bv->haveSelection(false);
1111 cur.clearSelection();
1112 LyXFont const old_font = real_current_font;
1114 // Prevents language turds in new lyxtexts under non-english
1115 BufferParams const & bufparams = cur.buffer().params();
1116 Language const * lang = cur.paragraph().getParLanguage(bufparams);
1117 current_font.setLanguage(lang);
1118 real_current_font.setLanguage(lang);
1120 string::const_iterator cit = cmd.argument.begin();
1121 string::const_iterator end = cmd.argument.end();
1122 for (; cit != end; ++cit)
1123 bv->owner()->getIntl().getTransManager().
1124 TranslateAndInsert(*cit, this);
1127 moveCursor(cur, false);
1129 needsUpdate = redoParagraph(cur.pit());
1131 // update only this paragraph
1132 cur.bv().update(Update::SinglePar | Update::Force);
1135 bv->updateScrollbar();
1140 InsetCommandParams p("url");
1141 string const data = InsetCommandMailer::params2string("url", p);
1142 bv->owner()->getDialogs().show("url", data, 0);
1146 case LFUN_HTMLURL: {
1147 InsetCommandParams p("htmlurl");
1148 string const data = InsetCommandMailer::params2string("url", p);
1149 bv->owner()->getDialogs().show("url", data, 0);
1153 case LFUN_INSERT_LABEL: {
1154 // Try to generate a valid label
1155 string const contents = cmd.argument.empty() ?
1156 cur.getPossibleLabel() : cmd.argument;
1158 InsetCommandParams p("label", contents);
1159 string const data = InsetCommandMailer::params2string("label", p);
1161 if (cmd.argument.empty()) {
1162 bv->owner()->getDialogs().show("label", data, 0);
1164 FuncRequest fr(LFUN_INSET_INSERT, data);
1172 case LFUN_INSET_LIST:
1173 case LFUN_INSET_THEOREM:
1174 case LFUN_INSET_CAPTION:
1176 case LFUN_INSERT_NOTE:
1177 case LFUN_INSERT_CHARSTYLE:
1178 case LFUN_INSERT_BOX:
1179 case LFUN_INSERT_BRANCH:
1180 case LFUN_INSERT_BIBITEM:
1181 case LFUN_INSET_ERT:
1182 case LFUN_INSET_FOOTNOTE:
1183 case LFUN_INSET_MARGINAL:
1184 case LFUN_INSET_OPTARG:
1185 case LFUN_ENVIRONMENT_INSERT:
1186 // Open the inset, and move the current selection
1188 doInsertInset(cur, this, cmd, true, true);
1192 case LFUN_TABULAR_INSERT:
1193 // if there were no arguments, just open the dialog
1194 if (doInsertInset(cur, this, cmd, false, true))
1197 bv->owner()->getDialogs().show("tabularcreate");
1201 case LFUN_INSET_FLOAT:
1202 case LFUN_INSET_WIDE_FLOAT:
1203 case LFUN_INSET_WRAP:
1204 doInsertInset(cur, this, cmd, true, true);
1206 // FIXME: the "Caption" name should not be hardcoded,
1207 // but given by the float definition.
1208 cur.dispatch(FuncRequest(LFUN_LAYOUT, "Caption"));
1211 case LFUN_INDEX_INSERT:
1212 // Just open the inset
1213 doInsertInset(cur, this, cmd, true, false);
1217 case LFUN_INDEX_PRINT:
1218 case LFUN_TOC_INSERT:
1220 case LFUN_INSERT_LINE:
1221 case LFUN_INSERT_PAGEBREAK:
1223 doInsertInset(cur, this, cmd, false, false);
1227 case LFUN_DEPTH_MIN:
1228 changeDepth(cur, DEC_DEPTH);
1231 case LFUN_DEPTH_PLUS:
1232 changeDepth(cur, INC_DEPTH);
1235 case LFUN_MATH_DISPLAY:
1236 mathDispatch(cur, cmd, true);
1239 case LFUN_MATH_IMPORT_SELECTION:
1240 case LFUN_MATH_MODE:
1241 if (cmd.argument == "on")
1242 // don't pass "on" as argument
1243 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1245 mathDispatch(cur, cmd, false);
1248 case LFUN_MATH_MACRO:
1249 if (cmd.argument.empty())
1250 cur.errorMessage(N_("Missing argument"));
1252 string s = cmd.argument;
1253 string const s1 = token(s, ' ', 1);
1254 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1255 string const s2 = token(s, ' ', 2);
1256 string const type = s2.empty() ? "newcommand" : s2;
1257 cur.insert(new MathMacroTemplate(token(s, ' ', 0), nargs, type));
1258 //cur.nextInset()->edit(cur, true);
1262 // passthrough hat and underscore outside mathed:
1263 case LFUN_SUBSCRIPT:
1264 mathDispatch(cur, FuncRequest(LFUN_SELFINSERT, "_"), false);
1266 case LFUN_SUPERSCRIPT:
1267 mathDispatch(cur, FuncRequest(LFUN_SELFINSERT, "^"), false);
1270 case LFUN_INSERT_MATH:
1271 case LFUN_INSERT_MATRIX:
1272 case LFUN_MATH_DELIM: {
1273 cur.insert(new MathHullInset);
1274 cur.dispatch(FuncRequest(LFUN_RIGHT));
1275 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
1281 LyXFont font(LyXFont::ALL_IGNORE);
1282 font.setEmph(LyXFont::TOGGLE);
1283 toggleAndShow(cur, this, font);
1288 LyXFont font(LyXFont::ALL_IGNORE);
1289 font.setSeries(LyXFont::BOLD_SERIES);
1290 toggleAndShow(cur, this, font);
1295 LyXFont font(LyXFont::ALL_IGNORE);
1296 font.setNoun(LyXFont::TOGGLE);
1297 toggleAndShow(cur, this, font);
1302 LyXFont font(LyXFont::ALL_IGNORE);
1303 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
1304 toggleAndShow(cur, this, font);
1309 LyXFont font(LyXFont::ALL_IGNORE);
1310 font.setFamily(LyXFont::SANS_FAMILY);
1311 toggleAndShow(cur, this, font);
1316 LyXFont font(LyXFont::ALL_IGNORE);
1317 font.setFamily(LyXFont::ROMAN_FAMILY);
1318 toggleAndShow(cur, this, font);
1322 case LFUN_DEFAULT: {
1323 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
1324 toggleAndShow(cur, this, font);
1328 case LFUN_UNDERLINE: {
1329 LyXFont font(LyXFont::ALL_IGNORE);
1330 font.setUnderbar(LyXFont::TOGGLE);
1331 toggleAndShow(cur, this, font);
1335 case LFUN_FONT_SIZE: {
1336 LyXFont font(LyXFont::ALL_IGNORE);
1337 font.setLyXSize(cmd.argument);
1338 toggleAndShow(cur, this, font);
1342 case LFUN_LANGUAGE: {
1343 Language const * lang = languages.getLanguage(cmd.argument);
1346 LyXFont font(LyXFont::ALL_IGNORE);
1347 font.setLanguage(lang);
1348 toggleAndShow(cur, this, font);
1353 case LFUN_FREEFONT_APPLY:
1354 toggleAndShow(cur, this, freefont, toggleall);
1355 cur.message(_("Character set"));
1358 // Set the freefont using the contents of \param data dispatched from
1359 // the frontends and apply it at the current cursor location.
1360 case LFUN_FREEFONT_UPDATE: {
1363 if (bv_funcs::string2font(cmd.argument, font, toggle)) {
1366 toggleAndShow(cur, this, freefont, toggleall);
1367 cur.message(_("Character set"));
1372 case LFUN_FINISHED_LEFT:
1373 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1376 case LFUN_FINISHED_RIGHT:
1377 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1381 case LFUN_FINISHED_UP:
1382 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_UP:\n" << cur << endl;
1386 case LFUN_FINISHED_DOWN:
1387 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_DOWN:\n" << cur << endl;
1391 case LFUN_LAYOUT_PARAGRAPH: {
1393 params2string(cur.paragraph(), data);
1394 data = "show\n" + data;
1395 bv->owner()->getDialogs().show("paragraph", data);
1399 case LFUN_PARAGRAPH_UPDATE: {
1400 if (!bv->owner()->getDialogs().visible("paragraph"))
1403 params2string(cur.paragraph(), data);
1405 // Will the paragraph accept changes from the dialog?
1406 InsetBase & inset = cur.inset();
1407 bool const accept = !inset.forceDefaultParagraphs(&inset);
1409 data = "update " + convert<string>(accept) + '\n' + data;
1410 bv->owner()->getDialogs().update("paragraph", data);
1415 case LFUN_CIRCUMFLEX:
1425 case LFUN_SPECIAL_CARON:
1428 case LFUN_HUNG_UMLAUT:
1431 bv->owner()->getLyXFunc().handleKeyFunc(cmd.action);
1432 if (!cmd.argument.empty())
1433 bv->owner()->getIntl().getTransManager()
1434 .TranslateAndInsert(cmd.argument[0], this);
1437 case LFUN_FLOAT_LIST: {
1438 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1439 if (tclass.floats().typeExist(cmd.argument)) {
1440 // not quite sure if we want this...
1442 cur.clearSelection();
1443 breakParagraph(cur);
1445 if (cur.lastpos() != 0) {
1447 breakParagraph(cur);
1450 setLayout(cur, tclass.defaultLayoutName());
1451 setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1452 insertInset(cur, new InsetFloatList(cmd.argument));
1455 lyxerr << "Non-existent float type: "
1456 << cmd.argument << endl;
1461 case LFUN_ACCEPT_CHANGE: {
1466 case LFUN_REJECT_CHANGE: {
1471 case LFUN_THESAURUS_ENTRY: {
1472 string arg = cmd.argument;
1474 arg = cur.selectionAsString(false);
1476 if (arg.size() > 100 || arg.empty()) {
1477 // Get word or selection
1478 selectWordWhenUnderCursor(cur, lyx::WHOLE_WORD);
1479 arg = cur.selectionAsString(false);
1482 bv->owner()->getDialogs().show("thesaurus", arg);
1486 case LFUN_PARAGRAPH_APPLY: {
1487 // Given data, an encoding of the ParagraphParameters
1488 // generated in the Paragraph dialog, this function sets
1489 // the current paragraph appropriately.
1490 istringstream is(cmd.argument);
1493 ParagraphParameters params;
1498 params.labelWidthString(),
1500 cur.message(_("Paragraph layout set"));
1504 case LFUN_INSET_DIALOG_SHOW: {
1505 InsetBase * inset = cur.nextInset();
1507 FuncRequest fr(LFUN_INSET_DIALOG_SHOW);
1508 inset->dispatch(cur, fr);
1514 if (cur.selection()) {
1515 cur.selection() = false;
1518 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1523 lyxerr[Debug::ACTION]
1524 << BOOST_CURRENT_FUNCTION
1525 << ": Command " << cmd
1526 << " not DISPATCHED by LyXText" << endl;
1532 && &oldTopSlice.inset() == &cur.inset()
1533 && oldTopSlice.idx() == cur.idx()
1535 && !cur.selection())
1542 bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
1543 FuncStatus & flag) const
1545 BOOST_ASSERT(cur.text() == this);
1546 LyXFont const & font = real_current_font;
1549 switch (cmd.action) {
1551 case LFUN_DEPTH_MIN:
1552 enable = changeDepthAllowed(cur, DEC_DEPTH);
1555 case LFUN_DEPTH_PLUS:
1556 enable = changeDepthAllowed(cur, INC_DEPTH);
1559 case LFUN_INSET_OPTARG:
1560 enable = numberOfOptArgs(cur.paragraph())
1561 < cur.paragraph().layout()->optionalargs;
1565 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1568 case LFUN_INSERT_BIBITEM:
1569 enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO);
1573 // the functions which insert insets
1574 InsetBase::Code code = InsetBase::NO_CODE;
1575 switch (cmd.action) {
1576 case LFUN_DIALOG_SHOW_NEW_INSET:
1577 if (cmd.argument == "bibitem")
1578 code = InsetBase::BIBITEM_CODE;
1579 else if (cmd.argument == "bibtex")
1580 code = InsetBase::BIBTEX_CODE;
1581 else if (cmd.argument == "box")
1582 code = InsetBase::BOX_CODE;
1583 else if (cmd.argument == "branch")
1584 code = InsetBase::BRANCH_CODE;
1585 else if (cmd.argument == "citation")
1586 code = InsetBase::CITE_CODE;
1587 else if (cmd.argument == "ert")
1588 code = InsetBase::ERT_CODE;
1589 else if (cmd.argument == "external")
1590 code = InsetBase::EXTERNAL_CODE;
1591 else if (cmd.argument == "float")
1592 code = InsetBase::FLOAT_CODE;
1593 else if (cmd.argument == "graphics")
1594 code = InsetBase::GRAPHICS_CODE;
1595 else if (cmd.argument == "include")
1596 code = InsetBase::INCLUDE_CODE;
1597 else if (cmd.argument == "index")
1598 code = InsetBase::INDEX_CODE;
1599 else if (cmd.argument == "label")
1600 code = InsetBase::LABEL_CODE;
1601 else if (cmd.argument == "note")
1602 code = InsetBase::NOTE_CODE;
1603 else if (cmd.argument == "ref")
1604 code = InsetBase::REF_CODE;
1605 else if (cmd.argument == "toc")
1606 code = InsetBase::TOC_CODE;
1607 else if (cmd.argument == "url")
1608 code = InsetBase::URL_CODE;
1609 else if (cmd.argument == "vspace")
1610 code = InsetBase::VSPACE_CODE;
1611 else if (cmd.argument == "wrap")
1612 code = InsetBase::WRAP_CODE;
1615 case LFUN_INSET_ERT:
1616 code = InsetBase::ERT_CODE;
1618 case LFUN_INSET_FOOTNOTE:
1619 code = InsetBase::FOOT_CODE;
1621 case LFUN_TABULAR_INSERT:
1622 code = InsetBase::TABULAR_CODE;
1624 case LFUN_INSET_MARGINAL:
1625 code = InsetBase::MARGIN_CODE;
1627 case LFUN_INSET_FLOAT:
1628 case LFUN_INSET_WIDE_FLOAT:
1629 code = InsetBase::FLOAT_CODE;
1631 case LFUN_INSET_WRAP:
1632 code = InsetBase::WRAP_CODE;
1634 case LFUN_FLOAT_LIST:
1635 code = InsetBase::FLOAT_LIST_CODE;
1638 case LFUN_INSET_LIST:
1639 code = InsetBase::LIST_CODE;
1641 case LFUN_INSET_THEOREM:
1642 code = InsetBase::THEOREM_CODE;
1645 case LFUN_INSET_CAPTION:
1646 code = InsetBase::CAPTION_CODE;
1648 case LFUN_INSERT_NOTE:
1649 code = InsetBase::NOTE_CODE;
1651 case LFUN_INSERT_CHARSTYLE:
1652 code = InsetBase::CHARSTYLE_CODE;
1653 if (buf->params().getLyXTextClass().charstyles().empty())
1656 case LFUN_INSERT_BOX:
1657 code = InsetBase::BOX_CODE;
1659 case LFUN_INSERT_BRANCH:
1660 code = InsetBase::BRANCH_CODE;
1661 if (buf->params().branchlist().empty())
1664 case LFUN_INSERT_LABEL:
1665 code = InsetBase::LABEL_CODE;
1667 case LFUN_INSET_OPTARG:
1668 code = InsetBase::OPTARG_CODE;
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;
1703 case LFUN_INSET_DIALOG_SHOW: {
1704 InsetBase * inset = cur.nextInset();
1707 code = inset->lyxCode();
1708 if (!(code == InsetBase::INCLUDE_CODE
1709 || code == InsetBase::BIBTEX_CODE
1710 || code == InsetBase::FLOAT_LIST_CODE
1711 || code == InsetBase::TOC_CODE))
1720 if (code != InsetBase::NO_CODE
1721 && (cur.empty() || !cur.inset().insetAllowed(code)))
1726 case LFUN_DIALOG_SHOW_NEW_INSET:
1727 case LFUN_INSET_ERT:
1728 case LFUN_INSERT_BOX:
1729 case LFUN_INSERT_BRANCH:
1730 case LFUN_ENVIRONMENT_INSERT:
1731 case LFUN_INDEX_INSERT:
1732 case LFUN_INDEX_PRINT:
1733 case LFUN_TOC_INSERT:
1737 case LFUN_HYPHENATION:
1738 case LFUN_LIGATURE_BREAK:
1740 case LFUN_MENU_SEPARATOR:
1742 case LFUN_END_OF_SENTENCE:
1743 case LFUN_SPACE_INSERT:
1744 case LFUN_INSET_DIALOG_SHOW:
1747 case LFUN_INSET_MODIFY:
1748 // We need to disable this, because we may get called for a
1750 // InsetTabular::getStatus() -> InsetText::getStatus()
1751 // and we don't handle LFUN_INSET_MODIFY.
1756 flag.setOnOff(font.emph() == LyXFont::ON);
1760 flag.setOnOff(font.noun() == LyXFont::ON);
1764 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1768 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1772 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1776 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1779 case LFUN_DELETE_WORD_FORWARD:
1780 case LFUN_DELETE_WORD_BACKWARD:
1781 case LFUN_DELETE_LINE_FORWARD:
1782 case LFUN_WORDRIGHT:
1792 case LFUN_UP_PARAGRAPHSEL:
1793 case LFUN_DOWN_PARAGRAPHSEL:
1798 case LFUN_WORDRIGHTSEL:
1799 case LFUN_WORDLEFTSEL:
1801 case LFUN_UP_PARAGRAPH:
1802 case LFUN_DOWN_PARAGRAPH:
1807 case LFUN_BREAKLINE:
1809 case LFUN_DELETE_SKIP:
1810 case LFUN_BACKSPACE:
1811 case LFUN_BACKSPACE_SKIP:
1812 case LFUN_BREAKPARAGRAPH:
1813 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1814 case LFUN_BREAKPARAGRAPH_SKIP:
1815 case LFUN_PARAGRAPH_SPACING:
1816 case LFUN_INSET_INSERT:
1817 case LFUN_NEXT_INSET_TOGGLE:
1818 case LFUN_UPCASE_WORD:
1819 case LFUN_LOWCASE_WORD:
1820 case LFUN_CAPITALIZE_WORD:
1821 case LFUN_TRANSPOSE_CHARS:
1828 case LFUN_GETLAYOUT:
1830 case LFUN_PASTESELECTION:
1831 case LFUN_DATE_INSERT:
1832 case LFUN_SELFINSERT:
1833 case LFUN_INSERT_LABEL:
1834 case LFUN_INSERT_NOTE:
1835 case LFUN_INSERT_CHARSTYLE:
1836 case LFUN_INSET_FLOAT:
1837 case LFUN_INSET_FOOTNOTE:
1838 case LFUN_INSET_MARGINAL:
1839 case LFUN_INSET_WIDE_FLOAT:
1840 case LFUN_INSET_WRAP:
1841 case LFUN_TABULAR_INSERT:
1842 case LFUN_INSERT_LINE:
1843 case LFUN_INSERT_PAGEBREAK:
1844 case LFUN_MATH_DISPLAY:
1845 case LFUN_MATH_IMPORT_SELECTION:
1846 case LFUN_MATH_MODE:
1847 case LFUN_MATH_MACRO:
1848 case LFUN_INSERT_MATH:
1849 case LFUN_INSERT_MATRIX:
1850 case LFUN_MATH_DELIM:
1851 case LFUN_SUBSCRIPT:
1852 case LFUN_SUPERSCRIPT:
1854 case LFUN_UNDERLINE:
1855 case LFUN_FONT_SIZE:
1857 case LFUN_FREEFONT_APPLY:
1858 case LFUN_FREEFONT_UPDATE:
1859 case LFUN_LAYOUT_PARAGRAPH:
1860 case LFUN_PARAGRAPH_UPDATE:
1862 case LFUN_CIRCUMFLEX:
1872 case LFUN_SPECIAL_CARON:
1875 case LFUN_HUNG_UMLAUT:
1878 case LFUN_FLOAT_LIST:
1879 case LFUN_ACCEPT_CHANGE:
1880 case LFUN_REJECT_CHANGE:
1881 case LFUN_THESAURUS_ENTRY:
1882 case LFUN_PARAGRAPH_APPLY:
1884 case LFUN_KEYMAP_TOGGLE:
1886 case LFUN_BEGINNINGBUF:
1887 case LFUN_BEGINNINGBUFSEL:
1888 case LFUN_ENDBUFSEL:
1889 // these are handled in our dispatch()
1896 flag.enabled(enable);