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 "bufferparams.h"
24 #include "BufferView.h"
26 #include "coordcache.h"
27 #include "CutAndPaste.h"
29 #include "dispatchresult.h"
31 #include "funcrequest.h"
39 #include "paragraph.h"
40 #include "paragraph_funcs.h"
41 #include "ParagraphParameters.h"
45 #include "frontends/Dialogs.h"
46 #include "frontends/LyXView.h"
48 #include "insets/insetcommand.h"
49 #include "insets/insetfloatlist.h"
50 #include "insets/insetnewline.h"
51 #include "insets/insetquotes.h"
52 #include "insets/insetspecialchar.h"
53 #include "insets/insettext.h"
55 #include "support/lstrings.h"
56 #include "support/lyxlib.h"
57 #include "support/tostr.h"
59 #include "mathed/math_hullinset.h"
60 #include "mathed/math_macrotemplate.h"
67 using lyx::cap::copySelection;
68 using lyx::cap::cutSelection;
69 using lyx::cap::pasteSelection;
70 using lyx::cap::replaceSelection;
72 using lyx::support::isStrUnsignedInt;
73 using lyx::support::strToUnsignedInt;
74 using lyx::support::atoi;
75 using lyx::support::token;
80 using std::istringstream;
84 extern string current_layout;
86 // the selection possible is needed, that only motion events are
87 // used, where the button press event was on the drawing area too
88 bool selection_possible = false;
94 LyXFont freefont(LyXFont::ALL_IGNORE);
95 bool toggleall = false;
98 void toggleAndShow(LCursor & cur, LyXText * text,
99 LyXFont const & font, bool toggleall = true)
101 text->toggleFree(cur, font, toggleall);
103 if (font.language() != ignore_language ||
104 font.number() != LyXFont::IGNORE) {
105 Paragraph & par = cur.paragraph();
106 text->bidi.computeTables(par, cur.buffer(), cur.textRow());
107 if (cur.boundary() !=
108 text->bidi.isBoundary(cur.buffer(), par,
110 text->real_current_font))
111 text->setCursor(cur, cur.par(), cur.pos(),
112 false, !cur.boundary());
117 void moveCursor(LCursor & cur, bool selecting)
119 if (selecting || cur.mark())
121 if (!cur.selection())
122 cur.bv().haveSelection(false);
123 cur.bv().switchKeyMap();
127 void finishChange(LCursor & cur, bool selecting)
130 moveCursor(cur, selecting);
134 void mathDispatch(LCursor & cur, FuncRequest const & cmd, bool display)
137 string sel = cur.selectionAsString(false);
138 lyxerr << "selection is: '" << sel << "'" << endl;
141 const int old_pos = cur.pos();
142 cur.insert(new MathHullInset);
143 BOOST_ASSERT(old_pos == cur.pos());
144 cur.nextInset()->edit(cur, true);
145 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
146 // don't do that also for LFUN_MATH_MODE unless you want end up with
147 // always changing to mathrm when opening an inlined inset
148 // -- I really hate "LyXfunc overloading"...
150 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
151 cur.dispatch(FuncRequest(LFUN_INSERT_MATH, cmd.argument));
153 // create a macro if we see "\\newcommand" somewhere, and an ordinary
155 cutSelection(cur, true, true);
156 if (sel.find("\\newcommand") == string::npos
157 && sel.find("\\def") == string::npos)
159 cur.insert(new MathHullInset);
160 cur.dispatch(FuncRequest(LFUN_RIGHT));
161 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
162 cur.dispatch(FuncRequest(LFUN_INSERT_MATH, sel));
164 istringstream is(sel);
165 cur.insert(new MathMacroTemplate(is));
168 cur.message(N_("Math editor mode"));
177 string const freefont2string()
180 if (font2string(freefont, toggleall, data))
187 bool LyXText::gotoNextInset(LCursor & cur,
188 vector<InsetOld_code> const & codes, string const & contents)
190 BOOST_ASSERT(this == cur.text());
191 par_type end = paragraphs().size();
192 par_type pit = cur.par();
193 pos_type pos = cur.pos();
197 if (pos + 1 < pars_[pit].size()) {
204 } while (pit != end &&
205 !(pars_[pit].isInset(pos) &&
206 (inset = pars_[pit].getInset(pos)) != 0 &&
207 find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
209 static_cast<InsetCommand *>(pars_[pit].getInset(pos))->getContents()
215 setCursor(cur, pit, pos, false);
220 void LyXText::gotoInset(LCursor & cur,
221 vector<InsetOld_code> const & codes, bool same_content)
223 cur.clearSelection();
227 && cur.pos() < cur.lastpos()
228 && cur.paragraph().isInset(cur.pos())) {
229 InsetBase const * inset = cur.paragraph().getInset(cur.pos());
230 if (find(codes.begin(), codes.end(), inset->lyxCode())
232 contents = static_cast<InsetCommand const *>(inset)->getContents();
235 if (!gotoNextInset(cur, codes, contents)) {
236 if (cur.pos() || cur.par() != 0) {
237 CursorSlice tmp = cur.top();
240 if (!gotoNextInset(cur, codes, contents)) {
242 cur.message(_("No more insets"));
245 cur.message(_("No more insets"));
252 void LyXText::gotoInset(LCursor & cur, InsetOld_code code, bool same_content)
254 gotoInset(cur, vector<InsetOld_code>(1, code), same_content);
258 void LyXText::cursorPrevious(LCursor & cur)
260 pos_type cpos = cur.pos();
261 lyx::par_type cpar = cur.par();
263 int x = cur.x_target();
264 int y = cur.bv().top_y();
265 setCursorFromCoordinates(cur, x, y);
267 if (cpar == cur.par() && cpos == cur.pos()) {
268 // we have a row which is taller than the workarea. The
269 // simplest solution is to move to the previous row instead.
273 cur.bv().updateScrollbar();
278 void LyXText::cursorNext(LCursor & cur)
280 pos_type cpos = cur.pos();
281 lyx::par_type cpar = cur.par();
283 int x = cur.x_target();
284 int y = cur.bv().top_y() + cur.bv().workHeight();
285 setCursorFromCoordinates(cur, x, y);
287 if (cpar == cur.par() && cpos == cur.pos()) {
288 // we have a row which is taller than the workarea. The
289 // simplest solution is to move to the next row instead.
293 cur.bv().updateScrollbar();
300 void specialChar(LCursor & cur, InsetSpecialChar::Kind kind)
302 lyx::cap::replaceSelection(cur);
303 cur.insert(new InsetSpecialChar(kind));
308 void doInsertInset(LCursor & cur, LyXText * text,
309 FuncRequest const & cmd, bool edit, bool pastesel)
311 InsetBase * inset = createInset(&cur.bv(), cmd);
317 if (cur.selection()) {
318 cur.bv().owner()->dispatch(FuncRequest(LFUN_CUT));
321 text->insertInset(cur, inset);
324 inset->edit(cur, true);
326 if (gotsel && pastesel)
327 cur.bv().owner()->dispatch(FuncRequest(LFUN_PASTE));
333 void LyXText::number(LCursor & cur)
335 LyXFont font(LyXFont::ALL_IGNORE);
336 font.setNumber(LyXFont::TOGGLE);
337 toggleAndShow(cur, this, font);
341 bool LyXText::isRTL(Paragraph const & par) const
343 return par.isRightToLeftPar(bv()->buffer()->params());
347 void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
349 lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
350 //lyxerr << "*** LyXText::dispatch: cmd: " << cmd << endl;
352 BOOST_ASSERT(cur.text() == this);
353 BufferView * bv = &cur.bv();
354 CursorSlice sl = cur.top();
355 bool sel = cur.selection();
358 switch (cmd.action) {
360 case LFUN_APPENDIX: {
361 Paragraph & par = cur.paragraph();
362 bool start = !par.params().startOfAppendix();
364 // ensure that we have only one start_of_appendix in this document
365 for (par_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
366 if (pars_[tmp].params().startOfAppendix()) {
368 pars_[tmp].params().startOfAppendix(false);
375 par.params().startOfAppendix(start);
377 // we can set the refreshing parameters now
383 case LFUN_DELETE_WORD_FORWARD:
384 cur.clearSelection();
385 deleteWordForward(cur);
386 finishChange(cur, false);
389 case LFUN_DELETE_WORD_BACKWARD:
390 cur.clearSelection();
391 deleteWordBackward(cur);
392 finishChange(cur, false);
395 case LFUN_DELETE_LINE_FORWARD:
396 cur.clearSelection();
397 deleteLineForward(cur);
398 finishChange(cur, false);
404 cur.clearSelection();
405 if (isRTL(cur.paragraph()))
406 cursorLeftOneWord(cur);
408 cursorRightOneWord(cur);
409 finishChange(cur, false);
415 cur.clearSelection();
416 if (isRTL(cur.paragraph()))
417 cursorRightOneWord(cur);
419 cursorLeftOneWord(cur);
420 finishChange(cur, false);
423 case LFUN_BEGINNINGBUF:
424 if (cur.size() == 1) {
426 cur.clearSelection();
428 finishChange(cur, false);
435 if (cur.size() == 1) {
437 cur.clearSelection();
439 finishChange(cur, false);
448 //lyxerr << "handle LFUN_RIGHT[SEL]:\n" << cur << endl;
449 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
450 if (isRTL(cur.paragraph()))
454 if (sl == cur.top()) {
456 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
463 //lyxerr << "handle LFUN_LEFT[SEL]:\n" << cur << endl;
464 cur.selHandle(cmd.action == LFUN_LEFTSEL);
465 if (isRTL(cur.paragraph()))
469 if (sl == cur.top()) {
471 cmd = FuncRequest(LFUN_FINISHED_LEFT);
478 //lyxerr << "handle LFUN_UP[SEL]:\n" << cur << endl;
479 cur.selHandle(cmd.action == LFUN_UPSEL);
481 if (sl == cur.top()) {
483 cmd = FuncRequest(LFUN_FINISHED_UP);
490 //lyxerr << "handle LFUN_DOWN[SEL]:\n" << cur << endl;
491 cur.selHandle(cmd.action == LFUN_DOWNSEL);
493 if (sl == cur.top()) {
495 cmd = FuncRequest(LFUN_FINISHED_DOWN);
499 case LFUN_UP_PARAGRAPHSEL:
500 if (!cur.selection())
502 cursorUpParagraph(cur);
503 finishChange(cur, true);
506 case LFUN_DOWN_PARAGRAPHSEL:
507 if (!cur.selection())
509 cursorDownParagraph(cur);
510 finishChange(cur, true);
514 if (!cur.selection())
517 finishChange(cur, true);
521 if (!cur.selection())
524 finishChange(cur, true);
528 if (!cur.selection())
531 finishChange(cur, true);
535 if (!cur.selection())
538 finishChange(cur, true);
541 case LFUN_WORDRIGHTSEL:
542 if (!cur.selection())
544 if (isRTL(cur.paragraph()))
545 cursorLeftOneWord(cur);
547 cursorRightOneWord(cur);
548 finishChange(cur, true);
551 case LFUN_WORDLEFTSEL:
552 if (!cur.selection())
554 if (isRTL(cur.paragraph()))
555 cursorRightOneWord(cur);
557 cursorLeftOneWord(cur);
558 finishChange(cur, true);
562 selectWord(cur, lyx::WHOLE_WORD);
563 finishChange(cur, true);
567 case LFUN_UP_PARAGRAPH:
570 cur.clearSelection();
571 cursorUpParagraph(cur);
572 finishChange(cur, false);
575 case LFUN_DOWN_PARAGRAPH:
578 cur.clearSelection();
579 cursorDownParagraph(cur);
580 finishChange(cur, false);
586 cur.clearSelection();
587 finishChange(cur, false);
588 if (cur.par() == 0 && cur.textRow().pos() == 0) {
590 cmd = FuncRequest(LFUN_FINISHED_UP);
599 cur.clearSelection();
600 finishChange(cur, false);
601 if (cur.par() == cur.lastpar()
602 && cur.textRow().endpos() == cur.lastpos()) {
604 cmd = FuncRequest(LFUN_FINISHED_DOWN);
612 cur.clearSelection();
614 finishChange(cur, false);
619 cur.clearSelection();
621 finishChange(cur, false);
624 case LFUN_BREAKLINE: {
625 // Not allowed by LaTeX (labels or empty par)
626 if (cur.pos() > cur.paragraph().beginOfBody()) {
627 lyx::cap::replaceSelection(cur);
628 cur.insert(new InsetNewline);
630 moveCursor(cur, false);
636 if (!cur.selection()) {
639 // It is possible to make it a lot faster still
640 // just comment out the line below...
642 cutSelection(cur, true, false);
644 moveCursor(cur, false);
647 case LFUN_DELETE_SKIP:
648 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
649 if (!cur.selection()) {
650 if (cur.pos() == cur.lastpos()) {
657 cutSelection(cur, true, false);
663 if (!cur.selection()) {
664 if (bv->owner()->getIntl().getTransManager().backspace()) {
667 // It is possible to make it a lot faster still
668 // just comment out the line below...
671 cutSelection(cur, true, false);
676 case LFUN_BACKSPACE_SKIP:
677 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
678 if (!cur.selection()) {
682 //CursorSlice cur = cursor();
686 cutSelection(cur, true, false);
690 case LFUN_BREAKPARAGRAPH:
691 lyx::cap::replaceSelection(cur);
692 breakParagraph(cur, 0);
697 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
698 lyx::cap::replaceSelection(cur);
699 breakParagraph(cur, 1);
704 case LFUN_BREAKPARAGRAPH_SKIP: {
705 // When at the beginning of a paragraph, remove
706 // indentation. Otherwise, do the same as LFUN_BREAKPARAGRAPH.
707 lyx::cap::replaceSelection(cur);
709 cur.paragraph().params().labelWidthString(string());
711 breakParagraph(cur, 0);
717 case LFUN_PARAGRAPH_SPACING: {
718 Paragraph & par = cur.paragraph();
719 Spacing::Space cur_spacing = par.params().spacing().getSpace();
720 float cur_value = 1.0;
721 if (cur_spacing == Spacing::Other)
722 cur_value = par.params().spacing().getValue();
724 istringstream is(cmd.argument);
727 Spacing::Space new_spacing = cur_spacing;
728 float new_value = cur_value;
730 lyxerr << "Missing argument to `paragraph-spacing'"
732 } else if (tmp == "single") {
733 new_spacing = Spacing::Single;
734 } else if (tmp == "onehalf") {
735 new_spacing = Spacing::Onehalf;
736 } else if (tmp == "double") {
737 new_spacing = Spacing::Double;
738 } else if (tmp == "other") {
739 new_spacing = Spacing::Other;
742 lyxerr << "new_value = " << tmpval << endl;
745 } else if (tmp == "default") {
746 new_spacing = Spacing::Default;
748 lyxerr << _("Unknown spacing argument: ")
749 << cmd.argument << endl;
751 if (cur_spacing != new_spacing || cur_value != new_value) {
752 par.params().spacing(Spacing(new_spacing, new_value));
758 case LFUN_INSET_APPLY: {
759 string const name = cmd.getArg(0);
760 InsetBase * inset = bv->owner()->getDialogs().getOpenInset(name);
762 FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument);
763 inset->dispatch(cur, fr);
765 FuncRequest fr(LFUN_INSET_INSERT, cmd.argument);
771 case LFUN_INSET_INSERT: {
773 InsetBase * inset = createInset(bv, cmd);
775 insertInset(cur, inset);
781 case LFUN_INSET_SETTINGS:
782 if (cur.inset().asUpdatableInset())
783 cur.inset().asUpdatableInset()->showInsetDialog(bv);
786 case LFUN_NEXT_INSET_TOGGLE: {
787 InsetBase * inset = cur.nextInset();
789 cur.clearSelection();
790 FuncRequest fr = cmd;
791 fr.action = LFUN_INSET_TOGGLE;
792 inset->dispatch(cur, fr);
797 case LFUN_KEYMAP_TOGGLE:
798 cur.clearSelection();
802 case LFUN_SPACE_INSERT:
803 if (cur.paragraph().layout()->free_spacing)
804 insertChar(cur, ' ');
806 doInsertInset(cur, this, cmd, false, false);
809 moveCursor(cur, false);
812 case LFUN_HYPHENATION:
813 specialChar(cur, InsetSpecialChar::HYPHENATION);
816 case LFUN_LIGATURE_BREAK:
817 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
821 specialChar(cur, InsetSpecialChar::LDOTS);
824 case LFUN_END_OF_SENTENCE:
825 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
828 case LFUN_MENU_SEPARATOR:
829 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
832 case LFUN_UPCASE_WORD:
833 changeCase(cur, LyXText::text_uppercase);
836 case LFUN_LOWCASE_WORD:
837 changeCase(cur, LyXText::text_lowercase);
840 case LFUN_CAPITALIZE_WORD:
841 changeCase(cur, LyXText::text_capitalization);
844 case LFUN_TRANSPOSE_CHARS:
850 cur.message(_("Paste"));
851 lyx::cap::replaceSelection(cur);
853 #warning FIXME Check if the arg is in the domain of available selections.
855 if (isStrUnsignedInt(cmd.argument))
856 pasteSelection(cur, strToUnsignedInt(cmd.argument));
858 pasteSelection(cur, 0);
859 cur.clearSelection(); // bug 393
865 cutSelection(cur, true, true);
866 cur.message(_("Cut"));
871 cur.message(_("Copy"));
875 cur.message(tostr(cursorX(cur.top())) + ' '
876 + tostr(cursorY(cur.top())));
882 istringstream is(cmd.argument);
885 lyxerr << "SETXY: Could not parse coordinates in '"
886 << cmd.argument << std::endl;
888 setCursorFromCoordinates(cur, x, y);
893 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
895 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
902 cur.message(cur.paragraph().layout()->name());
906 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
907 << cmd.argument << endl;
909 // This is not the good solution to the empty argument
910 // problem, but it will hopefully suffice for 1.2.0.
911 // The correct solution would be to augument the
912 // function list/array with information about what
913 // functions needs arguments and their type.
914 if (cmd.argument.empty()) {
915 cur.errorMessage(_("LyX function 'layout' needs an argument."));
919 // Derive layout number from given argument (string)
920 // and current buffer's textclass (number)
921 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
922 bool hasLayout = tclass.hasLayout(cmd.argument);
923 string layout = cmd.argument;
925 // If the entry is obsolete, use the new one instead.
927 string const & obs = tclass[layout]->obsoleted_by();
933 cur.errorMessage(string(N_("Layout ")) + cmd.argument +
938 bool change_layout = (current_layout != layout);
940 if (!change_layout && cur.selection() &&
941 cur.selBegin().par() != cur.selEnd().par())
943 par_type spit = cur.selBegin().par();
944 par_type epit = cur.selEnd().par() + 1;
945 while (spit != epit) {
946 if (pars_[spit].layout()->name() != current_layout) {
947 change_layout = true;
955 current_layout = layout;
956 setLayout(cur, layout);
957 bv->owner()->setLayout(layout);
963 case LFUN_PASTESELECTION: {
964 cur.clearSelection();
965 string const clip = bv->getClipboard();
967 if (cmd.argument == "paragraph")
968 insertStringAsParagraphs(cur, clip);
970 insertStringAsLines(cur, clip);
976 gotoInset(cur, InsetBase::ERROR_CODE, false);
980 gotoInset(cur, InsetBase::NOTE_CODE, false);
983 case LFUN_REFERENCE_GOTO: {
984 vector<InsetOld_code> tmp;
985 tmp.push_back(InsetBase::LABEL_CODE);
986 tmp.push_back(InsetBase::REF_CODE);
987 gotoInset(cur, tmp, true);
992 lyx::cap::replaceSelection(cur);
993 Paragraph & par = cur.paragraph();
994 lyx::pos_type pos = cur.pos();
998 else if (cur.prevInset() && cur.prevInset()->isSpace())
1001 c = par.getChar(pos - 1);
1003 LyXLayout_ptr const & style = par.layout();
1005 BufferParams const & bufparams = bv->buffer()->params();
1006 if (!style->pass_thru
1007 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1008 string arg = cmd.argument;
1009 if (arg == "single")
1010 cur.insert(new InsetQuotes(c,
1011 bufparams.quotes_language,
1012 InsetQuotes::SingleQ));
1013 else if (arg == "double")
1014 cur.insert(new InsetQuotes(c,
1015 bufparams.quotes_language,
1016 InsetQuotes::DoubleQ));
1018 cur.insert(new InsetQuotes(c, bufparams));
1022 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1026 case LFUN_DATE_INSERT: {
1027 lyx::cap::replaceSelection(cur);
1028 time_t now_time_t = time(NULL);
1029 struct tm * now_tm = localtime(&now_time_t);
1030 setlocale(LC_TIME, "");
1032 if (!cmd.argument.empty())
1035 arg = lyxrc.date_insert_format;
1037 int const datetmp_len =
1038 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1040 for (int i = 0; i < datetmp_len; i++)
1041 insertChar(cur, datetmp[i]);
1044 moveCursor(cur, false);
1048 case LFUN_MOUSE_TRIPLE:
1049 if (cmd.button() == mouse_button::button1) {
1050 selection_possible = true;
1055 bv->haveSelection(cur.selection());
1059 case LFUN_MOUSE_DOUBLE:
1060 if (cmd.button() == mouse_button::button1) {
1061 selection_possible = true;
1062 selectWord(cur, lyx::WHOLE_WORD_STRICT);
1063 bv->haveSelection(cur.selection());
1067 // Single-click on work area
1068 case LFUN_MOUSE_PRESS: {
1069 // Right click on a footnote flag opens float menu
1070 if (cmd.button() == mouse_button::button3) {
1071 cur.clearSelection();
1072 selection_possible = false;
1076 // Middle button press pastes if we have a selection
1077 // We do this here as if the selection was inside an inset
1078 // it could get cleared on the unlocking of the inset so
1079 // we have to check this first
1080 bool paste_internally = false;
1081 if (cmd.button() == mouse_button::button2 && cur.selection()) {
1082 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1083 paste_internally = true;
1086 selection_possible = true;
1088 // Clear the selection
1089 cur.clearSelection();
1091 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1094 cur.x_target() = cursorX(cur.top());
1096 // Has the cursor just left the inset?
1097 if (bv->cursor().inMathed() && !cur.inMathed())
1098 bv->cursor().inset().notifyCursorLeaves(bv->cursor());
1103 // Don't allow selection after a big jump.
1104 if (bv->fitCursor())
1105 selection_possible = false;
1107 // Insert primary selection with middle mouse
1108 // if there is a local selection in the current buffer,
1110 if (cmd.button() == mouse_button::button2) {
1111 if (paste_internally)
1112 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1114 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1115 selection_possible = false;
1121 case LFUN_MOUSE_MOTION: {
1122 // Only use motion with button 1
1123 //if (cmd.button() != mouse_button::button1)
1125 // We want to use only motion events for which
1126 // the button press event was on the drawing area too.
1127 if (!selection_possible) {
1128 lyxerr[Debug::ACTION] << "BufferView::Pimpl::"
1129 "dispatch: no selection possible\n";
1130 lyxerr << "BufferView::Pimpl::dispatch: no selection possible\n";
1134 // ignore motions deeper nested than the real anchor
1135 LCursor & bvcur = cur.bv().cursor();
1136 if (bvcur.anchor_.hasPart(cur)) {
1137 CursorSlice old = cur.top();
1138 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1140 // This is to allow jumping over large insets
1141 // FIXME: shouldn't be top-text-specific
1142 if (isMainText() && cur.top() == old) {
1143 if (cmd.y - bv->top_y() >= bv->workHeight())
1145 else if (cmd.y - bv->top_y() < 0)
1149 // don't set anchor_
1150 bv->cursor().setCursor(cur);
1151 bv->cursor().selection() = true;
1152 lyxerr << "MOTION: " << bv->cursor() << endl;
1157 case LFUN_MOUSE_RELEASE: {
1158 selection_possible = false;
1160 if (cmd.button() == mouse_button::button2)
1164 if (cmd.button() == mouse_button::button1)
1165 bv->haveSelection(cur.selection());
1168 bv->owner()->updateMenubar();
1169 bv->owner()->updateToolbars();
1173 case LFUN_SELFINSERT: {
1174 if (cmd.argument.empty())
1177 // Automatically delete the currently selected
1178 // text and replace it with what is being
1179 // typed in now. Depends on lyxrc settings
1180 // "auto_region_delete", which defaults to
1183 if (lyxrc.auto_region_delete) {
1184 if (cur.selection())
1185 cutSelection(cur, false, false);
1186 bv->haveSelection(false);
1189 cur.clearSelection();
1190 LyXFont const old_font = real_current_font;
1192 string::const_iterator cit = cmd.argument.begin();
1193 string::const_iterator end = cmd.argument.end();
1194 for (; cit != end; ++cit)
1195 bv->owner()->getIntl().getTransManager().
1196 TranslateAndInsert(*cit, this);
1199 moveCursor(cur, false);
1201 // real_current_font.number can change so we need to
1202 // update the minibuffer
1203 if (old_font != real_current_font)
1204 bv->updateScrollbar();
1209 InsetCommandParams p("url");
1210 string const data = InsetCommandMailer::params2string("url", p);
1211 bv->owner()->getDialogs().show("url", data, 0);
1215 case LFUN_HTMLURL: {
1216 InsetCommandParams p("htmlurl");
1217 string const data = InsetCommandMailer::params2string("url", p);
1218 bv->owner()->getDialogs().show("url", data, 0);
1222 case LFUN_INSERT_LABEL: {
1223 // Try to generate a valid label
1224 string const contents = cmd.argument.empty() ?
1225 cur.getPossibleLabel() : cmd.argument;
1227 InsetCommandParams p("label", contents);
1228 string const data = InsetCommandMailer::params2string("label", p);
1230 if (cmd.argument.empty()) {
1231 bv->owner()->getDialogs().show("label", data, 0);
1233 FuncRequest fr(LFUN_INSET_INSERT, data);
1241 case LFUN_INSET_LIST:
1242 case LFUN_INSET_THEOREM:
1243 case LFUN_INSET_CAPTION:
1245 case LFUN_INSERT_NOTE:
1246 case LFUN_INSERT_CHARSTYLE:
1247 case LFUN_INSERT_BOX:
1248 case LFUN_INSERT_BRANCH:
1249 case LFUN_INSERT_BIBITEM:
1250 case LFUN_INSET_ERT:
1251 case LFUN_INSET_FLOAT:
1252 case LFUN_INSET_FOOTNOTE:
1253 case LFUN_INSET_MARGINAL:
1254 case LFUN_INSET_OPTARG:
1255 case LFUN_INSET_WIDE_FLOAT:
1256 case LFUN_INSET_WRAP:
1257 case LFUN_TABULAR_INSERT:
1258 case LFUN_ENVIRONMENT_INSERT:
1259 // Open the inset, and move the current selection
1261 doInsertInset(cur, this, cmd, true, true);
1265 case LFUN_INDEX_INSERT:
1266 // Just open the inset
1267 doInsertInset(cur, this, cmd, true, false);
1271 case LFUN_INDEX_PRINT:
1272 case LFUN_TOC_INSERT:
1274 case LFUN_INSERT_LINE:
1275 case LFUN_INSERT_PAGEBREAK:
1277 doInsertInset(cur, this, cmd, false, false);
1281 case LFUN_DEPTH_MIN:
1282 changeDepth(cur, DEC_DEPTH);
1285 case LFUN_DEPTH_PLUS:
1286 changeDepth(cur, INC_DEPTH);
1289 case LFUN_MATH_DISPLAY:
1290 mathDispatch(cur, cmd, true);
1293 case LFUN_MATH_IMPORT_SELECTION:
1294 case LFUN_MATH_MODE:
1295 mathDispatch(cur, cmd, false);
1298 case LFUN_MATH_MACRO:
1299 if (cmd.argument.empty())
1300 cur.errorMessage(N_("Missing argument"));
1302 string s = cmd.argument;
1303 string const s1 = token(s, ' ', 1);
1304 int const nargs = s1.empty() ? 0 : atoi(s1);
1305 string const s2 = token(s, ' ', 2);
1306 string const type = s2.empty() ? "newcommand" : s2;
1307 cur.insert(new MathMacroTemplate(token(s, ' ', 0), nargs, s2));
1308 //cur.nextInset()->edit(cur, true);
1312 case LFUN_INSERT_MATH:
1313 case LFUN_INSERT_MATRIX:
1314 case LFUN_MATH_DELIM: {
1315 cur.insert(new MathHullInset);
1316 cur.dispatch(FuncRequest(LFUN_RIGHT));
1317 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
1323 LyXFont font(LyXFont::ALL_IGNORE);
1324 font.setEmph(LyXFont::TOGGLE);
1325 toggleAndShow(cur, this, font);
1330 LyXFont font(LyXFont::ALL_IGNORE);
1331 font.setSeries(LyXFont::BOLD_SERIES);
1332 toggleAndShow(cur, this, font);
1337 LyXFont font(LyXFont::ALL_IGNORE);
1338 font.setNoun(LyXFont::TOGGLE);
1339 toggleAndShow(cur, this, font);
1344 LyXFont font(LyXFont::ALL_IGNORE);
1345 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
1346 toggleAndShow(cur, this, font);
1351 LyXFont font(LyXFont::ALL_IGNORE);
1352 font.setFamily(LyXFont::SANS_FAMILY);
1353 toggleAndShow(cur, this, font);
1358 LyXFont font(LyXFont::ALL_IGNORE);
1359 font.setFamily(LyXFont::ROMAN_FAMILY);
1360 toggleAndShow(cur, this, font);
1364 case LFUN_DEFAULT: {
1365 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
1366 toggleAndShow(cur, this, font);
1370 case LFUN_UNDERLINE: {
1371 LyXFont font(LyXFont::ALL_IGNORE);
1372 font.setUnderbar(LyXFont::TOGGLE);
1373 toggleAndShow(cur, this, font);
1377 case LFUN_FONT_SIZE: {
1378 LyXFont font(LyXFont::ALL_IGNORE);
1379 font.setLyXSize(cmd.argument);
1380 toggleAndShow(cur, this, font);
1384 case LFUN_LANGUAGE: {
1385 Language const * lang = languages.getLanguage(cmd.argument);
1388 LyXFont font(LyXFont::ALL_IGNORE);
1389 font.setLanguage(lang);
1390 toggleAndShow(cur, this, font);
1395 case LFUN_FREEFONT_APPLY:
1396 toggleAndShow(cur, this, freefont, toggleall);
1397 cur.message(_("Character set"));
1400 // Set the freefont using the contents of \param data dispatched from
1401 // the frontends and apply it at the current cursor location.
1402 case LFUN_FREEFONT_UPDATE: {
1405 if (bv_funcs::string2font(cmd.argument, font, toggle)) {
1408 toggleAndShow(cur, this, freefont, toggleall);
1409 cur.message(_("Character set"));
1414 case LFUN_FINISHED_LEFT:
1415 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1418 case LFUN_FINISHED_RIGHT:
1419 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1423 case LFUN_FINISHED_UP:
1424 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_UP:\n" << cur << endl;
1428 case LFUN_FINISHED_DOWN:
1429 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_DOWN:\n" << cur << endl;
1433 case LFUN_LAYOUT_PARAGRAPH: {
1435 params2string(cur.paragraph(), data);
1436 data = "show\n" + data;
1437 bv->owner()->getDialogs().show("paragraph", data);
1441 case LFUN_PARAGRAPH_UPDATE: {
1442 if (!bv->owner()->getDialogs().visible("paragraph"))
1445 params2string(cur.paragraph(), data);
1447 // Will the paragraph accept changes from the dialog?
1448 InsetBase & inset = cur.inset();
1449 bool const accept = !inset.forceDefaultParagraphs(&inset);
1451 data = "update " + tostr(accept) + '\n' + data;
1452 bv->owner()->getDialogs().update("paragraph", data);
1457 case LFUN_CIRCUMFLEX:
1467 case LFUN_SPECIAL_CARON:
1470 case LFUN_HUNG_UMLAUT:
1473 bv->owner()->getLyXFunc().handleKeyFunc(cmd.action);
1474 if (!cmd.argument.empty())
1475 bv->owner()->getIntl().getTransManager()
1476 .TranslateAndInsert(cmd.argument[0], this);
1479 case LFUN_FLOAT_LIST: {
1480 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1481 if (tclass.floats().typeExist(cmd.argument)) {
1482 // not quite sure if we want this...
1484 cur.clearSelection();
1485 breakParagraph(cur);
1487 if (cur.lastpos() != 0) {
1489 breakParagraph(cur);
1492 setLayout(cur, tclass.defaultLayoutName());
1493 setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1494 insertInset(cur, new InsetFloatList(cmd.argument));
1497 lyxerr << "Non-existent float type: "
1498 << cmd.argument << endl;
1503 case LFUN_ACCEPT_CHANGE: {
1508 case LFUN_REJECT_CHANGE: {
1513 case LFUN_THESAURUS_ENTRY: {
1514 string arg = cmd.argument;
1516 arg = cur.selectionAsString(false);
1518 if (arg.size() > 100 || arg.empty()) {
1519 // Get word or selection
1520 selectWordWhenUnderCursor(cur, lyx::WHOLE_WORD);
1521 arg = cur.selectionAsString(false);
1524 bv->owner()->getDialogs().show("thesaurus", arg);
1528 case LFUN_PARAGRAPH_APPLY: {
1529 // Given data, an encoding of the ParagraphParameters
1530 // generated in the Paragraph dialog, this function sets
1531 // the current paragraph appropriately.
1532 istringstream is(cmd.argument);
1535 ParagraphParameters params;
1540 params.labelWidthString(),
1542 cur.message(_("Paragraph layout set"));
1546 case LFUN_INSET_DIALOG_SHOW: {
1547 InsetBase * inset = cur.nextInset();
1549 FuncRequest fr(LFUN_INSET_DIALOG_SHOW);
1550 inset->dispatch(cur, fr);
1556 if (cur.selection()) {
1557 cur.selection() = false;
1560 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1569 // avoid to update when navigating
1571 && &sl.inset() == &cur.inset()
1572 && sl.idx() == cur.idx()
1574 && cur.selection() == false)
1579 bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
1580 FuncStatus & flag) const
1582 BOOST_ASSERT(cur.text() == this);
1583 LyXFont const & font = real_current_font;
1586 switch (cmd.action) {
1588 case LFUN_DEPTH_MIN:
1589 enable = changeDepthAllowed(cur, DEC_DEPTH);
1592 case LFUN_DEPTH_PLUS:
1593 enable = changeDepthAllowed(cur, INC_DEPTH);
1596 case LFUN_INSET_OPTARG:
1597 enable = numberOfOptArgs(cur.paragraph())
1598 < cur.paragraph().layout()->optionalargs;
1602 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1606 // the functions which insert insets
1607 InsetOld::Code code = InsetOld::NO_CODE;
1608 switch (cmd.action) {
1609 case LFUN_DIALOG_SHOW_NEW_INSET:
1610 if (cmd.argument == "bibitem")
1611 code = InsetOld::BIBITEM_CODE;
1612 else if (cmd.argument == "bibtex")
1613 code = InsetOld::BIBTEX_CODE;
1614 else if (cmd.argument == "box")
1615 code = InsetOld::BOX_CODE;
1616 else if (cmd.argument == "branch")
1617 code = InsetOld::BRANCH_CODE;
1618 else if (cmd.argument == "citation")
1619 code = InsetOld::CITE_CODE;
1620 else if (cmd.argument == "ert")
1621 code = InsetOld::ERT_CODE;
1622 else if (cmd.argument == "external")
1623 code = InsetOld::EXTERNAL_CODE;
1624 else if (cmd.argument == "float")
1625 code = InsetOld::FLOAT_CODE;
1626 else if (cmd.argument == "graphics")
1627 code = InsetOld::GRAPHICS_CODE;
1628 else if (cmd.argument == "include")
1629 code = InsetOld::INCLUDE_CODE;
1630 else if (cmd.argument == "index")
1631 code = InsetOld::INDEX_CODE;
1632 else if (cmd.argument == "label")
1633 code = InsetOld::LABEL_CODE;
1634 else if (cmd.argument == "note")
1635 code = InsetOld::NOTE_CODE;
1636 else if (cmd.argument == "ref")
1637 code = InsetOld::REF_CODE;
1638 else if (cmd.argument == "toc")
1639 code = InsetOld::TOC_CODE;
1640 else if (cmd.argument == "url")
1641 code = InsetOld::URL_CODE;
1642 else if (cmd.argument == "vspace")
1643 code = InsetOld::VSPACE_CODE;
1644 else if (cmd.argument == "wrap")
1645 code = InsetOld::WRAP_CODE;
1648 case LFUN_INSET_ERT:
1649 code = InsetOld::ERT_CODE;
1651 case LFUN_INSET_FOOTNOTE:
1652 code = InsetOld::FOOT_CODE;
1654 case LFUN_TABULAR_INSERT:
1655 code = InsetOld::TABULAR_CODE;
1657 case LFUN_INSET_MARGINAL:
1658 code = InsetOld::MARGIN_CODE;
1660 case LFUN_INSET_FLOAT:
1661 case LFUN_INSET_WIDE_FLOAT:
1662 code = InsetOld::FLOAT_CODE;
1664 case LFUN_INSET_WRAP:
1665 code = InsetOld::WRAP_CODE;
1667 case LFUN_FLOAT_LIST:
1668 code = InsetOld::FLOAT_LIST_CODE;
1671 case LFUN_INSET_LIST:
1672 code = InsetOld::LIST_CODE;
1674 case LFUN_INSET_THEOREM:
1675 code = InsetOld::THEOREM_CODE;
1678 case LFUN_INSET_CAPTION:
1679 code = InsetOld::CAPTION_CODE;
1681 case LFUN_INSERT_NOTE:
1682 code = InsetOld::NOTE_CODE;
1684 case LFUN_INSERT_CHARSTYLE:
1685 code = InsetOld::CHARSTYLE_CODE;
1686 if (buf->params().getLyXTextClass().charstyles().empty())
1689 case LFUN_INSERT_BOX:
1690 code = InsetOld::BOX_CODE;
1692 case LFUN_INSERT_BRANCH:
1693 code = InsetOld::BRANCH_CODE;
1694 if (buf->params().branchlist().empty())
1697 case LFUN_INSERT_LABEL:
1698 code = InsetOld::LABEL_CODE;
1700 case LFUN_INSET_OPTARG:
1701 code = InsetOld::OPTARG_CODE;
1703 case LFUN_ENVIRONMENT_INSERT:
1704 code = InsetOld::BOX_CODE;
1706 case LFUN_INDEX_INSERT:
1707 code = InsetOld::INDEX_CODE;
1709 case LFUN_INDEX_PRINT:
1710 code = InsetOld::INDEX_PRINT_CODE;
1712 case LFUN_TOC_INSERT:
1713 code = InsetOld::TOC_CODE;
1717 code = InsetOld::URL_CODE;
1720 // always allow this, since we will inset a raw quote
1721 // if an inset is not allowed.
1723 case LFUN_HYPHENATION:
1724 case LFUN_LIGATURE_BREAK:
1726 case LFUN_MENU_SEPARATOR:
1728 case LFUN_END_OF_SENTENCE:
1729 code = InsetOld::SPECIALCHAR_CODE;
1731 case LFUN_SPACE_INSERT:
1732 // slight hack: we know this is allowed in math mode
1734 code = InsetOld::SPACE_CODE;
1736 case LFUN_INSET_DIALOG_SHOW: {
1737 InsetBase * inset = cur.nextInset();
1740 code = inset->lyxCode();
1741 if (!(code == InsetOld::INCLUDE_CODE
1742 || code == InsetOld::BIBTEX_CODE
1743 || code == InsetOld::FLOAT_LIST_CODE
1744 || code == InsetOld::TOC_CODE))
1753 if (code != InsetOld::NO_CODE
1754 && (cur.empty() || !cur.inset().insetAllowed(code)))
1759 case LFUN_DIALOG_SHOW_NEW_INSET:
1760 case LFUN_INSET_ERT:
1761 case LFUN_INSERT_BOX:
1762 case LFUN_INSERT_BRANCH:
1763 case LFUN_ENVIRONMENT_INSERT:
1764 case LFUN_INDEX_INSERT:
1765 case LFUN_INDEX_PRINT:
1766 case LFUN_TOC_INSERT:
1770 case LFUN_HYPHENATION:
1771 case LFUN_LIGATURE_BREAK:
1773 case LFUN_MENU_SEPARATOR:
1775 case LFUN_END_OF_SENTENCE:
1776 case LFUN_SPACE_INSERT:
1777 case LFUN_INSET_DIALOG_SHOW:
1781 flag.setOnOff(font.emph() == LyXFont::ON);
1785 flag.setOnOff(font.noun() == LyXFont::ON);
1789 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1793 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1797 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1801 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1804 case LFUN_DELETE_WORD_FORWARD:
1805 case LFUN_DELETE_WORD_BACKWARD:
1806 case LFUN_DELETE_LINE_FORWARD:
1807 case LFUN_WORDRIGHT:
1817 case LFUN_UP_PARAGRAPHSEL:
1818 case LFUN_DOWN_PARAGRAPHSEL:
1823 case LFUN_WORDRIGHTSEL:
1824 case LFUN_WORDLEFTSEL:
1826 case LFUN_UP_PARAGRAPH:
1827 case LFUN_DOWN_PARAGRAPH:
1832 case LFUN_BREAKLINE:
1834 case LFUN_DELETE_SKIP:
1835 case LFUN_BACKSPACE:
1836 case LFUN_BACKSPACE_SKIP:
1837 case LFUN_BREAKPARAGRAPH:
1838 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1839 case LFUN_BREAKPARAGRAPH_SKIP:
1840 case LFUN_PARAGRAPH_SPACING:
1841 case LFUN_INSET_APPLY:
1842 case LFUN_INSET_INSERT:
1843 case LFUN_NEXT_INSET_TOGGLE:
1844 case LFUN_UPCASE_WORD:
1845 case LFUN_LOWCASE_WORD:
1846 case LFUN_CAPITALIZE_WORD:
1847 case LFUN_TRANSPOSE_CHARS:
1854 case LFUN_GETLAYOUT:
1856 case LFUN_PASTESELECTION:
1857 case LFUN_GOTOERROR:
1859 case LFUN_REFERENCE_GOTO:
1860 case LFUN_DATE_INSERT:
1861 case LFUN_SELFINSERT:
1862 case LFUN_INSERT_LABEL:
1863 case LFUN_INSERT_NOTE:
1864 case LFUN_INSERT_CHARSTYLE:
1865 case LFUN_INSERT_BIBITEM:
1866 case LFUN_INSET_FLOAT:
1867 case LFUN_INSET_FOOTNOTE:
1868 case LFUN_INSET_MARGINAL:
1869 case LFUN_INSET_WIDE_FLOAT:
1870 case LFUN_INSET_WRAP:
1871 case LFUN_TABULAR_INSERT:
1872 case LFUN_INSERT_LINE:
1873 case LFUN_INSERT_PAGEBREAK:
1874 case LFUN_MATH_DISPLAY:
1875 case LFUN_MATH_IMPORT_SELECTION:
1876 case LFUN_MATH_MODE:
1877 case LFUN_MATH_MACRO:
1878 case LFUN_INSERT_MATH:
1879 case LFUN_INSERT_MATRIX:
1880 case LFUN_MATH_DELIM:
1882 case LFUN_UNDERLINE:
1883 case LFUN_FONT_SIZE:
1885 case LFUN_FREEFONT_APPLY:
1886 case LFUN_FREEFONT_UPDATE:
1887 case LFUN_LAYOUT_PARAGRAPH:
1888 case LFUN_PARAGRAPH_UPDATE:
1890 case LFUN_CIRCUMFLEX:
1900 case LFUN_SPECIAL_CARON:
1903 case LFUN_HUNG_UMLAUT:
1906 case LFUN_FLOAT_LIST:
1907 case LFUN_ACCEPT_CHANGE:
1908 case LFUN_REJECT_CHANGE:
1909 case LFUN_THESAURUS_ENTRY:
1910 case LFUN_PARAGRAPH_APPLY:
1912 case LFUN_KEYMAP_TOGGLE:
1913 // these are handled in our dispatch()
1918 case LFUN_BEGINNINGBUF:
1926 flag.enabled(enable);