3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Alfredo Braunstein
9 * \author Angus Leeming
11 * \author André Pönitz
13 * Full author contact details are available in file CREDITS.
20 #include "FloatList.h"
21 #include "FuncStatus.h"
23 #include "bufferparams.h"
24 #include "BufferView.h"
26 #include "coordcache.h"
27 #include "CutAndPaste.h"
29 #include "dispatchresult.h"
31 #include "funcrequest.h"
35 #include "LyXAction.h"
40 #include "paragraph.h"
41 #include "paragraph_funcs.h"
42 #include "ParagraphParameters.h"
46 #include "frontends/Dialogs.h"
47 #include "frontends/LyXView.h"
49 #include "insets/insetcommand.h"
50 #include "insets/insetfloatlist.h"
51 #include "insets/insetnewline.h"
52 #include "insets/insetquotes.h"
53 #include "insets/insetspecialchar.h"
54 #include "insets/insettext.h"
56 #include "support/lstrings.h"
57 #include "support/lyxlib.h"
58 #include "support/convert.h"
60 #include "mathed/math_hullinset.h"
61 #include "mathed/math_macrotemplate.h"
68 using lyx::cap::copySelection;
69 using lyx::cap::cutSelection;
70 using lyx::cap::pasteSelection;
71 using lyx::cap::replaceSelection;
73 using lyx::support::isStrUnsignedInt;
74 using lyx::support::token;
79 using std::istringstream;
83 extern string current_layout;
89 LyXFont freefont(LyXFont::ALL_IGNORE);
90 bool toggleall = false;
93 void toggleAndShow(LCursor & cur, LyXText * text,
94 LyXFont const & font, bool toggleall = true)
96 text->toggleFree(cur, font, toggleall);
98 if (font.language() != ignore_language ||
99 font.number() != LyXFont::IGNORE) {
100 Paragraph & par = cur.paragraph();
101 text->bidi.computeTables(par, cur.buffer(), cur.textRow());
102 if (cur.boundary() !=
103 text->bidi.isBoundary(cur.buffer(), par,
105 text->real_current_font))
106 text->setCursor(cur, cur.pit(), cur.pos(),
107 false, !cur.boundary());
112 void moveCursor(LCursor & cur, bool selecting)
114 if (selecting || cur.mark())
116 if (!cur.selection())
117 cur.bv().haveSelection(false);
118 cur.bv().switchKeyMap();
122 void finishChange(LCursor & cur, bool selecting)
125 moveCursor(cur, selecting);
129 void mathDispatch(LCursor & cur, FuncRequest const & cmd, bool display)
132 string sel = cur.selectionAsString(false);
133 lyxerr << "selection is: '" << sel << "'" << endl;
136 const int old_pos = cur.pos();
137 cur.insert(new MathHullInset);
138 BOOST_ASSERT(old_pos == cur.pos());
139 cur.nextInset()->edit(cur, true);
140 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
141 // don't do that also for LFUN_MATH_MODE
142 // unless you want end up with always changing
143 // to mathrm when opening an inlined inset --
144 // I really hate "LyXfunc overloading"...
146 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
147 cur.dispatch(FuncRequest(LFUN_INSERT_MATH, cmd.argument));
149 // create a macro if we see "\\newcommand"
150 // somewhere, and an ordinary formula
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))
184 bool LyXText::gotoNextInset(LCursor & cur,
185 vector<InsetBase_code> const & codes, string const & contents)
187 BOOST_ASSERT(this == cur.text());
188 pit_type end = paragraphs().size();
189 pit_type pit = cur.pit();
190 pos_type pos = cur.pos();
194 if (pos + 1 < pars_[pit].size()) {
201 } while (pit != end &&
202 !(pars_[pit].isInset(pos) &&
203 (inset = pars_[pit].getInset(pos)) != 0 &&
204 find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
206 static_cast<InsetCommand *>(pars_[pit].getInset(pos))->getContents()
212 setCursor(cur, pit, pos, false);
217 void LyXText::gotoInset(LCursor & cur,
218 vector<InsetBase_code> const & codes, bool same_content)
220 cur.clearSelection();
224 && cur.pos() < cur.lastpos()
225 && cur.paragraph().isInset(cur.pos())) {
226 InsetBase const * inset = cur.paragraph().getInset(cur.pos());
227 if (find(codes.begin(), codes.end(), inset->lyxCode())
229 contents = static_cast<InsetCommand const *>(inset)->getContents();
232 if (!gotoNextInset(cur, codes, contents)) {
233 if (cur.pos() || cur.pit() != 0) {
234 CursorSlice tmp = cur.top();
237 if (!gotoNextInset(cur, codes, contents)) {
239 cur.message(_("No more insets"));
242 cur.message(_("No more insets"));
249 void LyXText::gotoInset(LCursor & cur, InsetBase_code code, bool same_content)
251 gotoInset(cur, vector<InsetBase_code>(1, code), same_content);
255 bool LyXText::cursorPrevious(LCursor & cur)
257 pos_type cpos = cur.pos();
258 lyx::pit_type cpar = cur.pit();
260 int x = cur.x_target();
262 setCursorFromCoordinates(cur, x, 0);
263 bool updated = cursorUp(cur);
265 if (cpar == cur.pit() && cpos == cur.pos()) {
266 // we have a row which is taller than the workarea. The
267 // simplest solution is to move to the previous row instead.
268 updated |= cursorUp(cur);
271 cur.bv().updateScrollbar();
277 bool LyXText::cursorNext(LCursor & cur)
279 pos_type cpos = cur.pos();
280 lyx::pit_type cpar = cur.pit();
282 int x = cur.x_target();
283 setCursorFromCoordinates(cur, x, cur.bv().workHeight() - 1);
284 bool updated = cursorDown(cur);
286 if (cpar == cur.pit() && cpos == cur.pos()) {
287 // we have a row which is taller than the workarea. The
288 // simplest solution is to move to the next row instead.
289 updated |= cursorDown(cur);
292 cur.bv().updateScrollbar();
300 void specialChar(LCursor & cur, InsetSpecialChar::Kind kind)
302 lyx::cap::replaceSelection(cur);
303 cur.insert(new InsetSpecialChar(kind));
308 void doInsertInset(LCursor & cur, LyXText * text,
309 FuncRequest const & cmd, bool edit, bool pastesel)
311 InsetBase * inset = createInset(&cur.bv(), cmd);
317 if (cur.selection()) {
318 cur.bv().owner()->dispatch(FuncRequest(LFUN_CUT));
321 text->insertInset(cur, inset);
324 inset->edit(cur, true);
326 if (gotsel && pastesel)
327 cur.bv().owner()->dispatch(FuncRequest(LFUN_PASTE));
331 void update(LCursor & cur)
333 //we don't call update(true, false) directly to save a metrics call
334 if (cur.bv().fitCursor())
335 cur.bv().update(false, true);
342 void LyXText::number(LCursor & cur)
344 LyXFont font(LyXFont::ALL_IGNORE);
345 font.setNumber(LyXFont::TOGGLE);
346 toggleAndShow(cur, this, font);
350 bool LyXText::isRTL(Paragraph const & par) const
352 return par.isRightToLeftPar(bv()->buffer()->params());
356 void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
358 lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
359 lyxerr << "*** LyXText::dispatch: cmd: " << cmd << endl;
361 BOOST_ASSERT(cur.text() == this);
362 BufferView * bv = &cur.bv();
363 CursorSlice oldTopSlice = cur.top();
364 bool sel = cur.selection();
365 bool needsUpdate = !lyxaction.funcHasFlag(cmd.action, LyXAction::NoUpdate);
367 switch (cmd.action) {
369 case LFUN_APPENDIX: {
370 Paragraph & par = cur.paragraph();
371 bool start = !par.params().startOfAppendix();
373 // ensure that we have only one start_of_appendix in this document
374 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
375 if (pars_[tmp].params().startOfAppendix()) {
377 pars_[tmp].params().startOfAppendix(false);
383 par.params().startOfAppendix(start);
385 // we can set the refreshing parameters now
390 case LFUN_DELETE_WORD_FORWARD:
391 cur.clearSelection();
392 deleteWordForward(cur);
393 finishChange(cur, false);
396 case LFUN_DELETE_WORD_BACKWARD:
397 cur.clearSelection();
398 deleteWordBackward(cur);
399 finishChange(cur, false);
402 case LFUN_DELETE_LINE_FORWARD:
403 cur.clearSelection();
404 deleteLineForward(cur);
405 finishChange(cur, false);
410 cur.clearSelection();
411 if (isRTL(cur.paragraph()))
412 needsUpdate = cursorLeftOneWord(cur);
414 needsUpdate = cursorRightOneWord(cur);
415 finishChange(cur, false);
420 cur.clearSelection();
421 if (isRTL(cur.paragraph()))
422 needsUpdate = cursorRightOneWord(cur);
424 needsUpdate = cursorLeftOneWord(cur);
425 finishChange(cur, false);
428 case LFUN_BEGINNINGBUF:
429 if (cur.depth() == 1) {
431 cur.clearSelection();
433 finishChange(cur, false);
439 case LFUN_BEGINNINGBUFSEL:
440 if (cur.depth() == 1) {
441 if (!cur.selection())
444 finishChange(cur, true);
451 if (cur.depth() == 1) {
453 cur.clearSelection();
455 finishChange(cur, false);
462 if (cur.depth() == 1) {
463 if (!cur.selection())
466 finishChange(cur, true);
474 lyxerr << BOOST_CURRENT_FUNCTION
475 << " LFUN_RIGHT[SEL]:\n" << cur << endl;
476 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
477 if (isRTL(cur.paragraph()))
478 needsUpdate = cursorLeft(cur);
480 needsUpdate = cursorRight(cur);
481 if (!needsUpdate && oldTopSlice == cur.top()) {
483 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()))
492 needsUpdate = cursorRight(cur);
494 needsUpdate = cursorLeft(cur);
495 if (oldTopSlice == cur.top()) {
497 cmd = FuncRequest(LFUN_FINISHED_LEFT);
504 //lyxerr << "handle LFUN_UP[SEL]:\n" << cur << endl;
505 cur.selHandle(cmd.action == LFUN_UPSEL);
506 needsUpdate = cursorUp(cur);
507 if (oldTopSlice == cur.top()) {
509 cmd = FuncRequest(LFUN_FINISHED_UP);
516 //lyxerr << "handle LFUN_DOWN[SEL]:\n" << cur << endl;
517 cur.selHandle(cmd.action == LFUN_DOWNSEL);
518 needsUpdate = cursorDown(cur);
519 if (oldTopSlice == cur.top()) {
521 cmd = FuncRequest(LFUN_FINISHED_DOWN);
525 case LFUN_UP_PARAGRAPH:
527 cur.clearSelection();
528 needsUpdate = cursorUpParagraph(cur);
529 finishChange(cur, false);
532 case LFUN_UP_PARAGRAPHSEL:
533 if (!cur.selection())
535 cursorUpParagraph(cur);
536 finishChange(cur, true);
539 case LFUN_DOWN_PARAGRAPH:
541 cur.clearSelection();
542 needsUpdate = cursorDownParagraph(cur);
543 finishChange(cur, false);
546 case LFUN_DOWN_PARAGRAPHSEL:
547 if (!cur.selection())
549 cursorDownParagraph(cur);
550 finishChange(cur, true);
555 if (!cur.selection())
558 finishChange(cur, true);
563 if (!cur.selection())
566 finishChange(cur, true);
571 if (!cur.selection())
574 finishChange(cur, true);
579 if (!cur.selection())
582 finishChange(cur, true);
585 case LFUN_WORDRIGHTSEL:
586 if (!cur.selection())
588 if (isRTL(cur.paragraph()))
589 cursorLeftOneWord(cur);
591 cursorRightOneWord(cur);
592 finishChange(cur, true);
595 case LFUN_WORDLEFTSEL:
596 if (!cur.selection())
598 if (isRTL(cur.paragraph()))
599 cursorRightOneWord(cur);
601 cursorLeftOneWord(cur);
602 finishChange(cur, true);
606 selectWord(cur, lyx::WHOLE_WORD);
607 finishChange(cur, true);
614 cur.clearSelection();
615 finishChange(cur, false);
616 if (cur.pit() == 0 && cur.textRow().pos() == 0) {
618 cmd = FuncRequest(LFUN_FINISHED_UP);
620 needsUpdate = cursorPrevious(cur);
627 cur.clearSelection();
628 finishChange(cur, false);
629 if (cur.pit() == cur.lastpit()
630 && cur.textRow().endpos() == cur.lastpos()) {
632 cmd = FuncRequest(LFUN_FINISHED_DOWN);
634 needsUpdate = cursorNext(cur);
640 cur.clearSelection();
642 finishChange(cur, false);
647 cur.clearSelection();
649 finishChange(cur, false);
652 case LFUN_BREAKLINE: {
653 // Not allowed by LaTeX (labels or empty par)
654 if (cur.pos() > cur.paragraph().beginOfBody()) {
655 lyx::cap::replaceSelection(cur);
656 cur.insert(new InsetNewline);
658 moveCursor(cur, false);
664 if (!cur.selection()) {
667 // It is possible to make it a lot faster still
668 // just comment out the line below...
670 cutSelection(cur, true, false);
672 moveCursor(cur, false);
675 case LFUN_DELETE_SKIP:
676 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
677 if (!cur.selection()) {
678 if (cur.pos() == cur.lastpos()) {
685 cutSelection(cur, true, false);
691 if (!cur.selection()) {
692 if (bv->owner()->getIntl().getTransManager().backspace()) {
695 // It is possible to make it a lot faster still
696 // just comment out the line below...
699 cutSelection(cur, true, false);
704 case LFUN_BACKSPACE_SKIP:
705 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
706 if (!cur.selection()) {
710 //CursorSlice cur = cursor();
714 cutSelection(cur, true, false);
718 case LFUN_BREAKPARAGRAPH:
719 lyx::cap::replaceSelection(cur);
720 breakParagraph(cur, 0);
725 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
726 lyx::cap::replaceSelection(cur);
727 breakParagraph(cur, 1);
732 case LFUN_BREAKPARAGRAPH_SKIP: {
733 // When at the beginning of a paragraph, remove
734 // indentation. Otherwise, do the same as LFUN_BREAKPARAGRAPH.
735 lyx::cap::replaceSelection(cur);
737 cur.paragraph().params().labelWidthString(string());
739 breakParagraph(cur, 0);
745 case LFUN_PARAGRAPH_SPACING: {
746 Paragraph & par = cur.paragraph();
747 Spacing::Space cur_spacing = par.params().spacing().getSpace();
748 string cur_value = "1.0";
749 if (cur_spacing == Spacing::Other)
750 cur_value = par.params().spacing().getValueAsString();
752 istringstream is(cmd.argument);
755 Spacing::Space new_spacing = cur_spacing;
756 string new_value = cur_value;
758 lyxerr << "Missing argument to `paragraph-spacing'"
760 } else if (tmp == "single") {
761 new_spacing = Spacing::Single;
762 } else if (tmp == "onehalf") {
763 new_spacing = Spacing::Onehalf;
764 } else if (tmp == "double") {
765 new_spacing = Spacing::Double;
766 } else if (tmp == "other") {
767 new_spacing = Spacing::Other;
768 string tmpval = "0.0";
770 lyxerr << "new_value = " << tmpval << endl;
773 } else if (tmp == "default") {
774 new_spacing = Spacing::Default;
776 lyxerr << _("Unknown spacing argument: ")
777 << cmd.argument << endl;
779 if (cur_spacing != new_spacing || cur_value != new_value)
780 par.params().spacing(Spacing(new_spacing, new_value));
784 case LFUN_INSET_APPLY: {
785 string const name = cmd.getArg(0);
786 InsetBase * inset = bv->owner()->getDialogs().getOpenInset(name);
788 FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument);
789 inset->dispatch(cur, fr);
791 FuncRequest fr(LFUN_INSET_INSERT, cmd.argument);
797 case LFUN_INSET_INSERT: {
799 InsetBase * inset = createInset(bv, cmd);
801 insertInset(cur, inset);
807 case LFUN_INSET_SETTINGS:
808 if (cur.inset().asUpdatableInset())
809 cur.inset().asUpdatableInset()->showInsetDialog(bv);
812 case LFUN_NEXT_INSET_TOGGLE: {
813 InsetBase * inset = cur.nextInset();
815 cur.clearSelection();
816 FuncRequest fr = cmd;
817 fr.action = LFUN_INSET_TOGGLE;
818 inset->dispatch(cur, fr);
823 case LFUN_KEYMAP_TOGGLE:
824 cur.clearSelection();
828 case LFUN_SPACE_INSERT:
829 if (cur.paragraph().layout()->free_spacing)
830 insertChar(cur, ' ');
832 doInsertInset(cur, this, cmd, false, false);
835 moveCursor(cur, false);
838 case LFUN_HYPHENATION:
839 specialChar(cur, InsetSpecialChar::HYPHENATION);
842 case LFUN_LIGATURE_BREAK:
843 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
847 specialChar(cur, InsetSpecialChar::LDOTS);
850 case LFUN_END_OF_SENTENCE:
851 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
854 case LFUN_MENU_SEPARATOR:
855 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
858 case LFUN_UPCASE_WORD:
859 changeCase(cur, LyXText::text_uppercase);
862 case LFUN_LOWCASE_WORD:
863 changeCase(cur, LyXText::text_lowercase);
866 case LFUN_CAPITALIZE_WORD:
867 changeCase(cur, LyXText::text_capitalization);
870 case LFUN_TRANSPOSE_CHARS:
875 cur.message(_("Paste"));
876 lyx::cap::replaceSelection(cur);
878 #warning FIXME Check if the arg is in the domain of available selections.
880 if (isStrUnsignedInt(cmd.argument))
881 pasteSelection(cur, convert<unsigned int>(cmd.argument));
883 pasteSelection(cur, 0);
884 cur.clearSelection(); // bug 393
890 cutSelection(cur, true, true);
891 cur.message(_("Cut"));
896 cur.message(_("Copy"));
900 cur.message(convert<string>(cursorX(cur.top())) + ' '
901 + convert<string>(cursorY(cur.top())));
907 istringstream is(cmd.argument);
910 lyxerr << "SETXY: Could not parse coordinates in '"
911 << cmd.argument << std::endl;
913 setCursorFromCoordinates(cur, x, y);
918 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
920 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
927 cur.message(cur.paragraph().layout()->name());
931 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
932 << cmd.argument << endl;
934 // This is not the good solution to the empty argument
935 // problem, but it will hopefully suffice for 1.2.0.
936 // The correct solution would be to augument the
937 // function list/array with information about what
938 // functions needs arguments and their type.
939 if (cmd.argument.empty()) {
940 cur.errorMessage(_("LyX function 'layout' needs an argument."));
944 // Derive layout number from given argument (string)
945 // and current buffer's textclass (number)
946 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
947 bool hasLayout = tclass.hasLayout(cmd.argument);
948 string layout = cmd.argument;
950 // If the entry is obsolete, use the new one instead.
952 string const & obs = tclass[layout]->obsoleted_by();
958 cur.errorMessage(string(N_("Layout ")) + cmd.argument +
963 bool change_layout = (current_layout != layout);
965 if (!change_layout && cur.selection() &&
966 cur.selBegin().pit() != cur.selEnd().pit())
968 pit_type spit = cur.selBegin().pit();
969 pit_type epit = cur.selEnd().pit() + 1;
970 while (spit != epit) {
971 if (pars_[spit].layout()->name() != current_layout) {
972 change_layout = true;
980 current_layout = layout;
981 setLayout(cur, layout);
982 bv->owner()->setLayout(layout);
988 case LFUN_PASTESELECTION: {
989 cur.clearSelection();
990 string const clip = bv->getClipboard();
992 if (cmd.argument == "paragraph")
993 insertStringAsParagraphs(cur, clip);
995 insertStringAsLines(cur, clip);
1000 case LFUN_GOTOERROR:
1001 gotoInset(cur, InsetBase::ERROR_CODE, false);
1005 gotoInset(cur, InsetBase::NOTE_CODE, false);
1008 case LFUN_REFERENCE_GOTO: {
1009 vector<InsetBase_code> tmp;
1010 tmp.push_back(InsetBase::LABEL_CODE);
1011 tmp.push_back(InsetBase::REF_CODE);
1012 gotoInset(cur, tmp, true);
1017 lyx::cap::replaceSelection(cur);
1018 Paragraph & par = cur.paragraph();
1019 lyx::pos_type pos = cur.pos();
1023 else if (cur.prevInset() && cur.prevInset()->isSpace())
1026 c = par.getChar(pos - 1);
1028 LyXLayout_ptr const & style = par.layout();
1030 BufferParams const & bufparams = bv->buffer()->params();
1031 if (!style->pass_thru
1032 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1033 string arg = cmd.argument;
1034 if (arg == "single")
1035 cur.insert(new InsetQuotes(c,
1036 bufparams.quotes_language,
1037 InsetQuotes::SingleQ));
1038 else if (arg == "double")
1039 cur.insert(new InsetQuotes(c,
1040 bufparams.quotes_language,
1041 InsetQuotes::DoubleQ));
1043 cur.insert(new InsetQuotes(c, bufparams));
1047 bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1051 case LFUN_DATE_INSERT: {
1052 lyx::cap::replaceSelection(cur);
1053 time_t now_time_t = time(NULL);
1054 struct tm * now_tm = localtime(&now_time_t);
1055 setlocale(LC_TIME, "");
1057 if (!cmd.argument.empty())
1060 arg = lyxrc.date_insert_format;
1062 int const datetmp_len =
1063 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1065 for (int i = 0; i < datetmp_len; i++)
1066 insertChar(cur, datetmp[i]);
1069 moveCursor(cur, false);
1073 case LFUN_MOUSE_TRIPLE:
1074 if (cmd.button() == mouse_button::button1) {
1079 bv->haveSelection(cur.selection());
1083 case LFUN_MOUSE_DOUBLE:
1084 if (cmd.button() == mouse_button::button1) {
1085 selectWord(cur, lyx::WHOLE_WORD_STRICT);
1086 bv->haveSelection(cur.selection());
1090 // Single-click on work area
1091 case LFUN_MOUSE_PRESS: {
1092 // Right click on a footnote flag opens float menu
1093 if (cmd.button() == mouse_button::button3) {
1094 cur.clearSelection();
1098 // Middle button press pastes if we have a selection
1099 // We do this here as if the selection was inside an inset
1100 // it could get cleared on the unlocking of the inset so
1101 // we have to check this first
1102 bool paste_internally = false;
1103 if (cmd.button() == mouse_button::button2 && cur.selection()) {
1104 bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1105 paste_internally = true;
1108 // Clear the selection
1109 cur.clearSelection();
1111 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1116 // Has the cursor just left the inset?
1117 if (bv->cursor().inMathed() && !cur.inMathed())
1118 bv->cursor().inset().notifyCursorLeaves(bv->cursor());
1123 // Insert primary selection with middle mouse
1124 // if there is a local selection in the current buffer,
1126 if (cmd.button() == mouse_button::button2) {
1127 if (paste_internally)
1128 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1130 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1136 case LFUN_MOUSE_MOTION: {
1137 // Only use motion with button 1
1138 //if (cmd.button() != mouse_button::button1)
1141 // ignore motions deeper nested than the real anchor
1142 LCursor & bvcur = cur.bv().cursor();
1143 if (bvcur.anchor_.hasPart(cur)) {
1144 CursorSlice old = bvcur.top();
1146 int const wh = bv->workHeight();
1147 int const y = std::max(0, std::min(wh - 1, cmd.y));
1149 setCursorFromCoordinates(cur, cmd.x, y);
1150 cur.x_target() = cmd.x;
1155 // This is to allow jumping over large insets
1156 if (cur.top() == old) {
1163 if (cur.top() == old)
1166 // don't set anchor_
1167 bvcur.setCursor(cur);
1168 bvcur.selection() = true;
1169 lyxerr << "MOTION: " << bv->cursor() << endl;
1177 case LFUN_MOUSE_RELEASE: {
1178 if (cmd.button() == mouse_button::button2)
1182 if (cmd.button() == mouse_button::button1)
1183 bv->haveSelection(cur.selection());
1186 bv->owner()->updateMenubar();
1187 bv->owner()->updateToolbars();
1191 case LFUN_SELFINSERT: {
1192 if (cmd.argument.empty())
1195 // Automatically delete the currently selected
1196 // text and replace it with what is being
1197 // typed in now. Depends on lyxrc settings
1198 // "auto_region_delete", which defaults to
1201 if (lyxrc.auto_region_delete) {
1202 if (cur.selection())
1203 cutSelection(cur, false, false);
1204 bv->haveSelection(false);
1207 cur.clearSelection();
1208 LyXFont const old_font = real_current_font;
1210 string::const_iterator cit = cmd.argument.begin();
1211 string::const_iterator end = cmd.argument.end();
1212 for (; cit != end; ++cit)
1213 bv->owner()->getIntl().getTransManager().
1214 TranslateAndInsert(*cit, this);
1217 moveCursor(cur, false);
1219 // real_current_font.number can change so we need to
1220 // update the minibuffer
1221 if (old_font != real_current_font)
1222 bv->updateScrollbar();
1227 InsetCommandParams p("url");
1228 string const data = InsetCommandMailer::params2string("url", p);
1229 bv->owner()->getDialogs().show("url", data, 0);
1233 case LFUN_HTMLURL: {
1234 InsetCommandParams p("htmlurl");
1235 string const data = InsetCommandMailer::params2string("url", p);
1236 bv->owner()->getDialogs().show("url", data, 0);
1240 case LFUN_INSERT_LABEL: {
1241 // Try to generate a valid label
1242 string const contents = cmd.argument.empty() ?
1243 cur.getPossibleLabel() : cmd.argument;
1245 InsetCommandParams p("label", contents);
1246 string const data = InsetCommandMailer::params2string("label", p);
1248 if (cmd.argument.empty()) {
1249 bv->owner()->getDialogs().show("label", data, 0);
1251 FuncRequest fr(LFUN_INSET_INSERT, data);
1259 case LFUN_INSET_LIST:
1260 case LFUN_INSET_THEOREM:
1261 case LFUN_INSET_CAPTION:
1263 case LFUN_INSERT_NOTE:
1264 case LFUN_INSERT_CHARSTYLE:
1265 case LFUN_INSERT_BOX:
1266 case LFUN_INSERT_BRANCH:
1267 case LFUN_INSERT_BIBITEM:
1268 case LFUN_INSET_ERT:
1269 case LFUN_INSET_FLOAT:
1270 case LFUN_INSET_FOOTNOTE:
1271 case LFUN_INSET_MARGINAL:
1272 case LFUN_INSET_OPTARG:
1273 case LFUN_INSET_WIDE_FLOAT:
1274 case LFUN_INSET_WRAP:
1275 case LFUN_TABULAR_INSERT:
1276 case LFUN_ENVIRONMENT_INSERT:
1277 // Open the inset, and move the current selection
1279 doInsertInset(cur, this, cmd, true, true);
1283 case LFUN_INDEX_INSERT:
1284 // Just open the inset
1285 doInsertInset(cur, this, cmd, true, false);
1289 case LFUN_INDEX_PRINT:
1290 case LFUN_TOC_INSERT:
1292 case LFUN_INSERT_LINE:
1293 case LFUN_INSERT_PAGEBREAK:
1295 doInsertInset(cur, this, cmd, false, false);
1299 case LFUN_DEPTH_MIN:
1300 changeDepth(cur, DEC_DEPTH);
1303 case LFUN_DEPTH_PLUS:
1304 changeDepth(cur, INC_DEPTH);
1307 case LFUN_MATH_DISPLAY:
1308 mathDispatch(cur, cmd, true);
1311 case LFUN_MATH_IMPORT_SELECTION:
1312 case LFUN_MATH_MODE:
1313 if (cmd.argument == "on")
1314 // don't pass "on" as argument
1315 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1317 mathDispatch(cur, cmd, false);
1320 case LFUN_MATH_MACRO:
1321 if (cmd.argument.empty())
1322 cur.errorMessage(N_("Missing argument"));
1324 string s = cmd.argument;
1325 string const s1 = token(s, ' ', 1);
1326 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1327 string const s2 = token(s, ' ', 2);
1328 string const type = s2.empty() ? "newcommand" : s2;
1329 cur.insert(new MathMacroTemplate(token(s, ' ', 0), nargs, s2));
1330 //cur.nextInset()->edit(cur, true);
1334 // passthrough hat and underscore outside mathed:
1335 case LFUN_SUBSCRIPT:
1336 mathDispatch(cur, FuncRequest(LFUN_SELFINSERT, "_"), false);
1338 case LFUN_SUPERSCRIPT:
1339 mathDispatch(cur, FuncRequest(LFUN_SELFINSERT, "^"), false);
1342 case LFUN_INSERT_MATH:
1343 case LFUN_INSERT_MATRIX:
1344 case LFUN_MATH_DELIM: {
1345 cur.insert(new MathHullInset);
1346 cur.dispatch(FuncRequest(LFUN_RIGHT));
1347 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
1353 LyXFont font(LyXFont::ALL_IGNORE);
1354 font.setEmph(LyXFont::TOGGLE);
1355 toggleAndShow(cur, this, font);
1360 LyXFont font(LyXFont::ALL_IGNORE);
1361 font.setSeries(LyXFont::BOLD_SERIES);
1362 toggleAndShow(cur, this, font);
1367 LyXFont font(LyXFont::ALL_IGNORE);
1368 font.setNoun(LyXFont::TOGGLE);
1369 toggleAndShow(cur, this, font);
1374 LyXFont font(LyXFont::ALL_IGNORE);
1375 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
1376 toggleAndShow(cur, this, font);
1381 LyXFont font(LyXFont::ALL_IGNORE);
1382 font.setFamily(LyXFont::SANS_FAMILY);
1383 toggleAndShow(cur, this, font);
1388 LyXFont font(LyXFont::ALL_IGNORE);
1389 font.setFamily(LyXFont::ROMAN_FAMILY);
1390 toggleAndShow(cur, this, font);
1394 case LFUN_DEFAULT: {
1395 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
1396 toggleAndShow(cur, this, font);
1400 case LFUN_UNDERLINE: {
1401 LyXFont font(LyXFont::ALL_IGNORE);
1402 font.setUnderbar(LyXFont::TOGGLE);
1403 toggleAndShow(cur, this, font);
1407 case LFUN_FONT_SIZE: {
1408 LyXFont font(LyXFont::ALL_IGNORE);
1409 font.setLyXSize(cmd.argument);
1410 toggleAndShow(cur, this, font);
1414 case LFUN_LANGUAGE: {
1415 Language const * lang = languages.getLanguage(cmd.argument);
1418 LyXFont font(LyXFont::ALL_IGNORE);
1419 font.setLanguage(lang);
1420 toggleAndShow(cur, this, font);
1425 case LFUN_FREEFONT_APPLY:
1426 toggleAndShow(cur, this, freefont, toggleall);
1427 cur.message(_("Character set"));
1430 // Set the freefont using the contents of \param data dispatched from
1431 // the frontends and apply it at the current cursor location.
1432 case LFUN_FREEFONT_UPDATE: {
1435 if (bv_funcs::string2font(cmd.argument, font, toggle)) {
1438 toggleAndShow(cur, this, freefont, toggleall);
1439 cur.message(_("Character set"));
1444 case LFUN_FINISHED_LEFT:
1445 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1448 case LFUN_FINISHED_RIGHT:
1449 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1453 case LFUN_FINISHED_UP:
1454 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_UP:\n" << cur << endl;
1458 case LFUN_FINISHED_DOWN:
1459 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_DOWN:\n" << cur << endl;
1463 case LFUN_LAYOUT_PARAGRAPH: {
1465 params2string(cur.paragraph(), data);
1466 data = "show\n" + data;
1467 bv->owner()->getDialogs().show("paragraph", data);
1471 case LFUN_PARAGRAPH_UPDATE: {
1472 if (!bv->owner()->getDialogs().visible("paragraph"))
1475 params2string(cur.paragraph(), data);
1477 // Will the paragraph accept changes from the dialog?
1478 InsetBase & inset = cur.inset();
1479 bool const accept = !inset.forceDefaultParagraphs(&inset);
1481 data = "update " + convert<string>(accept) + '\n' + data;
1482 bv->owner()->getDialogs().update("paragraph", data);
1487 case LFUN_CIRCUMFLEX:
1497 case LFUN_SPECIAL_CARON:
1500 case LFUN_HUNG_UMLAUT:
1503 bv->owner()->getLyXFunc().handleKeyFunc(cmd.action);
1504 if (!cmd.argument.empty())
1505 bv->owner()->getIntl().getTransManager()
1506 .TranslateAndInsert(cmd.argument[0], this);
1509 case LFUN_FLOAT_LIST: {
1510 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1511 if (tclass.floats().typeExist(cmd.argument)) {
1512 // not quite sure if we want this...
1514 cur.clearSelection();
1515 breakParagraph(cur);
1517 if (cur.lastpos() != 0) {
1519 breakParagraph(cur);
1522 setLayout(cur, tclass.defaultLayoutName());
1523 setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1524 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);
1595 lyxerr << BOOST_CURRENT_FUNCTION
1596 << " Not DISPATCHED by LyXText" << endl;
1602 && &oldTopSlice.inset() == &cur.inset()
1603 && oldTopSlice.idx() == cur.idx()
1605 && !cur.selection())
1612 bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
1613 FuncStatus & flag) const
1615 BOOST_ASSERT(cur.text() == this);
1616 LyXFont const & font = real_current_font;
1619 switch (cmd.action) {
1621 case LFUN_DEPTH_MIN:
1622 enable = changeDepthAllowed(cur, DEC_DEPTH);
1625 case LFUN_DEPTH_PLUS:
1626 enable = changeDepthAllowed(cur, INC_DEPTH);
1629 case LFUN_INSET_OPTARG:
1630 enable = numberOfOptArgs(cur.paragraph())
1631 < cur.paragraph().layout()->optionalargs;
1635 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1639 // the functions which insert insets
1640 InsetBase::Code code = InsetBase::NO_CODE;
1641 switch (cmd.action) {
1642 case LFUN_DIALOG_SHOW_NEW_INSET:
1643 if (cmd.argument == "bibitem")
1644 code = InsetBase::BIBITEM_CODE;
1645 else if (cmd.argument == "bibtex")
1646 code = InsetBase::BIBTEX_CODE;
1647 else if (cmd.argument == "box")
1648 code = InsetBase::BOX_CODE;
1649 else if (cmd.argument == "branch")
1650 code = InsetBase::BRANCH_CODE;
1651 else if (cmd.argument == "citation")
1652 code = InsetBase::CITE_CODE;
1653 else if (cmd.argument == "ert")
1654 code = InsetBase::ERT_CODE;
1655 else if (cmd.argument == "external")
1656 code = InsetBase::EXTERNAL_CODE;
1657 else if (cmd.argument == "float")
1658 code = InsetBase::FLOAT_CODE;
1659 else if (cmd.argument == "graphics")
1660 code = InsetBase::GRAPHICS_CODE;
1661 else if (cmd.argument == "include")
1662 code = InsetBase::INCLUDE_CODE;
1663 else if (cmd.argument == "index")
1664 code = InsetBase::INDEX_CODE;
1665 else if (cmd.argument == "label")
1666 code = InsetBase::LABEL_CODE;
1667 else if (cmd.argument == "note")
1668 code = InsetBase::NOTE_CODE;
1669 else if (cmd.argument == "ref")
1670 code = InsetBase::REF_CODE;
1671 else if (cmd.argument == "toc")
1672 code = InsetBase::TOC_CODE;
1673 else if (cmd.argument == "url")
1674 code = InsetBase::URL_CODE;
1675 else if (cmd.argument == "vspace")
1676 code = InsetBase::VSPACE_CODE;
1677 else if (cmd.argument == "wrap")
1678 code = InsetBase::WRAP_CODE;
1681 case LFUN_INSET_ERT:
1682 code = InsetBase::ERT_CODE;
1684 case LFUN_INSET_FOOTNOTE:
1685 code = InsetBase::FOOT_CODE;
1687 case LFUN_TABULAR_INSERT:
1688 code = InsetBase::TABULAR_CODE;
1690 case LFUN_INSET_MARGINAL:
1691 code = InsetBase::MARGIN_CODE;
1693 case LFUN_INSET_FLOAT:
1694 case LFUN_INSET_WIDE_FLOAT:
1695 code = InsetBase::FLOAT_CODE;
1697 case LFUN_INSET_WRAP:
1698 code = InsetBase::WRAP_CODE;
1700 case LFUN_FLOAT_LIST:
1701 code = InsetBase::FLOAT_LIST_CODE;
1704 case LFUN_INSET_LIST:
1705 code = InsetBase::LIST_CODE;
1707 case LFUN_INSET_THEOREM:
1708 code = InsetBase::THEOREM_CODE;
1711 case LFUN_INSET_CAPTION:
1712 code = InsetBase::CAPTION_CODE;
1714 case LFUN_INSERT_NOTE:
1715 code = InsetBase::NOTE_CODE;
1717 case LFUN_INSERT_CHARSTYLE:
1718 code = InsetBase::CHARSTYLE_CODE;
1719 if (buf->params().getLyXTextClass().charstyles().empty())
1722 case LFUN_INSERT_BOX:
1723 code = InsetBase::BOX_CODE;
1725 case LFUN_INSERT_BRANCH:
1726 code = InsetBase::BRANCH_CODE;
1727 if (buf->params().branchlist().empty())
1730 case LFUN_INSERT_LABEL:
1731 code = InsetBase::LABEL_CODE;
1733 case LFUN_INSET_OPTARG:
1734 code = InsetBase::OPTARG_CODE;
1736 case LFUN_ENVIRONMENT_INSERT:
1737 code = InsetBase::BOX_CODE;
1739 case LFUN_INDEX_INSERT:
1740 code = InsetBase::INDEX_CODE;
1742 case LFUN_INDEX_PRINT:
1743 code = InsetBase::INDEX_PRINT_CODE;
1745 case LFUN_TOC_INSERT:
1746 code = InsetBase::TOC_CODE;
1750 code = InsetBase::URL_CODE;
1753 // always allow this, since we will inset a raw quote
1754 // if an inset is not allowed.
1756 case LFUN_HYPHENATION:
1757 case LFUN_LIGATURE_BREAK:
1759 case LFUN_MENU_SEPARATOR:
1761 case LFUN_END_OF_SENTENCE:
1762 code = InsetBase::SPECIALCHAR_CODE;
1764 case LFUN_SPACE_INSERT:
1765 // slight hack: we know this is allowed in math mode
1767 code = InsetBase::SPACE_CODE;
1769 case LFUN_INSET_DIALOG_SHOW: {
1770 InsetBase * inset = cur.nextInset();
1773 code = inset->lyxCode();
1774 if (!(code == InsetBase::INCLUDE_CODE
1775 || code == InsetBase::BIBTEX_CODE
1776 || code == InsetBase::FLOAT_LIST_CODE
1777 || code == InsetBase::TOC_CODE))
1786 if (code != InsetBase::NO_CODE
1787 && (cur.empty() || !cur.inset().insetAllowed(code)))
1792 case LFUN_DIALOG_SHOW_NEW_INSET:
1793 case LFUN_INSET_ERT:
1794 case LFUN_INSERT_BOX:
1795 case LFUN_INSERT_BRANCH:
1796 case LFUN_ENVIRONMENT_INSERT:
1797 case LFUN_INDEX_INSERT:
1798 case LFUN_INDEX_PRINT:
1799 case LFUN_TOC_INSERT:
1803 case LFUN_HYPHENATION:
1804 case LFUN_LIGATURE_BREAK:
1806 case LFUN_MENU_SEPARATOR:
1808 case LFUN_END_OF_SENTENCE:
1809 case LFUN_SPACE_INSERT:
1810 case LFUN_INSET_DIALOG_SHOW:
1814 flag.setOnOff(font.emph() == LyXFont::ON);
1818 flag.setOnOff(font.noun() == LyXFont::ON);
1822 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1826 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1830 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1834 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1837 case LFUN_DELETE_WORD_FORWARD:
1838 case LFUN_DELETE_WORD_BACKWARD:
1839 case LFUN_DELETE_LINE_FORWARD:
1840 case LFUN_WORDRIGHT:
1850 case LFUN_UP_PARAGRAPHSEL:
1851 case LFUN_DOWN_PARAGRAPHSEL:
1856 case LFUN_WORDRIGHTSEL:
1857 case LFUN_WORDLEFTSEL:
1859 case LFUN_UP_PARAGRAPH:
1860 case LFUN_DOWN_PARAGRAPH:
1865 case LFUN_BREAKLINE:
1867 case LFUN_DELETE_SKIP:
1868 case LFUN_BACKSPACE:
1869 case LFUN_BACKSPACE_SKIP:
1870 case LFUN_BREAKPARAGRAPH:
1871 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1872 case LFUN_BREAKPARAGRAPH_SKIP:
1873 case LFUN_PARAGRAPH_SPACING:
1874 case LFUN_INSET_APPLY:
1875 case LFUN_INSET_INSERT:
1876 case LFUN_NEXT_INSET_TOGGLE:
1877 case LFUN_UPCASE_WORD:
1878 case LFUN_LOWCASE_WORD:
1879 case LFUN_CAPITALIZE_WORD:
1880 case LFUN_TRANSPOSE_CHARS:
1887 case LFUN_GETLAYOUT:
1889 case LFUN_PASTESELECTION:
1890 case LFUN_GOTOERROR:
1892 case LFUN_REFERENCE_GOTO:
1893 case LFUN_DATE_INSERT:
1894 case LFUN_SELFINSERT:
1895 case LFUN_INSERT_LABEL:
1896 case LFUN_INSERT_NOTE:
1897 case LFUN_INSERT_CHARSTYLE:
1898 case LFUN_INSERT_BIBITEM:
1899 case LFUN_INSET_FLOAT:
1900 case LFUN_INSET_FOOTNOTE:
1901 case LFUN_INSET_MARGINAL:
1902 case LFUN_INSET_WIDE_FLOAT:
1903 case LFUN_INSET_WRAP:
1904 case LFUN_TABULAR_INSERT:
1905 case LFUN_INSERT_LINE:
1906 case LFUN_INSERT_PAGEBREAK:
1907 case LFUN_MATH_DISPLAY:
1908 case LFUN_MATH_IMPORT_SELECTION:
1909 case LFUN_MATH_MODE:
1910 case LFUN_MATH_MACRO:
1911 case LFUN_INSERT_MATH:
1912 case LFUN_INSERT_MATRIX:
1913 case LFUN_MATH_DELIM:
1914 case LFUN_SUBSCRIPT:
1915 case LFUN_SUPERSCRIPT:
1917 case LFUN_UNDERLINE:
1918 case LFUN_FONT_SIZE:
1920 case LFUN_FREEFONT_APPLY:
1921 case LFUN_FREEFONT_UPDATE:
1922 case LFUN_LAYOUT_PARAGRAPH:
1923 case LFUN_PARAGRAPH_UPDATE:
1925 case LFUN_CIRCUMFLEX:
1935 case LFUN_SPECIAL_CARON:
1938 case LFUN_HUNG_UMLAUT:
1941 case LFUN_FLOAT_LIST:
1942 case LFUN_ACCEPT_CHANGE:
1943 case LFUN_REJECT_CHANGE:
1944 case LFUN_THESAURUS_ENTRY:
1945 case LFUN_PARAGRAPH_APPLY:
1947 case LFUN_KEYMAP_TOGGLE:
1949 case LFUN_BEGINNINGBUF:
1950 case LFUN_BEGINNINGBUFSEL:
1951 case LFUN_ENDBUFSEL:
1952 // these are handled in our dispatch()
1959 flag.enabled(enable);