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 cur.insert(new MathHullInset);
141 cur.nextInset()->edit(cur, true);
142 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
143 // don't do that also for LFUN_MATH_MODE unless you want end up with
144 // always changing to mathrm when opening an inlined inset
145 // -- I really hate "LyXfunc overloading"...
147 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
148 cur.dispatch(FuncRequest(LFUN_INSERT_MATH, cmd.argument));
150 // create a macro if we see "\\newcommand" somewhere, and an ordinary
152 cutSelection(cur, true, true);
153 if (sel.find("\\newcommand") == string::npos
154 && sel.find("\\def") == string::npos)
156 cur.insert(new MathHullInset);
157 cur.dispatch(FuncRequest(LFUN_RIGHT));
158 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
159 cur.dispatch(FuncRequest(LFUN_INSERT_MATH, sel));
161 istringstream is(sel);
162 cur.insert(new MathMacroTemplate(is));
165 cur.message(N_("Math editor mode"));
174 string const freefont2string()
177 if (font2string(freefont, toggleall, data))
185 //takes absolute x,y coordinates
186 InsetBase * LyXText::checkInsetHit(int x, int y)
191 getParsInRange(paragraphs(),
193 bv()->top_y() - yo_ + bv()->workHeight(),
196 lyxerr << "checkInsetHit: x: " << x << " y: " << y << endl;
197 lyxerr << " pit: " << pit << " end: " << end << endl;
198 for (; pit != end; ++pit) {
199 InsetList::iterator iit = pars_[pit].insetlist.begin();
200 InsetList::iterator iend = pars_[pit].insetlist.end();
201 for (; iit != iend; ++iit) {
202 InsetBase * inset = iit->inset;
204 lyxerr << "examining inset " << inset
205 << " xo: " << inset->xo() << "..." << inset->xo() + inset->width()
206 << " yo: " << inset->yo() - inset->ascent() << "..."
207 << inset->yo() + inset->descent() << endl;
209 if (inset->covers(x, y)) {
210 lyxerr << "Hit inset: " << inset << endl;
215 //lyxerr << "No inset hit. " << endl;
220 bool LyXText::gotoNextInset(LCursor & cur,
221 vector<InsetOld_code> const & codes, string const & contents)
223 BOOST_ASSERT(this == cur.text());
224 par_type end = paragraphs().size();
225 par_type pit = cur.par();
226 pos_type pos = cur.pos();
230 if (pos + 1 < pars_[pit].size()) {
237 } while (pit != end &&
238 !(pars_[pit].isInset(pos) &&
239 (inset = pars_[pit].getInset(pos)) != 0 &&
240 find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
242 static_cast<InsetCommand *>(pars_[pit].getInset(pos))->getContents()
248 setCursor(cur, pit, pos, false);
253 void LyXText::gotoInset(LCursor & cur,
254 vector<InsetOld_code> const & codes, bool same_content)
256 cur.clearSelection();
260 && cur.pos() < cur.lastpos()
261 && cur.paragraph().isInset(cur.pos())) {
262 InsetBase const * inset = cur.paragraph().getInset(cur.pos());
263 if (find(codes.begin(), codes.end(), inset->lyxCode())
265 contents = static_cast<InsetCommand const *>(inset)->getContents();
268 if (!gotoNextInset(cur, codes, contents)) {
269 if (cur.pos() || cur.par() != 0) {
270 CursorSlice tmp = cur.top();
273 if (!gotoNextInset(cur, codes, contents)) {
275 cur.message(_("No more insets"));
278 cur.message(_("No more insets"));
285 void LyXText::gotoInset(LCursor & cur, InsetOld_code code, bool same_content)
287 gotoInset(cur, vector<InsetOld_code>(1, code), same_content);
291 void LyXText::cursorPrevious(LCursor & cur)
293 pos_type cpos = cur.pos();
294 lyx::par_type cpar = cur.par();
296 int x = cur.x_target();
297 int y = cur.bv().top_y();
298 setCursorFromCoordinates(cur, x, y);
300 if (cpar == cur.par() && cpos == cur.pos()) {
301 // we have a row which is taller than the workarea. The
302 // simplest solution is to move to the previous row instead.
306 cur.bv().updateScrollbar();
311 void LyXText::cursorNext(LCursor & cur)
313 pos_type cpos = cur.pos();
314 lyx::par_type cpar = cur.par();
316 int x = cur.x_target();
317 int y = cur.bv().top_y() + cur.bv().workHeight();
318 setCursorFromCoordinates(cur, x, y);
320 if (cpar == cur.par() && cpos == cur.pos()) {
321 // we have a row which is taller than the workarea. The
322 // simplest solution is to move to the next row instead.
326 cur.bv().updateScrollbar();
333 void specialChar(LCursor & cur, InsetSpecialChar::Kind kind)
335 lyx::cap::replaceSelection(cur);
336 cur.insert(new InsetSpecialChar(kind));
340 void doInsertInset(LCursor & cur, LyXText * text,
341 FuncRequest const & cmd, bool edit, bool pastesel)
343 InsetBase * inset = createInset(&cur.bv(), cmd);
349 if (cur.selection()) {
350 cur.bv().owner()->dispatch(FuncRequest(LFUN_CUT));
353 text->insertInset(cur, inset);
355 inset->edit(cur, true);
356 if (gotsel && pastesel)
357 cur.bv().owner()->dispatch(FuncRequest(LFUN_PASTE));
363 void LyXText::number(LCursor & cur)
365 LyXFont font(LyXFont::ALL_IGNORE);
366 font.setNumber(LyXFont::TOGGLE);
367 toggleAndShow(cur, this, font);
371 bool LyXText::isRTL(Paragraph const & par) const
373 return par.isRightToLeftPar(bv()->buffer()->params());
377 void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
379 lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
380 //lyxerr << "*** LyXText::dispatch: cmd: " << cmd << endl;
382 BOOST_ASSERT(cur.text() == this);
383 BufferView * bv = &cur.bv();
384 CursorSlice sl = cur.top();
385 bool sel = cur.selection();
388 switch (cmd.action) {
390 case LFUN_APPENDIX: {
391 Paragraph & par = cur.paragraph();
392 bool start = !par.params().startOfAppendix();
394 // ensure that we have only one start_of_appendix in this document
395 for (par_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
396 if (pars_[tmp].params().startOfAppendix()) {
398 pars_[tmp].params().startOfAppendix(false);
405 par.params().startOfAppendix(start);
407 // we can set the refreshing parameters now
413 case LFUN_DELETE_WORD_FORWARD:
414 cur.clearSelection();
415 deleteWordForward(cur);
416 finishChange(cur, false);
419 case LFUN_DELETE_WORD_BACKWARD:
420 cur.clearSelection();
421 deleteWordBackward(cur);
422 finishChange(cur, false);
425 case LFUN_DELETE_LINE_FORWARD:
426 cur.clearSelection();
427 deleteLineForward(cur);
428 finishChange(cur, false);
434 cur.clearSelection();
435 if (isRTL(cur.paragraph()))
436 cursorLeftOneWord(cur);
438 cursorRightOneWord(cur);
439 finishChange(cur, false);
445 cur.clearSelection();
446 if (isRTL(cur.paragraph()))
447 cursorRightOneWord(cur);
449 cursorLeftOneWord(cur);
450 finishChange(cur, false);
453 case LFUN_BEGINNINGBUF:
455 cur.clearSelection();
457 finishChange(cur, false);
462 cur.clearSelection();
464 finishChange(cur, false);
470 //lyxerr << "handle LFUN_RIGHT[SEL]:\n" << cur << endl;
471 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
472 if (isRTL(cur.paragraph()))
476 if (sl == cur.top()) {
478 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
485 //lyxerr << "handle LFUN_LEFT[SEL]:\n" << cur << endl;
486 cur.selHandle(cmd.action == LFUN_LEFTSEL);
487 if (isRTL(cur.paragraph()))
491 if (sl == cur.top()) {
493 cmd = FuncRequest(LFUN_FINISHED_LEFT);
500 //lyxerr << "handle LFUN_UP[SEL]:\n" << cur << endl;
501 cur.selHandle(cmd.action == LFUN_UPSEL);
503 if (sl == cur.top()) {
505 cmd = FuncRequest(LFUN_FINISHED_UP);
512 //lyxerr << "handle LFUN_DOWN[SEL]:\n" << cur << endl;
513 cur.selHandle(cmd.action == LFUN_DOWNSEL);
515 if (sl == cur.top()) {
517 cmd = FuncRequest(LFUN_FINISHED_DOWN);
521 case LFUN_UP_PARAGRAPHSEL:
522 if (!cur.selection())
524 cursorUpParagraph(cur);
525 finishChange(cur, true);
528 case LFUN_DOWN_PARAGRAPHSEL:
529 if (!cur.selection())
531 cursorDownParagraph(cur);
532 finishChange(cur, true);
536 if (!cur.selection())
539 finishChange(cur, true);
543 if (!cur.selection())
546 finishChange(cur, true);
550 if (!cur.selection())
553 finishChange(cur, true);
557 if (!cur.selection())
560 finishChange(cur, true);
563 case LFUN_WORDRIGHTSEL:
564 if (!cur.selection())
566 if (isRTL(cur.paragraph()))
567 cursorLeftOneWord(cur);
569 cursorRightOneWord(cur);
570 finishChange(cur, true);
573 case LFUN_WORDLEFTSEL:
574 if (!cur.selection())
576 if (isRTL(cur.paragraph()))
577 cursorRightOneWord(cur);
579 cursorLeftOneWord(cur);
580 finishChange(cur, true);
584 selectWord(cur, lyx::WHOLE_WORD);
585 finishChange(cur, true);
589 case LFUN_UP_PARAGRAPH:
592 cur.clearSelection();
593 cursorUpParagraph(cur);
594 finishChange(cur, false);
597 case LFUN_DOWN_PARAGRAPH:
600 cur.clearSelection();
601 cursorDownParagraph(cur);
602 finishChange(cur, false);
608 cur.clearSelection();
609 finishChange(cur, false);
610 if (cur.par() == 0 && cur.textRow().pos() == 0) {
612 cmd = FuncRequest(LFUN_FINISHED_UP);
621 cur.clearSelection();
622 finishChange(cur, false);
623 if (cur.par() == cur.lastpar()
624 && cur.textRow().endpos() == cur.lastpos()) {
626 cmd = FuncRequest(LFUN_FINISHED_DOWN);
634 cur.clearSelection();
636 finishChange(cur, false);
641 cur.clearSelection();
643 finishChange(cur, false);
646 case LFUN_BREAKLINE: {
647 // Not allowed by LaTeX (labels or empty par)
648 if (cur.pos() > cur.paragraph().beginOfBody()) {
649 lyx::cap::replaceSelection(cur);
650 cur.insert(new InsetNewline);
651 moveCursor(cur, false);
657 if (!cur.selection()) {
660 // It is possible to make it a lot faster still
661 // just comment out the line below...
663 cutSelection(cur, true, false);
665 moveCursor(cur, false);
668 case LFUN_DELETE_SKIP:
669 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
670 if (!cur.selection()) {
671 if (cur.pos() == cur.lastpos()) {
678 cutSelection(cur, true, false);
684 if (!cur.selection()) {
685 if (bv->owner()->getIntl().getTransManager().backspace()) {
688 // It is possible to make it a lot faster still
689 // just comment out the line below...
692 cutSelection(cur, true, false);
697 case LFUN_BACKSPACE_SKIP:
698 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
699 if (!cur.selection()) {
703 //CursorSlice cur = cursor();
707 cutSelection(cur, true, false);
711 case LFUN_BREAKPARAGRAPH:
712 lyx::cap::replaceSelection(cur);
713 breakParagraph(cur, 0);
718 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
719 lyx::cap::replaceSelection(cur);
720 breakParagraph(cur, 1);
725 case LFUN_BREAKPARAGRAPH_SKIP: {
726 // When at the beginning of a paragraph, remove
727 // indentation. Otherwise, do the same as LFUN_BREAKPARAGRAPH.
728 lyx::cap::replaceSelection(cur);
730 cur.paragraph().params().labelWidthString(string());
732 breakParagraph(cur, 0);
738 case LFUN_PARAGRAPH_SPACING: {
739 Paragraph & par = cur.paragraph();
740 Spacing::Space cur_spacing = par.params().spacing().getSpace();
741 float cur_value = 1.0;
742 if (cur_spacing == Spacing::Other)
743 cur_value = par.params().spacing().getValue();
745 istringstream is(cmd.argument);
748 Spacing::Space new_spacing = cur_spacing;
749 float new_value = cur_value;
751 lyxerr << "Missing argument to `paragraph-spacing'"
753 } else if (tmp == "single") {
754 new_spacing = Spacing::Single;
755 } else if (tmp == "onehalf") {
756 new_spacing = Spacing::Onehalf;
757 } else if (tmp == "double") {
758 new_spacing = Spacing::Double;
759 } else if (tmp == "other") {
760 new_spacing = Spacing::Other;
763 lyxerr << "new_value = " << tmpval << endl;
766 } else if (tmp == "default") {
767 new_spacing = Spacing::Default;
769 lyxerr << _("Unknown spacing argument: ")
770 << cmd.argument << endl;
772 if (cur_spacing != new_spacing || cur_value != new_value) {
773 par.params().spacing(Spacing(new_spacing, new_value));
779 case LFUN_INSET_APPLY: {
780 string const name = cmd.getArg(0);
781 InsetBase * inset = bv->owner()->getDialogs().getOpenInset(name);
783 FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument);
784 inset->dispatch(cur, fr);
786 FuncRequest fr(LFUN_INSET_INSERT, cmd.argument);
792 case LFUN_INSET_INSERT: {
794 InsetBase * inset = createInset(bv, cmd);
796 insertInset(cur, inset);
800 case LFUN_INSET_SETTINGS:
801 if (cur.inset().asUpdatableInset())
802 cur.inset().asUpdatableInset()->showInsetDialog(bv);
805 case LFUN_NEXT_INSET_TOGGLE: {
806 InsetBase * inset = cur.nextInset();
808 cur.clearSelection();
809 FuncRequest fr = cmd;
810 fr.action = LFUN_INSET_TOGGLE;
811 inset->dispatch(cur, fr);
816 case LFUN_KEYMAP_TOGGLE:
817 cur.clearSelection();
821 case LFUN_SPACE_INSERT:
822 if (cur.paragraph().layout()->free_spacing)
823 insertChar(cur, ' ');
825 doInsertInset(cur, this, cmd, false, false);
826 moveCursor(cur, false);
829 case LFUN_HYPHENATION:
830 specialChar(cur, InsetSpecialChar::HYPHENATION);
833 case LFUN_LIGATURE_BREAK:
834 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
838 specialChar(cur, InsetSpecialChar::LDOTS);
841 case LFUN_END_OF_SENTENCE:
842 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
845 case LFUN_MENU_SEPARATOR:
846 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
849 case LFUN_UPCASE_WORD:
850 changeCase(cur, LyXText::text_uppercase);
853 case LFUN_LOWCASE_WORD:
854 changeCase(cur, LyXText::text_lowercase);
857 case LFUN_CAPITALIZE_WORD:
858 changeCase(cur, LyXText::text_capitalization);
861 case LFUN_TRANSPOSE_CHARS:
867 cur.message(_("Paste"));
868 lyx::cap::replaceSelection(cur);
870 #warning FIXME Check if the arg is in the domain of available selections.
872 if (isStrUnsignedInt(cmd.argument))
873 pasteSelection(cur, strToUnsignedInt(cmd.argument));
875 pasteSelection(cur, 0);
876 cur.clearSelection(); // bug 393
882 cutSelection(cur, true, true);
883 cur.message(_("Cut"));
888 cur.message(_("Copy"));
892 cur.message(tostr(cursorX(cur.top())) + ' '
893 + tostr(cursorY(cur.top())));
899 istringstream is(cmd.argument);
902 lyxerr << "SETXY: Could not parse coordinates in '"
903 << cmd.argument << std::endl;
905 setCursorFromCoordinates(cur, x, y);
910 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
912 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
919 cur.message(cur.paragraph().layout()->name());
923 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
924 << cmd.argument << endl;
926 // This is not the good solution to the empty argument
927 // problem, but it will hopefully suffice for 1.2.0.
928 // The correct solution would be to augument the
929 // function list/array with information about what
930 // functions needs arguments and their type.
931 if (cmd.argument.empty()) {
932 cur.errorMessage(_("LyX function 'layout' needs an argument."));
936 // Derive layout number from given argument (string)
937 // and current buffer's textclass (number)
938 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
939 bool hasLayout = tclass.hasLayout(cmd.argument);
940 string layout = cmd.argument;
942 // If the entry is obsolete, use the new one instead.
944 string const & obs = tclass[layout]->obsoleted_by();
950 cur.errorMessage(string(N_("Layout ")) + cmd.argument +
955 bool change_layout = (current_layout != layout);
957 if (!change_layout && cur.selection() &&
958 cur.selBegin().par() != cur.selEnd().par())
960 par_type spit = cur.selBegin().par();
961 par_type epit = cur.selEnd().par() + 1;
962 while (spit != epit) {
963 if (pars_[spit].layout()->name() != current_layout) {
964 change_layout = true;
972 current_layout = layout;
973 setLayout(cur, layout);
974 bv->owner()->setLayout(layout);
980 case LFUN_PASTESELECTION: {
981 cur.clearSelection();
982 string const clip = bv->getClipboard();
984 if (cmd.argument == "paragraph")
985 insertStringAsParagraphs(cur, clip);
987 insertStringAsLines(cur, clip);
993 gotoInset(cur, InsetBase::ERROR_CODE, false);
997 gotoInset(cur, InsetBase::NOTE_CODE, false);
1000 case LFUN_REFERENCE_GOTO: {
1001 vector<InsetOld_code> tmp;
1002 tmp.push_back(InsetBase::LABEL_CODE);
1003 tmp.push_back(InsetBase::REF_CODE);
1004 gotoInset(cur, tmp, true);
1009 lyx::cap::replaceSelection(cur);
1010 Paragraph & par = cur.paragraph();
1011 lyx::pos_type pos = cur.pos();
1015 else if (cur.prevInset() && cur.prevInset()->isSpace())
1018 c = par.getChar(pos - 1);
1020 LyXLayout_ptr const & style = par.layout();
1022 BufferParams const & bufparams = bv->buffer()->params();
1023 if (!style->pass_thru
1024 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1025 string arg = cmd.argument;
1026 if (arg == "single")
1027 cur.insert(new InsetQuotes(c,
1028 bufparams.quotes_language,
1029 InsetQuotes::SingleQ));
1030 else if (arg == "double")
1031 cur.insert(new InsetQuotes(c,
1032 bufparams.quotes_language,
1033 InsetQuotes::DoubleQ));
1035 cur.insert(new InsetQuotes(c, bufparams));
1038 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1042 case LFUN_DATE_INSERT: {
1043 lyx::cap::replaceSelection(cur);
1044 time_t now_time_t = time(NULL);
1045 struct tm * now_tm = localtime(&now_time_t);
1046 setlocale(LC_TIME, "");
1048 if (!cmd.argument.empty())
1051 arg = lyxrc.date_insert_format;
1053 int const datetmp_len =
1054 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1056 for (int i = 0; i < datetmp_len; i++)
1057 insertChar(cur, datetmp[i]);
1060 moveCursor(cur, false);
1064 case LFUN_MOUSE_TRIPLE:
1065 if (cmd.button() == mouse_button::button1) {
1066 selection_possible = true;
1071 bv->haveSelection(cur.selection());
1075 case LFUN_MOUSE_DOUBLE:
1076 if (cmd.button() == mouse_button::button1) {
1077 selection_possible = true;
1078 selectWord(cur, lyx::WHOLE_WORD_STRICT);
1079 bv->haveSelection(cur.selection());
1083 case LFUN_MOUSE_MOTION: {
1084 // Only use motion with button 1
1085 //if (cmd.button() != mouse_button::button1)
1087 // We want to use only motion events for which
1088 // the button press event was on the drawing area too.
1089 if (!selection_possible) {
1090 lyxerr[Debug::ACTION] << "BufferView::Pimpl::"
1091 "dispatch: no selection possible\n";
1092 lyxerr << "BufferView::Pimpl::dispatch: no selection possible\n";
1096 // ignore motions deeper nested than the real anchor
1097 LCursor & bvcur = cur.bv().cursor();
1098 if (bvcur.selection() && bvcur.anchor_.size() < cur.size())
1101 CursorSlice old = cur.top();
1102 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1104 // This is to allow jumping over large insets
1105 // FIXME: shouldn't be top-text-specific
1106 if (isMainText() && cur.top() == old) {
1107 if (cmd.y - bv->top_y() >= bv->workHeight())
1109 else if (cmd.y - bv->top_y() < 0)
1113 // don't set anchor_
1114 bv->cursor().setCursor(cur, true);
1115 lyxerr << "MOTION: " << bv->cursor() << endl;
1119 // Single-click on work area
1120 case LFUN_MOUSE_PRESS: {
1121 // Right click on a footnote flag opens float menu
1122 if (cmd.button() == mouse_button::button3) {
1123 cur.clearSelection();
1124 selection_possible = false;
1128 // Middle button press pastes if we have a selection
1129 // We do this here as if the selection was inside an inset
1130 // it could get cleared on the unlocking of the inset so
1131 // we have to check this first
1132 bool paste_internally = false;
1133 if (cmd.button() == mouse_button::button2 && cur.selection()) {
1134 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1135 paste_internally = true;
1138 selection_possible = true;
1140 // Clear the selection
1141 cur.clearSelection();
1143 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1146 cur.x_target() = cursorX(cur.top());
1148 // Has the cursor just left the inset?
1149 if (bv->cursor().inMathed() && !cur.inMathed())
1150 bv->cursor().inset().notifyCursorLeaves(bv->cursor());
1155 // Don't allow selection after a big jump.
1156 if (bv->fitCursor())
1157 selection_possible = false;
1159 // Insert primary selection with middle mouse
1160 // if there is a local selection in the current buffer,
1162 if (cmd.button() == mouse_button::button2) {
1163 if (paste_internally)
1164 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1166 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1167 selection_possible = false;
1173 case LFUN_MOUSE_RELEASE: {
1174 selection_possible = false;
1176 if (cmd.button() == mouse_button::button2)
1180 if (cmd.button() == mouse_button::button1)
1181 bv->haveSelection(cur.selection());
1184 bv->owner()->updateMenubar();
1185 bv->owner()->updateToolbars();
1189 case LFUN_SELFINSERT: {
1190 if (cmd.argument.empty())
1193 // Automatically delete the currently selected
1194 // text and replace it with what is being
1195 // typed in now. Depends on lyxrc settings
1196 // "auto_region_delete", which defaults to
1199 if (lyxrc.auto_region_delete) {
1200 if (cur.selection())
1201 cutSelection(cur, false, false);
1202 bv->haveSelection(false);
1205 cur.clearSelection();
1206 LyXFont const old_font = real_current_font;
1208 string::const_iterator cit = cmd.argument.begin();
1209 string::const_iterator end = cmd.argument.end();
1210 for (; cit != end; ++cit)
1211 bv->owner()->getIntl().getTransManager().
1212 TranslateAndInsert(*cit, this);
1215 moveCursor(cur, false);
1217 // real_current_font.number can change so we need to
1218 // update the minibuffer
1219 if (old_font != real_current_font)
1220 bv->updateScrollbar();
1225 InsetCommandParams p("url");
1226 string const data = InsetCommandMailer::params2string("url", p);
1227 bv->owner()->getDialogs().show("url", data, 0);
1231 case LFUN_HTMLURL: {
1232 InsetCommandParams p("htmlurl");
1233 string const data = InsetCommandMailer::params2string("url", p);
1234 bv->owner()->getDialogs().show("url", data, 0);
1238 case LFUN_INSERT_LABEL: {
1239 // Try to generate a valid label
1240 string const contents = cmd.argument.empty() ?
1241 cur.getPossibleLabel() : cmd.argument;
1243 InsetCommandParams p("label", contents);
1244 string const data = InsetCommandMailer::params2string("label", p);
1246 if (cmd.argument.empty()) {
1247 bv->owner()->getDialogs().show("label", data, 0);
1249 FuncRequest fr(LFUN_INSET_INSERT, data);
1257 case LFUN_INSET_LIST:
1258 case LFUN_INSET_THEOREM:
1259 case LFUN_INSET_CAPTION:
1261 case LFUN_INSERT_NOTE:
1262 case LFUN_INSERT_CHARSTYLE:
1263 case LFUN_INSERT_BOX:
1264 case LFUN_INSERT_BRANCH:
1265 case LFUN_INSERT_BIBITEM:
1266 case LFUN_INSET_ERT:
1267 case LFUN_INSET_FLOAT:
1268 case LFUN_INSET_FOOTNOTE:
1269 case LFUN_INSET_MARGINAL:
1270 case LFUN_INSET_OPTARG:
1271 case LFUN_INSET_WIDE_FLOAT:
1272 case LFUN_INSET_WRAP:
1273 case LFUN_TABULAR_INSERT:
1274 case LFUN_ENVIRONMENT_INSERT:
1275 // Open the inset, and move the current selection
1277 doInsertInset(cur, this, cmd, true, true);
1280 case LFUN_INDEX_INSERT:
1281 // Just open the inset
1282 doInsertInset(cur, this, cmd, true, false);
1285 case LFUN_INDEX_PRINT:
1286 case LFUN_TOC_INSERT:
1288 case LFUN_INSERT_LINE:
1289 case LFUN_INSERT_PAGEBREAK:
1291 doInsertInset(cur, this, cmd, false, false);
1294 case LFUN_DEPTH_MIN:
1295 changeDepth(cur, DEC_DEPTH);
1298 case LFUN_DEPTH_PLUS:
1299 changeDepth(cur, INC_DEPTH);
1302 case LFUN_MATH_DISPLAY:
1303 mathDispatch(cur, cmd, true);
1306 case LFUN_MATH_IMPORT_SELECTION:
1307 case LFUN_MATH_MODE:
1308 mathDispatch(cur, cmd, false);
1311 case LFUN_MATH_MACRO:
1312 if (cmd.argument.empty())
1313 cur.errorMessage(N_("Missing argument"));
1315 string s = cmd.argument;
1316 string const s1 = token(s, ' ', 1);
1317 int const nargs = s1.empty() ? 0 : atoi(s1);
1318 string const s2 = token(s, ' ', 2);
1319 string const type = s2.empty() ? "newcommand" : s2;
1320 cur.insert(new MathMacroTemplate(token(s, ' ', 0), nargs, s2));
1321 //cur.nextInset()->edit(cur, true);
1325 case LFUN_INSERT_MATH:
1326 case LFUN_INSERT_MATRIX:
1327 case LFUN_MATH_DELIM: {
1328 cur.insert(new MathHullInset);
1329 cur.dispatch(FuncRequest(LFUN_RIGHT));
1330 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
1336 LyXFont font(LyXFont::ALL_IGNORE);
1337 font.setEmph(LyXFont::TOGGLE);
1338 toggleAndShow(cur, this, font);
1343 LyXFont font(LyXFont::ALL_IGNORE);
1344 font.setSeries(LyXFont::BOLD_SERIES);
1345 toggleAndShow(cur, this, font);
1350 LyXFont font(LyXFont::ALL_IGNORE);
1351 font.setNoun(LyXFont::TOGGLE);
1352 toggleAndShow(cur, this, font);
1357 LyXFont font(LyXFont::ALL_IGNORE);
1358 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
1359 toggleAndShow(cur, this, font);
1364 LyXFont font(LyXFont::ALL_IGNORE);
1365 font.setFamily(LyXFont::SANS_FAMILY);
1366 toggleAndShow(cur, this, font);
1371 LyXFont font(LyXFont::ALL_IGNORE);
1372 font.setFamily(LyXFont::ROMAN_FAMILY);
1373 toggleAndShow(cur, this, font);
1377 case LFUN_DEFAULT: {
1378 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
1379 toggleAndShow(cur, this, font);
1383 case LFUN_UNDERLINE: {
1384 LyXFont font(LyXFont::ALL_IGNORE);
1385 font.setUnderbar(LyXFont::TOGGLE);
1386 toggleAndShow(cur, this, font);
1390 case LFUN_FONT_SIZE: {
1391 LyXFont font(LyXFont::ALL_IGNORE);
1392 font.setLyXSize(cmd.argument);
1393 toggleAndShow(cur, this, font);
1397 case LFUN_LANGUAGE: {
1398 Language const * lang = languages.getLanguage(cmd.argument);
1401 LyXFont font(LyXFont::ALL_IGNORE);
1402 font.setLanguage(lang);
1403 toggleAndShow(cur, this, font);
1408 case LFUN_FREEFONT_APPLY:
1409 toggleAndShow(cur, this, freefont, toggleall);
1410 cur.message(_("Character set"));
1413 // Set the freefont using the contents of \param data dispatched from
1414 // the frontends and apply it at the current cursor location.
1415 case LFUN_FREEFONT_UPDATE: {
1418 if (bv_funcs::string2font(cmd.argument, font, toggle)) {
1421 toggleAndShow(cur, this, freefont, toggleall);
1422 cur.message(_("Character set"));
1427 case LFUN_FINISHED_LEFT:
1428 lyxerr << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1431 case LFUN_FINISHED_RIGHT:
1432 lyxerr << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1436 case LFUN_FINISHED_UP:
1437 lyxerr << "handle LFUN_FINISHED_UP:\n" << cur << endl;
1441 case LFUN_FINISHED_DOWN:
1442 lyxerr << "handle LFUN_FINISHED_DOWN:\n" << cur << endl;
1446 case LFUN_LAYOUT_PARAGRAPH: {
1448 params2string(cur.paragraph(), data);
1449 data = "show\n" + data;
1450 bv->owner()->getDialogs().show("paragraph", data);
1454 case LFUN_PARAGRAPH_UPDATE: {
1455 if (!bv->owner()->getDialogs().visible("paragraph"))
1458 params2string(cur.paragraph(), data);
1460 // Will the paragraph accept changes from the dialog?
1461 InsetBase & inset = cur.inset();
1462 bool const accept = !inset.forceDefaultParagraphs(&inset);
1464 data = "update " + tostr(accept) + '\n' + data;
1465 bv->owner()->getDialogs().update("paragraph", data);
1470 case LFUN_CIRCUMFLEX:
1480 case LFUN_SPECIAL_CARON:
1483 case LFUN_HUNG_UMLAUT:
1486 bv->owner()->getLyXFunc().handleKeyFunc(cmd.action);
1487 if (!cmd.argument.empty())
1488 bv->owner()->getIntl().getTransManager()
1489 .TranslateAndInsert(cmd.argument[0], this);
1492 case LFUN_FLOAT_LIST: {
1493 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1494 if (tclass.floats().typeExist(cmd.argument)) {
1495 // not quite sure if we want this...
1497 cur.clearSelection();
1498 breakParagraph(cur);
1500 if (cur.lastpos() != 0) {
1502 breakParagraph(cur);
1505 setLayout(cur, tclass.defaultLayoutName());
1506 setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1507 insertInset(cur, new InsetFloatList(cmd.argument));
1509 lyxerr << "Non-existent float type: "
1510 << cmd.argument << endl;
1515 case LFUN_ACCEPT_CHANGE: {
1520 case LFUN_REJECT_CHANGE: {
1525 case LFUN_THESAURUS_ENTRY: {
1526 string arg = cmd.argument;
1528 arg = cur.selectionAsString(false);
1530 if (arg.size() > 100 || arg.empty()) {
1531 // Get word or selection
1532 selectWordWhenUnderCursor(cur, lyx::WHOLE_WORD);
1533 arg = cur.selectionAsString(false);
1536 bv->owner()->getDialogs().show("thesaurus", arg);
1540 case LFUN_PARAGRAPH_APPLY: {
1541 // Given data, an encoding of the ParagraphParameters
1542 // generated in the Paragraph dialog, this function sets
1543 // the current paragraph appropriately.
1544 istringstream is(cmd.argument);
1547 ParagraphParameters params;
1552 params.labelWidthString(),
1554 cur.message(_("Paragraph layout set"));
1558 case LFUN_INSET_DIALOG_SHOW: {
1559 InsetBase * inset = cur.nextInset();
1561 FuncRequest fr(LFUN_INSET_DIALOG_SHOW);
1562 inset->dispatch(cur, fr);
1568 if (cur.selection()) {
1569 cur.selection() = false;
1572 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1581 // avoid to update when navigating
1583 && &sl.inset() == &cur.inset()
1584 && sl.idx() == cur.idx()
1586 && cur.selection() == false)
1591 bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
1592 FuncStatus & flag) const
1594 BOOST_ASSERT(cur.text() == this);
1595 LyXFont const & font = real_current_font;
1598 switch (cmd.action) {
1600 case LFUN_DEPTH_MIN:
1601 enable = changeDepthAllowed(cur, DEC_DEPTH);
1604 case LFUN_DEPTH_PLUS:
1605 enable = changeDepthAllowed(cur, INC_DEPTH);
1608 case LFUN_INSET_OPTARG:
1609 enable = cur.paragraph().layout()->optionalargs;
1613 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1617 // the functions which insert insets
1618 InsetOld::Code code = InsetOld::NO_CODE;
1619 switch (cmd.action) {
1620 case LFUN_DIALOG_SHOW_NEW_INSET:
1621 if (cmd.argument == "bibitem")
1622 code = InsetOld::BIBITEM_CODE;
1623 else if (cmd.argument == "bibtex")
1624 code = InsetOld::BIBTEX_CODE;
1625 else if (cmd.argument == "box")
1626 code = InsetOld::BOX_CODE;
1627 else if (cmd.argument == "branch")
1628 code = InsetOld::BRANCH_CODE;
1629 else if (cmd.argument == "citation")
1630 code = InsetOld::CITE_CODE;
1631 else if (cmd.argument == "ert")
1632 code = InsetOld::ERT_CODE;
1633 else if (cmd.argument == "external")
1634 code = InsetOld::EXTERNAL_CODE;
1635 else if (cmd.argument == "float")
1636 code = InsetOld::FLOAT_CODE;
1637 else if (cmd.argument == "graphics")
1638 code = InsetOld::GRAPHICS_CODE;
1639 else if (cmd.argument == "include")
1640 code = InsetOld::INCLUDE_CODE;
1641 else if (cmd.argument == "index")
1642 code = InsetOld::INDEX_CODE;
1643 else if (cmd.argument == "label")
1644 code = InsetOld::LABEL_CODE;
1645 else if (cmd.argument == "note")
1646 code = InsetOld::NOTE_CODE;
1647 else if (cmd.argument == "ref")
1648 code = InsetOld::REF_CODE;
1649 else if (cmd.argument == "toc")
1650 code = InsetOld::TOC_CODE;
1651 else if (cmd.argument == "url")
1652 code = InsetOld::URL_CODE;
1653 else if (cmd.argument == "vspace")
1654 code = InsetOld::VSPACE_CODE;
1655 else if (cmd.argument == "wrap")
1656 code = InsetOld::WRAP_CODE;
1659 case LFUN_INSET_ERT:
1660 code = InsetOld::ERT_CODE;
1662 case LFUN_INSET_FOOTNOTE:
1663 code = InsetOld::FOOT_CODE;
1665 case LFUN_TABULAR_INSERT:
1666 code = InsetOld::TABULAR_CODE;
1668 case LFUN_INSET_MARGINAL:
1669 code = InsetOld::MARGIN_CODE;
1671 case LFUN_INSET_FLOAT:
1672 case LFUN_INSET_WIDE_FLOAT:
1673 code = InsetOld::FLOAT_CODE;
1675 case LFUN_INSET_WRAP:
1676 code = InsetOld::WRAP_CODE;
1678 case LFUN_FLOAT_LIST:
1679 code = InsetOld::FLOAT_LIST_CODE;
1682 case LFUN_INSET_LIST:
1683 code = InsetOld::LIST_CODE;
1685 case LFUN_INSET_THEOREM:
1686 code = InsetOld::THEOREM_CODE;
1689 case LFUN_INSET_CAPTION:
1690 code = InsetOld::CAPTION_CODE;
1692 case LFUN_INSERT_NOTE:
1693 code = InsetOld::NOTE_CODE;
1695 case LFUN_INSERT_CHARSTYLE:
1696 code = InsetOld::CHARSTYLE_CODE;
1697 if (buf->params().getLyXTextClass().charstyles().empty())
1700 case LFUN_INSERT_BOX:
1701 code = InsetOld::BOX_CODE;
1703 case LFUN_INSERT_BRANCH:
1704 code = InsetOld::BRANCH_CODE;
1705 if (buf->params().branchlist().empty())
1708 case LFUN_INSERT_LABEL:
1709 code = InsetOld::LABEL_CODE;
1711 case LFUN_INSET_OPTARG:
1712 code = InsetOld::OPTARG_CODE;
1714 case LFUN_ENVIRONMENT_INSERT:
1715 code = InsetOld::BOX_CODE;
1717 case LFUN_INDEX_INSERT:
1718 code = InsetOld::INDEX_CODE;
1720 case LFUN_INDEX_PRINT:
1721 code = InsetOld::INDEX_PRINT_CODE;
1723 case LFUN_TOC_INSERT:
1724 code = InsetOld::TOC_CODE;
1728 code = InsetOld::URL_CODE;
1731 // always allow this, since we will inset a raw quote
1732 // if an inset is not allowed.
1734 case LFUN_HYPHENATION:
1735 case LFUN_LIGATURE_BREAK:
1737 case LFUN_MENU_SEPARATOR:
1739 case LFUN_END_OF_SENTENCE:
1740 code = InsetOld::SPECIALCHAR_CODE;
1742 case LFUN_SPACE_INSERT:
1743 // slight hack: we know this is allowed in math mode
1745 code = InsetOld::SPACE_CODE;
1747 case LFUN_INSET_DIALOG_SHOW: {
1748 InsetBase * inset = cur.nextInset();
1751 code = inset->lyxCode();
1752 if (!(code == InsetOld::INCLUDE_CODE
1753 || code == InsetOld::BIBTEX_CODE
1754 || code == InsetOld::FLOAT_LIST_CODE
1755 || code == InsetOld::TOC_CODE))
1764 if (code != InsetOld::NO_CODE
1765 && (cur.empty() || !cur.inset().insetAllowed(code)))
1770 case LFUN_DIALOG_SHOW_NEW_INSET:
1771 case LFUN_INSET_ERT:
1772 case LFUN_INSERT_BOX:
1773 case LFUN_INSERT_BRANCH:
1774 case LFUN_ENVIRONMENT_INSERT:
1775 case LFUN_INDEX_INSERT:
1776 case LFUN_INDEX_PRINT:
1777 case LFUN_TOC_INSERT:
1781 case LFUN_HYPHENATION:
1782 case LFUN_LIGATURE_BREAK:
1784 case LFUN_MENU_SEPARATOR:
1786 case LFUN_END_OF_SENTENCE:
1787 case LFUN_SPACE_INSERT:
1788 case LFUN_INSET_DIALOG_SHOW:
1792 flag.setOnOff(font.emph() == LyXFont::ON);
1796 flag.setOnOff(font.noun() == LyXFont::ON);
1800 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1804 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1808 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1812 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1815 case LFUN_DELETE_WORD_FORWARD:
1816 case LFUN_DELETE_WORD_BACKWARD:
1817 case LFUN_DELETE_LINE_FORWARD:
1818 case LFUN_WORDRIGHT:
1821 case LFUN_BEGINNINGBUF:
1830 case LFUN_UP_PARAGRAPHSEL:
1831 case LFUN_DOWN_PARAGRAPHSEL:
1836 case LFUN_WORDRIGHTSEL:
1837 case LFUN_WORDLEFTSEL:
1839 case LFUN_UP_PARAGRAPH:
1840 case LFUN_DOWN_PARAGRAPH:
1845 case LFUN_BREAKLINE:
1847 case LFUN_DELETE_SKIP:
1848 case LFUN_BACKSPACE:
1849 case LFUN_BACKSPACE_SKIP:
1850 case LFUN_BREAKPARAGRAPH:
1851 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1852 case LFUN_BREAKPARAGRAPH_SKIP:
1853 case LFUN_PARAGRAPH_SPACING:
1854 case LFUN_INSET_APPLY:
1855 case LFUN_INSET_INSERT:
1856 case LFUN_NEXT_INSET_TOGGLE:
1857 case LFUN_UPCASE_WORD:
1858 case LFUN_LOWCASE_WORD:
1859 case LFUN_CAPITALIZE_WORD:
1860 case LFUN_TRANSPOSE_CHARS:
1867 case LFUN_GETLAYOUT:
1869 case LFUN_PASTESELECTION:
1870 case LFUN_GOTOERROR:
1872 case LFUN_REFERENCE_GOTO:
1873 case LFUN_DATE_INSERT:
1874 case LFUN_SELFINSERT:
1875 case LFUN_INSERT_LABEL:
1876 case LFUN_INSERT_NOTE:
1877 case LFUN_INSERT_CHARSTYLE:
1878 case LFUN_INSERT_BIBITEM:
1879 case LFUN_INSET_FLOAT:
1880 case LFUN_INSET_FOOTNOTE:
1881 case LFUN_INSET_MARGINAL:
1882 case LFUN_INSET_WIDE_FLOAT:
1883 case LFUN_INSET_WRAP:
1884 case LFUN_TABULAR_INSERT:
1885 case LFUN_INSERT_LINE:
1886 case LFUN_INSERT_PAGEBREAK:
1887 case LFUN_MATH_DISPLAY:
1888 case LFUN_MATH_IMPORT_SELECTION:
1889 case LFUN_MATH_MODE:
1890 case LFUN_MATH_MACRO:
1891 case LFUN_INSERT_MATH:
1892 case LFUN_INSERT_MATRIX:
1893 case LFUN_MATH_DELIM:
1895 case LFUN_UNDERLINE:
1896 case LFUN_FONT_SIZE:
1898 case LFUN_FREEFONT_APPLY:
1899 case LFUN_FREEFONT_UPDATE:
1900 case LFUN_LAYOUT_PARAGRAPH:
1901 case LFUN_PARAGRAPH_UPDATE:
1903 case LFUN_CIRCUMFLEX:
1913 case LFUN_SPECIAL_CARON:
1916 case LFUN_HUNG_UMLAUT:
1919 case LFUN_FLOAT_LIST:
1920 case LFUN_ACCEPT_CHANGE:
1921 case LFUN_REJECT_CHANGE:
1922 case LFUN_THESAURUS_ENTRY:
1923 case LFUN_PARAGRAPH_APPLY:
1925 case LFUN_KEYMAP_TOGGLE:
1926 // these are handled in our dispatch()
1934 flag.enabled(enable);