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 "CutAndPaste.h"
28 #include "dispatchresult.h"
30 #include "funcrequest.h"
38 #include "paragraph.h"
39 #include "paragraph_funcs.h"
40 #include "ParagraphParameters.h"
44 #include "frontends/Dialogs.h"
45 #include "frontends/LyXView.h"
47 #include "insets/insetcommand.h"
48 #include "insets/insetfloatlist.h"
49 #include "insets/insetnewline.h"
50 #include "insets/insetquotes.h"
51 #include "insets/insetspecialchar.h"
52 #include "insets/insettext.h"
54 #include "support/lstrings.h"
55 #include "support/lyxlib.h"
56 #include "support/tostr.h"
58 #include "mathed/math_hullinset.h"
59 #include "mathed/math_macrotemplate.h"
66 using lyx::cap::copySelection;
67 using lyx::cap::cutSelection;
68 using lyx::cap::pasteSelection;
69 using lyx::cap::replaceSelection;
71 using lyx::support::isStrUnsignedInt;
72 using lyx::support::strToUnsignedInt;
73 using lyx::support::atoi;
74 using lyx::support::token;
79 using std::istringstream;
83 extern string current_layout;
85 // the selection possible is needed, that only motion events are
86 // used, where the button press event was on the drawing area too
87 bool selection_possible = false;
93 LyXFont freefont(LyXFont::ALL_IGNORE);
94 bool toggleall = false;
97 void toggleAndShow(LCursor & cur, LyXText * text,
98 LyXFont const & font, bool toggleall = true)
100 text->toggleFree(cur, font, toggleall);
102 if (font.language() != ignore_language ||
103 font.number() != LyXFont::IGNORE) {
104 Paragraph & par = cur.paragraph();
105 text->bidi.computeTables(par, cur.buffer(), cur.textRow());
106 if (cur.boundary() !=
107 text->bidi.isBoundary(cur.buffer(), par,
109 text->real_current_font))
110 text->setCursor(cur, cur.par(), cur.pos(),
111 false, !cur.boundary());
116 void moveCursor(LCursor & cur, bool selecting)
118 if (selecting || cur.mark())
120 if (!cur.selection())
121 cur.bv().haveSelection(false);
122 cur.bv().switchKeyMap();
126 void finishChange(LCursor & cur, bool selecting)
129 moveCursor(cur, selecting);
133 void mathDispatch(LCursor & cur, FuncRequest const & cmd, bool display)
136 string sel = cur.selectionAsString(false);
137 lyxerr << "selection is: '" << sel << "'" << endl;
140 const int old_pos = cur.pos();
141 cur.insert(new MathHullInset);
142 BOOST_ASSERT(old_pos == cur.pos());
143 cur.nextInset()->edit(cur, true);
144 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
145 // don't do that also for LFUN_MATH_MODE unless you want end up with
146 // always changing to mathrm when opening an inlined inset
147 // -- I really hate "LyXfunc overloading"...
149 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
150 cur.dispatch(FuncRequest(LFUN_INSERT_MATH, cmd.argument));
152 // create a macro if we see "\\newcommand" somewhere, and an ordinary
154 cutSelection(cur, true, true);
155 if (sel.find("\\newcommand") == string::npos
156 && sel.find("\\def") == string::npos)
158 cur.insert(new MathHullInset);
159 cur.dispatch(FuncRequest(LFUN_RIGHT));
160 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
161 cur.dispatch(FuncRequest(LFUN_INSERT_MATH, sel));
163 istringstream is(sel);
164 cur.insert(new MathMacroTemplate(is));
167 cur.message(N_("Math editor mode"));
176 string const freefont2string()
179 if (font2string(freefont, toggleall, data))
187 //takes absolute x,y coordinates
188 InsetBase * LyXText::checkInsetHit(int x, int y) const
193 getParsInRange(paragraphs(),
195 bv()->top_y() - yo_ + bv()->workHeight(),
198 lyxerr << "checkInsetHit: x: " << x << " y: " << y << endl;
199 lyxerr << " pit: " << pit << " end: " << end << endl;
200 for (; pit != end; ++pit) {
201 InsetList::const_iterator iit = pars_[pit].insetlist.begin();
202 InsetList::const_iterator iend = pars_[pit].insetlist.end();
203 for (; iit != iend; ++iit) {
204 InsetBase * inset = iit->inset;
206 lyxerr << "examining inset " << inset
207 << " xo: " << inset->xo() << "..." << inset->xo() + inset->width()
208 << " yo: " << inset->yo() - inset->ascent() << "..."
209 << inset->yo() + inset->descent() << endl;
211 if (inset->covers(x, y)) {
212 lyxerr << "Hit inset: " << inset << endl;
217 //lyxerr << "No inset hit. " << endl;
222 bool LyXText::gotoNextInset(LCursor & cur,
223 vector<InsetOld_code> const & codes, string const & contents)
225 BOOST_ASSERT(this == cur.text());
226 par_type end = paragraphs().size();
227 par_type pit = cur.par();
228 pos_type pos = cur.pos();
232 if (pos + 1 < pars_[pit].size()) {
239 } while (pit != end &&
240 !(pars_[pit].isInset(pos) &&
241 (inset = pars_[pit].getInset(pos)) != 0 &&
242 find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
244 static_cast<InsetCommand *>(pars_[pit].getInset(pos))->getContents()
250 setCursor(cur, pit, pos, false);
255 void LyXText::gotoInset(LCursor & cur,
256 vector<InsetOld_code> const & codes, bool same_content)
258 cur.clearSelection();
262 && cur.pos() < cur.lastpos()
263 && cur.paragraph().isInset(cur.pos())) {
264 InsetBase const * inset = cur.paragraph().getInset(cur.pos());
265 if (find(codes.begin(), codes.end(), inset->lyxCode())
267 contents = static_cast<InsetCommand const *>(inset)->getContents();
270 if (!gotoNextInset(cur, codes, contents)) {
271 if (cur.pos() || cur.par() != 0) {
272 CursorSlice tmp = cur.top();
275 if (!gotoNextInset(cur, codes, contents)) {
277 cur.message(_("No more insets"));
280 cur.message(_("No more insets"));
287 void LyXText::gotoInset(LCursor & cur, InsetOld_code code, bool same_content)
289 gotoInset(cur, vector<InsetOld_code>(1, code), same_content);
293 void LyXText::cursorPrevious(LCursor & cur)
295 pos_type cpos = cur.pos();
296 lyx::par_type cpar = cur.par();
298 int x = cur.x_target();
299 int y = cur.bv().top_y();
300 setCursorFromCoordinates(cur, x, y);
302 if (cpar == cur.par() && cpos == cur.pos()) {
303 // we have a row which is taller than the workarea. The
304 // simplest solution is to move to the previous row instead.
308 cur.bv().updateScrollbar();
313 void LyXText::cursorNext(LCursor & cur)
315 pos_type cpos = cur.pos();
316 lyx::par_type cpar = cur.par();
318 int x = cur.x_target();
319 int y = cur.bv().top_y() + cur.bv().workHeight();
320 setCursorFromCoordinates(cur, x, y);
322 if (cpar == cur.par() && cpos == cur.pos()) {
323 // we have a row which is taller than the workarea. The
324 // simplest solution is to move to the next row instead.
328 cur.bv().updateScrollbar();
335 void specialChar(LCursor & cur, InsetSpecialChar::Kind kind)
337 lyx::cap::replaceSelection(cur);
338 cur.insert(new InsetSpecialChar(kind));
342 void doInsertInset(LCursor & cur, LyXText * text,
343 FuncRequest const & cmd, bool edit, bool pastesel)
345 InsetBase * inset = createInset(&cur.bv(), cmd);
351 if (cur.selection()) {
352 cur.bv().owner()->dispatch(FuncRequest(LFUN_CUT));
355 text->insertInset(cur, inset);
358 inset->edit(cur, true);
360 if (gotsel && pastesel)
361 cur.bv().owner()->dispatch(FuncRequest(LFUN_PASTE));
367 void LyXText::number(LCursor & cur)
369 LyXFont font(LyXFont::ALL_IGNORE);
370 font.setNumber(LyXFont::TOGGLE);
371 toggleAndShow(cur, this, font);
375 bool LyXText::isRTL(Paragraph const & par) const
377 return par.isRightToLeftPar(bv()->buffer()->params());
381 void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
383 lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
384 //lyxerr << "*** LyXText::dispatch: cmd: " << cmd << endl;
386 BOOST_ASSERT(cur.text() == this);
387 BufferView * bv = &cur.bv();
388 CursorSlice sl = cur.top();
389 bool sel = cur.selection();
392 switch (cmd.action) {
394 case LFUN_APPENDIX: {
395 Paragraph & par = cur.paragraph();
396 bool start = !par.params().startOfAppendix();
398 // ensure that we have only one start_of_appendix in this document
399 for (par_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
400 if (pars_[tmp].params().startOfAppendix()) {
402 pars_[tmp].params().startOfAppendix(false);
409 par.params().startOfAppendix(start);
411 // we can set the refreshing parameters now
417 case LFUN_DELETE_WORD_FORWARD:
418 cur.clearSelection();
419 deleteWordForward(cur);
420 finishChange(cur, false);
423 case LFUN_DELETE_WORD_BACKWARD:
424 cur.clearSelection();
425 deleteWordBackward(cur);
426 finishChange(cur, false);
429 case LFUN_DELETE_LINE_FORWARD:
430 cur.clearSelection();
431 deleteLineForward(cur);
432 finishChange(cur, false);
438 cur.clearSelection();
439 if (isRTL(cur.paragraph()))
440 cursorLeftOneWord(cur);
442 cursorRightOneWord(cur);
443 finishChange(cur, false);
449 cur.clearSelection();
450 if (isRTL(cur.paragraph()))
451 cursorRightOneWord(cur);
453 cursorLeftOneWord(cur);
454 finishChange(cur, false);
457 case LFUN_BEGINNINGBUF:
458 if (cur.size() == 1) {
460 cur.clearSelection();
462 finishChange(cur, false);
469 if (cur.size() == 1) {
471 cur.clearSelection();
473 finishChange(cur, false);
482 //lyxerr << "handle LFUN_RIGHT[SEL]:\n" << cur << endl;
483 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
484 if (isRTL(cur.paragraph()))
488 if (sl == cur.top()) {
490 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
497 //lyxerr << "handle LFUN_LEFT[SEL]:\n" << cur << endl;
498 cur.selHandle(cmd.action == LFUN_LEFTSEL);
499 if (isRTL(cur.paragraph()))
503 if (sl == cur.top()) {
505 cmd = FuncRequest(LFUN_FINISHED_LEFT);
512 //lyxerr << "handle LFUN_UP[SEL]:\n" << cur << endl;
513 cur.selHandle(cmd.action == LFUN_UPSEL);
515 if (sl == cur.top()) {
517 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);
548 if (!cur.selection())
551 finishChange(cur, true);
555 if (!cur.selection())
558 finishChange(cur, true);
562 if (!cur.selection())
565 finishChange(cur, true);
569 if (!cur.selection())
572 finishChange(cur, true);
575 case LFUN_WORDRIGHTSEL:
576 if (!cur.selection())
578 if (isRTL(cur.paragraph()))
579 cursorLeftOneWord(cur);
581 cursorRightOneWord(cur);
582 finishChange(cur, true);
585 case LFUN_WORDLEFTSEL:
586 if (!cur.selection())
588 if (isRTL(cur.paragraph()))
589 cursorRightOneWord(cur);
591 cursorLeftOneWord(cur);
592 finishChange(cur, true);
596 selectWord(cur, lyx::WHOLE_WORD);
597 finishChange(cur, true);
601 case LFUN_UP_PARAGRAPH:
604 cur.clearSelection();
605 cursorUpParagraph(cur);
606 finishChange(cur, false);
609 case LFUN_DOWN_PARAGRAPH:
612 cur.clearSelection();
613 cursorDownParagraph(cur);
614 finishChange(cur, false);
620 cur.clearSelection();
621 finishChange(cur, false);
622 if (cur.par() == 0 && cur.textRow().pos() == 0) {
624 cmd = FuncRequest(LFUN_FINISHED_UP);
633 cur.clearSelection();
634 finishChange(cur, false);
635 if (cur.par() == cur.lastpar()
636 && cur.textRow().endpos() == cur.lastpos()) {
638 cmd = FuncRequest(LFUN_FINISHED_DOWN);
646 cur.clearSelection();
648 finishChange(cur, false);
653 cur.clearSelection();
655 finishChange(cur, false);
658 case LFUN_BREAKLINE: {
659 // Not allowed by LaTeX (labels or empty par)
660 if (cur.pos() > cur.paragraph().beginOfBody()) {
661 lyx::cap::replaceSelection(cur);
662 cur.insert(new InsetNewline);
663 moveCursor(cur, false);
669 if (!cur.selection()) {
672 // It is possible to make it a lot faster still
673 // just comment out the line below...
675 cutSelection(cur, true, false);
677 moveCursor(cur, false);
680 case LFUN_DELETE_SKIP:
681 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
682 if (!cur.selection()) {
683 if (cur.pos() == cur.lastpos()) {
690 cutSelection(cur, true, false);
696 if (!cur.selection()) {
697 if (bv->owner()->getIntl().getTransManager().backspace()) {
700 // It is possible to make it a lot faster still
701 // just comment out the line below...
704 cutSelection(cur, true, false);
709 case LFUN_BACKSPACE_SKIP:
710 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
711 if (!cur.selection()) {
715 //CursorSlice cur = cursor();
719 cutSelection(cur, true, false);
723 case LFUN_BREAKPARAGRAPH:
724 lyx::cap::replaceSelection(cur);
725 breakParagraph(cur, 0);
730 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
731 lyx::cap::replaceSelection(cur);
732 breakParagraph(cur, 1);
737 case LFUN_BREAKPARAGRAPH_SKIP: {
738 // When at the beginning of a paragraph, remove
739 // indentation. Otherwise, do the same as LFUN_BREAKPARAGRAPH.
740 lyx::cap::replaceSelection(cur);
742 cur.paragraph().params().labelWidthString(string());
744 breakParagraph(cur, 0);
750 case LFUN_PARAGRAPH_SPACING: {
751 Paragraph & par = cur.paragraph();
752 Spacing::Space cur_spacing = par.params().spacing().getSpace();
753 float cur_value = 1.0;
754 if (cur_spacing == Spacing::Other)
755 cur_value = par.params().spacing().getValue();
757 istringstream is(cmd.argument);
760 Spacing::Space new_spacing = cur_spacing;
761 float new_value = cur_value;
763 lyxerr << "Missing argument to `paragraph-spacing'"
765 } else if (tmp == "single") {
766 new_spacing = Spacing::Single;
767 } else if (tmp == "onehalf") {
768 new_spacing = Spacing::Onehalf;
769 } else if (tmp == "double") {
770 new_spacing = Spacing::Double;
771 } else if (tmp == "other") {
772 new_spacing = Spacing::Other;
775 lyxerr << "new_value = " << tmpval << endl;
778 } else if (tmp == "default") {
779 new_spacing = Spacing::Default;
781 lyxerr << _("Unknown spacing argument: ")
782 << cmd.argument << endl;
784 if (cur_spacing != new_spacing || cur_value != new_value) {
785 par.params().spacing(Spacing(new_spacing, new_value));
791 case LFUN_INSET_APPLY: {
792 string const name = cmd.getArg(0);
793 InsetBase * inset = bv->owner()->getDialogs().getOpenInset(name);
795 FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument);
796 inset->dispatch(cur, fr);
798 FuncRequest fr(LFUN_INSET_INSERT, cmd.argument);
804 case LFUN_INSET_INSERT: {
806 InsetBase * inset = createInset(bv, cmd);
808 insertInset(cur, inset);
814 case LFUN_INSET_SETTINGS:
815 if (cur.inset().asUpdatableInset())
816 cur.inset().asUpdatableInset()->showInsetDialog(bv);
819 case LFUN_NEXT_INSET_TOGGLE: {
820 InsetBase * inset = cur.nextInset();
822 cur.clearSelection();
823 FuncRequest fr = cmd;
824 fr.action = LFUN_INSET_TOGGLE;
825 inset->dispatch(cur, fr);
830 case LFUN_KEYMAP_TOGGLE:
831 cur.clearSelection();
835 case LFUN_SPACE_INSERT:
836 if (cur.paragraph().layout()->free_spacing)
837 insertChar(cur, ' ');
839 doInsertInset(cur, this, cmd, false, false);
840 moveCursor(cur, false);
843 case LFUN_HYPHENATION:
844 specialChar(cur, InsetSpecialChar::HYPHENATION);
847 case LFUN_LIGATURE_BREAK:
848 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
852 specialChar(cur, InsetSpecialChar::LDOTS);
855 case LFUN_END_OF_SENTENCE:
856 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
859 case LFUN_MENU_SEPARATOR:
860 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
863 case LFUN_UPCASE_WORD:
864 changeCase(cur, LyXText::text_uppercase);
867 case LFUN_LOWCASE_WORD:
868 changeCase(cur, LyXText::text_lowercase);
871 case LFUN_CAPITALIZE_WORD:
872 changeCase(cur, LyXText::text_capitalization);
875 case LFUN_TRANSPOSE_CHARS:
881 cur.message(_("Paste"));
882 lyx::cap::replaceSelection(cur);
884 #warning FIXME Check if the arg is in the domain of available selections.
886 if (isStrUnsignedInt(cmd.argument))
887 pasteSelection(cur, strToUnsignedInt(cmd.argument));
889 pasteSelection(cur, 0);
890 cur.clearSelection(); // bug 393
896 cutSelection(cur, true, true);
897 cur.message(_("Cut"));
902 cur.message(_("Copy"));
906 cur.message(tostr(cursorX(cur.top())) + ' '
907 + tostr(cursorY(cur.top())));
913 istringstream is(cmd.argument);
916 lyxerr << "SETXY: Could not parse coordinates in '"
917 << cmd.argument << std::endl;
919 setCursorFromCoordinates(cur, x, y);
924 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
926 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
933 cur.message(cur.paragraph().layout()->name());
937 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
938 << cmd.argument << endl;
940 // This is not the good solution to the empty argument
941 // problem, but it will hopefully suffice for 1.2.0.
942 // The correct solution would be to augument the
943 // function list/array with information about what
944 // functions needs arguments and their type.
945 if (cmd.argument.empty()) {
946 cur.errorMessage(_("LyX function 'layout' needs an argument."));
950 // Derive layout number from given argument (string)
951 // and current buffer's textclass (number)
952 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
953 bool hasLayout = tclass.hasLayout(cmd.argument);
954 string layout = cmd.argument;
956 // If the entry is obsolete, use the new one instead.
958 string const & obs = tclass[layout]->obsoleted_by();
964 cur.errorMessage(string(N_("Layout ")) + cmd.argument +
969 bool change_layout = (current_layout != layout);
971 if (!change_layout && cur.selection() &&
972 cur.selBegin().par() != cur.selEnd().par())
974 par_type spit = cur.selBegin().par();
975 par_type epit = cur.selEnd().par() + 1;
976 while (spit != epit) {
977 if (pars_[spit].layout()->name() != current_layout) {
978 change_layout = true;
986 current_layout = layout;
987 setLayout(cur, layout);
988 bv->owner()->setLayout(layout);
994 case LFUN_PASTESELECTION: {
995 cur.clearSelection();
996 string const clip = bv->getClipboard();
998 if (cmd.argument == "paragraph")
999 insertStringAsParagraphs(cur, clip);
1001 insertStringAsLines(cur, clip);
1006 case LFUN_GOTOERROR:
1007 gotoInset(cur, InsetBase::ERROR_CODE, false);
1011 gotoInset(cur, InsetBase::NOTE_CODE, false);
1014 case LFUN_REFERENCE_GOTO: {
1015 vector<InsetOld_code> tmp;
1016 tmp.push_back(InsetBase::LABEL_CODE);
1017 tmp.push_back(InsetBase::REF_CODE);
1018 gotoInset(cur, tmp, true);
1023 lyx::cap::replaceSelection(cur);
1024 Paragraph & par = cur.paragraph();
1025 lyx::pos_type pos = cur.pos();
1029 else if (cur.prevInset() && cur.prevInset()->isSpace())
1032 c = par.getChar(pos - 1);
1034 LyXLayout_ptr const & style = par.layout();
1036 BufferParams const & bufparams = bv->buffer()->params();
1037 if (!style->pass_thru
1038 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1039 string arg = cmd.argument;
1040 if (arg == "single")
1041 cur.insert(new InsetQuotes(c,
1042 bufparams.quotes_language,
1043 InsetQuotes::SingleQ));
1044 else if (arg == "double")
1045 cur.insert(new InsetQuotes(c,
1046 bufparams.quotes_language,
1047 InsetQuotes::DoubleQ));
1049 cur.insert(new InsetQuotes(c, bufparams));
1053 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1057 case LFUN_DATE_INSERT: {
1058 lyx::cap::replaceSelection(cur);
1059 time_t now_time_t = time(NULL);
1060 struct tm * now_tm = localtime(&now_time_t);
1061 setlocale(LC_TIME, "");
1063 if (!cmd.argument.empty())
1066 arg = lyxrc.date_insert_format;
1068 int const datetmp_len =
1069 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1071 for (int i = 0; i < datetmp_len; i++)
1072 insertChar(cur, datetmp[i]);
1075 moveCursor(cur, false);
1079 case LFUN_MOUSE_TRIPLE:
1080 if (cmd.button() == mouse_button::button1) {
1081 selection_possible = true;
1086 bv->haveSelection(cur.selection());
1090 case LFUN_MOUSE_DOUBLE:
1091 if (cmd.button() == mouse_button::button1) {
1092 selection_possible = true;
1093 selectWord(cur, lyx::WHOLE_WORD_STRICT);
1094 bv->haveSelection(cur.selection());
1098 case LFUN_MOUSE_MOTION: {
1099 // Only use motion with button 1
1100 //if (cmd.button() != mouse_button::button1)
1102 // We want to use only motion events for which
1103 // the button press event was on the drawing area too.
1104 if (!selection_possible) {
1105 lyxerr[Debug::ACTION] << "BufferView::Pimpl::"
1106 "dispatch: no selection possible\n";
1107 lyxerr << "BufferView::Pimpl::dispatch: no selection possible\n";
1111 // ignore motions deeper nested than the real anchor
1112 LCursor & bvcur = cur.bv().cursor();
1113 if (bvcur.selection() && bvcur.anchor_.size() < cur.size())
1116 CursorSlice old = cur.top();
1117 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1119 // This is to allow jumping over large insets
1120 // FIXME: shouldn't be top-text-specific
1121 if (isMainText() && cur.top() == old) {
1122 if (cmd.y - bv->top_y() >= bv->workHeight())
1124 else if (cmd.y - bv->top_y() < 0)
1128 // don't set anchor_
1129 bv->cursor().setCursor(cur, true);
1130 lyxerr << "MOTION: " << bv->cursor() << endl;
1134 // Single-click on work area
1135 case LFUN_MOUSE_PRESS: {
1136 // Right click on a footnote flag opens float menu
1137 if (cmd.button() == mouse_button::button3) {
1138 cur.clearSelection();
1139 selection_possible = false;
1143 // Middle button press pastes if we have a selection
1144 // We do this here as if the selection was inside an inset
1145 // it could get cleared on the unlocking of the inset so
1146 // we have to check this first
1147 bool paste_internally = false;
1148 if (cmd.button() == mouse_button::button2 && cur.selection()) {
1149 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1150 paste_internally = true;
1153 selection_possible = true;
1155 // Clear the selection
1156 cur.clearSelection();
1158 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1161 cur.x_target() = cursorX(cur.top());
1163 // Has the cursor just left the inset?
1164 if (bv->cursor().inMathed() && !cur.inMathed())
1165 bv->cursor().inset().notifyCursorLeaves(bv->cursor());
1170 // Don't allow selection after a big jump.
1171 if (bv->fitCursor())
1172 selection_possible = false;
1174 // Insert primary selection with middle mouse
1175 // if there is a local selection in the current buffer,
1177 if (cmd.button() == mouse_button::button2) {
1178 if (paste_internally)
1179 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1181 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1182 selection_possible = false;
1188 case LFUN_MOUSE_RELEASE: {
1189 selection_possible = false;
1191 if (cmd.button() == mouse_button::button2)
1195 if (cmd.button() == mouse_button::button1)
1196 bv->haveSelection(cur.selection());
1199 bv->owner()->updateMenubar();
1200 bv->owner()->updateToolbars();
1204 case LFUN_SELFINSERT: {
1205 if (cmd.argument.empty())
1208 // Automatically delete the currently selected
1209 // text and replace it with what is being
1210 // typed in now. Depends on lyxrc settings
1211 // "auto_region_delete", which defaults to
1214 if (lyxrc.auto_region_delete) {
1215 if (cur.selection())
1216 cutSelection(cur, false, false);
1217 bv->haveSelection(false);
1220 cur.clearSelection();
1221 LyXFont const old_font = real_current_font;
1223 string::const_iterator cit = cmd.argument.begin();
1224 string::const_iterator end = cmd.argument.end();
1225 for (; cit != end; ++cit)
1226 bv->owner()->getIntl().getTransManager().
1227 TranslateAndInsert(*cit, this);
1230 moveCursor(cur, false);
1232 // real_current_font.number can change so we need to
1233 // update the minibuffer
1234 if (old_font != real_current_font)
1235 bv->updateScrollbar();
1240 InsetCommandParams p("url");
1241 string const data = InsetCommandMailer::params2string("url", p);
1242 bv->owner()->getDialogs().show("url", data, 0);
1246 case LFUN_HTMLURL: {
1247 InsetCommandParams p("htmlurl");
1248 string const data = InsetCommandMailer::params2string("url", p);
1249 bv->owner()->getDialogs().show("url", data, 0);
1253 case LFUN_INSERT_LABEL: {
1254 // Try to generate a valid label
1255 string const contents = cmd.argument.empty() ?
1256 cur.getPossibleLabel() : cmd.argument;
1258 InsetCommandParams p("label", contents);
1259 string const data = InsetCommandMailer::params2string("label", p);
1261 if (cmd.argument.empty()) {
1262 bv->owner()->getDialogs().show("label", data, 0);
1264 FuncRequest fr(LFUN_INSET_INSERT, data);
1272 case LFUN_INSET_LIST:
1273 case LFUN_INSET_THEOREM:
1274 case LFUN_INSET_CAPTION:
1276 case LFUN_INSERT_NOTE:
1277 case LFUN_INSERT_CHARSTYLE:
1278 case LFUN_INSERT_BOX:
1279 case LFUN_INSERT_BRANCH:
1280 case LFUN_INSERT_BIBITEM:
1281 case LFUN_INSET_ERT:
1282 case LFUN_INSET_FLOAT:
1283 case LFUN_INSET_FOOTNOTE:
1284 case LFUN_INSET_MARGINAL:
1285 case LFUN_INSET_OPTARG:
1286 case LFUN_INSET_WIDE_FLOAT:
1287 case LFUN_INSET_WRAP:
1288 case LFUN_TABULAR_INSERT:
1289 case LFUN_ENVIRONMENT_INSERT:
1290 // Open the inset, and move the current selection
1292 doInsertInset(cur, this, cmd, true, true);
1296 case LFUN_INDEX_INSERT:
1297 // Just open the inset
1298 doInsertInset(cur, this, cmd, true, false);
1302 case LFUN_INDEX_PRINT:
1303 case LFUN_TOC_INSERT:
1305 case LFUN_INSERT_LINE:
1306 case LFUN_INSERT_PAGEBREAK:
1308 doInsertInset(cur, this, cmd, false, false);
1312 case LFUN_DEPTH_MIN:
1313 changeDepth(cur, DEC_DEPTH);
1316 case LFUN_DEPTH_PLUS:
1317 changeDepth(cur, INC_DEPTH);
1320 case LFUN_MATH_DISPLAY:
1321 mathDispatch(cur, cmd, true);
1324 case LFUN_MATH_IMPORT_SELECTION:
1325 case LFUN_MATH_MODE:
1326 mathDispatch(cur, cmd, false);
1329 case LFUN_MATH_MACRO:
1330 if (cmd.argument.empty())
1331 cur.errorMessage(N_("Missing argument"));
1333 string s = cmd.argument;
1334 string const s1 = token(s, ' ', 1);
1335 int const nargs = s1.empty() ? 0 : atoi(s1);
1336 string const s2 = token(s, ' ', 2);
1337 string const type = s2.empty() ? "newcommand" : s2;
1338 cur.insert(new MathMacroTemplate(token(s, ' ', 0), nargs, s2));
1339 //cur.nextInset()->edit(cur, true);
1343 case LFUN_INSERT_MATH:
1344 case LFUN_INSERT_MATRIX:
1345 case LFUN_MATH_DELIM: {
1346 cur.insert(new MathHullInset);
1347 cur.dispatch(FuncRequest(LFUN_RIGHT));
1348 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
1354 LyXFont font(LyXFont::ALL_IGNORE);
1355 font.setEmph(LyXFont::TOGGLE);
1356 toggleAndShow(cur, this, font);
1361 LyXFont font(LyXFont::ALL_IGNORE);
1362 font.setSeries(LyXFont::BOLD_SERIES);
1363 toggleAndShow(cur, this, font);
1368 LyXFont font(LyXFont::ALL_IGNORE);
1369 font.setNoun(LyXFont::TOGGLE);
1370 toggleAndShow(cur, this, font);
1375 LyXFont font(LyXFont::ALL_IGNORE);
1376 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
1377 toggleAndShow(cur, this, font);
1382 LyXFont font(LyXFont::ALL_IGNORE);
1383 font.setFamily(LyXFont::SANS_FAMILY);
1384 toggleAndShow(cur, this, font);
1389 LyXFont font(LyXFont::ALL_IGNORE);
1390 font.setFamily(LyXFont::ROMAN_FAMILY);
1391 toggleAndShow(cur, this, font);
1395 case LFUN_DEFAULT: {
1396 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
1397 toggleAndShow(cur, this, font);
1401 case LFUN_UNDERLINE: {
1402 LyXFont font(LyXFont::ALL_IGNORE);
1403 font.setUnderbar(LyXFont::TOGGLE);
1404 toggleAndShow(cur, this, font);
1408 case LFUN_FONT_SIZE: {
1409 LyXFont font(LyXFont::ALL_IGNORE);
1410 font.setLyXSize(cmd.argument);
1411 toggleAndShow(cur, this, font);
1415 case LFUN_LANGUAGE: {
1416 Language const * lang = languages.getLanguage(cmd.argument);
1419 LyXFont font(LyXFont::ALL_IGNORE);
1420 font.setLanguage(lang);
1421 toggleAndShow(cur, this, font);
1426 case LFUN_FREEFONT_APPLY:
1427 toggleAndShow(cur, this, freefont, toggleall);
1428 cur.message(_("Character set"));
1431 // Set the freefont using the contents of \param data dispatched from
1432 // the frontends and apply it at the current cursor location.
1433 case LFUN_FREEFONT_UPDATE: {
1436 if (bv_funcs::string2font(cmd.argument, font, toggle)) {
1439 toggleAndShow(cur, this, freefont, toggleall);
1440 cur.message(_("Character set"));
1445 case LFUN_FINISHED_LEFT:
1446 lyxerr << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1449 case LFUN_FINISHED_RIGHT:
1450 lyxerr << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1454 case LFUN_FINISHED_UP:
1455 lyxerr << "handle LFUN_FINISHED_UP:\n" << cur << endl;
1459 case LFUN_FINISHED_DOWN:
1460 lyxerr << "handle LFUN_FINISHED_DOWN:\n" << cur << endl;
1464 case LFUN_LAYOUT_PARAGRAPH: {
1466 params2string(cur.paragraph(), data);
1467 data = "show\n" + data;
1468 bv->owner()->getDialogs().show("paragraph", data);
1472 case LFUN_PARAGRAPH_UPDATE: {
1473 if (!bv->owner()->getDialogs().visible("paragraph"))
1476 params2string(cur.paragraph(), data);
1478 // Will the paragraph accept changes from the dialog?
1479 InsetBase & inset = cur.inset();
1480 bool const accept = !inset.forceDefaultParagraphs(&inset);
1482 data = "update " + tostr(accept) + '\n' + data;
1483 bv->owner()->getDialogs().update("paragraph", data);
1488 case LFUN_CIRCUMFLEX:
1498 case LFUN_SPECIAL_CARON:
1501 case LFUN_HUNG_UMLAUT:
1504 bv->owner()->getLyXFunc().handleKeyFunc(cmd.action);
1505 if (!cmd.argument.empty())
1506 bv->owner()->getIntl().getTransManager()
1507 .TranslateAndInsert(cmd.argument[0], this);
1510 case LFUN_FLOAT_LIST: {
1511 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1512 if (tclass.floats().typeExist(cmd.argument)) {
1513 // not quite sure if we want this...
1515 cur.clearSelection();
1516 breakParagraph(cur);
1518 if (cur.lastpos() != 0) {
1520 breakParagraph(cur);
1523 setLayout(cur, tclass.defaultLayoutName());
1524 setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1525 insertInset(cur, new InsetFloatList(cmd.argument));
1527 lyxerr << "Non-existent float type: "
1528 << cmd.argument << endl;
1533 case LFUN_ACCEPT_CHANGE: {
1538 case LFUN_REJECT_CHANGE: {
1543 case LFUN_THESAURUS_ENTRY: {
1544 string arg = cmd.argument;
1546 arg = cur.selectionAsString(false);
1548 if (arg.size() > 100 || arg.empty()) {
1549 // Get word or selection
1550 selectWordWhenUnderCursor(cur, lyx::WHOLE_WORD);
1551 arg = cur.selectionAsString(false);
1554 bv->owner()->getDialogs().show("thesaurus", arg);
1558 case LFUN_PARAGRAPH_APPLY: {
1559 // Given data, an encoding of the ParagraphParameters
1560 // generated in the Paragraph dialog, this function sets
1561 // the current paragraph appropriately.
1562 istringstream is(cmd.argument);
1565 ParagraphParameters params;
1570 params.labelWidthString(),
1572 cur.message(_("Paragraph layout set"));
1576 case LFUN_INSET_DIALOG_SHOW: {
1577 InsetBase * inset = cur.nextInset();
1579 FuncRequest fr(LFUN_INSET_DIALOG_SHOW);
1580 inset->dispatch(cur, fr);
1586 if (cur.selection()) {
1587 cur.selection() = false;
1590 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1599 // avoid to update when navigating
1601 && &sl.inset() == &cur.inset()
1602 && sl.idx() == cur.idx()
1604 && cur.selection() == false)
1609 bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
1610 FuncStatus & flag) const
1612 BOOST_ASSERT(cur.text() == this);
1613 LyXFont const & font = real_current_font;
1616 switch (cmd.action) {
1618 case LFUN_DEPTH_MIN:
1619 enable = changeDepthAllowed(cur, DEC_DEPTH);
1622 case LFUN_DEPTH_PLUS:
1623 enable = changeDepthAllowed(cur, INC_DEPTH);
1626 case LFUN_INSET_OPTARG:
1627 enable = cur.paragraph().layout()->optionalargs;
1631 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1635 // the functions which insert insets
1636 InsetOld::Code code = InsetOld::NO_CODE;
1637 switch (cmd.action) {
1638 case LFUN_DIALOG_SHOW_NEW_INSET:
1639 if (cmd.argument == "bibitem")
1640 code = InsetOld::BIBITEM_CODE;
1641 else if (cmd.argument == "bibtex")
1642 code = InsetOld::BIBTEX_CODE;
1643 else if (cmd.argument == "box")
1644 code = InsetOld::BOX_CODE;
1645 else if (cmd.argument == "branch")
1646 code = InsetOld::BRANCH_CODE;
1647 else if (cmd.argument == "citation")
1648 code = InsetOld::CITE_CODE;
1649 else if (cmd.argument == "ert")
1650 code = InsetOld::ERT_CODE;
1651 else if (cmd.argument == "external")
1652 code = InsetOld::EXTERNAL_CODE;
1653 else if (cmd.argument == "float")
1654 code = InsetOld::FLOAT_CODE;
1655 else if (cmd.argument == "graphics")
1656 code = InsetOld::GRAPHICS_CODE;
1657 else if (cmd.argument == "include")
1658 code = InsetOld::INCLUDE_CODE;
1659 else if (cmd.argument == "index")
1660 code = InsetOld::INDEX_CODE;
1661 else if (cmd.argument == "label")
1662 code = InsetOld::LABEL_CODE;
1663 else if (cmd.argument == "note")
1664 code = InsetOld::NOTE_CODE;
1665 else if (cmd.argument == "ref")
1666 code = InsetOld::REF_CODE;
1667 else if (cmd.argument == "toc")
1668 code = InsetOld::TOC_CODE;
1669 else if (cmd.argument == "url")
1670 code = InsetOld::URL_CODE;
1671 else if (cmd.argument == "vspace")
1672 code = InsetOld::VSPACE_CODE;
1673 else if (cmd.argument == "wrap")
1674 code = InsetOld::WRAP_CODE;
1677 case LFUN_INSET_ERT:
1678 code = InsetOld::ERT_CODE;
1680 case LFUN_INSET_FOOTNOTE:
1681 code = InsetOld::FOOT_CODE;
1683 case LFUN_TABULAR_INSERT:
1684 code = InsetOld::TABULAR_CODE;
1686 case LFUN_INSET_MARGINAL:
1687 code = InsetOld::MARGIN_CODE;
1689 case LFUN_INSET_FLOAT:
1690 case LFUN_INSET_WIDE_FLOAT:
1691 code = InsetOld::FLOAT_CODE;
1693 case LFUN_INSET_WRAP:
1694 code = InsetOld::WRAP_CODE;
1696 case LFUN_FLOAT_LIST:
1697 code = InsetOld::FLOAT_LIST_CODE;
1700 case LFUN_INSET_LIST:
1701 code = InsetOld::LIST_CODE;
1703 case LFUN_INSET_THEOREM:
1704 code = InsetOld::THEOREM_CODE;
1707 case LFUN_INSET_CAPTION:
1708 code = InsetOld::CAPTION_CODE;
1710 case LFUN_INSERT_NOTE:
1711 code = InsetOld::NOTE_CODE;
1713 case LFUN_INSERT_CHARSTYLE:
1714 code = InsetOld::CHARSTYLE_CODE;
1715 if (buf->params().getLyXTextClass().charstyles().empty())
1718 case LFUN_INSERT_BOX:
1719 code = InsetOld::BOX_CODE;
1721 case LFUN_INSERT_BRANCH:
1722 code = InsetOld::BRANCH_CODE;
1723 if (buf->params().branchlist().empty())
1726 case LFUN_INSERT_LABEL:
1727 code = InsetOld::LABEL_CODE;
1729 case LFUN_INSET_OPTARG:
1730 code = InsetOld::OPTARG_CODE;
1732 case LFUN_ENVIRONMENT_INSERT:
1733 code = InsetOld::BOX_CODE;
1735 case LFUN_INDEX_INSERT:
1736 code = InsetOld::INDEX_CODE;
1738 case LFUN_INDEX_PRINT:
1739 code = InsetOld::INDEX_PRINT_CODE;
1741 case LFUN_TOC_INSERT:
1742 code = InsetOld::TOC_CODE;
1746 code = InsetOld::URL_CODE;
1749 // always allow this, since we will inset a raw quote
1750 // if an inset is not allowed.
1752 case LFUN_HYPHENATION:
1753 case LFUN_LIGATURE_BREAK:
1755 case LFUN_MENU_SEPARATOR:
1757 case LFUN_END_OF_SENTENCE:
1758 code = InsetOld::SPECIALCHAR_CODE;
1760 case LFUN_SPACE_INSERT:
1761 // slight hack: we know this is allowed in math mode
1763 code = InsetOld::SPACE_CODE;
1765 case LFUN_INSET_DIALOG_SHOW: {
1766 InsetBase * inset = cur.nextInset();
1769 code = inset->lyxCode();
1770 if (!(code == InsetOld::INCLUDE_CODE
1771 || code == InsetOld::BIBTEX_CODE
1772 || code == InsetOld::FLOAT_LIST_CODE
1773 || code == InsetOld::TOC_CODE))
1782 if (code != InsetOld::NO_CODE
1783 && (cur.empty() || !cur.inset().insetAllowed(code)))
1788 case LFUN_DIALOG_SHOW_NEW_INSET:
1789 case LFUN_INSET_ERT:
1790 case LFUN_INSERT_BOX:
1791 case LFUN_INSERT_BRANCH:
1792 case LFUN_ENVIRONMENT_INSERT:
1793 case LFUN_INDEX_INSERT:
1794 case LFUN_INDEX_PRINT:
1795 case LFUN_TOC_INSERT:
1799 case LFUN_HYPHENATION:
1800 case LFUN_LIGATURE_BREAK:
1802 case LFUN_MENU_SEPARATOR:
1804 case LFUN_END_OF_SENTENCE:
1805 case LFUN_SPACE_INSERT:
1806 case LFUN_INSET_DIALOG_SHOW:
1810 flag.setOnOff(font.emph() == LyXFont::ON);
1814 flag.setOnOff(font.noun() == LyXFont::ON);
1818 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1822 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1826 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1830 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1833 case LFUN_DELETE_WORD_FORWARD:
1834 case LFUN_DELETE_WORD_BACKWARD:
1835 case LFUN_DELETE_LINE_FORWARD:
1836 case LFUN_WORDRIGHT:
1846 case LFUN_UP_PARAGRAPHSEL:
1847 case LFUN_DOWN_PARAGRAPHSEL:
1852 case LFUN_WORDRIGHTSEL:
1853 case LFUN_WORDLEFTSEL:
1855 case LFUN_UP_PARAGRAPH:
1856 case LFUN_DOWN_PARAGRAPH:
1861 case LFUN_BREAKLINE:
1863 case LFUN_DELETE_SKIP:
1864 case LFUN_BACKSPACE:
1865 case LFUN_BACKSPACE_SKIP:
1866 case LFUN_BREAKPARAGRAPH:
1867 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1868 case LFUN_BREAKPARAGRAPH_SKIP:
1869 case LFUN_PARAGRAPH_SPACING:
1870 case LFUN_INSET_APPLY:
1871 case LFUN_INSET_INSERT:
1872 case LFUN_NEXT_INSET_TOGGLE:
1873 case LFUN_UPCASE_WORD:
1874 case LFUN_LOWCASE_WORD:
1875 case LFUN_CAPITALIZE_WORD:
1876 case LFUN_TRANSPOSE_CHARS:
1883 case LFUN_GETLAYOUT:
1885 case LFUN_PASTESELECTION:
1886 case LFUN_GOTOERROR:
1888 case LFUN_REFERENCE_GOTO:
1889 case LFUN_DATE_INSERT:
1890 case LFUN_SELFINSERT:
1891 case LFUN_INSERT_LABEL:
1892 case LFUN_INSERT_NOTE:
1893 case LFUN_INSERT_CHARSTYLE:
1894 case LFUN_INSERT_BIBITEM:
1895 case LFUN_INSET_FLOAT:
1896 case LFUN_INSET_FOOTNOTE:
1897 case LFUN_INSET_MARGINAL:
1898 case LFUN_INSET_WIDE_FLOAT:
1899 case LFUN_INSET_WRAP:
1900 case LFUN_TABULAR_INSERT:
1901 case LFUN_INSERT_LINE:
1902 case LFUN_INSERT_PAGEBREAK:
1903 case LFUN_MATH_DISPLAY:
1904 case LFUN_MATH_IMPORT_SELECTION:
1905 case LFUN_MATH_MODE:
1906 case LFUN_MATH_MACRO:
1907 case LFUN_INSERT_MATH:
1908 case LFUN_INSERT_MATRIX:
1909 case LFUN_MATH_DELIM:
1911 case LFUN_UNDERLINE:
1912 case LFUN_FONT_SIZE:
1914 case LFUN_FREEFONT_APPLY:
1915 case LFUN_FREEFONT_UPDATE:
1916 case LFUN_LAYOUT_PARAGRAPH:
1917 case LFUN_PARAGRAPH_UPDATE:
1919 case LFUN_CIRCUMFLEX:
1929 case LFUN_SPECIAL_CARON:
1932 case LFUN_HUNG_UMLAUT:
1935 case LFUN_FLOAT_LIST:
1936 case LFUN_ACCEPT_CHANGE:
1937 case LFUN_REJECT_CHANGE:
1938 case LFUN_THESAURUS_ENTRY:
1939 case LFUN_PARAGRAPH_APPLY:
1941 case LFUN_KEYMAP_TOGGLE:
1942 // these are handled in our dispatch()
1947 case LFUN_BEGINNINGBUF:
1955 flag.enabled(enable);