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.pit(), 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
147 // unless you want end up with always changing
148 // to mathrm when opening an inlined inset --
149 // I really hate "LyXfunc overloading"...
151 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
152 cur.dispatch(FuncRequest(LFUN_INSERT_MATH, cmd.argument));
154 // create a macro if we see "\\newcommand"
155 // somewhere, and an ordinary formula
157 cutSelection(cur, true, true);
158 if (sel.find("\\newcommand") == string::npos
159 && sel.find("\\def") == string::npos)
161 cur.insert(new MathHullInset);
162 cur.dispatch(FuncRequest(LFUN_RIGHT));
163 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
164 cur.dispatch(FuncRequest(LFUN_INSERT_MATH, sel));
166 istringstream is(sel);
167 cur.insert(new MathMacroTemplate(is));
170 cur.message(N_("Math editor mode"));
179 string const freefont2string()
182 if (font2string(freefont, toggleall, data))
189 bool LyXText::gotoNextInset(LCursor & cur,
190 vector<InsetBase_code> const & codes, string const & contents)
192 BOOST_ASSERT(this == cur.text());
193 pit_type end = paragraphs().size();
194 pit_type pit = cur.pit();
195 pos_type pos = cur.pos();
199 if (pos + 1 < pars_[pit].size()) {
206 } while (pit != end &&
207 !(pars_[pit].isInset(pos) &&
208 (inset = pars_[pit].getInset(pos)) != 0 &&
209 find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
211 static_cast<InsetCommand *>(pars_[pit].getInset(pos))->getContents()
217 setCursor(cur, pit, pos, false);
222 void LyXText::gotoInset(LCursor & cur,
223 vector<InsetBase_code> const & codes, bool same_content)
225 cur.clearSelection();
229 && cur.pos() < cur.lastpos()
230 && cur.paragraph().isInset(cur.pos())) {
231 InsetBase const * inset = cur.paragraph().getInset(cur.pos());
232 if (find(codes.begin(), codes.end(), inset->lyxCode())
234 contents = static_cast<InsetCommand const *>(inset)->getContents();
237 if (!gotoNextInset(cur, codes, contents)) {
238 if (cur.pos() || cur.pit() != 0) {
239 CursorSlice tmp = cur.top();
242 if (!gotoNextInset(cur, codes, contents)) {
244 cur.message(_("No more insets"));
247 cur.message(_("No more insets"));
254 void LyXText::gotoInset(LCursor & cur, InsetBase_code code, bool same_content)
256 gotoInset(cur, vector<InsetBase_code>(1, code), same_content);
260 void LyXText::cursorPrevious(LCursor & cur)
262 pos_type cpos = cur.pos();
263 lyx::pit_type cpar = cur.pit();
265 int x = cur.x_target();
267 setCursorFromCoordinates(cur, x, 0);
270 if (cpar == cur.pit() && cpos == cur.pos()) {
271 // we have a row which is taller than the workarea. The
272 // simplest solution is to move to the previous row instead.
276 cur.bv().updateScrollbar();
281 void LyXText::cursorNext(LCursor & cur)
283 pos_type cpos = cur.pos();
284 lyx::pit_type cpar = cur.pit();
286 int x = cur.x_target();
287 setCursorFromCoordinates(cur, x, cur.bv().workHeight() - 1);
290 if (cpar == cur.pit() && cpos == cur.pos()) {
291 // we have a row which is taller than the workarea. The
292 // simplest solution is to move to the next row instead.
296 cur.bv().updateScrollbar();
303 void specialChar(LCursor & cur, InsetSpecialChar::Kind kind)
305 lyx::cap::replaceSelection(cur);
306 cur.insert(new InsetSpecialChar(kind));
311 void doInsertInset(LCursor & cur, LyXText * text,
312 FuncRequest const & cmd, bool edit, bool pastesel)
314 InsetBase * inset = createInset(&cur.bv(), cmd);
320 if (cur.selection()) {
321 cur.bv().owner()->dispatch(FuncRequest(LFUN_CUT));
324 text->insertInset(cur, inset);
327 inset->edit(cur, true);
329 if (gotsel && pastesel)
330 cur.bv().owner()->dispatch(FuncRequest(LFUN_PASTE));
334 void update(LCursor & cur)
336 //we don't call update(true, false) directly to save a metrics call
337 if (cur.bv().fitCursor())
338 cur.bv().update(false, true);
345 void LyXText::number(LCursor & cur)
347 LyXFont font(LyXFont::ALL_IGNORE);
348 font.setNumber(LyXFont::TOGGLE);
349 toggleAndShow(cur, this, font);
353 bool LyXText::isRTL(Paragraph const & par) const
355 return par.isRightToLeftPar(bv()->buffer()->params());
359 void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
361 lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
362 //lyxerr << "*** LyXText::dispatch: cmd: " << cmd << endl;
364 BOOST_ASSERT(cur.text() == this);
365 BufferView * bv = &cur.bv();
366 CursorSlice sl = cur.top();
367 bool sel = cur.selection();
370 switch (cmd.action) {
372 case LFUN_APPENDIX: {
373 Paragraph & par = cur.paragraph();
374 bool start = !par.params().startOfAppendix();
376 // ensure that we have only one start_of_appendix in this document
377 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
378 if (pars_[tmp].params().startOfAppendix()) {
380 pars_[tmp].params().startOfAppendix(false);
386 par.params().startOfAppendix(start);
388 // we can set the refreshing parameters now
393 case LFUN_DELETE_WORD_FORWARD:
394 cur.clearSelection();
395 deleteWordForward(cur);
396 finishChange(cur, false);
399 case LFUN_DELETE_WORD_BACKWARD:
400 cur.clearSelection();
401 deleteWordBackward(cur);
402 finishChange(cur, false);
405 case LFUN_DELETE_LINE_FORWARD:
406 cur.clearSelection();
407 deleteLineForward(cur);
408 finishChange(cur, false);
414 cur.clearSelection();
415 if (isRTL(cur.paragraph()))
416 cursorLeftOneWord(cur);
418 cursorRightOneWord(cur);
419 finishChange(cur, false);
425 cur.clearSelection();
426 if (isRTL(cur.paragraph()))
427 cursorRightOneWord(cur);
429 cursorLeftOneWord(cur);
430 finishChange(cur, false);
433 case LFUN_BEGINNINGBUF:
434 if (cur.size() == 1) {
436 cur.clearSelection();
438 finishChange(cur, false);
444 case LFUN_BEGINNINGBUFSEL:
445 if (cur.size() == 1) {
446 if (!cur.selection())
449 finishChange(cur, true);
456 if (cur.size() == 1) {
458 cur.clearSelection();
460 finishChange(cur, false);
467 if (cur.size() == 1) {
468 if (!cur.selection())
471 finishChange(cur, true);
480 //lyxerr << "handle LFUN_RIGHT[SEL]:\n" << cur << endl;
481 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
482 if (isRTL(cur.paragraph()))
486 if (sl == cur.top()) {
488 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
495 //lyxerr << "handle LFUN_LEFT[SEL]:\n" << cur << endl;
496 cur.selHandle(cmd.action == LFUN_LEFTSEL);
497 if (isRTL(cur.paragraph()))
501 if (sl == cur.top()) {
503 cmd = FuncRequest(LFUN_FINISHED_LEFT);
511 //lyxerr << "handle LFUN_UP[SEL]:\n" << cur << endl;
512 cur.selHandle(cmd.action == LFUN_UPSEL);
514 if (sl == cur.top()) {
516 cmd = FuncRequest(LFUN_FINISHED_UP);
524 //lyxerr << "handle LFUN_DOWN[SEL]:\n" << cur << endl;
525 cur.selHandle(cmd.action == LFUN_DOWNSEL);
527 if (sl == cur.top()) {
529 cmd = FuncRequest(LFUN_FINISHED_DOWN);
533 case LFUN_UP_PARAGRAPHSEL:
534 if (!cur.selection())
536 cursorUpParagraph(cur);
537 finishChange(cur, true);
540 case LFUN_DOWN_PARAGRAPHSEL:
541 if (!cur.selection())
543 cursorDownParagraph(cur);
544 finishChange(cur, true);
549 if (!cur.selection())
552 finishChange(cur, true);
557 if (!cur.selection())
560 finishChange(cur, true);
565 if (!cur.selection())
568 finishChange(cur, true);
573 if (!cur.selection())
576 finishChange(cur, true);
579 case LFUN_WORDRIGHTSEL:
580 if (!cur.selection())
582 if (isRTL(cur.paragraph()))
583 cursorLeftOneWord(cur);
585 cursorRightOneWord(cur);
586 finishChange(cur, true);
589 case LFUN_WORDLEFTSEL:
590 if (!cur.selection())
592 if (isRTL(cur.paragraph()))
593 cursorRightOneWord(cur);
595 cursorLeftOneWord(cur);
596 finishChange(cur, true);
600 selectWord(cur, lyx::WHOLE_WORD);
601 finishChange(cur, true);
605 case LFUN_UP_PARAGRAPH:
608 cur.clearSelection();
609 cursorUpParagraph(cur);
610 finishChange(cur, false);
613 case LFUN_DOWN_PARAGRAPH:
616 cur.clearSelection();
617 cursorDownParagraph(cur);
618 finishChange(cur, false);
625 cur.clearSelection();
626 finishChange(cur, false);
627 if (cur.pit() == 0 && cur.textRow().pos() == 0) {
629 cmd = FuncRequest(LFUN_FINISHED_UP);
639 cur.clearSelection();
640 finishChange(cur, false);
641 if (cur.pit() == cur.lastpit()
642 && cur.textRow().endpos() == cur.lastpos()) {
644 cmd = FuncRequest(LFUN_FINISHED_DOWN);
652 cur.clearSelection();
654 finishChange(cur, false);
659 cur.clearSelection();
661 finishChange(cur, false);
664 case LFUN_BREAKLINE: {
665 // Not allowed by LaTeX (labels or empty par)
666 if (cur.pos() > cur.paragraph().beginOfBody()) {
667 lyx::cap::replaceSelection(cur);
668 cur.insert(new InsetNewline);
670 moveCursor(cur, false);
676 if (!cur.selection()) {
679 // It is possible to make it a lot faster still
680 // just comment out the line below...
682 cutSelection(cur, true, false);
684 moveCursor(cur, false);
687 case LFUN_DELETE_SKIP:
688 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
689 if (!cur.selection()) {
690 if (cur.pos() == cur.lastpos()) {
697 cutSelection(cur, true, false);
703 if (!cur.selection()) {
704 if (bv->owner()->getIntl().getTransManager().backspace()) {
707 // It is possible to make it a lot faster still
708 // just comment out the line below...
711 cutSelection(cur, true, false);
716 case LFUN_BACKSPACE_SKIP:
717 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
718 if (!cur.selection()) {
722 //CursorSlice cur = cursor();
726 cutSelection(cur, true, false);
730 case LFUN_BREAKPARAGRAPH:
731 lyx::cap::replaceSelection(cur);
732 breakParagraph(cur, 0);
737 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
738 lyx::cap::replaceSelection(cur);
739 breakParagraph(cur, 1);
744 case LFUN_BREAKPARAGRAPH_SKIP: {
745 // When at the beginning of a paragraph, remove
746 // indentation. Otherwise, do the same as LFUN_BREAKPARAGRAPH.
747 lyx::cap::replaceSelection(cur);
749 cur.paragraph().params().labelWidthString(string());
751 breakParagraph(cur, 0);
757 case LFUN_PARAGRAPH_SPACING: {
758 Paragraph & par = cur.paragraph();
759 Spacing::Space cur_spacing = par.params().spacing().getSpace();
760 float cur_value = 1.0;
761 if (cur_spacing == Spacing::Other)
762 cur_value = par.params().spacing().getValue();
764 istringstream is(cmd.argument);
767 Spacing::Space new_spacing = cur_spacing;
768 float new_value = cur_value;
770 lyxerr << "Missing argument to `paragraph-spacing'"
772 } else if (tmp == "single") {
773 new_spacing = Spacing::Single;
774 } else if (tmp == "onehalf") {
775 new_spacing = Spacing::Onehalf;
776 } else if (tmp == "double") {
777 new_spacing = Spacing::Double;
778 } else if (tmp == "other") {
779 new_spacing = Spacing::Other;
782 lyxerr << "new_value = " << tmpval << endl;
785 } else if (tmp == "default") {
786 new_spacing = Spacing::Default;
788 lyxerr << _("Unknown spacing argument: ")
789 << cmd.argument << endl;
791 if (cur_spacing != new_spacing || cur_value != new_value)
792 par.params().spacing(Spacing(new_spacing, new_value));
796 case LFUN_INSET_APPLY: {
797 string const name = cmd.getArg(0);
798 InsetBase * inset = bv->owner()->getDialogs().getOpenInset(name);
800 FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument);
801 inset->dispatch(cur, fr);
803 FuncRequest fr(LFUN_INSET_INSERT, cmd.argument);
809 case LFUN_INSET_INSERT: {
811 InsetBase * inset = createInset(bv, cmd);
813 insertInset(cur, inset);
819 case LFUN_INSET_SETTINGS:
820 if (cur.inset().asUpdatableInset())
821 cur.inset().asUpdatableInset()->showInsetDialog(bv);
824 case LFUN_NEXT_INSET_TOGGLE: {
825 InsetBase * inset = cur.nextInset();
827 cur.clearSelection();
828 FuncRequest fr = cmd;
829 fr.action = LFUN_INSET_TOGGLE;
830 inset->dispatch(cur, fr);
835 case LFUN_KEYMAP_TOGGLE:
836 cur.clearSelection();
840 case LFUN_SPACE_INSERT:
841 if (cur.paragraph().layout()->free_spacing)
842 insertChar(cur, ' ');
844 doInsertInset(cur, this, cmd, false, false);
847 moveCursor(cur, false);
850 case LFUN_HYPHENATION:
851 specialChar(cur, InsetSpecialChar::HYPHENATION);
854 case LFUN_LIGATURE_BREAK:
855 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
859 specialChar(cur, InsetSpecialChar::LDOTS);
862 case LFUN_END_OF_SENTENCE:
863 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
866 case LFUN_MENU_SEPARATOR:
867 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
870 case LFUN_UPCASE_WORD:
871 changeCase(cur, LyXText::text_uppercase);
874 case LFUN_LOWCASE_WORD:
875 changeCase(cur, LyXText::text_lowercase);
878 case LFUN_CAPITALIZE_WORD:
879 changeCase(cur, LyXText::text_capitalization);
882 case LFUN_TRANSPOSE_CHARS:
887 cur.message(_("Paste"));
888 lyx::cap::replaceSelection(cur);
890 #warning FIXME Check if the arg is in the domain of available selections.
892 if (isStrUnsignedInt(cmd.argument))
893 pasteSelection(cur, strToUnsignedInt(cmd.argument));
895 pasteSelection(cur, 0);
896 cur.clearSelection(); // bug 393
902 cutSelection(cur, true, true);
903 cur.message(_("Cut"));
908 cur.message(_("Copy"));
912 cur.message(tostr(cursorX(cur.top())) + ' '
913 + tostr(cursorY(cur.top())));
919 istringstream is(cmd.argument);
922 lyxerr << "SETXY: Could not parse coordinates in '"
923 << cmd.argument << std::endl;
925 setCursorFromCoordinates(cur, x, y);
930 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
932 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
939 cur.message(cur.paragraph().layout()->name());
943 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
944 << cmd.argument << endl;
946 // This is not the good solution to the empty argument
947 // problem, but it will hopefully suffice for 1.2.0.
948 // The correct solution would be to augument the
949 // function list/array with information about what
950 // functions needs arguments and their type.
951 if (cmd.argument.empty()) {
952 cur.errorMessage(_("LyX function 'layout' needs an argument."));
956 // Derive layout number from given argument (string)
957 // and current buffer's textclass (number)
958 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
959 bool hasLayout = tclass.hasLayout(cmd.argument);
960 string layout = cmd.argument;
962 // If the entry is obsolete, use the new one instead.
964 string const & obs = tclass[layout]->obsoleted_by();
970 cur.errorMessage(string(N_("Layout ")) + cmd.argument +
975 bool change_layout = (current_layout != layout);
977 if (!change_layout && cur.selection() &&
978 cur.selBegin().pit() != cur.selEnd().pit())
980 pit_type spit = cur.selBegin().pit();
981 pit_type epit = cur.selEnd().pit() + 1;
982 while (spit != epit) {
983 if (pars_[spit].layout()->name() != current_layout) {
984 change_layout = true;
992 current_layout = layout;
993 setLayout(cur, layout);
994 bv->owner()->setLayout(layout);
1000 case LFUN_PASTESELECTION: {
1001 cur.clearSelection();
1002 string const clip = bv->getClipboard();
1003 if (!clip.empty()) {
1004 if (cmd.argument == "paragraph")
1005 insertStringAsParagraphs(cur, clip);
1007 insertStringAsLines(cur, clip);
1012 case LFUN_GOTOERROR:
1013 gotoInset(cur, InsetBase::ERROR_CODE, false);
1017 gotoInset(cur, InsetBase::NOTE_CODE, false);
1020 case LFUN_REFERENCE_GOTO: {
1021 vector<InsetBase_code> tmp;
1022 tmp.push_back(InsetBase::LABEL_CODE);
1023 tmp.push_back(InsetBase::REF_CODE);
1024 gotoInset(cur, tmp, true);
1029 lyx::cap::replaceSelection(cur);
1030 Paragraph & par = cur.paragraph();
1031 lyx::pos_type pos = cur.pos();
1035 else if (cur.prevInset() && cur.prevInset()->isSpace())
1038 c = par.getChar(pos - 1);
1040 LyXLayout_ptr const & style = par.layout();
1042 BufferParams const & bufparams = bv->buffer()->params();
1043 if (!style->pass_thru
1044 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1045 string arg = cmd.argument;
1046 if (arg == "single")
1047 cur.insert(new InsetQuotes(c,
1048 bufparams.quotes_language,
1049 InsetQuotes::SingleQ));
1050 else if (arg == "double")
1051 cur.insert(new InsetQuotes(c,
1052 bufparams.quotes_language,
1053 InsetQuotes::DoubleQ));
1055 cur.insert(new InsetQuotes(c, bufparams));
1059 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1063 case LFUN_DATE_INSERT: {
1064 lyx::cap::replaceSelection(cur);
1065 time_t now_time_t = time(NULL);
1066 struct tm * now_tm = localtime(&now_time_t);
1067 setlocale(LC_TIME, "");
1069 if (!cmd.argument.empty())
1072 arg = lyxrc.date_insert_format;
1074 int const datetmp_len =
1075 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1077 for (int i = 0; i < datetmp_len; i++)
1078 insertChar(cur, datetmp[i]);
1081 moveCursor(cur, false);
1085 case LFUN_MOUSE_TRIPLE:
1086 if (cmd.button() == mouse_button::button1) {
1087 selection_possible = true;
1092 bv->haveSelection(cur.selection());
1096 case LFUN_MOUSE_DOUBLE:
1097 if (cmd.button() == mouse_button::button1) {
1098 selection_possible = true;
1099 selectWord(cur, lyx::WHOLE_WORD_STRICT);
1100 bv->haveSelection(cur.selection());
1104 // Single-click on work area
1105 case LFUN_MOUSE_PRESS: {
1106 // Right click on a footnote flag opens float menu
1107 if (cmd.button() == mouse_button::button3) {
1108 cur.clearSelection();
1109 selection_possible = false;
1113 // Middle button press pastes if we have a selection
1114 // We do this here as if the selection was inside an inset
1115 // it could get cleared on the unlocking of the inset so
1116 // we have to check this first
1117 bool paste_internally = false;
1118 if (cmd.button() == mouse_button::button2 && cur.selection()) {
1119 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1120 paste_internally = true;
1123 selection_possible = true;
1125 // Clear the selection
1126 cur.clearSelection();
1128 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1133 // Has the cursor just left the inset?
1134 if (bv->cursor().inMathed() && !cur.inMathed())
1135 bv->cursor().inset().notifyCursorLeaves(bv->cursor());
1140 // Don't allow selection after a big jump.
1141 //if (bv->fitCursor())
1142 // selection_possible = false;
1144 // Insert primary selection with middle mouse
1145 // if there is a local selection in the current buffer,
1147 if (cmd.button() == mouse_button::button2) {
1148 if (paste_internally)
1149 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1151 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1152 selection_possible = false;
1158 case LFUN_MOUSE_MOTION: {
1159 // Only use motion with button 1
1160 //if (cmd.button() != mouse_button::button1)
1162 // We want to use only motion events for which
1163 // the button press event was on the drawing area too.
1164 if (!selection_possible) {
1165 lyxerr[Debug::ACTION] << "BufferView::Pimpl::"
1166 "dispatch: no selection possible\n";
1167 lyxerr << "BufferView::Pimpl::dispatch: no selection possible\n";
1171 // ignore motions deeper nested than the real anchor
1172 LCursor & bvcur = cur.bv().cursor();
1173 if (bvcur.anchor_.hasPart(cur)) {
1174 CursorSlice old = bvcur.top();
1176 int const wh = bv->workHeight();
1177 int const y = std::max(0, std::min(wh - 1, cmd.y));
1179 setCursorFromCoordinates(cur, cmd.x, y);
1180 cur.x_target() = cmd.x;
1185 // This is to allow jumping over large insets
1186 if (cur.top() == old) {
1193 if (cur.top() == old)
1196 // don't set anchor_
1197 bvcur.setCursor(cur);
1198 bvcur.selection() = true;
1199 lyxerr << "MOTION: " << bv->cursor() << endl;
1207 case LFUN_MOUSE_RELEASE: {
1208 selection_possible = false;
1210 if (cmd.button() == mouse_button::button2)
1214 if (cmd.button() == mouse_button::button1)
1215 bv->haveSelection(cur.selection());
1218 bv->owner()->updateMenubar();
1219 bv->owner()->updateToolbars();
1223 case LFUN_SELFINSERT: {
1224 if (cmd.argument.empty())
1227 // Automatically delete the currently selected
1228 // text and replace it with what is being
1229 // typed in now. Depends on lyxrc settings
1230 // "auto_region_delete", which defaults to
1233 if (lyxrc.auto_region_delete) {
1234 if (cur.selection())
1235 cutSelection(cur, false, false);
1236 bv->haveSelection(false);
1239 cur.clearSelection();
1240 LyXFont const old_font = real_current_font;
1242 string::const_iterator cit = cmd.argument.begin();
1243 string::const_iterator end = cmd.argument.end();
1244 for (; cit != end; ++cit)
1245 bv->owner()->getIntl().getTransManager().
1246 TranslateAndInsert(*cit, this);
1249 moveCursor(cur, false);
1251 // real_current_font.number can change so we need to
1252 // update the minibuffer
1253 if (old_font != real_current_font)
1254 bv->updateScrollbar();
1259 InsetCommandParams p("url");
1260 string const data = InsetCommandMailer::params2string("url", p);
1261 bv->owner()->getDialogs().show("url", data, 0);
1265 case LFUN_HTMLURL: {
1266 InsetCommandParams p("htmlurl");
1267 string const data = InsetCommandMailer::params2string("url", p);
1268 bv->owner()->getDialogs().show("url", data, 0);
1272 case LFUN_INSERT_LABEL: {
1273 // Try to generate a valid label
1274 string const contents = cmd.argument.empty() ?
1275 cur.getPossibleLabel() : cmd.argument;
1277 InsetCommandParams p("label", contents);
1278 string const data = InsetCommandMailer::params2string("label", p);
1280 if (cmd.argument.empty()) {
1281 bv->owner()->getDialogs().show("label", data, 0);
1283 FuncRequest fr(LFUN_INSET_INSERT, data);
1291 case LFUN_INSET_LIST:
1292 case LFUN_INSET_THEOREM:
1293 case LFUN_INSET_CAPTION:
1295 case LFUN_INSERT_NOTE:
1296 case LFUN_INSERT_CHARSTYLE:
1297 case LFUN_INSERT_BOX:
1298 case LFUN_INSERT_BRANCH:
1299 case LFUN_INSERT_BIBITEM:
1300 case LFUN_INSET_ERT:
1301 case LFUN_INSET_FLOAT:
1302 case LFUN_INSET_FOOTNOTE:
1303 case LFUN_INSET_MARGINAL:
1304 case LFUN_INSET_OPTARG:
1305 case LFUN_INSET_WIDE_FLOAT:
1306 case LFUN_INSET_WRAP:
1307 case LFUN_TABULAR_INSERT:
1308 case LFUN_ENVIRONMENT_INSERT:
1309 // Open the inset, and move the current selection
1311 doInsertInset(cur, this, cmd, true, true);
1315 case LFUN_INDEX_INSERT:
1316 // Just open the inset
1317 doInsertInset(cur, this, cmd, true, false);
1321 case LFUN_INDEX_PRINT:
1322 case LFUN_TOC_INSERT:
1324 case LFUN_INSERT_LINE:
1325 case LFUN_INSERT_PAGEBREAK:
1327 doInsertInset(cur, this, cmd, false, false);
1331 case LFUN_DEPTH_MIN:
1332 changeDepth(cur, DEC_DEPTH);
1335 case LFUN_DEPTH_PLUS:
1336 changeDepth(cur, INC_DEPTH);
1339 case LFUN_MATH_DISPLAY:
1340 mathDispatch(cur, cmd, true);
1343 case LFUN_MATH_IMPORT_SELECTION:
1344 case LFUN_MATH_MODE:
1345 mathDispatch(cur, cmd, false);
1348 case LFUN_MATH_MACRO:
1349 if (cmd.argument.empty())
1350 cur.errorMessage(N_("Missing argument"));
1352 string s = cmd.argument;
1353 string const s1 = token(s, ' ', 1);
1354 int const nargs = s1.empty() ? 0 : atoi(s1);
1355 string const s2 = token(s, ' ', 2);
1356 string const type = s2.empty() ? "newcommand" : s2;
1357 cur.insert(new MathMacroTemplate(token(s, ' ', 0), nargs, s2));
1358 //cur.nextInset()->edit(cur, true);
1362 case LFUN_INSERT_MATH:
1363 case LFUN_INSERT_MATRIX:
1364 case LFUN_MATH_DELIM: {
1365 cur.insert(new MathHullInset);
1366 cur.dispatch(FuncRequest(LFUN_RIGHT));
1367 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
1373 LyXFont font(LyXFont::ALL_IGNORE);
1374 font.setEmph(LyXFont::TOGGLE);
1375 toggleAndShow(cur, this, font);
1380 LyXFont font(LyXFont::ALL_IGNORE);
1381 font.setSeries(LyXFont::BOLD_SERIES);
1382 toggleAndShow(cur, this, font);
1387 LyXFont font(LyXFont::ALL_IGNORE);
1388 font.setNoun(LyXFont::TOGGLE);
1389 toggleAndShow(cur, this, font);
1394 LyXFont font(LyXFont::ALL_IGNORE);
1395 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
1396 toggleAndShow(cur, this, font);
1401 LyXFont font(LyXFont::ALL_IGNORE);
1402 font.setFamily(LyXFont::SANS_FAMILY);
1403 toggleAndShow(cur, this, font);
1408 LyXFont font(LyXFont::ALL_IGNORE);
1409 font.setFamily(LyXFont::ROMAN_FAMILY);
1410 toggleAndShow(cur, this, font);
1414 case LFUN_DEFAULT: {
1415 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
1416 toggleAndShow(cur, this, font);
1420 case LFUN_UNDERLINE: {
1421 LyXFont font(LyXFont::ALL_IGNORE);
1422 font.setUnderbar(LyXFont::TOGGLE);
1423 toggleAndShow(cur, this, font);
1427 case LFUN_FONT_SIZE: {
1428 LyXFont font(LyXFont::ALL_IGNORE);
1429 font.setLyXSize(cmd.argument);
1430 toggleAndShow(cur, this, font);
1434 case LFUN_LANGUAGE: {
1435 Language const * lang = languages.getLanguage(cmd.argument);
1438 LyXFont font(LyXFont::ALL_IGNORE);
1439 font.setLanguage(lang);
1440 toggleAndShow(cur, this, font);
1445 case LFUN_FREEFONT_APPLY:
1446 toggleAndShow(cur, this, freefont, toggleall);
1447 cur.message(_("Character set"));
1450 // Set the freefont using the contents of \param data dispatched from
1451 // the frontends and apply it at the current cursor location.
1452 case LFUN_FREEFONT_UPDATE: {
1455 if (bv_funcs::string2font(cmd.argument, font, toggle)) {
1458 toggleAndShow(cur, this, freefont, toggleall);
1459 cur.message(_("Character set"));
1464 case LFUN_FINISHED_LEFT:
1465 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1468 case LFUN_FINISHED_RIGHT:
1469 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1473 case LFUN_FINISHED_UP:
1474 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_UP:\n" << cur << endl;
1478 case LFUN_FINISHED_DOWN:
1479 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_DOWN:\n" << cur << endl;
1483 case LFUN_LAYOUT_PARAGRAPH: {
1485 params2string(cur.paragraph(), data);
1486 data = "show\n" + data;
1487 bv->owner()->getDialogs().show("paragraph", data);
1491 case LFUN_PARAGRAPH_UPDATE: {
1492 if (!bv->owner()->getDialogs().visible("paragraph"))
1495 params2string(cur.paragraph(), data);
1497 // Will the paragraph accept changes from the dialog?
1498 InsetBase & inset = cur.inset();
1499 bool const accept = !inset.forceDefaultParagraphs(&inset);
1501 data = "update " + tostr(accept) + '\n' + data;
1502 bv->owner()->getDialogs().update("paragraph", data);
1507 case LFUN_CIRCUMFLEX:
1517 case LFUN_SPECIAL_CARON:
1520 case LFUN_HUNG_UMLAUT:
1523 bv->owner()->getLyXFunc().handleKeyFunc(cmd.action);
1524 if (!cmd.argument.empty())
1525 bv->owner()->getIntl().getTransManager()
1526 .TranslateAndInsert(cmd.argument[0], this);
1529 case LFUN_FLOAT_LIST: {
1530 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1531 if (tclass.floats().typeExist(cmd.argument)) {
1532 // not quite sure if we want this...
1534 cur.clearSelection();
1535 breakParagraph(cur);
1537 if (cur.lastpos() != 0) {
1539 breakParagraph(cur);
1542 setLayout(cur, tclass.defaultLayoutName());
1543 setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1544 insertInset(cur, new InsetFloatList(cmd.argument));
1547 lyxerr << "Non-existent float type: "
1548 << cmd.argument << endl;
1553 case LFUN_ACCEPT_CHANGE: {
1558 case LFUN_REJECT_CHANGE: {
1563 case LFUN_THESAURUS_ENTRY: {
1564 string arg = cmd.argument;
1566 arg = cur.selectionAsString(false);
1568 if (arg.size() > 100 || arg.empty()) {
1569 // Get word or selection
1570 selectWordWhenUnderCursor(cur, lyx::WHOLE_WORD);
1571 arg = cur.selectionAsString(false);
1574 bv->owner()->getDialogs().show("thesaurus", arg);
1578 case LFUN_PARAGRAPH_APPLY: {
1579 // Given data, an encoding of the ParagraphParameters
1580 // generated in the Paragraph dialog, this function sets
1581 // the current paragraph appropriately.
1582 istringstream is(cmd.argument);
1585 ParagraphParameters params;
1590 params.labelWidthString(),
1592 cur.message(_("Paragraph layout set"));
1596 case LFUN_INSET_DIALOG_SHOW: {
1597 InsetBase * inset = cur.nextInset();
1599 FuncRequest fr(LFUN_INSET_DIALOG_SHOW);
1600 inset->dispatch(cur, fr);
1606 if (cur.selection()) {
1607 cur.selection() = false;
1610 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1619 // avoid to update when navigating
1621 && &sl.inset() == &cur.inset()
1622 && sl.idx() == cur.idx()
1624 && cur.selection() == false)
1629 bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
1630 FuncStatus & flag) const
1632 BOOST_ASSERT(cur.text() == this);
1633 LyXFont const & font = real_current_font;
1636 switch (cmd.action) {
1638 case LFUN_DEPTH_MIN:
1639 enable = changeDepthAllowed(cur, DEC_DEPTH);
1642 case LFUN_DEPTH_PLUS:
1643 enable = changeDepthAllowed(cur, INC_DEPTH);
1646 case LFUN_INSET_OPTARG:
1647 enable = numberOfOptArgs(cur.paragraph())
1648 < cur.paragraph().layout()->optionalargs;
1652 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1656 // the functions which insert insets
1657 InsetBase::Code code = InsetBase::NO_CODE;
1658 switch (cmd.action) {
1659 case LFUN_DIALOG_SHOW_NEW_INSET:
1660 if (cmd.argument == "bibitem")
1661 code = InsetBase::BIBITEM_CODE;
1662 else if (cmd.argument == "bibtex")
1663 code = InsetBase::BIBTEX_CODE;
1664 else if (cmd.argument == "box")
1665 code = InsetBase::BOX_CODE;
1666 else if (cmd.argument == "branch")
1667 code = InsetBase::BRANCH_CODE;
1668 else if (cmd.argument == "citation")
1669 code = InsetBase::CITE_CODE;
1670 else if (cmd.argument == "ert")
1671 code = InsetBase::ERT_CODE;
1672 else if (cmd.argument == "external")
1673 code = InsetBase::EXTERNAL_CODE;
1674 else if (cmd.argument == "float")
1675 code = InsetBase::FLOAT_CODE;
1676 else if (cmd.argument == "graphics")
1677 code = InsetBase::GRAPHICS_CODE;
1678 else if (cmd.argument == "include")
1679 code = InsetBase::INCLUDE_CODE;
1680 else if (cmd.argument == "index")
1681 code = InsetBase::INDEX_CODE;
1682 else if (cmd.argument == "label")
1683 code = InsetBase::LABEL_CODE;
1684 else if (cmd.argument == "note")
1685 code = InsetBase::NOTE_CODE;
1686 else if (cmd.argument == "ref")
1687 code = InsetBase::REF_CODE;
1688 else if (cmd.argument == "toc")
1689 code = InsetBase::TOC_CODE;
1690 else if (cmd.argument == "url")
1691 code = InsetBase::URL_CODE;
1692 else if (cmd.argument == "vspace")
1693 code = InsetBase::VSPACE_CODE;
1694 else if (cmd.argument == "wrap")
1695 code = InsetBase::WRAP_CODE;
1698 case LFUN_INSET_ERT:
1699 code = InsetBase::ERT_CODE;
1701 case LFUN_INSET_FOOTNOTE:
1702 code = InsetBase::FOOT_CODE;
1704 case LFUN_TABULAR_INSERT:
1705 code = InsetBase::TABULAR_CODE;
1707 case LFUN_INSET_MARGINAL:
1708 code = InsetBase::MARGIN_CODE;
1710 case LFUN_INSET_FLOAT:
1711 case LFUN_INSET_WIDE_FLOAT:
1712 code = InsetBase::FLOAT_CODE;
1714 case LFUN_INSET_WRAP:
1715 code = InsetBase::WRAP_CODE;
1717 case LFUN_FLOAT_LIST:
1718 code = InsetBase::FLOAT_LIST_CODE;
1721 case LFUN_INSET_LIST:
1722 code = InsetBase::LIST_CODE;
1724 case LFUN_INSET_THEOREM:
1725 code = InsetBase::THEOREM_CODE;
1728 case LFUN_INSET_CAPTION:
1729 code = InsetBase::CAPTION_CODE;
1731 case LFUN_INSERT_NOTE:
1732 code = InsetBase::NOTE_CODE;
1734 case LFUN_INSERT_CHARSTYLE:
1735 code = InsetBase::CHARSTYLE_CODE;
1736 if (buf->params().getLyXTextClass().charstyles().empty())
1739 case LFUN_INSERT_BOX:
1740 code = InsetBase::BOX_CODE;
1742 case LFUN_INSERT_BRANCH:
1743 code = InsetBase::BRANCH_CODE;
1744 if (buf->params().branchlist().empty())
1747 case LFUN_INSERT_LABEL:
1748 code = InsetBase::LABEL_CODE;
1750 case LFUN_INSET_OPTARG:
1751 code = InsetBase::OPTARG_CODE;
1753 case LFUN_ENVIRONMENT_INSERT:
1754 code = InsetBase::BOX_CODE;
1756 case LFUN_INDEX_INSERT:
1757 code = InsetBase::INDEX_CODE;
1759 case LFUN_INDEX_PRINT:
1760 code = InsetBase::INDEX_PRINT_CODE;
1762 case LFUN_TOC_INSERT:
1763 code = InsetBase::TOC_CODE;
1767 code = InsetBase::URL_CODE;
1770 // always allow this, since we will inset a raw quote
1771 // if an inset is not allowed.
1773 case LFUN_HYPHENATION:
1774 case LFUN_LIGATURE_BREAK:
1776 case LFUN_MENU_SEPARATOR:
1778 case LFUN_END_OF_SENTENCE:
1779 code = InsetBase::SPECIALCHAR_CODE;
1781 case LFUN_SPACE_INSERT:
1782 // slight hack: we know this is allowed in math mode
1784 code = InsetBase::SPACE_CODE;
1786 case LFUN_INSET_DIALOG_SHOW: {
1787 InsetBase * inset = cur.nextInset();
1790 code = inset->lyxCode();
1791 if (!(code == InsetBase::INCLUDE_CODE
1792 || code == InsetBase::BIBTEX_CODE
1793 || code == InsetBase::FLOAT_LIST_CODE
1794 || code == InsetBase::TOC_CODE))
1803 if (code != InsetBase::NO_CODE
1804 && (cur.empty() || !cur.inset().insetAllowed(code)))
1809 case LFUN_DIALOG_SHOW_NEW_INSET:
1810 case LFUN_INSET_ERT:
1811 case LFUN_INSERT_BOX:
1812 case LFUN_INSERT_BRANCH:
1813 case LFUN_ENVIRONMENT_INSERT:
1814 case LFUN_INDEX_INSERT:
1815 case LFUN_INDEX_PRINT:
1816 case LFUN_TOC_INSERT:
1820 case LFUN_HYPHENATION:
1821 case LFUN_LIGATURE_BREAK:
1823 case LFUN_MENU_SEPARATOR:
1825 case LFUN_END_OF_SENTENCE:
1826 case LFUN_SPACE_INSERT:
1827 case LFUN_INSET_DIALOG_SHOW:
1831 flag.setOnOff(font.emph() == LyXFont::ON);
1835 flag.setOnOff(font.noun() == LyXFont::ON);
1839 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1843 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1847 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1851 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1854 case LFUN_DELETE_WORD_FORWARD:
1855 case LFUN_DELETE_WORD_BACKWARD:
1856 case LFUN_DELETE_LINE_FORWARD:
1857 case LFUN_WORDRIGHT:
1867 case LFUN_UP_PARAGRAPHSEL:
1868 case LFUN_DOWN_PARAGRAPHSEL:
1873 case LFUN_WORDRIGHTSEL:
1874 case LFUN_WORDLEFTSEL:
1876 case LFUN_UP_PARAGRAPH:
1877 case LFUN_DOWN_PARAGRAPH:
1882 case LFUN_BREAKLINE:
1884 case LFUN_DELETE_SKIP:
1885 case LFUN_BACKSPACE:
1886 case LFUN_BACKSPACE_SKIP:
1887 case LFUN_BREAKPARAGRAPH:
1888 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1889 case LFUN_BREAKPARAGRAPH_SKIP:
1890 case LFUN_PARAGRAPH_SPACING:
1891 case LFUN_INSET_APPLY:
1892 case LFUN_INSET_INSERT:
1893 case LFUN_NEXT_INSET_TOGGLE:
1894 case LFUN_UPCASE_WORD:
1895 case LFUN_LOWCASE_WORD:
1896 case LFUN_CAPITALIZE_WORD:
1897 case LFUN_TRANSPOSE_CHARS:
1904 case LFUN_GETLAYOUT:
1906 case LFUN_PASTESELECTION:
1907 case LFUN_GOTOERROR:
1909 case LFUN_REFERENCE_GOTO:
1910 case LFUN_DATE_INSERT:
1911 case LFUN_SELFINSERT:
1912 case LFUN_INSERT_LABEL:
1913 case LFUN_INSERT_NOTE:
1914 case LFUN_INSERT_CHARSTYLE:
1915 case LFUN_INSERT_BIBITEM:
1916 case LFUN_INSET_FLOAT:
1917 case LFUN_INSET_FOOTNOTE:
1918 case LFUN_INSET_MARGINAL:
1919 case LFUN_INSET_WIDE_FLOAT:
1920 case LFUN_INSET_WRAP:
1921 case LFUN_TABULAR_INSERT:
1922 case LFUN_INSERT_LINE:
1923 case LFUN_INSERT_PAGEBREAK:
1924 case LFUN_MATH_DISPLAY:
1925 case LFUN_MATH_IMPORT_SELECTION:
1926 case LFUN_MATH_MODE:
1927 case LFUN_MATH_MACRO:
1928 case LFUN_INSERT_MATH:
1929 case LFUN_INSERT_MATRIX:
1930 case LFUN_MATH_DELIM:
1932 case LFUN_UNDERLINE:
1933 case LFUN_FONT_SIZE:
1935 case LFUN_FREEFONT_APPLY:
1936 case LFUN_FREEFONT_UPDATE:
1937 case LFUN_LAYOUT_PARAGRAPH:
1938 case LFUN_PARAGRAPH_UPDATE:
1940 case LFUN_CIRCUMFLEX:
1950 case LFUN_SPECIAL_CARON:
1953 case LFUN_HUNG_UMLAUT:
1956 case LFUN_FLOAT_LIST:
1957 case LFUN_ACCEPT_CHANGE:
1958 case LFUN_REJECT_CHANGE:
1959 case LFUN_THESAURUS_ENTRY:
1960 case LFUN_PARAGRAPH_APPLY:
1962 case LFUN_KEYMAP_TOGGLE:
1964 case LFUN_BEGINNINGBUF:
1965 case LFUN_BEGINNINGBUFSEL:
1966 case LFUN_ENDBUFSEL:
1967 // these are handled in our dispatch()
1974 flag.enabled(enable);