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() && cur.boundary() == oldBoundary) {
420 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
426 //lyxerr << "handle LFUN_LEFT[SEL]:\n" << cur << endl;
427 cur.selHandle(cmd.action == LFUN_LEFTSEL);
428 if (isRTL(cur.paragraph()))
429 needsUpdate = cursorRight(cur);
431 needsUpdate = cursorLeft(cur);
432 if (!needsUpdate && oldTopSlice == cur.top() &&
433 cur.boundary() == oldBoundary)
436 cmd = FuncRequest(LFUN_FINISHED_LEFT);
443 //lyxerr << "handle LFUN_UP[SEL]:\n" << cur << endl;
444 cur.selHandle(cmd.action == LFUN_UPSEL);
445 needsUpdate = cursorUp(cur);
446 if (!needsUpdate && oldTopSlice == cur.top() &&
447 cur.boundary() == oldBoundary)
450 cmd = FuncRequest(LFUN_FINISHED_UP);
457 //lyxerr << "handle LFUN_DOWN[SEL]:\n" << cur << endl;
458 cur.selHandle(cmd.action == LFUN_DOWNSEL);
459 needsUpdate = cursorDown(cur);
460 if (!needsUpdate && oldTopSlice == cur.top() &&
461 cur.boundary() == oldBoundary)
464 cmd = FuncRequest(LFUN_FINISHED_DOWN);
468 case LFUN_UP_PARAGRAPH:
470 cur.clearSelection();
471 needsUpdate = cursorUpParagraph(cur);
472 finishChange(cur, false);
475 case LFUN_UP_PARAGRAPHSEL:
476 if (!cur.selection())
478 cursorUpParagraph(cur);
479 finishChange(cur, true);
482 case LFUN_DOWN_PARAGRAPH:
484 cur.clearSelection();
485 needsUpdate = cursorDownParagraph(cur);
486 finishChange(cur, false);
489 case LFUN_DOWN_PARAGRAPHSEL:
490 if (!cur.selection())
492 cursorDownParagraph(cur);
493 finishChange(cur, true);
498 if (!cur.selection())
501 finishChange(cur, true);
506 if (!cur.selection())
509 finishChange(cur, true);
514 if (!cur.selection())
517 finishChange(cur, true);
522 if (!cur.selection())
525 finishChange(cur, true);
528 case LFUN_WORDRIGHTSEL:
529 if (!cur.selection())
531 if (isRTL(cur.paragraph()))
532 cursorLeftOneWord(cur);
534 cursorRightOneWord(cur);
535 finishChange(cur, true);
538 case LFUN_WORDLEFTSEL:
539 if (!cur.selection())
541 if (isRTL(cur.paragraph()))
542 cursorRightOneWord(cur);
544 cursorLeftOneWord(cur);
545 finishChange(cur, true);
549 selectWord(cur, lyx::WHOLE_WORD);
550 finishChange(cur, true);
557 cur.clearSelection();
558 finishChange(cur, false);
559 if (cur.pit() == 0 && cur.textRow().pos() == 0) {
561 cmd = FuncRequest(LFUN_FINISHED_UP);
563 needsUpdate = cursorPrevious(cur);
570 cur.clearSelection();
571 finishChange(cur, false);
572 if (cur.pit() == cur.lastpit()
573 && cur.textRow().endpos() == cur.lastpos()) {
575 cmd = FuncRequest(LFUN_FINISHED_DOWN);
577 needsUpdate = cursorNext(cur);
583 cur.clearSelection();
585 finishChange(cur, false);
590 cur.clearSelection();
592 finishChange(cur, false);
595 case LFUN_BREAKLINE: {
596 // Not allowed by LaTeX (labels or empty par)
597 if (cur.pos() > cur.paragraph().beginOfBody()) {
598 lyx::cap::replaceSelection(cur);
599 cur.insert(new InsetNewline);
601 moveCursor(cur, false);
607 if (!cur.selection()) {
610 // It is possible to make it a lot faster still
611 // just comment out the line below...
613 cutSelection(cur, true, false);
615 moveCursor(cur, false);
618 case LFUN_DELETE_SKIP:
619 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
620 if (!cur.selection()) {
621 if (cur.pos() == cur.lastpos()) {
628 cutSelection(cur, true, false);
634 if (!cur.selection()) {
635 if (bv->owner()->getIntl().getTransManager().backspace()) {
638 // It is possible to make it a lot faster still
639 // just comment out the line below...
642 cutSelection(cur, true, false);
647 case LFUN_BACKSPACE_SKIP:
648 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
649 if (!cur.selection()) {
653 //CursorSlice cur = cursor();
657 cutSelection(cur, true, false);
661 case LFUN_BREAKPARAGRAPH:
662 lyx::cap::replaceSelection(cur);
663 breakParagraph(cur, 0);
668 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
669 lyx::cap::replaceSelection(cur);
670 breakParagraph(cur, 1);
675 case LFUN_BREAKPARAGRAPH_SKIP: {
676 // When at the beginning of a paragraph, remove
677 // indentation. Otherwise, do the same as LFUN_BREAKPARAGRAPH.
678 lyx::cap::replaceSelection(cur);
680 cur.paragraph().params().labelWidthString(string());
682 breakParagraph(cur, 0);
688 case LFUN_PARAGRAPH_SPACING: {
689 Paragraph & par = cur.paragraph();
690 Spacing::Space cur_spacing = par.params().spacing().getSpace();
691 string cur_value = "1.0";
692 if (cur_spacing == Spacing::Other)
693 cur_value = par.params().spacing().getValueAsString();
695 istringstream is(cmd.argument);
698 Spacing::Space new_spacing = cur_spacing;
699 string new_value = cur_value;
701 lyxerr << "Missing argument to `paragraph-spacing'"
703 } else if (tmp == "single") {
704 new_spacing = Spacing::Single;
705 } else if (tmp == "onehalf") {
706 new_spacing = Spacing::Onehalf;
707 } else if (tmp == "double") {
708 new_spacing = Spacing::Double;
709 } else if (tmp == "other") {
710 new_spacing = Spacing::Other;
711 string tmpval = "0.0";
713 lyxerr << "new_value = " << tmpval << endl;
716 } else if (tmp == "default") {
717 new_spacing = Spacing::Default;
719 lyxerr << _("Unknown spacing argument: ")
720 << cmd.argument << endl;
722 if (cur_spacing != new_spacing || cur_value != new_value)
723 par.params().spacing(Spacing(new_spacing, new_value));
727 case LFUN_INSET_INSERT: {
729 InsetBase * inset = createInset(bv, cmd);
731 insertInset(cur, inset);
737 case LFUN_INSET_SETTINGS:
738 cur.inset().showInsetDialog(bv);
741 case LFUN_NEXT_INSET_TOGGLE: {
742 InsetBase * inset = cur.nextInset();
744 cur.clearSelection();
745 FuncRequest fr = cmd;
746 fr.action = LFUN_INSET_TOGGLE;
747 inset->dispatch(cur, fr);
752 case LFUN_KEYMAP_TOGGLE:
753 cur.clearSelection();
757 case LFUN_SPACE_INSERT:
758 if (cur.paragraph().layout()->free_spacing)
759 insertChar(cur, ' ');
761 doInsertInset(cur, this, cmd, false, false);
764 moveCursor(cur, false);
767 case LFUN_HYPHENATION:
768 specialChar(cur, InsetSpecialChar::HYPHENATION);
771 case LFUN_LIGATURE_BREAK:
772 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
776 specialChar(cur, InsetSpecialChar::LDOTS);
779 case LFUN_END_OF_SENTENCE:
780 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
783 case LFUN_MENU_SEPARATOR:
784 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
787 case LFUN_UPCASE_WORD:
788 changeCase(cur, LyXText::text_uppercase);
791 case LFUN_LOWCASE_WORD:
792 changeCase(cur, LyXText::text_lowercase);
795 case LFUN_CAPITALIZE_WORD:
796 changeCase(cur, LyXText::text_capitalization);
799 case LFUN_TRANSPOSE_CHARS:
804 cur.message(_("Paste"));
805 lyx::cap::replaceSelection(cur);
807 #warning FIXME Check if the arg is in the domain of available selections.
809 if (isStrUnsignedInt(cmd.argument))
810 pasteSelection(cur, convert<unsigned int>(cmd.argument));
812 pasteSelection(cur, 0);
813 cur.clearSelection(); // bug 393
819 cutSelection(cur, true, true);
820 cur.message(_("Cut"));
825 cur.message(_("Copy"));
829 cur.message(convert<string>(cursorX(cur.top(), cur.boundary())) + ' '
830 + convert<string>(cursorY(cur.top(), cur.boundary())));
836 istringstream is(cmd.argument);
839 lyxerr << "SETXY: Could not parse coordinates in '"
840 << cmd.argument << std::endl;
842 setCursorFromCoordinates(cur, x, y);
847 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
849 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
856 cur.message(cur.paragraph().layout()->name());
860 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
861 << cmd.argument << endl;
863 // This is not the good solution to the empty argument
864 // problem, but it will hopefully suffice for 1.2.0.
865 // The correct solution would be to augument the
866 // function list/array with information about what
867 // functions needs arguments and their type.
868 if (cmd.argument.empty()) {
869 cur.errorMessage(_("LyX function 'layout' needs an argument."));
873 // Derive layout number from given argument (string)
874 // and current buffer's textclass (number)
875 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
876 bool hasLayout = tclass.hasLayout(cmd.argument);
877 string layout = cmd.argument;
879 // If the entry is obsolete, use the new one instead.
881 string const & obs = tclass[layout]->obsoleted_by();
887 cur.errorMessage(string(N_("Layout ")) + cmd.argument +
892 bool change_layout = (current_layout != layout);
894 if (!change_layout && cur.selection() &&
895 cur.selBegin().pit() != cur.selEnd().pit())
897 pit_type spit = cur.selBegin().pit();
898 pit_type epit = cur.selEnd().pit() + 1;
899 while (spit != epit) {
900 if (pars_[spit].layout()->name() != current_layout) {
901 change_layout = true;
909 current_layout = layout;
910 setLayout(cur, layout);
911 bv->owner()->setLayout(layout);
917 case LFUN_PASTESELECTION: {
918 cur.clearSelection();
919 string const clip = bv->getClipboard();
921 if (cmd.argument == "paragraph")
922 insertStringAsParagraphs(cur, clip);
924 insertStringAsLines(cur, clip);
930 lyx::cap::replaceSelection(cur);
931 Paragraph & par = cur.paragraph();
932 lyx::pos_type pos = cur.pos();
936 else if (cur.prevInset() && cur.prevInset()->isSpace())
939 c = par.getChar(pos - 1);
941 LyXLayout_ptr const & style = par.layout();
943 BufferParams const & bufparams = bv->buffer()->params();
944 if (!style->pass_thru
945 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
946 string arg = cmd.argument;
948 cur.insert(new InsetQuotes(c,
949 bufparams.quotes_language,
950 InsetQuotes::SingleQ));
951 else if (arg == "double")
952 cur.insert(new InsetQuotes(c,
953 bufparams.quotes_language,
954 InsetQuotes::DoubleQ));
956 cur.insert(new InsetQuotes(c, bufparams));
960 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
964 case LFUN_DATE_INSERT:
965 if (cmd.argument.empty())
966 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT,
967 lyx::formatted_time(lyx::current_time())));
969 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT,
970 lyx::formatted_time(lyx::current_time(), cmd.argument)));
973 case LFUN_MOUSE_TRIPLE:
974 if (cmd.button() == mouse_button::button1) {
980 bv->haveSelection(cur.selection());
984 case LFUN_MOUSE_DOUBLE:
985 if (cmd.button() == mouse_button::button1) {
986 selectWord(cur, lyx::WHOLE_WORD_STRICT);
988 bv->haveSelection(cur.selection());
992 // Single-click on work area
993 case LFUN_MOUSE_PRESS: {
994 // Right click on a footnote flag opens float menu
995 if (cmd.button() == mouse_button::button3) {
996 cur.clearSelection();
1000 // Middle button press pastes if we have a selection
1001 // We do this here as if the selection was inside an inset
1002 // it could get cleared on the unlocking of the inset so
1003 // we have to check this first
1004 bool paste_internally = false;
1005 if (cmd.button() == mouse_button::button2 && cur.selection()) {
1006 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1007 paste_internally = true;
1010 // Clear the selection
1011 cur.clearSelection();
1013 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1018 // Has the cursor just left the inset?
1019 if (bv->cursor().inMathed() && !cur.inMathed())
1020 bv->cursor().inset().notifyCursorLeaves(bv->cursor());
1025 // Insert primary selection with middle mouse
1026 // if there is a local selection in the current buffer,
1028 if (cmd.button() == mouse_button::button2) {
1029 if (paste_internally)
1030 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1032 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1038 case LFUN_MOUSE_MOTION: {
1039 // Only use motion with button 1
1040 //if (cmd.button() != mouse_button::button1)
1043 // ignore motions deeper nested than the real anchor
1044 LCursor & bvcur = cur.bv().cursor();
1045 if (bvcur.anchor_.hasPart(cur)) {
1046 CursorSlice old = bvcur.top();
1048 int const wh = bv->workHeight();
1049 int const y = std::max(0, std::min(wh - 1, cmd.y));
1051 setCursorFromCoordinates(cur, cmd.x, y);
1052 cur.x_target() = cmd.x;
1057 // This is to allow jumping over large insets
1058 if (cur.top() == old) {
1065 if (cur.top() == old)
1068 // don't set anchor_
1069 bvcur.setCursor(cur);
1070 bvcur.selection() = true;
1071 //lyxerr << "MOTION: " << bv->cursor() << endl;
1079 case LFUN_MOUSE_RELEASE: {
1080 if (cmd.button() == mouse_button::button2)
1084 if (cmd.button() == mouse_button::button1)
1085 bv->haveSelection(cur.selection());
1088 bv->owner()->updateMenubar();
1089 bv->owner()->updateToolbars();
1093 case LFUN_SELFINSERT: {
1094 if (cmd.argument.empty())
1097 // Automatically delete the currently selected
1098 // text and replace it with what is being
1099 // typed in now. Depends on lyxrc settings
1100 // "auto_region_delete", which defaults to
1103 if (lyxrc.auto_region_delete) {
1104 if (cur.selection())
1105 cutSelection(cur, false, false);
1106 bv->haveSelection(false);
1109 cur.clearSelection();
1110 LyXFont const old_font = real_current_font;
1112 // Prevents language turds in new lyxtexts under non-english
1113 BufferParams const & bufparams = cur.buffer().params();
1114 Language const * lang = cur.paragraph().getParLanguage(bufparams);
1115 current_font.setLanguage(lang);
1116 real_current_font.setLanguage(lang);
1118 string::const_iterator cit = cmd.argument.begin();
1119 string::const_iterator end = cmd.argument.end();
1120 for (; cit != end; ++cit)
1121 bv->owner()->getIntl().getTransManager().
1122 TranslateAndInsert(*cit, this);
1125 moveCursor(cur, false);
1127 needsUpdate = redoParagraph(cur.pit());
1129 // update only this paragraph
1130 cur.bv().update(Update::SinglePar | Update::Force);
1133 bv->updateScrollbar();
1138 InsetCommandParams p("url");
1139 string const data = InsetCommandMailer::params2string("url", p);
1140 bv->owner()->getDialogs().show("url", data, 0);
1144 case LFUN_HTMLURL: {
1145 InsetCommandParams p("htmlurl");
1146 string const data = InsetCommandMailer::params2string("url", p);
1147 bv->owner()->getDialogs().show("url", data, 0);
1151 case LFUN_INSERT_LABEL: {
1152 // Try to generate a valid label
1153 string const contents = cmd.argument.empty() ?
1154 cur.getPossibleLabel() : cmd.argument;
1156 InsetCommandParams p("label", contents);
1157 string const data = InsetCommandMailer::params2string("label", p);
1159 if (cmd.argument.empty()) {
1160 bv->owner()->getDialogs().show("label", data, 0);
1162 FuncRequest fr(LFUN_INSET_INSERT, data);
1170 case LFUN_INSET_LIST:
1171 case LFUN_INSET_THEOREM:
1172 case LFUN_INSET_CAPTION:
1174 case LFUN_INSERT_NOTE:
1175 case LFUN_INSERT_CHARSTYLE:
1176 case LFUN_INSERT_BOX:
1177 case LFUN_INSERT_BRANCH:
1178 case LFUN_INSERT_BIBITEM:
1179 case LFUN_INSET_ERT:
1180 case LFUN_INSET_FOOTNOTE:
1181 case LFUN_INSET_MARGINAL:
1182 case LFUN_INSET_OPTARG:
1183 case LFUN_ENVIRONMENT_INSERT:
1184 // Open the inset, and move the current selection
1186 doInsertInset(cur, this, cmd, true, true);
1190 case LFUN_TABULAR_INSERT:
1191 // if there were no arguments, just open the dialog
1192 if (doInsertInset(cur, this, cmd, false, true))
1195 bv->owner()->getDialogs().show("tabularcreate");
1199 case LFUN_INSET_FLOAT:
1200 case LFUN_INSET_WIDE_FLOAT:
1201 case LFUN_INSET_WRAP:
1202 doInsertInset(cur, this, cmd, true, true);
1204 // FIXME: the "Caption" name should not be hardcoded,
1205 // but given by the float definition.
1206 cur.dispatch(FuncRequest(LFUN_LAYOUT, "Caption"));
1209 case LFUN_INDEX_INSERT:
1210 // Just open the inset
1211 doInsertInset(cur, this, cmd, true, false);
1215 case LFUN_INDEX_PRINT:
1216 case LFUN_TOC_INSERT:
1218 case LFUN_INSERT_LINE:
1219 case LFUN_INSERT_PAGEBREAK:
1221 doInsertInset(cur, this, cmd, false, false);
1225 case LFUN_DEPTH_MIN:
1226 changeDepth(cur, DEC_DEPTH);
1229 case LFUN_DEPTH_PLUS:
1230 changeDepth(cur, INC_DEPTH);
1233 case LFUN_MATH_DISPLAY:
1234 mathDispatch(cur, cmd, true);
1237 case LFUN_MATH_IMPORT_SELECTION:
1238 case LFUN_MATH_MODE:
1239 if (cmd.argument == "on")
1240 // don't pass "on" as argument
1241 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1243 mathDispatch(cur, cmd, false);
1246 case LFUN_MATH_MACRO:
1247 if (cmd.argument.empty())
1248 cur.errorMessage(N_("Missing argument"));
1250 string s = cmd.argument;
1251 string const s1 = token(s, ' ', 1);
1252 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1253 string const s2 = token(s, ' ', 2);
1254 string const type = s2.empty() ? "newcommand" : s2;
1255 cur.insert(new MathMacroTemplate(token(s, ' ', 0), nargs, s2));
1256 //cur.nextInset()->edit(cur, true);
1260 // passthrough hat and underscore outside mathed:
1261 case LFUN_SUBSCRIPT:
1262 mathDispatch(cur, FuncRequest(LFUN_SELFINSERT, "_"), false);
1264 case LFUN_SUPERSCRIPT:
1265 mathDispatch(cur, FuncRequest(LFUN_SELFINSERT, "^"), false);
1268 case LFUN_INSERT_MATH:
1269 case LFUN_INSERT_MATRIX:
1270 case LFUN_MATH_DELIM: {
1271 cur.insert(new MathHullInset);
1272 cur.dispatch(FuncRequest(LFUN_RIGHT));
1273 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
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 InsetBase & inset = cur.inset();
1405 bool const accept = !inset.forceDefaultParagraphs(&inset);
1407 data = "update " + convert<string>(accept) + '\n' + data;
1408 bv->owner()->getDialogs().update("paragraph", data);
1413 case LFUN_CIRCUMFLEX:
1423 case LFUN_SPECIAL_CARON:
1426 case LFUN_HUNG_UMLAUT:
1429 bv->owner()->getLyXFunc().handleKeyFunc(cmd.action);
1430 if (!cmd.argument.empty())
1431 bv->owner()->getIntl().getTransManager()
1432 .TranslateAndInsert(cmd.argument[0], this);
1435 case LFUN_FLOAT_LIST: {
1436 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1437 if (tclass.floats().typeExist(cmd.argument)) {
1438 // not quite sure if we want this...
1440 cur.clearSelection();
1441 breakParagraph(cur);
1443 if (cur.lastpos() != 0) {
1445 breakParagraph(cur);
1448 setLayout(cur, tclass.defaultLayoutName());
1449 setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1450 insertInset(cur, new InsetFloatList(cmd.argument));
1453 lyxerr << "Non-existent float type: "
1454 << cmd.argument << endl;
1459 case LFUN_ACCEPT_CHANGE: {
1464 case LFUN_REJECT_CHANGE: {
1469 case LFUN_THESAURUS_ENTRY: {
1470 string arg = cmd.argument;
1472 arg = cur.selectionAsString(false);
1474 if (arg.size() > 100 || arg.empty()) {
1475 // Get word or selection
1476 selectWordWhenUnderCursor(cur, lyx::WHOLE_WORD);
1477 arg = cur.selectionAsString(false);
1480 bv->owner()->getDialogs().show("thesaurus", arg);
1484 case LFUN_PARAGRAPH_APPLY: {
1485 // Given data, an encoding of the ParagraphParameters
1486 // generated in the Paragraph dialog, this function sets
1487 // the current paragraph appropriately.
1488 istringstream is(cmd.argument);
1491 ParagraphParameters params;
1496 params.labelWidthString(),
1498 cur.message(_("Paragraph layout set"));
1502 case LFUN_INSET_DIALOG_SHOW: {
1503 InsetBase * inset = cur.nextInset();
1505 FuncRequest fr(LFUN_INSET_DIALOG_SHOW);
1506 inset->dispatch(cur, fr);
1512 if (cur.selection()) {
1513 cur.selection() = false;
1516 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1521 lyxerr << BOOST_CURRENT_FUNCTION
1522 << " Not DISPATCHED by LyXText" << endl;
1528 && &oldTopSlice.inset() == &cur.inset()
1529 && oldTopSlice.idx() == cur.idx()
1531 && !cur.selection())
1538 bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
1539 FuncStatus & flag) const
1541 BOOST_ASSERT(cur.text() == this);
1542 LyXFont const & font = real_current_font;
1545 switch (cmd.action) {
1547 case LFUN_DEPTH_MIN:
1548 enable = changeDepthAllowed(cur, DEC_DEPTH);
1551 case LFUN_DEPTH_PLUS:
1552 enable = changeDepthAllowed(cur, INC_DEPTH);
1555 case LFUN_INSET_OPTARG:
1556 enable = numberOfOptArgs(cur.paragraph())
1557 < cur.paragraph().layout()->optionalargs;
1561 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1564 case LFUN_INSERT_BIBITEM:
1565 enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO);
1569 // the functions which insert insets
1570 InsetBase::Code code = InsetBase::NO_CODE;
1571 switch (cmd.action) {
1572 case LFUN_DIALOG_SHOW_NEW_INSET:
1573 if (cmd.argument == "bibitem")
1574 code = InsetBase::BIBITEM_CODE;
1575 else if (cmd.argument == "bibtex")
1576 code = InsetBase::BIBTEX_CODE;
1577 else if (cmd.argument == "box")
1578 code = InsetBase::BOX_CODE;
1579 else if (cmd.argument == "branch")
1580 code = InsetBase::BRANCH_CODE;
1581 else if (cmd.argument == "citation")
1582 code = InsetBase::CITE_CODE;
1583 else if (cmd.argument == "ert")
1584 code = InsetBase::ERT_CODE;
1585 else if (cmd.argument == "external")
1586 code = InsetBase::EXTERNAL_CODE;
1587 else if (cmd.argument == "float")
1588 code = InsetBase::FLOAT_CODE;
1589 else if (cmd.argument == "graphics")
1590 code = InsetBase::GRAPHICS_CODE;
1591 else if (cmd.argument == "include")
1592 code = InsetBase::INCLUDE_CODE;
1593 else if (cmd.argument == "index")
1594 code = InsetBase::INDEX_CODE;
1595 else if (cmd.argument == "label")
1596 code = InsetBase::LABEL_CODE;
1597 else if (cmd.argument == "note")
1598 code = InsetBase::NOTE_CODE;
1599 else if (cmd.argument == "ref")
1600 code = InsetBase::REF_CODE;
1601 else if (cmd.argument == "toc")
1602 code = InsetBase::TOC_CODE;
1603 else if (cmd.argument == "url")
1604 code = InsetBase::URL_CODE;
1605 else if (cmd.argument == "vspace")
1606 code = InsetBase::VSPACE_CODE;
1607 else if (cmd.argument == "wrap")
1608 code = InsetBase::WRAP_CODE;
1611 case LFUN_INSET_ERT:
1612 code = InsetBase::ERT_CODE;
1614 case LFUN_INSET_FOOTNOTE:
1615 code = InsetBase::FOOT_CODE;
1617 case LFUN_TABULAR_INSERT:
1618 code = InsetBase::TABULAR_CODE;
1620 case LFUN_INSET_MARGINAL:
1621 code = InsetBase::MARGIN_CODE;
1623 case LFUN_INSET_FLOAT:
1624 case LFUN_INSET_WIDE_FLOAT:
1625 code = InsetBase::FLOAT_CODE;
1627 case LFUN_INSET_WRAP:
1628 code = InsetBase::WRAP_CODE;
1630 case LFUN_FLOAT_LIST:
1631 code = InsetBase::FLOAT_LIST_CODE;
1634 case LFUN_INSET_LIST:
1635 code = InsetBase::LIST_CODE;
1637 case LFUN_INSET_THEOREM:
1638 code = InsetBase::THEOREM_CODE;
1641 case LFUN_INSET_CAPTION:
1642 code = InsetBase::CAPTION_CODE;
1644 case LFUN_INSERT_NOTE:
1645 code = InsetBase::NOTE_CODE;
1647 case LFUN_INSERT_CHARSTYLE:
1648 code = InsetBase::CHARSTYLE_CODE;
1649 if (buf->params().getLyXTextClass().charstyles().empty())
1652 case LFUN_INSERT_BOX:
1653 code = InsetBase::BOX_CODE;
1655 case LFUN_INSERT_BRANCH:
1656 code = InsetBase::BRANCH_CODE;
1657 if (buf->params().branchlist().empty())
1660 case LFUN_INSERT_LABEL:
1661 code = InsetBase::LABEL_CODE;
1663 case LFUN_INSET_OPTARG:
1664 code = InsetBase::OPTARG_CODE;
1666 case LFUN_ENVIRONMENT_INSERT:
1667 code = InsetBase::BOX_CODE;
1669 case LFUN_INDEX_INSERT:
1670 code = InsetBase::INDEX_CODE;
1672 case LFUN_INDEX_PRINT:
1673 code = InsetBase::INDEX_PRINT_CODE;
1675 case LFUN_TOC_INSERT:
1676 code = InsetBase::TOC_CODE;
1680 code = InsetBase::URL_CODE;
1683 // always allow this, since we will inset a raw quote
1684 // if an inset is not allowed.
1686 case LFUN_HYPHENATION:
1687 case LFUN_LIGATURE_BREAK:
1689 case LFUN_MENU_SEPARATOR:
1691 case LFUN_END_OF_SENTENCE:
1692 code = InsetBase::SPECIALCHAR_CODE;
1694 case LFUN_SPACE_INSERT:
1695 // slight hack: we know this is allowed in math mode
1697 code = InsetBase::SPACE_CODE;
1699 case LFUN_INSET_DIALOG_SHOW: {
1700 InsetBase * inset = cur.nextInset();
1703 code = inset->lyxCode();
1704 if (!(code == InsetBase::INCLUDE_CODE
1705 || code == InsetBase::BIBTEX_CODE
1706 || code == InsetBase::FLOAT_LIST_CODE
1707 || code == InsetBase::TOC_CODE))
1716 if (code != InsetBase::NO_CODE
1717 && (cur.empty() || !cur.inset().insetAllowed(code)))
1722 case LFUN_DIALOG_SHOW_NEW_INSET:
1723 case LFUN_INSET_ERT:
1724 case LFUN_INSERT_BOX:
1725 case LFUN_INSERT_BRANCH:
1726 case LFUN_ENVIRONMENT_INSERT:
1727 case LFUN_INDEX_INSERT:
1728 case LFUN_INDEX_PRINT:
1729 case LFUN_TOC_INSERT:
1733 case LFUN_HYPHENATION:
1734 case LFUN_LIGATURE_BREAK:
1736 case LFUN_MENU_SEPARATOR:
1738 case LFUN_END_OF_SENTENCE:
1739 case LFUN_SPACE_INSERT:
1740 case LFUN_INSET_DIALOG_SHOW:
1743 case LFUN_INSET_MODIFY:
1744 // We need to disable this, because we may get called for a
1746 // InsetTabular::getStatus() -> InsetText::getStatus()
1747 // and we don't handle LFUN_INSET_MODIFY.
1752 flag.setOnOff(font.emph() == LyXFont::ON);
1756 flag.setOnOff(font.noun() == LyXFont::ON);
1760 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1764 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1768 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1772 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1775 case LFUN_DELETE_WORD_FORWARD:
1776 case LFUN_DELETE_WORD_BACKWARD:
1777 case LFUN_DELETE_LINE_FORWARD:
1778 case LFUN_WORDRIGHT:
1788 case LFUN_UP_PARAGRAPHSEL:
1789 case LFUN_DOWN_PARAGRAPHSEL:
1794 case LFUN_WORDRIGHTSEL:
1795 case LFUN_WORDLEFTSEL:
1797 case LFUN_UP_PARAGRAPH:
1798 case LFUN_DOWN_PARAGRAPH:
1803 case LFUN_BREAKLINE:
1805 case LFUN_DELETE_SKIP:
1806 case LFUN_BACKSPACE:
1807 case LFUN_BACKSPACE_SKIP:
1808 case LFUN_BREAKPARAGRAPH:
1809 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1810 case LFUN_BREAKPARAGRAPH_SKIP:
1811 case LFUN_PARAGRAPH_SPACING:
1812 case LFUN_INSET_INSERT:
1813 case LFUN_NEXT_INSET_TOGGLE:
1814 case LFUN_UPCASE_WORD:
1815 case LFUN_LOWCASE_WORD:
1816 case LFUN_CAPITALIZE_WORD:
1817 case LFUN_TRANSPOSE_CHARS:
1824 case LFUN_GETLAYOUT:
1826 case LFUN_PASTESELECTION:
1827 case LFUN_DATE_INSERT:
1828 case LFUN_SELFINSERT:
1829 case LFUN_INSERT_LABEL:
1830 case LFUN_INSERT_NOTE:
1831 case LFUN_INSERT_CHARSTYLE:
1832 case LFUN_INSET_FLOAT:
1833 case LFUN_INSET_FOOTNOTE:
1834 case LFUN_INSET_MARGINAL:
1835 case LFUN_INSET_WIDE_FLOAT:
1836 case LFUN_INSET_WRAP:
1837 case LFUN_TABULAR_INSERT:
1838 case LFUN_INSERT_LINE:
1839 case LFUN_INSERT_PAGEBREAK:
1840 case LFUN_MATH_DISPLAY:
1841 case LFUN_MATH_IMPORT_SELECTION:
1842 case LFUN_MATH_MODE:
1843 case LFUN_MATH_MACRO:
1844 case LFUN_INSERT_MATH:
1845 case LFUN_INSERT_MATRIX:
1846 case LFUN_MATH_DELIM:
1847 case LFUN_SUBSCRIPT:
1848 case LFUN_SUPERSCRIPT:
1850 case LFUN_UNDERLINE:
1851 case LFUN_FONT_SIZE:
1853 case LFUN_FREEFONT_APPLY:
1854 case LFUN_FREEFONT_UPDATE:
1855 case LFUN_LAYOUT_PARAGRAPH:
1856 case LFUN_PARAGRAPH_UPDATE:
1858 case LFUN_CIRCUMFLEX:
1868 case LFUN_SPECIAL_CARON:
1871 case LFUN_HUNG_UMLAUT:
1874 case LFUN_FLOAT_LIST:
1875 case LFUN_ACCEPT_CHANGE:
1876 case LFUN_REJECT_CHANGE:
1877 case LFUN_THESAURUS_ENTRY:
1878 case LFUN_PARAGRAPH_APPLY:
1880 case LFUN_KEYMAP_TOGGLE:
1882 case LFUN_BEGINNINGBUF:
1883 case LFUN_BEGINNINGBUFSEL:
1884 case LFUN_ENDBUFSEL:
1885 // these are handled in our dispatch()
1892 flag.enabled(enable);