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 sel = cur.selection();
296 bool needsUpdate = !lyxaction.funcHasFlag(cmd.action, LyXAction::NoUpdate);
298 switch (cmd.action) {
300 case LFUN_APPENDIX: {
301 Paragraph & par = cur.paragraph();
302 bool start = !par.params().startOfAppendix();
305 #warning The code below only makes sense at top level.
306 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
308 // ensure that we have only one start_of_appendix in this document
309 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
310 if (pars_[tmp].params().startOfAppendix()) {
312 pars_[tmp].params().startOfAppendix(false);
318 par.params().startOfAppendix(start);
320 // we can set the refreshing parameters now
321 updateCounters(cur.buffer());
325 case LFUN_DELETE_WORD_FORWARD:
326 cur.clearSelection();
327 deleteWordForward(cur);
328 finishChange(cur, false);
331 case LFUN_DELETE_WORD_BACKWARD:
332 cur.clearSelection();
333 deleteWordBackward(cur);
334 finishChange(cur, false);
337 case LFUN_DELETE_LINE_FORWARD:
338 cur.clearSelection();
339 deleteLineForward(cur);
340 finishChange(cur, false);
345 cur.clearSelection();
346 if (isRTL(cur.paragraph()))
347 needsUpdate = cursorLeftOneWord(cur);
349 needsUpdate = cursorRightOneWord(cur);
350 finishChange(cur, false);
355 cur.clearSelection();
356 if (isRTL(cur.paragraph()))
357 needsUpdate = cursorRightOneWord(cur);
359 needsUpdate = cursorLeftOneWord(cur);
360 finishChange(cur, false);
363 case LFUN_BEGINNINGBUF:
364 if (cur.depth() == 1) {
366 cur.clearSelection();
368 finishChange(cur, false);
374 case LFUN_BEGINNINGBUFSEL:
375 if (cur.depth() == 1) {
376 if (!cur.selection())
379 finishChange(cur, true);
386 if (cur.depth() == 1) {
388 cur.clearSelection();
390 finishChange(cur, false);
397 if (cur.depth() == 1) {
398 if (!cur.selection())
401 finishChange(cur, true);
409 //lyxerr << BOOST_CURRENT_FUNCTION
410 // << " LFUN_RIGHT[SEL]:\n" << cur << endl;
411 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
412 if (isRTL(cur.paragraph()))
413 needsUpdate = cursorLeft(cur);
415 needsUpdate = cursorRight(cur);
416 if (!needsUpdate && oldTopSlice == cur.top()) {
418 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
424 //lyxerr << "handle LFUN_LEFT[SEL]:\n" << cur << endl;
425 cur.selHandle(cmd.action == LFUN_LEFTSEL);
426 if (isRTL(cur.paragraph()))
427 needsUpdate = cursorRight(cur);
429 needsUpdate = cursorLeft(cur);
430 if (oldTopSlice == cur.top()) {
432 cmd = FuncRequest(LFUN_FINISHED_LEFT);
439 //lyxerr << "handle LFUN_UP[SEL]:\n" << cur << endl;
440 cur.selHandle(cmd.action == LFUN_UPSEL);
441 needsUpdate = cursorUp(cur);
442 if (oldTopSlice == cur.top()) {
444 cmd = FuncRequest(LFUN_FINISHED_UP);
451 //lyxerr << "handle LFUN_DOWN[SEL]:\n" << cur << endl;
452 cur.selHandle(cmd.action == LFUN_DOWNSEL);
453 needsUpdate = cursorDown(cur);
454 if (oldTopSlice == cur.top()) {
456 cmd = FuncRequest(LFUN_FINISHED_DOWN);
460 case LFUN_UP_PARAGRAPH:
462 cur.clearSelection();
463 needsUpdate = cursorUpParagraph(cur);
464 finishChange(cur, false);
467 case LFUN_UP_PARAGRAPHSEL:
468 if (!cur.selection())
470 cursorUpParagraph(cur);
471 finishChange(cur, true);
474 case LFUN_DOWN_PARAGRAPH:
476 cur.clearSelection();
477 needsUpdate = cursorDownParagraph(cur);
478 finishChange(cur, false);
481 case LFUN_DOWN_PARAGRAPHSEL:
482 if (!cur.selection())
484 cursorDownParagraph(cur);
485 finishChange(cur, true);
490 if (!cur.selection())
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);
520 case LFUN_WORDRIGHTSEL:
521 if (!cur.selection())
523 if (isRTL(cur.paragraph()))
524 cursorLeftOneWord(cur);
526 cursorRightOneWord(cur);
527 finishChange(cur, true);
530 case LFUN_WORDLEFTSEL:
531 if (!cur.selection())
533 if (isRTL(cur.paragraph()))
534 cursorRightOneWord(cur);
536 cursorLeftOneWord(cur);
537 finishChange(cur, true);
541 selectWord(cur, lyx::WHOLE_WORD);
542 finishChange(cur, true);
549 cur.clearSelection();
550 finishChange(cur, false);
551 if (cur.pit() == 0 && cur.textRow().pos() == 0) {
553 cmd = FuncRequest(LFUN_FINISHED_UP);
555 needsUpdate = cursorPrevious(cur);
562 cur.clearSelection();
563 finishChange(cur, false);
564 if (cur.pit() == cur.lastpit()
565 && cur.textRow().endpos() == cur.lastpos()) {
567 cmd = FuncRequest(LFUN_FINISHED_DOWN);
569 needsUpdate = cursorNext(cur);
575 cur.clearSelection();
577 finishChange(cur, false);
582 cur.clearSelection();
584 finishChange(cur, false);
587 case LFUN_BREAKLINE: {
588 // Not allowed by LaTeX (labels or empty par)
589 if (cur.pos() > cur.paragraph().beginOfBody()) {
590 lyx::cap::replaceSelection(cur);
591 cur.insert(new InsetNewline);
593 moveCursor(cur, false);
599 if (!cur.selection()) {
602 // It is possible to make it a lot faster still
603 // just comment out the line below...
605 cutSelection(cur, true, false);
607 moveCursor(cur, false);
610 case LFUN_DELETE_SKIP:
611 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
612 if (!cur.selection()) {
613 if (cur.pos() == cur.lastpos()) {
620 cutSelection(cur, true, false);
626 if (!cur.selection()) {
627 if (bv->owner()->getIntl().getTransManager().backspace()) {
630 // It is possible to make it a lot faster still
631 // just comment out the line below...
634 cutSelection(cur, true, false);
639 case LFUN_BACKSPACE_SKIP:
640 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
641 if (!cur.selection()) {
645 //CursorSlice cur = cursor();
649 cutSelection(cur, true, false);
653 case LFUN_BREAKPARAGRAPH:
654 lyx::cap::replaceSelection(cur);
655 breakParagraph(cur, 0);
660 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
661 lyx::cap::replaceSelection(cur);
662 breakParagraph(cur, 1);
667 case LFUN_BREAKPARAGRAPH_SKIP: {
668 // When at the beginning of a paragraph, remove
669 // indentation. Otherwise, do the same as LFUN_BREAKPARAGRAPH.
670 lyx::cap::replaceSelection(cur);
672 cur.paragraph().params().labelWidthString(string());
674 breakParagraph(cur, 0);
680 case LFUN_PARAGRAPH_SPACING: {
681 Paragraph & par = cur.paragraph();
682 Spacing::Space cur_spacing = par.params().spacing().getSpace();
683 string cur_value = "1.0";
684 if (cur_spacing == Spacing::Other)
685 cur_value = par.params().spacing().getValueAsString();
687 istringstream is(cmd.argument);
690 Spacing::Space new_spacing = cur_spacing;
691 string new_value = cur_value;
693 lyxerr << "Missing argument to `paragraph-spacing'"
695 } else if (tmp == "single") {
696 new_spacing = Spacing::Single;
697 } else if (tmp == "onehalf") {
698 new_spacing = Spacing::Onehalf;
699 } else if (tmp == "double") {
700 new_spacing = Spacing::Double;
701 } else if (tmp == "other") {
702 new_spacing = Spacing::Other;
703 string tmpval = "0.0";
705 lyxerr << "new_value = " << tmpval << endl;
708 } else if (tmp == "default") {
709 new_spacing = Spacing::Default;
711 lyxerr << _("Unknown spacing argument: ")
712 << cmd.argument << endl;
714 if (cur_spacing != new_spacing || cur_value != new_value)
715 par.params().spacing(Spacing(new_spacing, new_value));
719 case LFUN_INSET_INSERT: {
721 InsetBase * inset = createInset(bv, cmd);
723 insertInset(cur, inset);
729 case LFUN_INSET_SETTINGS:
730 cur.inset().showInsetDialog(bv);
733 case LFUN_NEXT_INSET_TOGGLE: {
734 InsetBase * inset = cur.nextInset();
736 cur.clearSelection();
737 FuncRequest fr = cmd;
738 fr.action = LFUN_INSET_TOGGLE;
739 inset->dispatch(cur, fr);
744 case LFUN_KEYMAP_TOGGLE:
745 cur.clearSelection();
749 case LFUN_SPACE_INSERT:
750 if (cur.paragraph().layout()->free_spacing)
751 insertChar(cur, ' ');
753 doInsertInset(cur, this, cmd, false, false);
756 moveCursor(cur, false);
759 case LFUN_HYPHENATION:
760 specialChar(cur, InsetSpecialChar::HYPHENATION);
763 case LFUN_LIGATURE_BREAK:
764 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
768 specialChar(cur, InsetSpecialChar::LDOTS);
771 case LFUN_END_OF_SENTENCE:
772 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
775 case LFUN_MENU_SEPARATOR:
776 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
779 case LFUN_UPCASE_WORD:
780 changeCase(cur, LyXText::text_uppercase);
783 case LFUN_LOWCASE_WORD:
784 changeCase(cur, LyXText::text_lowercase);
787 case LFUN_CAPITALIZE_WORD:
788 changeCase(cur, LyXText::text_capitalization);
791 case LFUN_TRANSPOSE_CHARS:
796 cur.message(_("Paste"));
797 lyx::cap::replaceSelection(cur);
799 #warning FIXME Check if the arg is in the domain of available selections.
801 if (isStrUnsignedInt(cmd.argument))
802 pasteSelection(cur, convert<unsigned int>(cmd.argument));
804 pasteSelection(cur, 0);
805 cur.clearSelection(); // bug 393
811 cutSelection(cur, true, true);
812 cur.message(_("Cut"));
817 cur.message(_("Copy"));
821 cur.message(convert<string>(cursorX(cur.top(), cur.boundary())) + ' '
822 + convert<string>(cursorY(cur.top(), cur.boundary())));
828 istringstream is(cmd.argument);
831 lyxerr << "SETXY: Could not parse coordinates in '"
832 << cmd.argument << std::endl;
834 setCursorFromCoordinates(cur, x, y);
839 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
841 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
848 cur.message(cur.paragraph().layout()->name());
852 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
853 << cmd.argument << endl;
855 // This is not the good solution to the empty argument
856 // problem, but it will hopefully suffice for 1.2.0.
857 // The correct solution would be to augument the
858 // function list/array with information about what
859 // functions needs arguments and their type.
860 if (cmd.argument.empty()) {
861 cur.errorMessage(_("LyX function 'layout' needs an argument."));
865 // Derive layout number from given argument (string)
866 // and current buffer's textclass (number)
867 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
868 bool hasLayout = tclass.hasLayout(cmd.argument);
869 string layout = cmd.argument;
871 // If the entry is obsolete, use the new one instead.
873 string const & obs = tclass[layout]->obsoleted_by();
879 cur.errorMessage(string(N_("Layout ")) + cmd.argument +
884 bool change_layout = (current_layout != layout);
886 if (!change_layout && cur.selection() &&
887 cur.selBegin().pit() != cur.selEnd().pit())
889 pit_type spit = cur.selBegin().pit();
890 pit_type epit = cur.selEnd().pit() + 1;
891 while (spit != epit) {
892 if (pars_[spit].layout()->name() != current_layout) {
893 change_layout = true;
901 current_layout = layout;
902 setLayout(cur, layout);
903 bv->owner()->setLayout(layout);
909 case LFUN_PASTESELECTION: {
910 cur.clearSelection();
911 string const clip = bv->getClipboard();
913 if (cmd.argument == "paragraph")
914 insertStringAsParagraphs(cur, clip);
916 insertStringAsLines(cur, clip);
922 lyx::cap::replaceSelection(cur);
923 Paragraph & par = cur.paragraph();
924 lyx::pos_type pos = cur.pos();
928 else if (cur.prevInset() && cur.prevInset()->isSpace())
931 c = par.getChar(pos - 1);
933 LyXLayout_ptr const & style = par.layout();
935 BufferParams const & bufparams = bv->buffer()->params();
936 if (!style->pass_thru
937 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
938 string arg = cmd.argument;
940 cur.insert(new InsetQuotes(c,
941 bufparams.quotes_language,
942 InsetQuotes::SingleQ));
943 else if (arg == "double")
944 cur.insert(new InsetQuotes(c,
945 bufparams.quotes_language,
946 InsetQuotes::DoubleQ));
948 cur.insert(new InsetQuotes(c, bufparams));
952 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
956 case LFUN_DATE_INSERT:
957 if (cmd.argument.empty())
958 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT,
959 lyx::formatted_time(lyx::current_time())));
961 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT,
962 lyx::formatted_time(lyx::current_time(), cmd.argument)));
965 case LFUN_MOUSE_TRIPLE:
966 if (cmd.button() == mouse_button::button1) {
972 bv->haveSelection(cur.selection());
976 case LFUN_MOUSE_DOUBLE:
977 if (cmd.button() == mouse_button::button1) {
978 selectWord(cur, lyx::WHOLE_WORD_STRICT);
980 bv->haveSelection(cur.selection());
984 // Single-click on work area
985 case LFUN_MOUSE_PRESS: {
986 // Right click on a footnote flag opens float menu
987 if (cmd.button() == mouse_button::button3) {
988 cur.clearSelection();
992 // Middle button press pastes if we have a selection
993 // We do this here as if the selection was inside an inset
994 // it could get cleared on the unlocking of the inset so
995 // we have to check this first
996 bool paste_internally = false;
997 if (cmd.button() == mouse_button::button2 && cur.selection()) {
998 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
999 paste_internally = true;
1002 // Clear the selection
1003 cur.clearSelection();
1005 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1010 // Has the cursor just left the inset?
1011 if (bv->cursor().inMathed() && !cur.inMathed())
1012 bv->cursor().inset().notifyCursorLeaves(bv->cursor());
1017 // Insert primary selection with middle mouse
1018 // if there is a local selection in the current buffer,
1020 if (cmd.button() == mouse_button::button2) {
1021 if (paste_internally)
1022 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1024 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1030 case LFUN_MOUSE_MOTION: {
1031 // Only use motion with button 1
1032 //if (cmd.button() != mouse_button::button1)
1035 // ignore motions deeper nested than the real anchor
1036 LCursor & bvcur = cur.bv().cursor();
1037 if (bvcur.anchor_.hasPart(cur)) {
1038 CursorSlice old = bvcur.top();
1040 int const wh = bv->workHeight();
1041 int const y = std::max(0, std::min(wh - 1, cmd.y));
1043 setCursorFromCoordinates(cur, cmd.x, y);
1044 cur.x_target() = cmd.x;
1049 // This is to allow jumping over large insets
1050 if (cur.top() == old) {
1057 if (cur.top() == old)
1060 // don't set anchor_
1061 bvcur.setCursor(cur);
1062 bvcur.selection() = true;
1063 //lyxerr << "MOTION: " << bv->cursor() << endl;
1071 case LFUN_MOUSE_RELEASE: {
1072 if (cmd.button() == mouse_button::button2)
1076 if (cmd.button() == mouse_button::button1)
1077 bv->haveSelection(cur.selection());
1080 bv->owner()->updateMenubar();
1081 bv->owner()->updateToolbars();
1085 case LFUN_SELFINSERT: {
1086 if (cmd.argument.empty())
1089 // Automatically delete the currently selected
1090 // text and replace it with what is being
1091 // typed in now. Depends on lyxrc settings
1092 // "auto_region_delete", which defaults to
1095 if (lyxrc.auto_region_delete) {
1096 if (cur.selection())
1097 cutSelection(cur, false, false);
1098 bv->haveSelection(false);
1101 cur.clearSelection();
1102 LyXFont const old_font = real_current_font;
1104 // Prevents language turds in new lyxtexts under non-english
1105 BufferParams const & bufparams = cur.buffer().params();
1106 Language const * lang = cur.paragraph().getParLanguage(bufparams);
1107 current_font.setLanguage(lang);
1108 real_current_font.setLanguage(lang);
1110 string::const_iterator cit = cmd.argument.begin();
1111 string::const_iterator end = cmd.argument.end();
1112 for (; cit != end; ++cit)
1113 bv->owner()->getIntl().getTransManager().
1114 TranslateAndInsert(*cit, this);
1117 moveCursor(cur, false);
1119 needsUpdate = redoParagraph(cur.pit());
1121 // update only this paragraph
1122 cur.bv().update(Update::SinglePar | Update::Force);
1125 bv->updateScrollbar();
1130 InsetCommandParams p("url");
1131 string const data = InsetCommandMailer::params2string("url", p);
1132 bv->owner()->getDialogs().show("url", data, 0);
1136 case LFUN_HTMLURL: {
1137 InsetCommandParams p("htmlurl");
1138 string const data = InsetCommandMailer::params2string("url", p);
1139 bv->owner()->getDialogs().show("url", data, 0);
1143 case LFUN_INSERT_LABEL: {
1144 // Try to generate a valid label
1145 string const contents = cmd.argument.empty() ?
1146 cur.getPossibleLabel() : cmd.argument;
1148 InsetCommandParams p("label", contents);
1149 string const data = InsetCommandMailer::params2string("label", p);
1151 if (cmd.argument.empty()) {
1152 bv->owner()->getDialogs().show("label", data, 0);
1154 FuncRequest fr(LFUN_INSET_INSERT, data);
1162 case LFUN_INSET_LIST:
1163 case LFUN_INSET_THEOREM:
1164 case LFUN_INSET_CAPTION:
1166 case LFUN_INSERT_NOTE:
1167 case LFUN_INSERT_CHARSTYLE:
1168 case LFUN_INSERT_BOX:
1169 case LFUN_INSERT_BRANCH:
1170 case LFUN_INSERT_BIBITEM:
1171 case LFUN_INSET_ERT:
1172 case LFUN_INSET_FOOTNOTE:
1173 case LFUN_INSET_MARGINAL:
1174 case LFUN_INSET_OPTARG:
1175 case LFUN_ENVIRONMENT_INSERT:
1176 // Open the inset, and move the current selection
1178 doInsertInset(cur, this, cmd, true, true);
1182 case LFUN_TABULAR_INSERT:
1183 // if there were no arguments, just open the dialog
1184 if (doInsertInset(cur, this, cmd, false, true))
1187 bv->owner()->getDialogs().show("tabularcreate");
1191 case LFUN_INSET_FLOAT:
1192 case LFUN_INSET_WIDE_FLOAT:
1193 case LFUN_INSET_WRAP:
1194 doInsertInset(cur, this, cmd, true, true);
1196 // FIXME: the "Caption" name should not be hardcoded,
1197 // but given by the float definition.
1198 cur.dispatch(FuncRequest(LFUN_LAYOUT, "Caption"));
1201 case LFUN_INDEX_INSERT:
1202 // Just open the inset
1203 doInsertInset(cur, this, cmd, true, false);
1207 case LFUN_INDEX_PRINT:
1208 case LFUN_TOC_INSERT:
1210 case LFUN_INSERT_LINE:
1211 case LFUN_INSERT_PAGEBREAK:
1213 doInsertInset(cur, this, cmd, false, false);
1217 case LFUN_DEPTH_MIN:
1218 changeDepth(cur, DEC_DEPTH);
1221 case LFUN_DEPTH_PLUS:
1222 changeDepth(cur, INC_DEPTH);
1225 case LFUN_MATH_DISPLAY:
1226 mathDispatch(cur, cmd, true);
1229 case LFUN_MATH_IMPORT_SELECTION:
1230 case LFUN_MATH_MODE:
1231 if (cmd.argument == "on")
1232 // don't pass "on" as argument
1233 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1235 mathDispatch(cur, cmd, false);
1238 case LFUN_MATH_MACRO:
1239 if (cmd.argument.empty())
1240 cur.errorMessage(N_("Missing argument"));
1242 string s = cmd.argument;
1243 string const s1 = token(s, ' ', 1);
1244 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1245 string const s2 = token(s, ' ', 2);
1246 string const type = s2.empty() ? "newcommand" : s2;
1247 cur.insert(new MathMacroTemplate(token(s, ' ', 0), nargs, s2));
1248 //cur.nextInset()->edit(cur, true);
1252 // passthrough hat and underscore outside mathed:
1253 case LFUN_SUBSCRIPT:
1254 mathDispatch(cur, FuncRequest(LFUN_SELFINSERT, "_"), false);
1256 case LFUN_SUPERSCRIPT:
1257 mathDispatch(cur, FuncRequest(LFUN_SELFINSERT, "^"), false);
1260 case LFUN_INSERT_MATH:
1261 case LFUN_INSERT_MATRIX:
1262 case LFUN_MATH_DELIM: {
1263 cur.insert(new MathHullInset);
1264 cur.dispatch(FuncRequest(LFUN_RIGHT));
1265 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
1271 LyXFont font(LyXFont::ALL_IGNORE);
1272 font.setEmph(LyXFont::TOGGLE);
1273 toggleAndShow(cur, this, font);
1278 LyXFont font(LyXFont::ALL_IGNORE);
1279 font.setSeries(LyXFont::BOLD_SERIES);
1280 toggleAndShow(cur, this, font);
1285 LyXFont font(LyXFont::ALL_IGNORE);
1286 font.setNoun(LyXFont::TOGGLE);
1287 toggleAndShow(cur, this, font);
1292 LyXFont font(LyXFont::ALL_IGNORE);
1293 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
1294 toggleAndShow(cur, this, font);
1299 LyXFont font(LyXFont::ALL_IGNORE);
1300 font.setFamily(LyXFont::SANS_FAMILY);
1301 toggleAndShow(cur, this, font);
1306 LyXFont font(LyXFont::ALL_IGNORE);
1307 font.setFamily(LyXFont::ROMAN_FAMILY);
1308 toggleAndShow(cur, this, font);
1312 case LFUN_DEFAULT: {
1313 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
1314 toggleAndShow(cur, this, font);
1318 case LFUN_UNDERLINE: {
1319 LyXFont font(LyXFont::ALL_IGNORE);
1320 font.setUnderbar(LyXFont::TOGGLE);
1321 toggleAndShow(cur, this, font);
1325 case LFUN_FONT_SIZE: {
1326 LyXFont font(LyXFont::ALL_IGNORE);
1327 font.setLyXSize(cmd.argument);
1328 toggleAndShow(cur, this, font);
1332 case LFUN_LANGUAGE: {
1333 Language const * lang = languages.getLanguage(cmd.argument);
1336 LyXFont font(LyXFont::ALL_IGNORE);
1337 font.setLanguage(lang);
1338 toggleAndShow(cur, this, font);
1343 case LFUN_FREEFONT_APPLY:
1344 toggleAndShow(cur, this, freefont, toggleall);
1345 cur.message(_("Character set"));
1348 // Set the freefont using the contents of \param data dispatched from
1349 // the frontends and apply it at the current cursor location.
1350 case LFUN_FREEFONT_UPDATE: {
1353 if (bv_funcs::string2font(cmd.argument, font, toggle)) {
1356 toggleAndShow(cur, this, freefont, toggleall);
1357 cur.message(_("Character set"));
1362 case LFUN_FINISHED_LEFT:
1363 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1366 case LFUN_FINISHED_RIGHT:
1367 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1371 case LFUN_FINISHED_UP:
1372 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_UP:\n" << cur << endl;
1376 case LFUN_FINISHED_DOWN:
1377 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_DOWN:\n" << cur << endl;
1381 case LFUN_LAYOUT_PARAGRAPH: {
1383 params2string(cur.paragraph(), data);
1384 data = "show\n" + data;
1385 bv->owner()->getDialogs().show("paragraph", data);
1389 case LFUN_PARAGRAPH_UPDATE: {
1390 if (!bv->owner()->getDialogs().visible("paragraph"))
1393 params2string(cur.paragraph(), data);
1395 // Will the paragraph accept changes from the dialog?
1396 InsetBase & inset = cur.inset();
1397 bool const accept = !inset.forceDefaultParagraphs(&inset);
1399 data = "update " + convert<string>(accept) + '\n' + data;
1400 bv->owner()->getDialogs().update("paragraph", data);
1405 case LFUN_CIRCUMFLEX:
1415 case LFUN_SPECIAL_CARON:
1418 case LFUN_HUNG_UMLAUT:
1421 bv->owner()->getLyXFunc().handleKeyFunc(cmd.action);
1422 if (!cmd.argument.empty())
1423 bv->owner()->getIntl().getTransManager()
1424 .TranslateAndInsert(cmd.argument[0], this);
1427 case LFUN_FLOAT_LIST: {
1428 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1429 if (tclass.floats().typeExist(cmd.argument)) {
1430 // not quite sure if we want this...
1432 cur.clearSelection();
1433 breakParagraph(cur);
1435 if (cur.lastpos() != 0) {
1437 breakParagraph(cur);
1440 setLayout(cur, tclass.defaultLayoutName());
1441 setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1442 insertInset(cur, new InsetFloatList(cmd.argument));
1445 lyxerr << "Non-existent float type: "
1446 << cmd.argument << endl;
1451 case LFUN_ACCEPT_CHANGE: {
1456 case LFUN_REJECT_CHANGE: {
1461 case LFUN_THESAURUS_ENTRY: {
1462 string arg = cmd.argument;
1464 arg = cur.selectionAsString(false);
1466 if (arg.size() > 100 || arg.empty()) {
1467 // Get word or selection
1468 selectWordWhenUnderCursor(cur, lyx::WHOLE_WORD);
1469 arg = cur.selectionAsString(false);
1472 bv->owner()->getDialogs().show("thesaurus", arg);
1476 case LFUN_PARAGRAPH_APPLY: {
1477 // Given data, an encoding of the ParagraphParameters
1478 // generated in the Paragraph dialog, this function sets
1479 // the current paragraph appropriately.
1480 istringstream is(cmd.argument);
1483 ParagraphParameters params;
1488 params.labelWidthString(),
1490 cur.message(_("Paragraph layout set"));
1494 case LFUN_INSET_DIALOG_SHOW: {
1495 InsetBase * inset = cur.nextInset();
1497 FuncRequest fr(LFUN_INSET_DIALOG_SHOW);
1498 inset->dispatch(cur, fr);
1504 if (cur.selection()) {
1505 cur.selection() = false;
1508 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1513 lyxerr << BOOST_CURRENT_FUNCTION
1514 << " Not DISPATCHED by LyXText" << endl;
1520 && &oldTopSlice.inset() == &cur.inset()
1521 && oldTopSlice.idx() == cur.idx()
1523 && !cur.selection())
1530 bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
1531 FuncStatus & flag) const
1533 BOOST_ASSERT(cur.text() == this);
1534 LyXFont const & font = real_current_font;
1537 switch (cmd.action) {
1539 case LFUN_DEPTH_MIN:
1540 enable = changeDepthAllowed(cur, DEC_DEPTH);
1543 case LFUN_DEPTH_PLUS:
1544 enable = changeDepthAllowed(cur, INC_DEPTH);
1547 case LFUN_INSET_OPTARG:
1548 enable = numberOfOptArgs(cur.paragraph())
1549 < cur.paragraph().layout()->optionalargs;
1553 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1556 case LFUN_INSERT_BIBITEM:
1557 enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO);
1561 // the functions which insert insets
1562 InsetBase::Code code = InsetBase::NO_CODE;
1563 switch (cmd.action) {
1564 case LFUN_DIALOG_SHOW_NEW_INSET:
1565 if (cmd.argument == "bibitem")
1566 code = InsetBase::BIBITEM_CODE;
1567 else if (cmd.argument == "bibtex")
1568 code = InsetBase::BIBTEX_CODE;
1569 else if (cmd.argument == "box")
1570 code = InsetBase::BOX_CODE;
1571 else if (cmd.argument == "branch")
1572 code = InsetBase::BRANCH_CODE;
1573 else if (cmd.argument == "citation")
1574 code = InsetBase::CITE_CODE;
1575 else if (cmd.argument == "ert")
1576 code = InsetBase::ERT_CODE;
1577 else if (cmd.argument == "external")
1578 code = InsetBase::EXTERNAL_CODE;
1579 else if (cmd.argument == "float")
1580 code = InsetBase::FLOAT_CODE;
1581 else if (cmd.argument == "graphics")
1582 code = InsetBase::GRAPHICS_CODE;
1583 else if (cmd.argument == "include")
1584 code = InsetBase::INCLUDE_CODE;
1585 else if (cmd.argument == "index")
1586 code = InsetBase::INDEX_CODE;
1587 else if (cmd.argument == "label")
1588 code = InsetBase::LABEL_CODE;
1589 else if (cmd.argument == "note")
1590 code = InsetBase::NOTE_CODE;
1591 else if (cmd.argument == "ref")
1592 code = InsetBase::REF_CODE;
1593 else if (cmd.argument == "toc")
1594 code = InsetBase::TOC_CODE;
1595 else if (cmd.argument == "url")
1596 code = InsetBase::URL_CODE;
1597 else if (cmd.argument == "vspace")
1598 code = InsetBase::VSPACE_CODE;
1599 else if (cmd.argument == "wrap")
1600 code = InsetBase::WRAP_CODE;
1603 case LFUN_INSET_ERT:
1604 code = InsetBase::ERT_CODE;
1606 case LFUN_INSET_FOOTNOTE:
1607 code = InsetBase::FOOT_CODE;
1609 case LFUN_TABULAR_INSERT:
1610 code = InsetBase::TABULAR_CODE;
1612 case LFUN_INSET_MARGINAL:
1613 code = InsetBase::MARGIN_CODE;
1615 case LFUN_INSET_FLOAT:
1616 case LFUN_INSET_WIDE_FLOAT:
1617 code = InsetBase::FLOAT_CODE;
1619 case LFUN_INSET_WRAP:
1620 code = InsetBase::WRAP_CODE;
1622 case LFUN_FLOAT_LIST:
1623 code = InsetBase::FLOAT_LIST_CODE;
1626 case LFUN_INSET_LIST:
1627 code = InsetBase::LIST_CODE;
1629 case LFUN_INSET_THEOREM:
1630 code = InsetBase::THEOREM_CODE;
1633 case LFUN_INSET_CAPTION:
1634 code = InsetBase::CAPTION_CODE;
1636 case LFUN_INSERT_NOTE:
1637 code = InsetBase::NOTE_CODE;
1639 case LFUN_INSERT_CHARSTYLE:
1640 code = InsetBase::CHARSTYLE_CODE;
1641 if (buf->params().getLyXTextClass().charstyles().empty())
1644 case LFUN_INSERT_BOX:
1645 code = InsetBase::BOX_CODE;
1647 case LFUN_INSERT_BRANCH:
1648 code = InsetBase::BRANCH_CODE;
1649 if (buf->params().branchlist().empty())
1652 case LFUN_INSERT_LABEL:
1653 code = InsetBase::LABEL_CODE;
1655 case LFUN_INSET_OPTARG:
1656 code = InsetBase::OPTARG_CODE;
1658 case LFUN_ENVIRONMENT_INSERT:
1659 code = InsetBase::BOX_CODE;
1661 case LFUN_INDEX_INSERT:
1662 code = InsetBase::INDEX_CODE;
1664 case LFUN_INDEX_PRINT:
1665 code = InsetBase::INDEX_PRINT_CODE;
1667 case LFUN_TOC_INSERT:
1668 code = InsetBase::TOC_CODE;
1672 code = InsetBase::URL_CODE;
1675 // always allow this, since we will inset a raw quote
1676 // if an inset is not allowed.
1678 case LFUN_HYPHENATION:
1679 case LFUN_LIGATURE_BREAK:
1681 case LFUN_MENU_SEPARATOR:
1683 case LFUN_END_OF_SENTENCE:
1684 code = InsetBase::SPECIALCHAR_CODE;
1686 case LFUN_SPACE_INSERT:
1687 // slight hack: we know this is allowed in math mode
1689 code = InsetBase::SPACE_CODE;
1691 case LFUN_INSET_DIALOG_SHOW: {
1692 InsetBase * inset = cur.nextInset();
1695 code = inset->lyxCode();
1696 if (!(code == InsetBase::INCLUDE_CODE
1697 || code == InsetBase::BIBTEX_CODE
1698 || code == InsetBase::FLOAT_LIST_CODE
1699 || code == InsetBase::TOC_CODE))
1708 if (code != InsetBase::NO_CODE
1709 && (cur.empty() || !cur.inset().insetAllowed(code)))
1714 case LFUN_DIALOG_SHOW_NEW_INSET:
1715 case LFUN_INSET_ERT:
1716 case LFUN_INSERT_BOX:
1717 case LFUN_INSERT_BRANCH:
1718 case LFUN_ENVIRONMENT_INSERT:
1719 case LFUN_INDEX_INSERT:
1720 case LFUN_INDEX_PRINT:
1721 case LFUN_TOC_INSERT:
1725 case LFUN_HYPHENATION:
1726 case LFUN_LIGATURE_BREAK:
1728 case LFUN_MENU_SEPARATOR:
1730 case LFUN_END_OF_SENTENCE:
1731 case LFUN_SPACE_INSERT:
1732 case LFUN_INSET_DIALOG_SHOW:
1735 case LFUN_INSET_MODIFY:
1736 // We need to disable this, because we may get called for a
1738 // InsetTabular::getStatus() -> InsetText::getStatus()
1739 // and we don't handle LFUN_INSET_MODIFY.
1744 flag.setOnOff(font.emph() == LyXFont::ON);
1748 flag.setOnOff(font.noun() == LyXFont::ON);
1752 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1756 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1760 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1764 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1767 case LFUN_DELETE_WORD_FORWARD:
1768 case LFUN_DELETE_WORD_BACKWARD:
1769 case LFUN_DELETE_LINE_FORWARD:
1770 case LFUN_WORDRIGHT:
1780 case LFUN_UP_PARAGRAPHSEL:
1781 case LFUN_DOWN_PARAGRAPHSEL:
1786 case LFUN_WORDRIGHTSEL:
1787 case LFUN_WORDLEFTSEL:
1789 case LFUN_UP_PARAGRAPH:
1790 case LFUN_DOWN_PARAGRAPH:
1795 case LFUN_BREAKLINE:
1797 case LFUN_DELETE_SKIP:
1798 case LFUN_BACKSPACE:
1799 case LFUN_BACKSPACE_SKIP:
1800 case LFUN_BREAKPARAGRAPH:
1801 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1802 case LFUN_BREAKPARAGRAPH_SKIP:
1803 case LFUN_PARAGRAPH_SPACING:
1804 case LFUN_INSET_INSERT:
1805 case LFUN_NEXT_INSET_TOGGLE:
1806 case LFUN_UPCASE_WORD:
1807 case LFUN_LOWCASE_WORD:
1808 case LFUN_CAPITALIZE_WORD:
1809 case LFUN_TRANSPOSE_CHARS:
1816 case LFUN_GETLAYOUT:
1818 case LFUN_PASTESELECTION:
1819 case LFUN_DATE_INSERT:
1820 case LFUN_SELFINSERT:
1821 case LFUN_INSERT_LABEL:
1822 case LFUN_INSERT_NOTE:
1823 case LFUN_INSERT_CHARSTYLE:
1824 case LFUN_INSET_FLOAT:
1825 case LFUN_INSET_FOOTNOTE:
1826 case LFUN_INSET_MARGINAL:
1827 case LFUN_INSET_WIDE_FLOAT:
1828 case LFUN_INSET_WRAP:
1829 case LFUN_TABULAR_INSERT:
1830 case LFUN_INSERT_LINE:
1831 case LFUN_INSERT_PAGEBREAK:
1832 case LFUN_MATH_DISPLAY:
1833 case LFUN_MATH_IMPORT_SELECTION:
1834 case LFUN_MATH_MODE:
1835 case LFUN_MATH_MACRO:
1836 case LFUN_INSERT_MATH:
1837 case LFUN_INSERT_MATRIX:
1838 case LFUN_MATH_DELIM:
1839 case LFUN_SUBSCRIPT:
1840 case LFUN_SUPERSCRIPT:
1842 case LFUN_UNDERLINE:
1843 case LFUN_FONT_SIZE:
1845 case LFUN_FREEFONT_APPLY:
1846 case LFUN_FREEFONT_UPDATE:
1847 case LFUN_LAYOUT_PARAGRAPH:
1848 case LFUN_PARAGRAPH_UPDATE:
1850 case LFUN_CIRCUMFLEX:
1860 case LFUN_SPECIAL_CARON:
1863 case LFUN_HUNG_UMLAUT:
1866 case LFUN_FLOAT_LIST:
1867 case LFUN_ACCEPT_CHANGE:
1868 case LFUN_REJECT_CHANGE:
1869 case LFUN_THESAURUS_ENTRY:
1870 case LFUN_PARAGRAPH_APPLY:
1872 case LFUN_KEYMAP_TOGGLE:
1874 case LFUN_BEGINNINGBUF:
1875 case LFUN_BEGINNINGBUFSEL:
1876 case LFUN_ENDBUFSEL:
1877 // these are handled in our dispatch()
1884 flag.enabled(enable);