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"
61 #include "mathed/math_hullinset.h"
62 #include "mathed/math_macrotemplate.h"
64 #include <boost/current_function.hpp>
71 using lyx::cap::copySelection;
72 using lyx::cap::cutSelection;
73 using lyx::cap::pasteSelection;
74 using lyx::cap::replaceSelection;
76 using lyx::support::isStrUnsignedInt;
77 using lyx::support::token;
81 using std::istringstream;
84 extern string current_layout;
90 LyXFont freefont(LyXFont::ALL_IGNORE);
91 bool toggleall = false;
94 void toggleAndShow(LCursor & cur, LyXText * text,
95 LyXFont const & font, bool toggleall = true)
97 text->toggleFree(cur, font, toggleall);
99 if (font.language() != ignore_language ||
100 font.number() != LyXFont::IGNORE) {
101 Paragraph & par = cur.paragraph();
102 text->bidi.computeTables(par, cur.buffer(), cur.textRow());
103 if (cur.boundary() !=
104 text->bidi.isBoundary(cur.buffer(), par,
106 text->real_current_font))
107 text->setCursor(cur, cur.pit(), cur.pos(),
108 false, !cur.boundary());
113 void moveCursor(LCursor & cur, bool selecting)
115 if (selecting || cur.mark())
117 if (!cur.selection())
118 cur.bv().haveSelection(false);
119 cur.bv().switchKeyMap();
123 void finishChange(LCursor & cur, bool selecting)
126 moveCursor(cur, selecting);
130 void mathDispatch(LCursor & cur, FuncRequest const & cmd, bool display)
133 string sel = cur.selectionAsString(false);
134 lyxerr << "selection is: '" << sel << "'" << endl;
137 const int old_pos = cur.pos();
138 cur.insert(new MathHullInset);
139 BOOST_ASSERT(old_pos == cur.pos());
140 cur.nextInset()->edit(cur, true);
141 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
142 // don't do that also for LFUN_MATH_MODE
143 // unless you want end up with always changing
144 // to mathrm when opening an inlined inset --
145 // I really hate "LyXfunc overloading"...
147 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
148 cur.dispatch(FuncRequest(LFUN_INSERT_MATH, cmd.argument));
150 // create a macro if we see "\\newcommand"
151 // somewhere, and an ordinary formula
153 cutSelection(cur, true, true);
154 if (sel.find("\\newcommand") == string::npos
155 && sel.find("\\def") == string::npos)
157 cur.insert(new MathHullInset);
158 cur.dispatch(FuncRequest(LFUN_RIGHT));
159 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
160 cur.dispatch(FuncRequest(LFUN_INSERT_MATH, sel));
162 istringstream is(sel);
163 cur.insert(new MathMacroTemplate(is));
166 cur.message(N_("Math editor mode"));
175 string const freefont2string()
178 if (font2string(freefont, toggleall, data))
185 bool LyXText::cursorPrevious(LCursor & cur)
187 pos_type cpos = cur.pos();
188 lyx::pit_type cpar = cur.pit();
190 int x = cur.x_target();
192 setCursorFromCoordinates(cur, x, 0);
193 bool updated = cursorUp(cur);
195 if (cpar == cur.pit() && cpos == cur.pos()) {
196 // we have a row which is taller than the workarea. The
197 // simplest solution is to move to the previous row instead.
198 updated |= cursorUp(cur);
201 cur.bv().updateScrollbar();
207 bool LyXText::cursorNext(LCursor & cur)
209 pos_type cpos = cur.pos();
210 lyx::pit_type cpar = cur.pit();
212 int x = cur.x_target();
213 setCursorFromCoordinates(cur, x, cur.bv().workHeight() - 1);
214 bool updated = cursorDown(cur);
216 if (cpar == cur.pit() && cpos == cur.pos()) {
217 // we have a row which is taller than the workarea. The
218 // simplest solution is to move to the next row instead.
219 updated |= cursorDown(cur);
222 cur.bv().updateScrollbar();
230 void specialChar(LCursor & cur, InsetSpecialChar::Kind kind)
232 lyx::cap::replaceSelection(cur);
233 cur.insert(new InsetSpecialChar(kind));
238 bool doInsertInset(LCursor & cur, LyXText * text,
239 FuncRequest const & cmd, bool edit, bool pastesel)
241 InsetBase * inset = createInset(&cur.bv(), cmd);
247 if (cur.selection()) {
248 cur.bv().owner()->dispatch(FuncRequest(LFUN_CUT));
251 text->insertInset(cur, inset);
254 inset->edit(cur, true);
256 if (gotsel && pastesel)
257 cur.bv().owner()->dispatch(FuncRequest(LFUN_PASTE));
262 void update(LCursor & cur)
264 //we don't call update(true, false) directly to save a metrics call
265 if (cur.bv().fitCursor())
266 cur.bv().update(false, true);
273 void LyXText::number(LCursor & cur)
275 LyXFont font(LyXFont::ALL_IGNORE);
276 font.setNumber(LyXFont::TOGGLE);
277 toggleAndShow(cur, this, font);
281 bool LyXText::isRTL(Paragraph const & par) const
283 return par.isRightToLeftPar(bv()->buffer()->params());
287 void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
289 lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
290 lyxerr << "*** 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 if (cur.inset().asUpdatableInset())
731 cur.inset().asUpdatableInset()->showInsetDialog(bv);
734 case LFUN_NEXT_INSET_TOGGLE: {
735 InsetBase * inset = cur.nextInset();
737 cur.clearSelection();
738 FuncRequest fr = cmd;
739 fr.action = LFUN_INSET_TOGGLE;
740 inset->dispatch(cur, fr);
745 case LFUN_KEYMAP_TOGGLE:
746 cur.clearSelection();
750 case LFUN_SPACE_INSERT:
751 if (cur.paragraph().layout()->free_spacing)
752 insertChar(cur, ' ');
754 doInsertInset(cur, this, cmd, false, false);
757 moveCursor(cur, false);
760 case LFUN_HYPHENATION:
761 specialChar(cur, InsetSpecialChar::HYPHENATION);
764 case LFUN_LIGATURE_BREAK:
765 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
769 specialChar(cur, InsetSpecialChar::LDOTS);
772 case LFUN_END_OF_SENTENCE:
773 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
776 case LFUN_MENU_SEPARATOR:
777 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
780 case LFUN_UPCASE_WORD:
781 changeCase(cur, LyXText::text_uppercase);
784 case LFUN_LOWCASE_WORD:
785 changeCase(cur, LyXText::text_lowercase);
788 case LFUN_CAPITALIZE_WORD:
789 changeCase(cur, LyXText::text_capitalization);
792 case LFUN_TRANSPOSE_CHARS:
797 cur.message(_("Paste"));
798 lyx::cap::replaceSelection(cur);
800 #warning FIXME Check if the arg is in the domain of available selections.
802 if (isStrUnsignedInt(cmd.argument))
803 pasteSelection(cur, convert<unsigned int>(cmd.argument));
805 pasteSelection(cur, 0);
806 cur.clearSelection(); // bug 393
812 cutSelection(cur, true, true);
813 cur.message(_("Cut"));
818 cur.message(_("Copy"));
822 cur.message(convert<string>(cursorX(cur.top())) + ' '
823 + convert<string>(cursorY(cur.top())));
829 istringstream is(cmd.argument);
832 lyxerr << "SETXY: Could not parse coordinates in '"
833 << cmd.argument << std::endl;
835 setCursorFromCoordinates(cur, x, y);
840 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
842 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
849 cur.message(cur.paragraph().layout()->name());
853 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
854 << cmd.argument << endl;
856 // This is not the good solution to the empty argument
857 // problem, but it will hopefully suffice for 1.2.0.
858 // The correct solution would be to augument the
859 // function list/array with information about what
860 // functions needs arguments and their type.
861 if (cmd.argument.empty()) {
862 cur.errorMessage(_("LyX function 'layout' needs an argument."));
866 // Derive layout number from given argument (string)
867 // and current buffer's textclass (number)
868 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
869 bool hasLayout = tclass.hasLayout(cmd.argument);
870 string layout = cmd.argument;
872 // If the entry is obsolete, use the new one instead.
874 string const & obs = tclass[layout]->obsoleted_by();
880 cur.errorMessage(string(N_("Layout ")) + cmd.argument +
885 bool change_layout = (current_layout != layout);
887 if (!change_layout && cur.selection() &&
888 cur.selBegin().pit() != cur.selEnd().pit())
890 pit_type spit = cur.selBegin().pit();
891 pit_type epit = cur.selEnd().pit() + 1;
892 while (spit != epit) {
893 if (pars_[spit].layout()->name() != current_layout) {
894 change_layout = true;
902 current_layout = layout;
903 setLayout(cur, layout);
904 bv->owner()->setLayout(layout);
910 case LFUN_PASTESELECTION: {
911 cur.clearSelection();
912 string const clip = bv->getClipboard();
914 if (cmd.argument == "paragraph")
915 insertStringAsParagraphs(cur, clip);
917 insertStringAsLines(cur, clip);
923 lyx::cap::replaceSelection(cur);
924 Paragraph & par = cur.paragraph();
925 lyx::pos_type pos = cur.pos();
929 else if (cur.prevInset() && cur.prevInset()->isSpace())
932 c = par.getChar(pos - 1);
934 LyXLayout_ptr const & style = par.layout();
936 BufferParams const & bufparams = bv->buffer()->params();
937 if (!style->pass_thru
938 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
939 string arg = cmd.argument;
941 cur.insert(new InsetQuotes(c,
942 bufparams.quotes_language,
943 InsetQuotes::SingleQ));
944 else if (arg == "double")
945 cur.insert(new InsetQuotes(c,
946 bufparams.quotes_language,
947 InsetQuotes::DoubleQ));
949 cur.insert(new InsetQuotes(c, bufparams));
953 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
957 case LFUN_DATE_INSERT: {
958 lyx::cap::replaceSelection(cur);
959 time_t now_time_t = time(NULL);
960 struct tm * now_tm = localtime(&now_time_t);
961 setlocale(LC_TIME, "");
963 if (!cmd.argument.empty())
966 arg = lyxrc.date_insert_format;
968 int const datetmp_len =
969 ::strftime(datetmp, 32, arg.c_str(), now_tm);
971 for (int i = 0; i < datetmp_len; i++)
972 insertChar(cur, datetmp[i]);
975 moveCursor(cur, false);
979 case LFUN_MOUSE_TRIPLE:
980 if (cmd.button() == mouse_button::button1) {
986 bv->haveSelection(cur.selection());
990 case LFUN_MOUSE_DOUBLE:
991 if (cmd.button() == mouse_button::button1) {
992 selectWord(cur, lyx::WHOLE_WORD_STRICT);
994 bv->haveSelection(cur.selection());
998 // Single-click on work area
999 case LFUN_MOUSE_PRESS: {
1000 // Right click on a footnote flag opens float menu
1001 if (cmd.button() == mouse_button::button3) {
1002 cur.clearSelection();
1006 // Middle button press pastes if we have a selection
1007 // We do this here as if the selection was inside an inset
1008 // it could get cleared on the unlocking of the inset so
1009 // we have to check this first
1010 bool paste_internally = false;
1011 if (cmd.button() == mouse_button::button2 && cur.selection()) {
1012 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1013 paste_internally = true;
1016 // Clear the selection
1017 cur.clearSelection();
1019 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1024 // Has the cursor just left the inset?
1025 if (bv->cursor().inMathed() && !cur.inMathed())
1026 bv->cursor().inset().notifyCursorLeaves(bv->cursor());
1031 // Insert primary selection with middle mouse
1032 // if there is a local selection in the current buffer,
1034 if (cmd.button() == mouse_button::button2) {
1035 if (paste_internally)
1036 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1038 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1044 case LFUN_MOUSE_MOTION: {
1045 // Only use motion with button 1
1046 //if (cmd.button() != mouse_button::button1)
1049 // ignore motions deeper nested than the real anchor
1050 LCursor & bvcur = cur.bv().cursor();
1051 if (bvcur.anchor_.hasPart(cur)) {
1052 CursorSlice old = bvcur.top();
1054 int const wh = bv->workHeight();
1055 int const y = std::max(0, std::min(wh - 1, cmd.y));
1057 setCursorFromCoordinates(cur, cmd.x, y);
1058 cur.x_target() = cmd.x;
1063 // This is to allow jumping over large insets
1064 if (cur.top() == old) {
1071 if (cur.top() == old)
1074 // don't set anchor_
1075 bvcur.setCursor(cur);
1076 bvcur.selection() = true;
1077 lyxerr << "MOTION: " << bv->cursor() << endl;
1085 case LFUN_MOUSE_RELEASE: {
1086 if (cmd.button() == mouse_button::button2)
1090 if (cmd.button() == mouse_button::button1)
1091 bv->haveSelection(cur.selection());
1094 bv->owner()->updateMenubar();
1095 bv->owner()->updateToolbars();
1099 case LFUN_SELFINSERT: {
1100 if (cmd.argument.empty())
1103 // Automatically delete the currently selected
1104 // text and replace it with what is being
1105 // typed in now. Depends on lyxrc settings
1106 // "auto_region_delete", which defaults to
1109 if (lyxrc.auto_region_delete) {
1110 if (cur.selection())
1111 cutSelection(cur, false, false);
1112 bv->haveSelection(false);
1115 cur.clearSelection();
1116 LyXFont const old_font = real_current_font;
1118 // Prevents language turds in new lyxtexts under non-english
1119 BufferParams const & bufparams = cur.buffer().params();
1120 Language const * lang = cur.paragraph().getParLanguage(bufparams);
1121 current_font.setLanguage(lang);
1122 real_current_font.setLanguage(lang);
1124 string::const_iterator cit = cmd.argument.begin();
1125 string::const_iterator end = cmd.argument.end();
1126 for (; cit != end; ++cit)
1127 bv->owner()->getIntl().getTransManager().
1128 TranslateAndInsert(*cit, this);
1131 moveCursor(cur, false);
1133 // real_current_font.number can change so we need to
1134 // update the minibuffer
1135 if (old_font != real_current_font)
1136 bv->updateScrollbar();
1141 InsetCommandParams p("url");
1142 string const data = InsetCommandMailer::params2string("url", p);
1143 bv->owner()->getDialogs().show("url", data, 0);
1147 case LFUN_HTMLURL: {
1148 InsetCommandParams p("htmlurl");
1149 string const data = InsetCommandMailer::params2string("url", p);
1150 bv->owner()->getDialogs().show("url", data, 0);
1154 case LFUN_INSERT_LABEL: {
1155 // Try to generate a valid label
1156 string const contents = cmd.argument.empty() ?
1157 cur.getPossibleLabel() : cmd.argument;
1159 InsetCommandParams p("label", contents);
1160 string const data = InsetCommandMailer::params2string("label", p);
1162 if (cmd.argument.empty()) {
1163 bv->owner()->getDialogs().show("label", data, 0);
1165 FuncRequest fr(LFUN_INSET_INSERT, data);
1173 case LFUN_INSET_LIST:
1174 case LFUN_INSET_THEOREM:
1175 case LFUN_INSET_CAPTION:
1177 case LFUN_INSERT_NOTE:
1178 case LFUN_INSERT_CHARSTYLE:
1179 case LFUN_INSERT_BOX:
1180 case LFUN_INSERT_BRANCH:
1181 case LFUN_INSERT_BIBITEM:
1182 case LFUN_INSET_ERT:
1183 case LFUN_INSET_FOOTNOTE:
1184 case LFUN_INSET_MARGINAL:
1185 case LFUN_INSET_OPTARG:
1186 case LFUN_ENVIRONMENT_INSERT:
1187 // Open the inset, and move the current selection
1189 doInsertInset(cur, this, cmd, true, true);
1193 case LFUN_TABULAR_INSERT:
1194 // if there were no arguments, just open the dialog
1195 if (doInsertInset(cur, this, cmd, false, true))
1198 bv->owner()->getDialogs().show("tabularcreate");
1202 case LFUN_INSET_FLOAT:
1203 case LFUN_INSET_WIDE_FLOAT:
1204 case LFUN_INSET_WRAP:
1205 doInsertInset(cur, this, cmd, true, true);
1207 // FIXME: the "Caption" name should not be hardcoded,
1208 // but given by the float definition.
1209 cur.dispatch(FuncRequest(LFUN_LAYOUT, "Caption"));
1212 case LFUN_INDEX_INSERT:
1213 // Just open the inset
1214 doInsertInset(cur, this, cmd, true, false);
1218 case LFUN_INDEX_PRINT:
1219 case LFUN_TOC_INSERT:
1221 case LFUN_INSERT_LINE:
1222 case LFUN_INSERT_PAGEBREAK:
1224 doInsertInset(cur, this, cmd, false, false);
1228 case LFUN_DEPTH_MIN:
1229 changeDepth(cur, DEC_DEPTH);
1232 case LFUN_DEPTH_PLUS:
1233 changeDepth(cur, INC_DEPTH);
1236 case LFUN_MATH_DISPLAY:
1237 mathDispatch(cur, cmd, true);
1240 case LFUN_MATH_IMPORT_SELECTION:
1241 case LFUN_MATH_MODE:
1242 if (cmd.argument == "on")
1243 // don't pass "on" as argument
1244 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1246 mathDispatch(cur, cmd, false);
1249 case LFUN_MATH_MACRO:
1250 if (cmd.argument.empty())
1251 cur.errorMessage(N_("Missing argument"));
1253 string s = cmd.argument;
1254 string const s1 = token(s, ' ', 1);
1255 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1256 string const s2 = token(s, ' ', 2);
1257 string const type = s2.empty() ? "newcommand" : s2;
1258 cur.insert(new MathMacroTemplate(token(s, ' ', 0), nargs, s2));
1259 //cur.nextInset()->edit(cur, true);
1263 // passthrough hat and underscore outside mathed:
1264 case LFUN_SUBSCRIPT:
1265 mathDispatch(cur, FuncRequest(LFUN_SELFINSERT, "_"), false);
1267 case LFUN_SUPERSCRIPT:
1268 mathDispatch(cur, FuncRequest(LFUN_SELFINSERT, "^"), false);
1271 case LFUN_INSERT_MATH:
1272 case LFUN_INSERT_MATRIX:
1273 case LFUN_MATH_DELIM: {
1274 cur.insert(new MathHullInset);
1275 cur.dispatch(FuncRequest(LFUN_RIGHT));
1276 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
1282 LyXFont font(LyXFont::ALL_IGNORE);
1283 font.setEmph(LyXFont::TOGGLE);
1284 toggleAndShow(cur, this, font);
1289 LyXFont font(LyXFont::ALL_IGNORE);
1290 font.setSeries(LyXFont::BOLD_SERIES);
1291 toggleAndShow(cur, this, font);
1296 LyXFont font(LyXFont::ALL_IGNORE);
1297 font.setNoun(LyXFont::TOGGLE);
1298 toggleAndShow(cur, this, font);
1303 LyXFont font(LyXFont::ALL_IGNORE);
1304 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
1305 toggleAndShow(cur, this, font);
1310 LyXFont font(LyXFont::ALL_IGNORE);
1311 font.setFamily(LyXFont::SANS_FAMILY);
1312 toggleAndShow(cur, this, font);
1317 LyXFont font(LyXFont::ALL_IGNORE);
1318 font.setFamily(LyXFont::ROMAN_FAMILY);
1319 toggleAndShow(cur, this, font);
1323 case LFUN_DEFAULT: {
1324 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
1325 toggleAndShow(cur, this, font);
1329 case LFUN_UNDERLINE: {
1330 LyXFont font(LyXFont::ALL_IGNORE);
1331 font.setUnderbar(LyXFont::TOGGLE);
1332 toggleAndShow(cur, this, font);
1336 case LFUN_FONT_SIZE: {
1337 LyXFont font(LyXFont::ALL_IGNORE);
1338 font.setLyXSize(cmd.argument);
1339 toggleAndShow(cur, this, font);
1343 case LFUN_LANGUAGE: {
1344 Language const * lang = languages.getLanguage(cmd.argument);
1347 LyXFont font(LyXFont::ALL_IGNORE);
1348 font.setLanguage(lang);
1349 toggleAndShow(cur, this, font);
1354 case LFUN_FREEFONT_APPLY:
1355 toggleAndShow(cur, this, freefont, toggleall);
1356 cur.message(_("Character set"));
1359 // Set the freefont using the contents of \param data dispatched from
1360 // the frontends and apply it at the current cursor location.
1361 case LFUN_FREEFONT_UPDATE: {
1364 if (bv_funcs::string2font(cmd.argument, font, toggle)) {
1367 toggleAndShow(cur, this, freefont, toggleall);
1368 cur.message(_("Character set"));
1373 case LFUN_FINISHED_LEFT:
1374 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1377 case LFUN_FINISHED_RIGHT:
1378 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1382 case LFUN_FINISHED_UP:
1383 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_UP:\n" << cur << endl;
1387 case LFUN_FINISHED_DOWN:
1388 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_DOWN:\n" << cur << endl;
1392 case LFUN_LAYOUT_PARAGRAPH: {
1394 params2string(cur.paragraph(), data);
1395 data = "show\n" + data;
1396 bv->owner()->getDialogs().show("paragraph", data);
1400 case LFUN_PARAGRAPH_UPDATE: {
1401 if (!bv->owner()->getDialogs().visible("paragraph"))
1404 params2string(cur.paragraph(), data);
1406 // Will the paragraph accept changes from the dialog?
1407 InsetBase & inset = cur.inset();
1408 bool const accept = !inset.forceDefaultParagraphs(&inset);
1410 data = "update " + convert<string>(accept) + '\n' + data;
1411 bv->owner()->getDialogs().update("paragraph", data);
1416 case LFUN_CIRCUMFLEX:
1426 case LFUN_SPECIAL_CARON:
1429 case LFUN_HUNG_UMLAUT:
1432 bv->owner()->getLyXFunc().handleKeyFunc(cmd.action);
1433 if (!cmd.argument.empty())
1434 bv->owner()->getIntl().getTransManager()
1435 .TranslateAndInsert(cmd.argument[0], this);
1438 case LFUN_FLOAT_LIST: {
1439 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1440 if (tclass.floats().typeExist(cmd.argument)) {
1441 // not quite sure if we want this...
1443 cur.clearSelection();
1444 breakParagraph(cur);
1446 if (cur.lastpos() != 0) {
1448 breakParagraph(cur);
1451 setLayout(cur, tclass.defaultLayoutName());
1452 setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1453 insertInset(cur, new InsetFloatList(cmd.argument));
1456 lyxerr << "Non-existent float type: "
1457 << cmd.argument << endl;
1462 case LFUN_ACCEPT_CHANGE: {
1467 case LFUN_REJECT_CHANGE: {
1472 case LFUN_THESAURUS_ENTRY: {
1473 string arg = cmd.argument;
1475 arg = cur.selectionAsString(false);
1477 if (arg.size() > 100 || arg.empty()) {
1478 // Get word or selection
1479 selectWordWhenUnderCursor(cur, lyx::WHOLE_WORD);
1480 arg = cur.selectionAsString(false);
1483 bv->owner()->getDialogs().show("thesaurus", arg);
1487 case LFUN_PARAGRAPH_APPLY: {
1488 // Given data, an encoding of the ParagraphParameters
1489 // generated in the Paragraph dialog, this function sets
1490 // the current paragraph appropriately.
1491 istringstream is(cmd.argument);
1494 ParagraphParameters params;
1499 params.labelWidthString(),
1501 cur.message(_("Paragraph layout set"));
1505 case LFUN_INSET_DIALOG_SHOW: {
1506 InsetBase * inset = cur.nextInset();
1508 FuncRequest fr(LFUN_INSET_DIALOG_SHOW);
1509 inset->dispatch(cur, fr);
1515 if (cur.selection()) {
1516 cur.selection() = false;
1519 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1524 lyxerr << BOOST_CURRENT_FUNCTION
1525 << " Not DISPATCHED by LyXText" << endl;
1531 && &oldTopSlice.inset() == &cur.inset()
1532 && oldTopSlice.idx() == cur.idx()
1534 && !cur.selection())
1541 bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
1542 FuncStatus & flag) const
1544 BOOST_ASSERT(cur.text() == this);
1545 LyXFont const & font = real_current_font;
1548 switch (cmd.action) {
1550 case LFUN_DEPTH_MIN:
1551 enable = changeDepthAllowed(cur, DEC_DEPTH);
1554 case LFUN_DEPTH_PLUS:
1555 enable = changeDepthAllowed(cur, INC_DEPTH);
1558 case LFUN_INSET_OPTARG:
1559 enable = numberOfOptArgs(cur.paragraph())
1560 < cur.paragraph().layout()->optionalargs;
1564 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1568 // the functions which insert insets
1569 InsetBase::Code code = InsetBase::NO_CODE;
1570 switch (cmd.action) {
1571 case LFUN_DIALOG_SHOW_NEW_INSET:
1572 if (cmd.argument == "bibitem")
1573 code = InsetBase::BIBITEM_CODE;
1574 else if (cmd.argument == "bibtex")
1575 code = InsetBase::BIBTEX_CODE;
1576 else if (cmd.argument == "box")
1577 code = InsetBase::BOX_CODE;
1578 else if (cmd.argument == "branch")
1579 code = InsetBase::BRANCH_CODE;
1580 else if (cmd.argument == "citation")
1581 code = InsetBase::CITE_CODE;
1582 else if (cmd.argument == "ert")
1583 code = InsetBase::ERT_CODE;
1584 else if (cmd.argument == "external")
1585 code = InsetBase::EXTERNAL_CODE;
1586 else if (cmd.argument == "float")
1587 code = InsetBase::FLOAT_CODE;
1588 else if (cmd.argument == "graphics")
1589 code = InsetBase::GRAPHICS_CODE;
1590 else if (cmd.argument == "include")
1591 code = InsetBase::INCLUDE_CODE;
1592 else if (cmd.argument == "index")
1593 code = InsetBase::INDEX_CODE;
1594 else if (cmd.argument == "label")
1595 code = InsetBase::LABEL_CODE;
1596 else if (cmd.argument == "note")
1597 code = InsetBase::NOTE_CODE;
1598 else if (cmd.argument == "ref")
1599 code = InsetBase::REF_CODE;
1600 else if (cmd.argument == "toc")
1601 code = InsetBase::TOC_CODE;
1602 else if (cmd.argument == "url")
1603 code = InsetBase::URL_CODE;
1604 else if (cmd.argument == "vspace")
1605 code = InsetBase::VSPACE_CODE;
1606 else if (cmd.argument == "wrap")
1607 code = InsetBase::WRAP_CODE;
1610 case LFUN_INSET_ERT:
1611 code = InsetBase::ERT_CODE;
1613 case LFUN_INSET_FOOTNOTE:
1614 code = InsetBase::FOOT_CODE;
1616 case LFUN_TABULAR_INSERT:
1617 code = InsetBase::TABULAR_CODE;
1619 case LFUN_INSET_MARGINAL:
1620 code = InsetBase::MARGIN_CODE;
1622 case LFUN_INSET_FLOAT:
1623 case LFUN_INSET_WIDE_FLOAT:
1624 code = InsetBase::FLOAT_CODE;
1626 case LFUN_INSET_WRAP:
1627 code = InsetBase::WRAP_CODE;
1629 case LFUN_FLOAT_LIST:
1630 code = InsetBase::FLOAT_LIST_CODE;
1633 case LFUN_INSET_LIST:
1634 code = InsetBase::LIST_CODE;
1636 case LFUN_INSET_THEOREM:
1637 code = InsetBase::THEOREM_CODE;
1640 case LFUN_INSET_CAPTION:
1641 code = InsetBase::CAPTION_CODE;
1643 case LFUN_INSERT_NOTE:
1644 code = InsetBase::NOTE_CODE;
1646 case LFUN_INSERT_CHARSTYLE:
1647 code = InsetBase::CHARSTYLE_CODE;
1648 if (buf->params().getLyXTextClass().charstyles().empty())
1651 case LFUN_INSERT_BOX:
1652 code = InsetBase::BOX_CODE;
1654 case LFUN_INSERT_BRANCH:
1655 code = InsetBase::BRANCH_CODE;
1656 if (buf->params().branchlist().empty())
1659 case LFUN_INSERT_LABEL:
1660 code = InsetBase::LABEL_CODE;
1662 case LFUN_INSET_OPTARG:
1663 code = InsetBase::OPTARG_CODE;
1665 case LFUN_ENVIRONMENT_INSERT:
1666 code = InsetBase::BOX_CODE;
1668 case LFUN_INDEX_INSERT:
1669 code = InsetBase::INDEX_CODE;
1671 case LFUN_INDEX_PRINT:
1672 code = InsetBase::INDEX_PRINT_CODE;
1674 case LFUN_TOC_INSERT:
1675 code = InsetBase::TOC_CODE;
1679 code = InsetBase::URL_CODE;
1682 // always allow this, since we will inset a raw quote
1683 // if an inset is not allowed.
1685 case LFUN_HYPHENATION:
1686 case LFUN_LIGATURE_BREAK:
1688 case LFUN_MENU_SEPARATOR:
1690 case LFUN_END_OF_SENTENCE:
1691 code = InsetBase::SPECIALCHAR_CODE;
1693 case LFUN_SPACE_INSERT:
1694 // slight hack: we know this is allowed in math mode
1696 code = InsetBase::SPACE_CODE;
1698 case LFUN_INSET_DIALOG_SHOW: {
1699 InsetBase * inset = cur.nextInset();
1702 code = inset->lyxCode();
1703 if (!(code == InsetBase::INCLUDE_CODE
1704 || code == InsetBase::BIBTEX_CODE
1705 || code == InsetBase::FLOAT_LIST_CODE
1706 || code == InsetBase::TOC_CODE))
1715 if (code != InsetBase::NO_CODE
1716 && (cur.empty() || !cur.inset().insetAllowed(code)))
1721 case LFUN_DIALOG_SHOW_NEW_INSET:
1722 case LFUN_INSET_ERT:
1723 case LFUN_INSERT_BOX:
1724 case LFUN_INSERT_BRANCH:
1725 case LFUN_ENVIRONMENT_INSERT:
1726 case LFUN_INDEX_INSERT:
1727 case LFUN_INDEX_PRINT:
1728 case LFUN_TOC_INSERT:
1732 case LFUN_HYPHENATION:
1733 case LFUN_LIGATURE_BREAK:
1735 case LFUN_MENU_SEPARATOR:
1737 case LFUN_END_OF_SENTENCE:
1738 case LFUN_SPACE_INSERT:
1739 case LFUN_INSET_DIALOG_SHOW:
1742 case LFUN_INSET_MODIFY:
1743 // We need to disable this, because we may get called for a
1745 // InsetTabular::getStatus() -> InsetText::getStatus()
1746 // and we don't handle LFUN_INSET_MODIFY.
1751 flag.setOnOff(font.emph() == LyXFont::ON);
1755 flag.setOnOff(font.noun() == LyXFont::ON);
1759 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1763 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1767 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1771 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1774 case LFUN_DELETE_WORD_FORWARD:
1775 case LFUN_DELETE_WORD_BACKWARD:
1776 case LFUN_DELETE_LINE_FORWARD:
1777 case LFUN_WORDRIGHT:
1787 case LFUN_UP_PARAGRAPHSEL:
1788 case LFUN_DOWN_PARAGRAPHSEL:
1793 case LFUN_WORDRIGHTSEL:
1794 case LFUN_WORDLEFTSEL:
1796 case LFUN_UP_PARAGRAPH:
1797 case LFUN_DOWN_PARAGRAPH:
1802 case LFUN_BREAKLINE:
1804 case LFUN_DELETE_SKIP:
1805 case LFUN_BACKSPACE:
1806 case LFUN_BACKSPACE_SKIP:
1807 case LFUN_BREAKPARAGRAPH:
1808 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1809 case LFUN_BREAKPARAGRAPH_SKIP:
1810 case LFUN_PARAGRAPH_SPACING:
1811 case LFUN_INSET_INSERT:
1812 case LFUN_NEXT_INSET_TOGGLE:
1813 case LFUN_UPCASE_WORD:
1814 case LFUN_LOWCASE_WORD:
1815 case LFUN_CAPITALIZE_WORD:
1816 case LFUN_TRANSPOSE_CHARS:
1823 case LFUN_GETLAYOUT:
1825 case LFUN_PASTESELECTION:
1826 case LFUN_DATE_INSERT:
1827 case LFUN_SELFINSERT:
1828 case LFUN_INSERT_LABEL:
1829 case LFUN_INSERT_NOTE:
1830 case LFUN_INSERT_CHARSTYLE:
1831 case LFUN_INSERT_BIBITEM:
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);