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:
459 cur.clearSelection();
461 finishChange(cur, false);
466 cur.clearSelection();
468 finishChange(cur, false);
474 //lyxerr << "handle LFUN_RIGHT[SEL]:\n" << cur << endl;
475 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
476 if (isRTL(cur.paragraph()))
480 if (sl == cur.top()) {
482 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
489 //lyxerr << "handle LFUN_LEFT[SEL]:\n" << cur << endl;
490 cur.selHandle(cmd.action == LFUN_LEFTSEL);
491 if (isRTL(cur.paragraph()))
495 if (sl == cur.top()) {
497 cmd = FuncRequest(LFUN_FINISHED_LEFT);
504 //lyxerr << "handle LFUN_UP[SEL]:\n" << cur << endl;
505 cur.selHandle(cmd.action == LFUN_UPSEL);
507 if (sl == cur.top()) {
509 cmd = FuncRequest(LFUN_FINISHED_UP);
516 //lyxerr << "handle LFUN_DOWN[SEL]:\n" << cur << endl;
517 cur.selHandle(cmd.action == LFUN_DOWNSEL);
519 if (sl == cur.top()) {
521 cmd = FuncRequest(LFUN_FINISHED_DOWN);
525 case LFUN_UP_PARAGRAPHSEL:
526 if (!cur.selection())
528 cursorUpParagraph(cur);
529 finishChange(cur, true);
532 case LFUN_DOWN_PARAGRAPHSEL:
533 if (!cur.selection())
535 cursorDownParagraph(cur);
536 finishChange(cur, true);
540 if (!cur.selection())
543 finishChange(cur, true);
547 if (!cur.selection())
550 finishChange(cur, true);
554 if (!cur.selection())
557 finishChange(cur, true);
561 if (!cur.selection())
564 finishChange(cur, true);
567 case LFUN_WORDRIGHTSEL:
568 if (!cur.selection())
570 if (isRTL(cur.paragraph()))
571 cursorLeftOneWord(cur);
573 cursorRightOneWord(cur);
574 finishChange(cur, true);
577 case LFUN_WORDLEFTSEL:
578 if (!cur.selection())
580 if (isRTL(cur.paragraph()))
581 cursorRightOneWord(cur);
583 cursorLeftOneWord(cur);
584 finishChange(cur, true);
588 selectWord(cur, lyx::WHOLE_WORD);
589 finishChange(cur, true);
593 case LFUN_UP_PARAGRAPH:
596 cur.clearSelection();
597 cursorUpParagraph(cur);
598 finishChange(cur, false);
601 case LFUN_DOWN_PARAGRAPH:
604 cur.clearSelection();
605 cursorDownParagraph(cur);
606 finishChange(cur, false);
612 cur.clearSelection();
613 finishChange(cur, false);
614 if (cur.par() == 0 && cur.textRow().pos() == 0) {
616 cmd = FuncRequest(LFUN_FINISHED_UP);
625 cur.clearSelection();
626 finishChange(cur, false);
627 if (cur.par() == cur.lastpar()
628 && cur.textRow().endpos() == cur.lastpos()) {
630 cmd = FuncRequest(LFUN_FINISHED_DOWN);
638 cur.clearSelection();
640 finishChange(cur, false);
645 cur.clearSelection();
647 finishChange(cur, false);
650 case LFUN_BREAKLINE: {
651 // Not allowed by LaTeX (labels or empty par)
652 if (cur.pos() > cur.paragraph().beginOfBody()) {
653 lyx::cap::replaceSelection(cur);
654 cur.insert(new InsetNewline);
655 moveCursor(cur, false);
661 if (!cur.selection()) {
664 // It is possible to make it a lot faster still
665 // just comment out the line below...
667 cutSelection(cur, true, false);
669 moveCursor(cur, false);
672 case LFUN_DELETE_SKIP:
673 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
674 if (!cur.selection()) {
675 if (cur.pos() == cur.lastpos()) {
682 cutSelection(cur, true, false);
688 if (!cur.selection()) {
689 if (bv->owner()->getIntl().getTransManager().backspace()) {
692 // It is possible to make it a lot faster still
693 // just comment out the line below...
696 cutSelection(cur, true, false);
701 case LFUN_BACKSPACE_SKIP:
702 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
703 if (!cur.selection()) {
707 //CursorSlice cur = cursor();
711 cutSelection(cur, true, false);
715 case LFUN_BREAKPARAGRAPH:
716 lyx::cap::replaceSelection(cur);
717 breakParagraph(cur, 0);
722 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
723 lyx::cap::replaceSelection(cur);
724 breakParagraph(cur, 1);
729 case LFUN_BREAKPARAGRAPH_SKIP: {
730 // When at the beginning of a paragraph, remove
731 // indentation. Otherwise, do the same as LFUN_BREAKPARAGRAPH.
732 lyx::cap::replaceSelection(cur);
734 cur.paragraph().params().labelWidthString(string());
736 breakParagraph(cur, 0);
742 case LFUN_PARAGRAPH_SPACING: {
743 Paragraph & par = cur.paragraph();
744 Spacing::Space cur_spacing = par.params().spacing().getSpace();
745 float cur_value = 1.0;
746 if (cur_spacing == Spacing::Other)
747 cur_value = par.params().spacing().getValue();
749 istringstream is(cmd.argument);
752 Spacing::Space new_spacing = cur_spacing;
753 float new_value = cur_value;
755 lyxerr << "Missing argument to `paragraph-spacing'"
757 } else if (tmp == "single") {
758 new_spacing = Spacing::Single;
759 } else if (tmp == "onehalf") {
760 new_spacing = Spacing::Onehalf;
761 } else if (tmp == "double") {
762 new_spacing = Spacing::Double;
763 } else if (tmp == "other") {
764 new_spacing = Spacing::Other;
767 lyxerr << "new_value = " << tmpval << endl;
770 } else if (tmp == "default") {
771 new_spacing = Spacing::Default;
773 lyxerr << _("Unknown spacing argument: ")
774 << cmd.argument << endl;
776 if (cur_spacing != new_spacing || cur_value != new_value) {
777 par.params().spacing(Spacing(new_spacing, new_value));
783 case LFUN_INSET_APPLY: {
784 string const name = cmd.getArg(0);
785 InsetBase * inset = bv->owner()->getDialogs().getOpenInset(name);
787 FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument);
788 inset->dispatch(cur, fr);
790 FuncRequest fr(LFUN_INSET_INSERT, cmd.argument);
796 case LFUN_INSET_INSERT: {
798 InsetBase * inset = createInset(bv, cmd);
800 insertInset(cur, inset);
806 case LFUN_INSET_SETTINGS:
807 if (cur.inset().asUpdatableInset())
808 cur.inset().asUpdatableInset()->showInsetDialog(bv);
811 case LFUN_NEXT_INSET_TOGGLE: {
812 InsetBase * inset = cur.nextInset();
814 cur.clearSelection();
815 FuncRequest fr = cmd;
816 fr.action = LFUN_INSET_TOGGLE;
817 inset->dispatch(cur, fr);
822 case LFUN_KEYMAP_TOGGLE:
823 cur.clearSelection();
827 case LFUN_SPACE_INSERT:
828 if (cur.paragraph().layout()->free_spacing)
829 insertChar(cur, ' ');
831 doInsertInset(cur, this, cmd, false, false);
832 moveCursor(cur, false);
835 case LFUN_HYPHENATION:
836 specialChar(cur, InsetSpecialChar::HYPHENATION);
839 case LFUN_LIGATURE_BREAK:
840 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
844 specialChar(cur, InsetSpecialChar::LDOTS);
847 case LFUN_END_OF_SENTENCE:
848 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
851 case LFUN_MENU_SEPARATOR:
852 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
855 case LFUN_UPCASE_WORD:
856 changeCase(cur, LyXText::text_uppercase);
859 case LFUN_LOWCASE_WORD:
860 changeCase(cur, LyXText::text_lowercase);
863 case LFUN_CAPITALIZE_WORD:
864 changeCase(cur, LyXText::text_capitalization);
867 case LFUN_TRANSPOSE_CHARS:
873 cur.message(_("Paste"));
874 lyx::cap::replaceSelection(cur);
876 #warning FIXME Check if the arg is in the domain of available selections.
878 if (isStrUnsignedInt(cmd.argument))
879 pasteSelection(cur, strToUnsignedInt(cmd.argument));
881 pasteSelection(cur, 0);
882 cur.clearSelection(); // bug 393
888 cutSelection(cur, true, true);
889 cur.message(_("Cut"));
894 cur.message(_("Copy"));
898 cur.message(tostr(cursorX(cur.top())) + ' '
899 + tostr(cursorY(cur.top())));
905 istringstream is(cmd.argument);
908 lyxerr << "SETXY: Could not parse coordinates in '"
909 << cmd.argument << std::endl;
911 setCursorFromCoordinates(cur, x, y);
916 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
918 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
925 cur.message(cur.paragraph().layout()->name());
929 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
930 << cmd.argument << endl;
932 // This is not the good solution to the empty argument
933 // problem, but it will hopefully suffice for 1.2.0.
934 // The correct solution would be to augument the
935 // function list/array with information about what
936 // functions needs arguments and their type.
937 if (cmd.argument.empty()) {
938 cur.errorMessage(_("LyX function 'layout' needs an argument."));
942 // Derive layout number from given argument (string)
943 // and current buffer's textclass (number)
944 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
945 bool hasLayout = tclass.hasLayout(cmd.argument);
946 string layout = cmd.argument;
948 // If the entry is obsolete, use the new one instead.
950 string const & obs = tclass[layout]->obsoleted_by();
956 cur.errorMessage(string(N_("Layout ")) + cmd.argument +
961 bool change_layout = (current_layout != layout);
963 if (!change_layout && cur.selection() &&
964 cur.selBegin().par() != cur.selEnd().par())
966 par_type spit = cur.selBegin().par();
967 par_type epit = cur.selEnd().par() + 1;
968 while (spit != epit) {
969 if (pars_[spit].layout()->name() != current_layout) {
970 change_layout = true;
978 current_layout = layout;
979 setLayout(cur, layout);
980 bv->owner()->setLayout(layout);
986 case LFUN_PASTESELECTION: {
987 cur.clearSelection();
988 string const clip = bv->getClipboard();
990 if (cmd.argument == "paragraph")
991 insertStringAsParagraphs(cur, clip);
993 insertStringAsLines(cur, clip);
999 gotoInset(cur, InsetBase::ERROR_CODE, false);
1003 gotoInset(cur, InsetBase::NOTE_CODE, false);
1006 case LFUN_REFERENCE_GOTO: {
1007 vector<InsetOld_code> tmp;
1008 tmp.push_back(InsetBase::LABEL_CODE);
1009 tmp.push_back(InsetBase::REF_CODE);
1010 gotoInset(cur, tmp, true);
1015 lyx::cap::replaceSelection(cur);
1016 Paragraph & par = cur.paragraph();
1017 lyx::pos_type pos = cur.pos();
1021 else if (cur.prevInset() && cur.prevInset()->isSpace())
1024 c = par.getChar(pos - 1);
1026 LyXLayout_ptr const & style = par.layout();
1028 BufferParams const & bufparams = bv->buffer()->params();
1029 if (!style->pass_thru
1030 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1031 string arg = cmd.argument;
1032 if (arg == "single")
1033 cur.insert(new InsetQuotes(c,
1034 bufparams.quotes_language,
1035 InsetQuotes::SingleQ));
1036 else if (arg == "double")
1037 cur.insert(new InsetQuotes(c,
1038 bufparams.quotes_language,
1039 InsetQuotes::DoubleQ));
1041 cur.insert(new InsetQuotes(c, bufparams));
1045 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1049 case LFUN_DATE_INSERT: {
1050 lyx::cap::replaceSelection(cur);
1051 time_t now_time_t = time(NULL);
1052 struct tm * now_tm = localtime(&now_time_t);
1053 setlocale(LC_TIME, "");
1055 if (!cmd.argument.empty())
1058 arg = lyxrc.date_insert_format;
1060 int const datetmp_len =
1061 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1063 for (int i = 0; i < datetmp_len; i++)
1064 insertChar(cur, datetmp[i]);
1067 moveCursor(cur, false);
1071 case LFUN_MOUSE_TRIPLE:
1072 if (cmd.button() == mouse_button::button1) {
1073 selection_possible = true;
1078 bv->haveSelection(cur.selection());
1082 case LFUN_MOUSE_DOUBLE:
1083 if (cmd.button() == mouse_button::button1) {
1084 selection_possible = true;
1085 selectWord(cur, lyx::WHOLE_WORD_STRICT);
1086 bv->haveSelection(cur.selection());
1090 case LFUN_MOUSE_MOTION: {
1091 // Only use motion with button 1
1092 //if (cmd.button() != mouse_button::button1)
1094 // We want to use only motion events for which
1095 // the button press event was on the drawing area too.
1096 if (!selection_possible) {
1097 lyxerr[Debug::ACTION] << "BufferView::Pimpl::"
1098 "dispatch: no selection possible\n";
1099 lyxerr << "BufferView::Pimpl::dispatch: no selection possible\n";
1103 // ignore motions deeper nested than the real anchor
1104 LCursor & bvcur = cur.bv().cursor();
1105 if (bvcur.selection() && bvcur.anchor_.size() < cur.size())
1108 CursorSlice old = cur.top();
1109 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1111 // This is to allow jumping over large insets
1112 // FIXME: shouldn't be top-text-specific
1113 if (isMainText() && cur.top() == old) {
1114 if (cmd.y - bv->top_y() >= bv->workHeight())
1116 else if (cmd.y - bv->top_y() < 0)
1120 // don't set anchor_
1121 bv->cursor().setCursor(cur, true);
1122 lyxerr << "MOTION: " << bv->cursor() << endl;
1126 // Single-click on work area
1127 case LFUN_MOUSE_PRESS: {
1128 // Right click on a footnote flag opens float menu
1129 if (cmd.button() == mouse_button::button3) {
1130 cur.clearSelection();
1131 selection_possible = false;
1135 // Middle button press pastes if we have a selection
1136 // We do this here as if the selection was inside an inset
1137 // it could get cleared on the unlocking of the inset so
1138 // we have to check this first
1139 bool paste_internally = false;
1140 if (cmd.button() == mouse_button::button2 && cur.selection()) {
1141 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1142 paste_internally = true;
1145 selection_possible = true;
1147 // Clear the selection
1148 cur.clearSelection();
1150 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1153 cur.x_target() = cursorX(cur.top());
1155 // Has the cursor just left the inset?
1156 if (bv->cursor().inMathed() && !cur.inMathed())
1157 bv->cursor().inset().notifyCursorLeaves(bv->cursor());
1162 // Don't allow selection after a big jump.
1163 if (bv->fitCursor())
1164 selection_possible = false;
1166 // Insert primary selection with middle mouse
1167 // if there is a local selection in the current buffer,
1169 if (cmd.button() == mouse_button::button2) {
1170 if (paste_internally)
1171 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1173 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1174 selection_possible = false;
1180 case LFUN_MOUSE_RELEASE: {
1181 selection_possible = false;
1183 if (cmd.button() == mouse_button::button2)
1187 if (cmd.button() == mouse_button::button1)
1188 bv->haveSelection(cur.selection());
1191 bv->owner()->updateMenubar();
1192 bv->owner()->updateToolbars();
1196 case LFUN_SELFINSERT: {
1197 if (cmd.argument.empty())
1200 // Automatically delete the currently selected
1201 // text and replace it with what is being
1202 // typed in now. Depends on lyxrc settings
1203 // "auto_region_delete", which defaults to
1206 if (lyxrc.auto_region_delete) {
1207 if (cur.selection())
1208 cutSelection(cur, false, false);
1209 bv->haveSelection(false);
1212 cur.clearSelection();
1213 LyXFont const old_font = real_current_font;
1215 string::const_iterator cit = cmd.argument.begin();
1216 string::const_iterator end = cmd.argument.end();
1217 for (; cit != end; ++cit)
1218 bv->owner()->getIntl().getTransManager().
1219 TranslateAndInsert(*cit, this);
1222 moveCursor(cur, false);
1224 // real_current_font.number can change so we need to
1225 // update the minibuffer
1226 if (old_font != real_current_font)
1227 bv->updateScrollbar();
1232 InsetCommandParams p("url");
1233 string const data = InsetCommandMailer::params2string("url", p);
1234 bv->owner()->getDialogs().show("url", data, 0);
1238 case LFUN_HTMLURL: {
1239 InsetCommandParams p("htmlurl");
1240 string const data = InsetCommandMailer::params2string("url", p);
1241 bv->owner()->getDialogs().show("url", data, 0);
1245 case LFUN_INSERT_LABEL: {
1246 // Try to generate a valid label
1247 string const contents = cmd.argument.empty() ?
1248 cur.getPossibleLabel() : cmd.argument;
1250 InsetCommandParams p("label", contents);
1251 string const data = InsetCommandMailer::params2string("label", p);
1253 if (cmd.argument.empty()) {
1254 bv->owner()->getDialogs().show("label", data, 0);
1256 FuncRequest fr(LFUN_INSET_INSERT, data);
1264 case LFUN_INSET_LIST:
1265 case LFUN_INSET_THEOREM:
1266 case LFUN_INSET_CAPTION:
1268 case LFUN_INSERT_NOTE:
1269 case LFUN_INSERT_CHARSTYLE:
1270 case LFUN_INSERT_BOX:
1271 case LFUN_INSERT_BRANCH:
1272 case LFUN_INSERT_BIBITEM:
1273 case LFUN_INSET_ERT:
1274 case LFUN_INSET_FLOAT:
1275 case LFUN_INSET_FOOTNOTE:
1276 case LFUN_INSET_MARGINAL:
1277 case LFUN_INSET_OPTARG:
1278 case LFUN_INSET_WIDE_FLOAT:
1279 case LFUN_INSET_WRAP:
1280 case LFUN_TABULAR_INSERT:
1281 case LFUN_ENVIRONMENT_INSERT:
1282 // Open the inset, and move the current selection
1284 doInsertInset(cur, this, cmd, true, true);
1288 case LFUN_INDEX_INSERT:
1289 // Just open the inset
1290 doInsertInset(cur, this, cmd, true, false);
1294 case LFUN_INDEX_PRINT:
1295 case LFUN_TOC_INSERT:
1297 case LFUN_INSERT_LINE:
1298 case LFUN_INSERT_PAGEBREAK:
1300 doInsertInset(cur, this, cmd, false, false);
1304 case LFUN_DEPTH_MIN:
1305 changeDepth(cur, DEC_DEPTH);
1308 case LFUN_DEPTH_PLUS:
1309 changeDepth(cur, INC_DEPTH);
1312 case LFUN_MATH_DISPLAY:
1313 mathDispatch(cur, cmd, true);
1316 case LFUN_MATH_IMPORT_SELECTION:
1317 case LFUN_MATH_MODE:
1318 mathDispatch(cur, cmd, false);
1321 case LFUN_MATH_MACRO:
1322 if (cmd.argument.empty())
1323 cur.errorMessage(N_("Missing argument"));
1325 string s = cmd.argument;
1326 string const s1 = token(s, ' ', 1);
1327 int const nargs = s1.empty() ? 0 : atoi(s1);
1328 string const s2 = token(s, ' ', 2);
1329 string const type = s2.empty() ? "newcommand" : s2;
1330 cur.insert(new MathMacroTemplate(token(s, ' ', 0), nargs, s2));
1331 //cur.nextInset()->edit(cur, true);
1335 case LFUN_INSERT_MATH:
1336 case LFUN_INSERT_MATRIX:
1337 case LFUN_MATH_DELIM: {
1338 cur.insert(new MathHullInset);
1339 cur.dispatch(FuncRequest(LFUN_RIGHT));
1340 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
1346 LyXFont font(LyXFont::ALL_IGNORE);
1347 font.setEmph(LyXFont::TOGGLE);
1348 toggleAndShow(cur, this, font);
1353 LyXFont font(LyXFont::ALL_IGNORE);
1354 font.setSeries(LyXFont::BOLD_SERIES);
1355 toggleAndShow(cur, this, font);
1360 LyXFont font(LyXFont::ALL_IGNORE);
1361 font.setNoun(LyXFont::TOGGLE);
1362 toggleAndShow(cur, this, font);
1367 LyXFont font(LyXFont::ALL_IGNORE);
1368 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
1369 toggleAndShow(cur, this, font);
1374 LyXFont font(LyXFont::ALL_IGNORE);
1375 font.setFamily(LyXFont::SANS_FAMILY);
1376 toggleAndShow(cur, this, font);
1381 LyXFont font(LyXFont::ALL_IGNORE);
1382 font.setFamily(LyXFont::ROMAN_FAMILY);
1383 toggleAndShow(cur, this, font);
1387 case LFUN_DEFAULT: {
1388 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
1389 toggleAndShow(cur, this, font);
1393 case LFUN_UNDERLINE: {
1394 LyXFont font(LyXFont::ALL_IGNORE);
1395 font.setUnderbar(LyXFont::TOGGLE);
1396 toggleAndShow(cur, this, font);
1400 case LFUN_FONT_SIZE: {
1401 LyXFont font(LyXFont::ALL_IGNORE);
1402 font.setLyXSize(cmd.argument);
1403 toggleAndShow(cur, this, font);
1407 case LFUN_LANGUAGE: {
1408 Language const * lang = languages.getLanguage(cmd.argument);
1411 LyXFont font(LyXFont::ALL_IGNORE);
1412 font.setLanguage(lang);
1413 toggleAndShow(cur, this, font);
1418 case LFUN_FREEFONT_APPLY:
1419 toggleAndShow(cur, this, freefont, toggleall);
1420 cur.message(_("Character set"));
1423 // Set the freefont using the contents of \param data dispatched from
1424 // the frontends and apply it at the current cursor location.
1425 case LFUN_FREEFONT_UPDATE: {
1428 if (bv_funcs::string2font(cmd.argument, font, toggle)) {
1431 toggleAndShow(cur, this, freefont, toggleall);
1432 cur.message(_("Character set"));
1437 case LFUN_FINISHED_LEFT:
1438 lyxerr << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1441 case LFUN_FINISHED_RIGHT:
1442 lyxerr << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1446 case LFUN_FINISHED_UP:
1447 lyxerr << "handle LFUN_FINISHED_UP:\n" << cur << endl;
1451 case LFUN_FINISHED_DOWN:
1452 lyxerr << "handle LFUN_FINISHED_DOWN:\n" << cur << endl;
1456 case LFUN_LAYOUT_PARAGRAPH: {
1458 params2string(cur.paragraph(), data);
1459 data = "show\n" + data;
1460 bv->owner()->getDialogs().show("paragraph", data);
1464 case LFUN_PARAGRAPH_UPDATE: {
1465 if (!bv->owner()->getDialogs().visible("paragraph"))
1468 params2string(cur.paragraph(), data);
1470 // Will the paragraph accept changes from the dialog?
1471 InsetBase & inset = cur.inset();
1472 bool const accept = !inset.forceDefaultParagraphs(&inset);
1474 data = "update " + tostr(accept) + '\n' + data;
1475 bv->owner()->getDialogs().update("paragraph", data);
1480 case LFUN_CIRCUMFLEX:
1490 case LFUN_SPECIAL_CARON:
1493 case LFUN_HUNG_UMLAUT:
1496 bv->owner()->getLyXFunc().handleKeyFunc(cmd.action);
1497 if (!cmd.argument.empty())
1498 bv->owner()->getIntl().getTransManager()
1499 .TranslateAndInsert(cmd.argument[0], this);
1502 case LFUN_FLOAT_LIST: {
1503 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1504 if (tclass.floats().typeExist(cmd.argument)) {
1505 // not quite sure if we want this...
1507 cur.clearSelection();
1508 breakParagraph(cur);
1510 if (cur.lastpos() != 0) {
1512 breakParagraph(cur);
1515 setLayout(cur, tclass.defaultLayoutName());
1516 setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1517 insertInset(cur, new InsetFloatList(cmd.argument));
1519 lyxerr << "Non-existent float type: "
1520 << cmd.argument << endl;
1525 case LFUN_ACCEPT_CHANGE: {
1530 case LFUN_REJECT_CHANGE: {
1535 case LFUN_THESAURUS_ENTRY: {
1536 string arg = cmd.argument;
1538 arg = cur.selectionAsString(false);
1540 if (arg.size() > 100 || arg.empty()) {
1541 // Get word or selection
1542 selectWordWhenUnderCursor(cur, lyx::WHOLE_WORD);
1543 arg = cur.selectionAsString(false);
1546 bv->owner()->getDialogs().show("thesaurus", arg);
1550 case LFUN_PARAGRAPH_APPLY: {
1551 // Given data, an encoding of the ParagraphParameters
1552 // generated in the Paragraph dialog, this function sets
1553 // the current paragraph appropriately.
1554 istringstream is(cmd.argument);
1557 ParagraphParameters params;
1562 params.labelWidthString(),
1564 cur.message(_("Paragraph layout set"));
1568 case LFUN_INSET_DIALOG_SHOW: {
1569 InsetBase * inset = cur.nextInset();
1571 FuncRequest fr(LFUN_INSET_DIALOG_SHOW);
1572 inset->dispatch(cur, fr);
1578 if (cur.selection()) {
1579 cur.selection() = false;
1582 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1591 // avoid to update when navigating
1593 && &sl.inset() == &cur.inset()
1594 && sl.idx() == cur.idx()
1596 && cur.selection() == false)
1601 bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
1602 FuncStatus & flag) const
1604 BOOST_ASSERT(cur.text() == this);
1605 LyXFont const & font = real_current_font;
1608 switch (cmd.action) {
1610 case LFUN_DEPTH_MIN:
1611 enable = changeDepthAllowed(cur, DEC_DEPTH);
1614 case LFUN_DEPTH_PLUS:
1615 enable = changeDepthAllowed(cur, INC_DEPTH);
1618 case LFUN_INSET_OPTARG:
1619 enable = cur.paragraph().layout()->optionalargs;
1623 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1627 // the functions which insert insets
1628 InsetOld::Code code = InsetOld::NO_CODE;
1629 switch (cmd.action) {
1630 case LFUN_DIALOG_SHOW_NEW_INSET:
1631 if (cmd.argument == "bibitem")
1632 code = InsetOld::BIBITEM_CODE;
1633 else if (cmd.argument == "bibtex")
1634 code = InsetOld::BIBTEX_CODE;
1635 else if (cmd.argument == "box")
1636 code = InsetOld::BOX_CODE;
1637 else if (cmd.argument == "branch")
1638 code = InsetOld::BRANCH_CODE;
1639 else if (cmd.argument == "citation")
1640 code = InsetOld::CITE_CODE;
1641 else if (cmd.argument == "ert")
1642 code = InsetOld::ERT_CODE;
1643 else if (cmd.argument == "external")
1644 code = InsetOld::EXTERNAL_CODE;
1645 else if (cmd.argument == "float")
1646 code = InsetOld::FLOAT_CODE;
1647 else if (cmd.argument == "graphics")
1648 code = InsetOld::GRAPHICS_CODE;
1649 else if (cmd.argument == "include")
1650 code = InsetOld::INCLUDE_CODE;
1651 else if (cmd.argument == "index")
1652 code = InsetOld::INDEX_CODE;
1653 else if (cmd.argument == "label")
1654 code = InsetOld::LABEL_CODE;
1655 else if (cmd.argument == "note")
1656 code = InsetOld::NOTE_CODE;
1657 else if (cmd.argument == "ref")
1658 code = InsetOld::REF_CODE;
1659 else if (cmd.argument == "toc")
1660 code = InsetOld::TOC_CODE;
1661 else if (cmd.argument == "url")
1662 code = InsetOld::URL_CODE;
1663 else if (cmd.argument == "vspace")
1664 code = InsetOld::VSPACE_CODE;
1665 else if (cmd.argument == "wrap")
1666 code = InsetOld::WRAP_CODE;
1669 case LFUN_INSET_ERT:
1670 code = InsetOld::ERT_CODE;
1672 case LFUN_INSET_FOOTNOTE:
1673 code = InsetOld::FOOT_CODE;
1675 case LFUN_TABULAR_INSERT:
1676 code = InsetOld::TABULAR_CODE;
1678 case LFUN_INSET_MARGINAL:
1679 code = InsetOld::MARGIN_CODE;
1681 case LFUN_INSET_FLOAT:
1682 case LFUN_INSET_WIDE_FLOAT:
1683 code = InsetOld::FLOAT_CODE;
1685 case LFUN_INSET_WRAP:
1686 code = InsetOld::WRAP_CODE;
1688 case LFUN_FLOAT_LIST:
1689 code = InsetOld::FLOAT_LIST_CODE;
1692 case LFUN_INSET_LIST:
1693 code = InsetOld::LIST_CODE;
1695 case LFUN_INSET_THEOREM:
1696 code = InsetOld::THEOREM_CODE;
1699 case LFUN_INSET_CAPTION:
1700 code = InsetOld::CAPTION_CODE;
1702 case LFUN_INSERT_NOTE:
1703 code = InsetOld::NOTE_CODE;
1705 case LFUN_INSERT_CHARSTYLE:
1706 code = InsetOld::CHARSTYLE_CODE;
1707 if (buf->params().getLyXTextClass().charstyles().empty())
1710 case LFUN_INSERT_BOX:
1711 code = InsetOld::BOX_CODE;
1713 case LFUN_INSERT_BRANCH:
1714 code = InsetOld::BRANCH_CODE;
1715 if (buf->params().branchlist().empty())
1718 case LFUN_INSERT_LABEL:
1719 code = InsetOld::LABEL_CODE;
1721 case LFUN_INSET_OPTARG:
1722 code = InsetOld::OPTARG_CODE;
1724 case LFUN_ENVIRONMENT_INSERT:
1725 code = InsetOld::BOX_CODE;
1727 case LFUN_INDEX_INSERT:
1728 code = InsetOld::INDEX_CODE;
1730 case LFUN_INDEX_PRINT:
1731 code = InsetOld::INDEX_PRINT_CODE;
1733 case LFUN_TOC_INSERT:
1734 code = InsetOld::TOC_CODE;
1738 code = InsetOld::URL_CODE;
1741 // always allow this, since we will inset a raw quote
1742 // if an inset is not allowed.
1744 case LFUN_HYPHENATION:
1745 case LFUN_LIGATURE_BREAK:
1747 case LFUN_MENU_SEPARATOR:
1749 case LFUN_END_OF_SENTENCE:
1750 code = InsetOld::SPECIALCHAR_CODE;
1752 case LFUN_SPACE_INSERT:
1753 // slight hack: we know this is allowed in math mode
1755 code = InsetOld::SPACE_CODE;
1757 case LFUN_INSET_DIALOG_SHOW: {
1758 InsetBase * inset = cur.nextInset();
1761 code = inset->lyxCode();
1762 if (!(code == InsetOld::INCLUDE_CODE
1763 || code == InsetOld::BIBTEX_CODE
1764 || code == InsetOld::FLOAT_LIST_CODE
1765 || code == InsetOld::TOC_CODE))
1774 if (code != InsetOld::NO_CODE
1775 && (cur.empty() || !cur.inset().insetAllowed(code)))
1780 case LFUN_DIALOG_SHOW_NEW_INSET:
1781 case LFUN_INSET_ERT:
1782 case LFUN_INSERT_BOX:
1783 case LFUN_INSERT_BRANCH:
1784 case LFUN_ENVIRONMENT_INSERT:
1785 case LFUN_INDEX_INSERT:
1786 case LFUN_INDEX_PRINT:
1787 case LFUN_TOC_INSERT:
1791 case LFUN_HYPHENATION:
1792 case LFUN_LIGATURE_BREAK:
1794 case LFUN_MENU_SEPARATOR:
1796 case LFUN_END_OF_SENTENCE:
1797 case LFUN_SPACE_INSERT:
1798 case LFUN_INSET_DIALOG_SHOW:
1802 flag.setOnOff(font.emph() == LyXFont::ON);
1806 flag.setOnOff(font.noun() == LyXFont::ON);
1810 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1814 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1818 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1822 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1825 case LFUN_DELETE_WORD_FORWARD:
1826 case LFUN_DELETE_WORD_BACKWARD:
1827 case LFUN_DELETE_LINE_FORWARD:
1828 case LFUN_WORDRIGHT:
1831 case LFUN_BEGINNINGBUF:
1840 case LFUN_UP_PARAGRAPHSEL:
1841 case LFUN_DOWN_PARAGRAPHSEL:
1846 case LFUN_WORDRIGHTSEL:
1847 case LFUN_WORDLEFTSEL:
1849 case LFUN_UP_PARAGRAPH:
1850 case LFUN_DOWN_PARAGRAPH:
1855 case LFUN_BREAKLINE:
1857 case LFUN_DELETE_SKIP:
1858 case LFUN_BACKSPACE:
1859 case LFUN_BACKSPACE_SKIP:
1860 case LFUN_BREAKPARAGRAPH:
1861 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1862 case LFUN_BREAKPARAGRAPH_SKIP:
1863 case LFUN_PARAGRAPH_SPACING:
1864 case LFUN_INSET_APPLY:
1865 case LFUN_INSET_INSERT:
1866 case LFUN_NEXT_INSET_TOGGLE:
1867 case LFUN_UPCASE_WORD:
1868 case LFUN_LOWCASE_WORD:
1869 case LFUN_CAPITALIZE_WORD:
1870 case LFUN_TRANSPOSE_CHARS:
1877 case LFUN_GETLAYOUT:
1879 case LFUN_PASTESELECTION:
1880 case LFUN_GOTOERROR:
1882 case LFUN_REFERENCE_GOTO:
1883 case LFUN_DATE_INSERT:
1884 case LFUN_SELFINSERT:
1885 case LFUN_INSERT_LABEL:
1886 case LFUN_INSERT_NOTE:
1887 case LFUN_INSERT_CHARSTYLE:
1888 case LFUN_INSERT_BIBITEM:
1889 case LFUN_INSET_FLOAT:
1890 case LFUN_INSET_FOOTNOTE:
1891 case LFUN_INSET_MARGINAL:
1892 case LFUN_INSET_WIDE_FLOAT:
1893 case LFUN_INSET_WRAP:
1894 case LFUN_TABULAR_INSERT:
1895 case LFUN_INSERT_LINE:
1896 case LFUN_INSERT_PAGEBREAK:
1897 case LFUN_MATH_DISPLAY:
1898 case LFUN_MATH_IMPORT_SELECTION:
1899 case LFUN_MATH_MODE:
1900 case LFUN_MATH_MACRO:
1901 case LFUN_INSERT_MATH:
1902 case LFUN_INSERT_MATRIX:
1903 case LFUN_MATH_DELIM:
1905 case LFUN_UNDERLINE:
1906 case LFUN_FONT_SIZE:
1908 case LFUN_FREEFONT_APPLY:
1909 case LFUN_FREEFONT_UPDATE:
1910 case LFUN_LAYOUT_PARAGRAPH:
1911 case LFUN_PARAGRAPH_UPDATE:
1913 case LFUN_CIRCUMFLEX:
1923 case LFUN_SPECIAL_CARON:
1926 case LFUN_HUNG_UMLAUT:
1929 case LFUN_FLOAT_LIST:
1930 case LFUN_ACCEPT_CHANGE:
1931 case LFUN_REJECT_CHANGE:
1932 case LFUN_THESAURUS_ENTRY:
1933 case LFUN_PARAGRAPH_APPLY:
1935 case LFUN_KEYMAP_TOGGLE:
1936 // these are handled in our dispatch()
1944 flag.enabled(enable);