]> git.lyx.org Git - lyx.git/blob - src/Text3.cpp
Transfer the setting of the current LyXView from workArea::focusInEvent() to GuiView...
[lyx.git] / src / Text3.cpp
1 /**
2  * \file text3.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author Lars Gullik Bjønnes
8  * \author Alfredo Braunstein
9  * \author Angus Leeming
10  * \author John Levon
11  * \author André Pönitz
12  *
13  * Full author contact details are available in file CREDITS.
14  */
15
16 #include <config.h>
17
18 #include "Text.h"
19
20 #include "Bidi.h"
21 #include "BranchList.h"
22 #include "FloatList.h"
23 #include "FuncStatus.h"
24 #include "Buffer.h"
25 #include "buffer_funcs.h"
26 #include "BufferParams.h"
27 #include "BufferView.h"
28 #include "Cursor.h"
29 #include "CutAndPaste.h"
30 #include "debug.h"
31 #include "DispatchResult.h"
32 #include "ErrorList.h"
33 #include "factory.h"
34 #include "FuncRequest.h"
35 #include "gettext.h"
36 #include "Intl.h"
37 #include "Language.h"
38 #include "Layout.h"
39 #include "LyXAction.h"
40 #include "LyXFunc.h"
41 #include "Lexer.h"
42 #include "LyXRC.h"
43 #include "Paragraph.h"
44 #include "paragraph_funcs.h"
45 #include "ParagraphParameters.h"
46 #include "Undo.h"
47 #include "VSpace.h"
48 #include "ParIterator.h"
49
50 #include "frontends/Clipboard.h"
51 #include "frontends/Selection.h"
52
53 #include "insets/InsetCollapsable.h"
54 #include "insets/InsetCommand.h"
55 #include "insets/InsetFloatList.h"
56 #include "insets/InsetNewline.h"
57 #include "insets/InsetQuotes.h"
58 #include "insets/InsetSpecialChar.h"
59 #include "insets/InsetText.h"
60
61 #include "support/lstrings.h"
62 #include "support/lyxlib.h"
63 #include "support/convert.h"
64 #include "support/lyxtime.h"
65
66 #include "mathed/InsetMathHull.h"
67 #include "mathed/MathMacroTemplate.h"
68
69 #include <boost/current_function.hpp>
70
71 #include <clocale>
72 #include <sstream>
73
74 using std::endl;
75 using std::string;
76 using std::istringstream;
77 using std::ostringstream;
78
79 namespace lyx {
80
81 using cap::copySelection;
82 using cap::cutSelection;
83 using cap::pasteFromStack;
84 using cap::pasteClipboard;
85 using cap::replaceSelection;
86
87 using support::isStrUnsignedInt;
88 using support::token;
89
90 // globals...
91 static Font freefont(Font::ALL_IGNORE);
92 static bool toggleall = false;
93
94 static void toggleAndShow(Cursor & cur, Text * text,
95         Font const & font, bool toggleall = true)
96 {
97         text->toggleFree(cur, font, toggleall);
98
99         if (font.language() != ignore_language ||
100                         font.number() != Font::IGNORE) {
101                 TextMetrics const & tm = cur.bv().textMetrics(text);
102                 if (cur.boundary() != tm.isRTLBoundary(cur.pit(),
103                                                                                                                 cur.pos(), cur.real_current_font))
104                         text->setCursor(cur, cur.pit(), cur.pos(),
105                                                                                         false, !cur.boundary());
106         }
107 }
108
109
110 static void moveCursor(Cursor & cur, bool selecting)
111 {
112         if (selecting || cur.mark())
113                 cur.setSelection();
114 }
115
116
117 static void finishChange(Cursor & cur, bool selecting)
118 {
119         finishUndo();
120         moveCursor(cur, selecting);
121 }
122
123
124 static void mathDispatch(Cursor & cur, FuncRequest const & cmd, bool display)
125 {
126         recordUndo(cur);
127         docstring sel = cur.selectionAsString(false);
128
129         // It may happen that sel is empty but there is a selection
130         replaceSelection(cur);
131
132         if (sel.empty()) {
133 #ifdef ENABLE_ASSERTIONS
134                 const int old_pos = cur.pos();
135 #endif
136                 cur.insert(new InsetMathHull(hullSimple));
137                 BOOST_ASSERT(old_pos == cur.pos());
138                 cur.nextInset()->edit(cur, true);
139                 // don't do that also for LFUN_MATH_MODE
140                 // unless you want end up with always changing
141                 // to mathrm when opening an inlined inset --
142                 // I really hate "LyXfunc overloading"...
143                 if (display)
144                         cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
145                 // Avoid an unnecessary undo step if cmd.argument
146                 // is empty
147                 if (!cmd.argument().empty())
148                         cur.dispatch(FuncRequest(LFUN_MATH_INSERT,
149                                                  cmd.argument()));
150         } else {
151                 // create a macro if we see "\\newcommand"
152                 // somewhere, and an ordinary formula
153                 // otherwise
154                 if (sel.find(from_ascii("\\newcommand")) == string::npos
155                                 && sel.find(from_ascii("\\def")) == string::npos)
156                 {
157                         InsetMathHull * formula = new InsetMathHull;
158                         istringstream is(to_utf8(sel));
159                         Lexer lex(0, 0);
160                         lex.setStream(is);
161                         formula->read(cur.buffer(), lex);
162                         if (formula->getType() == hullNone)
163                                 // Don't create pseudo formulas if
164                                 // delimiters are left out
165                                 formula->mutate(hullSimple);
166                         cur.insert(formula);
167                 } else {
168                         cur.insert(new MathMacroTemplate(sel));
169                 }
170         }
171         cur.message(from_utf8(N_("Math editor mode")));
172 }
173
174
175 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
176 {
177         recordUndo(cur);
178         cap::replaceSelection(cur);
179         cur.insert(new InsetSpecialChar(kind));
180         cur.posRight();
181 }
182
183
184 static bool doInsertInset(Cursor & cur, Text * text,
185         FuncRequest const & cmd, bool edit, bool pastesel)
186 {
187         Inset * inset = createInset(&cur.bv(), cmd);
188         if (!inset)
189                 return false;
190
191         recordUndo(cur);
192         if (cmd.action == LFUN_INDEX_INSERT) {
193                 docstring ds = support::subst(text->getStringToIndex(cur), '\n', ' ');
194                 text->insertInset(cur, inset);
195                 if (edit)
196                         inset->edit(cur, true);
197                 // Now put this into inset
198                 static_cast<InsetCollapsable *>(inset)->text_.insertStringAsParagraphs(cur, ds);
199         } else {
200                 bool gotsel = false;
201                 if (cur.selection()) {
202                         lyx::dispatch(FuncRequest(LFUN_CUT));
203                         gotsel = true;
204                 }
205                 text->insertInset(cur, inset);
206
207                 if (edit)
208                         inset->edit(cur, true);
209
210                 if (gotsel && pastesel) {
211                         lyx::dispatch(FuncRequest(LFUN_PASTE, "0"));
212                         // reset first par to default
213                         if (cur.lastpit() != 0 || cur.lastpos() != 0) {
214                                 LayoutPtr const layout =
215                                         cur.buffer().params().getTextClass().defaultLayout();
216                                 cur.text()->paragraphs().begin()->layout(layout);
217                         }
218                 }
219         }
220         return true;
221 }
222
223
224 string const freefont2string()
225 {
226         return freefont.toString(toggleall);
227 }
228
229
230 void Text::number(Cursor & cur)
231 {
232         Font font(Font::ALL_IGNORE);
233         font.setNumber(Font::TOGGLE);
234         toggleAndShow(cur, this, font);
235 }
236
237
238 bool Text::isRTL(Buffer const & buffer, Paragraph const & par) const
239 {
240         return par.isRTL(buffer.params());
241 }
242
243
244 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
245 {
246         LYXERR(Debug::ACTION) << "Text::dispatch: cmd: " << cmd << endl;
247
248         // FIXME: We use the update flag to indicates wether a singlePar or a
249         // full screen update is needed. We reset it here but shall we restore it
250         // at the end?
251         cur.noUpdate();
252
253         BOOST_ASSERT(cur.text() == this);
254         BufferView * bv = &cur.bv();
255         TextMetrics & tm = cur.bv().textMetrics(this);
256         CursorSlice oldTopSlice = cur.top();
257         bool oldBoundary = cur.boundary();
258         bool sel = cur.selection();
259         // Signals that, even if needsUpdate == false, an update of the
260         // cursor paragraph is required
261         bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
262                 LyXAction::SingleParUpdate);
263         // Signals that a full-screen update is required
264         bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
265                 LyXAction::NoUpdate) || singleParUpdate);
266         // Remember the old paragraph metric (_outer_ paragraph!)
267         ParagraphMetrics const & pm = cur.bv().parMetrics(
268                 cur.bottom().text(), cur.bottom().pit());
269         Dimension olddim = pm.dim();
270
271         switch (cmd.action) {
272
273         case LFUN_PARAGRAPH_MOVE_DOWN: {
274                 pit_type const pit = cur.pit();
275                 recUndo(cur, pit, pit + 1);
276                 finishUndo();
277                 std::swap(pars_[pit], pars_[pit + 1]);
278                 updateLabels(cur.buffer());
279                 needsUpdate = true;
280                 ++cur.pit();
281                 break;
282         }
283
284         case LFUN_PARAGRAPH_MOVE_UP: {
285                 pit_type const pit = cur.pit();
286                 recUndo(cur, pit - 1, pit);
287                 finishUndo();
288                 std::swap(pars_[pit], pars_[pit - 1]);
289                 updateLabels(cur.buffer());
290                 --cur.pit();
291                 needsUpdate = true;
292                 break;
293         }
294
295         case LFUN_APPENDIX: {
296                 Paragraph & par = cur.paragraph();
297                 bool start = !par.params().startOfAppendix();
298
299 // FIXME: The code below only makes sense at top level.
300 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
301                 // ensure that we have only one start_of_appendix in this document
302                 // FIXME: this don't work for multipart document!
303                 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
304                         if (pars_[tmp].params().startOfAppendix()) {
305                                 recUndo(cur, tmp);
306                                 pars_[tmp].params().startOfAppendix(false);
307                                 break;
308                         }
309                 }
310
311                 recordUndo(cur);
312                 par.params().startOfAppendix(start);
313
314                 // we can set the refreshing parameters now
315                 updateLabels(cur.buffer());
316                 break;
317         }
318
319         case LFUN_WORD_DELETE_FORWARD:
320                 if (cur.selection()) {
321                         cutSelection(cur, true, false);
322                 } else
323                         deleteWordForward(cur);
324                 finishChange(cur, false);
325                 break;
326
327         case LFUN_WORD_DELETE_BACKWARD:
328                 if (cur.selection()) {
329                         cutSelection(cur, true, false);
330                 } else
331                         deleteWordBackward(cur);
332                 finishChange(cur, false);
333                 break;
334
335         case LFUN_LINE_DELETE:
336                 if (cur.selection()) {
337                         cutSelection(cur, true, false);
338                 } else
339                         tm.deleteLineForward(cur);
340                 finishChange(cur, false);
341                 break;
342
343         case LFUN_BUFFER_BEGIN:
344         case LFUN_BUFFER_BEGIN_SELECT:
345                 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_BEGIN_SELECT);
346                 if (cur.depth() == 1) {
347                         needsUpdate |= cursorTop(cur);
348                 } else {
349                         cur.undispatched();
350                 }
351                 break;
352
353         case LFUN_BUFFER_END:
354         case LFUN_BUFFER_END_SELECT:
355                 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_END_SELECT);
356                 if (cur.depth() == 1) {
357                         needsUpdate |= cursorBottom(cur);
358                 } else {
359                         cur.undispatched();
360                 }
361                 break;
362
363         case LFUN_CHAR_FORWARD:
364         case LFUN_CHAR_FORWARD_SELECT:
365                 //lyxerr << BOOST_CURRENT_FUNCTION
366                 //       << " LFUN_CHAR_FORWARD[SEL]:\n" << cur << endl;
367                 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
368                 if (reverseDirectionNeeded(cur))
369                         needsUpdate |= cursorLeft(cur);
370                 else
371                         needsUpdate |= cursorRight(cur);
372
373                 if (!needsUpdate && oldTopSlice == cur.top()
374                                 && cur.boundary() == oldBoundary) {
375                         cur.undispatched();
376                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
377                 }
378                 break;
379
380         case LFUN_CHAR_BACKWARD:
381         case LFUN_CHAR_BACKWARD_SELECT:
382                 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
383                 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
384                 if (reverseDirectionNeeded(cur))
385                         needsUpdate |= cursorRight(cur);
386                 else
387                         needsUpdate |= cursorLeft(cur);
388
389                 if (!needsUpdate && oldTopSlice == cur.top()
390                         && cur.boundary() == oldBoundary) {
391                         cur.undispatched();
392                         cmd = FuncRequest(LFUN_FINISHED_LEFT);
393                 }
394                 break;
395
396         case LFUN_UP_SELECT:
397         case LFUN_DOWN_SELECT:
398         case LFUN_UP:
399         case LFUN_DOWN: {
400                 // stop/start the selection
401                 bool select = cmd.action == LFUN_DOWN_SELECT ||
402                         cmd.action == LFUN_UP_SELECT;
403                 cur.selHandle(select);
404                 
405                 // move cursor up/down
406                 bool up = cmd.action == LFUN_UP_SELECT || cmd.action == LFUN_UP;
407                 bool const successful = cur.upDownInText(up, needsUpdate);
408                 if (successful) {
409                         // notify insets which were left and get their update flags 
410                         notifyCursorLeaves(cur.beforeDispatchCursor(), cur);
411                         cur.fixIfBroken();
412                         
413                         // redraw if you leave mathed (for the decorations)
414                         needsUpdate |= cur.beforeDispatchCursor().inMathed();
415                 } else
416                         cur.undispatched();
417                 
418                 break;
419         }
420
421         case LFUN_PARAGRAPH_UP:
422         case LFUN_PARAGRAPH_UP_SELECT:
423                 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
424                 needsUpdate |= cursorUpParagraph(cur);
425                 break;
426
427         case LFUN_PARAGRAPH_DOWN:
428         case LFUN_PARAGRAPH_DOWN_SELECT:
429                 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
430                 needsUpdate |= cursorDownParagraph(cur);
431                 break;
432
433         case LFUN_SCREEN_UP_SELECT:
434                 needsUpdate |= cur.selHandle(true);
435                 if (cur.pit() == 0 && cur.textRow().pos() == 0)
436                         cur.undispatched();
437                 else {
438                         tm.cursorPrevious(cur);
439                 }
440                 break;
441
442         case LFUN_SCREEN_DOWN_SELECT:
443                 needsUpdate |= cur.selHandle(true);
444                 if (cur.pit() == cur.lastpit()
445                           && cur.textRow().endpos() == cur.lastpos())
446                         cur.undispatched();
447                 else {
448                         tm.cursorNext(cur);
449                 }
450                 break;
451
452         case LFUN_LINE_BEGIN:
453         case LFUN_LINE_BEGIN_SELECT:
454                 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_BEGIN_SELECT);
455                 needsUpdate |= tm.cursorHome(cur);
456                 break;
457
458         case LFUN_LINE_END:
459         case LFUN_LINE_END_SELECT:
460                 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
461                 needsUpdate |= tm.cursorEnd(cur);
462                 break;
463
464         case LFUN_WORD_FORWARD:
465         case LFUN_WORD_FORWARD_SELECT:
466                 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT);
467                 if (reverseDirectionNeeded(cur))
468                         needsUpdate |= cursorLeftOneWord(cur);
469                 else
470                         needsUpdate |= cursorRightOneWord(cur);
471                 break;
472
473         case LFUN_WORD_BACKWARD:
474         case LFUN_WORD_BACKWARD_SELECT:
475                 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT);
476                 if (reverseDirectionNeeded(cur))
477                         needsUpdate |= cursorRightOneWord(cur);
478                 else
479                         needsUpdate |= cursorLeftOneWord(cur);
480                 break;
481
482         case LFUN_WORD_SELECT: {
483                 selectWord(cur, WHOLE_WORD);
484                 finishChange(cur, true);
485                 break;
486         }
487
488         case LFUN_BREAK_LINE: {
489                 // Not allowed by LaTeX (labels or empty par)
490                 if (cur.pos() > cur.paragraph().beginOfBody()) {
491                         // this avoids a double undo
492                         // FIXME: should not be needed, ideally
493                         if (!cur.selection())
494                                 recordUndo(cur);
495                         cap::replaceSelection(cur);
496                         cur.insert(new InsetNewline);
497                         cur.posRight();
498                         moveCursor(cur, false);
499                 }
500                 break;
501         }
502
503         case LFUN_CHAR_DELETE_FORWARD:
504                 if (!cur.selection()) {
505                         if (cur.pos() == cur.paragraph().size())
506                                 // Par boundary, force full-screen update
507                                 singleParUpdate = false;
508                         needsUpdate |= erase(cur);
509                         cur.resetAnchor();
510                         // It is possible to make it a lot faster still
511                         // just comment out the line below...
512                 } else {
513                         cutSelection(cur, true, false);
514                         singleParUpdate = false;
515                 }
516                 moveCursor(cur, false);
517                 break;
518
519         case LFUN_DELETE_FORWARD_SKIP:
520                 // Reverse the effect of LFUN_BREAK_PARAGRAPH_SKIP.
521                 if (!cur.selection()) {
522                         if (cur.pos() == cur.lastpos()) {
523                                 cursorRight(cur);
524                                 cursorLeft(cur);
525                         }
526                         erase(cur);
527                         cur.resetAnchor();
528                 } else {
529                         cutSelection(cur, true, false);
530                 }
531                 break;
532
533
534         case LFUN_CHAR_DELETE_BACKWARD:
535                 if (!cur.selection()) {
536                         if (bv->getIntl().getTransManager().backspace()) {
537                                 // Par boundary, full-screen update
538                                 if (cur.pos() == 0)
539                                         singleParUpdate = false;
540                                 needsUpdate |= backspace(cur);
541                                 cur.resetAnchor();
542                                 // It is possible to make it a lot faster still
543                                 // just comment out the line below...
544                         }
545                 } else {
546                         cutSelection(cur, true, false);
547                         singleParUpdate = false;
548                 }
549                 break;
550
551         case LFUN_DELETE_BACKWARD_SKIP:
552                 // Reverse the effect of LFUN_BREAK_PARAGRAPH_SKIP.
553                 if (!cur.selection()) {
554                         // FIXME: look here
555                         //CursorSlice cur = cursor();
556                         backspace(cur);
557                         //anchor() = cur;
558                 } else {
559                         cutSelection(cur, true, false);
560                 }
561                 break;
562
563         case LFUN_BREAK_PARAGRAPH:
564                 cap::replaceSelection(cur);
565                 breakParagraph(cur, false);
566                 cur.resetAnchor();
567                 break;
568
569         case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
570                 cap::replaceSelection(cur);
571                 breakParagraph(cur, true);
572                 cur.resetAnchor();
573                 break;
574
575         case LFUN_BREAK_PARAGRAPH_SKIP: {
576                 // When at the beginning of a paragraph, remove
577                 // indentation.  Otherwise, do the same as LFUN_BREAK_PARAGRAPH.
578                 cap::replaceSelection(cur);
579                 if (cur.pos() == 0)
580                         cur.paragraph().params().labelWidthString(docstring());
581                 else
582                         breakParagraph(cur, false);
583                 cur.resetAnchor();
584                 break;
585         }
586
587         // TODO
588         // With the creation of LFUN_PARAGRAPH_PARAMS, this is now redundant,
589         // as its duties can be performed there. Should it be removed??
590         // FIXME For now, it can just dispatch LFUN_PARAGRAPH_PARAMS...
591         case LFUN_PARAGRAPH_SPACING: {
592                 Paragraph & par = cur.paragraph();
593                 Spacing::Space cur_spacing = par.params().spacing().getSpace();
594                 string cur_value = "1.0";
595                 if (cur_spacing == Spacing::Other)
596                         cur_value = par.params().spacing().getValueAsString();
597
598                 istringstream is(to_utf8(cmd.argument()));
599                 string tmp;
600                 is >> tmp;
601                 Spacing::Space new_spacing = cur_spacing;
602                 string new_value = cur_value;
603                 if (tmp.empty()) {
604                         lyxerr << "Missing argument to `paragraph-spacing'"
605                                << endl;
606                 } else if (tmp == "single") {
607                         new_spacing = Spacing::Single;
608                 } else if (tmp == "onehalf") {
609                         new_spacing = Spacing::Onehalf;
610                 } else if (tmp == "double") {
611                         new_spacing = Spacing::Double;
612                 } else if (tmp == "other") {
613                         new_spacing = Spacing::Other;
614                         string tmpval = "0.0";
615                         is >> tmpval;
616                         lyxerr << "new_value = " << tmpval << endl;
617                         if (tmpval != "0.0")
618                                 new_value = tmpval;
619                 } else if (tmp == "default") {
620                         new_spacing = Spacing::Default;
621                 } else {
622                         lyxerr << to_utf8(_("Unknown spacing argument: "))
623                                << to_utf8(cmd.argument()) << endl;
624                 }
625                 if (cur_spacing != new_spacing || cur_value != new_value)
626                         par.params().spacing(Spacing(new_spacing, new_value));
627                 break;
628         }
629
630         case LFUN_INSET_INSERT: {
631                 recordUndo(cur);
632                 Inset * inset = createInset(bv, cmd);
633                 if (inset) {
634                         // FIXME (Abdel 01/02/2006):
635                         // What follows would be a partial fix for bug 2154:
636                         //   http://bugzilla.lyx.org/show_bug.cgi?id=2154
637                         // This automatically put the label inset _after_ a
638                         // numbered section. It should be possible to extend the mechanism
639                         // to any kind of LateX environement.
640                         // The correct way to fix that bug would be at LateX generation.
641                         // I'll let the code here for reference as it could be used for some
642                         // other feature like "automatic labelling".
643                         /*
644                         Paragraph & par = pars_[cur.pit()];
645                         if (inset->lyxCode() == Inset::LABEL_CODE
646                                 && par.layout()->labeltype == LABEL_COUNTER) {
647                                 // Go to the end of the paragraph
648                                 // Warning: Because of Change-Tracking, the last
649                                 // position is 'size()' and not 'size()-1':
650                                 cur.pos() = par.size();
651                                 // Insert a new paragraph
652                                 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
653                                 dispatch(cur, fr);
654                         }
655                         */
656                         if (cur.selection())
657                                 cutSelection(cur, true, false);
658                         insertInset(cur, inset);
659                         cur.posRight();
660                 }
661                 break;
662         }
663
664         case LFUN_INSET_DISSOLVE:
665                 needsUpdate |= dissolveInset(cur);
666                 break;
667
668         case LFUN_INSET_SETTINGS:
669                 cur.inset().showInsetDialog(bv);
670                 break;
671
672         case LFUN_SPACE_INSERT:
673                 if (cur.paragraph().layout()->free_spacing)
674                         insertChar(cur, ' ');
675                 else {
676                         doInsertInset(cur, this, cmd, false, false);
677                         cur.posRight();
678                 }
679                 moveCursor(cur, false);
680                 break;
681
682         case LFUN_HYPHENATION_POINT_INSERT:
683                 specialChar(cur, InsetSpecialChar::HYPHENATION);
684                 break;
685
686         case LFUN_LIGATURE_BREAK_INSERT:
687                 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
688                 break;
689
690         case LFUN_DOTS_INSERT:
691                 specialChar(cur, InsetSpecialChar::LDOTS);
692                 break;
693
694         case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
695                 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
696                 break;
697
698         case LFUN_MENU_SEPARATOR_INSERT:
699                 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
700                 break;
701
702         case LFUN_WORD_UPCASE:
703                 changeCase(cur, Text::text_uppercase);
704                 break;
705
706         case LFUN_WORD_LOWCASE:
707                 changeCase(cur, Text::text_lowercase);
708                 break;
709
710         case LFUN_WORD_CAPITALIZE:
711                 changeCase(cur, Text::text_capitalization);
712                 break;
713
714         case LFUN_CHARS_TRANSPOSE:
715                 charsTranspose(cur);
716                 break;
717
718         case LFUN_PASTE:
719                 cur.message(_("Paste"));
720                 cap::replaceSelection(cur);
721                 if (cmd.argument().empty() && !theClipboard().isInternal())
722                         pasteClipboard(cur, bv->buffer().errorList("Paste"));
723                 else {
724                         string const arg(to_utf8(cmd.argument()));
725                         pasteFromStack(cur, bv->buffer().errorList("Paste"),
726                                         isStrUnsignedInt(arg) ?
727                                                 convert<unsigned int>(arg) :
728                                                 0);
729                 }
730                 bv->buffer().errors("Paste");
731                 cur.clearSelection(); // bug 393
732                 finishUndo();
733                 break;
734
735         case LFUN_CUT:
736                 cutSelection(cur, true, true);
737                 cur.message(_("Cut"));
738                 break;
739
740         case LFUN_COPY:
741                 copySelection(cur);
742                 cur.message(_("Copy"));
743                 break;
744
745         case LFUN_SERVER_GET_XY:
746                 cur.message(from_utf8(
747                         convert<string>(tm.cursorX(cur.top(), cur.boundary()))
748                         + ' ' + convert<string>(tm.cursorY(cur.top(), cur.boundary()))));
749                 break;
750
751         case LFUN_SERVER_SET_XY: {
752                 int x = 0;
753                 int y = 0;
754                 istringstream is(to_utf8(cmd.argument()));
755                 is >> x >> y;
756                 if (!is)
757                         lyxerr << "SETXY: Could not parse coordinates in '"
758                                << to_utf8(cmd.argument()) << std::endl;
759                 else
760                         tm.setCursorFromCoordinates(cur, x, y);
761                 break;
762         }
763
764         case LFUN_SERVER_GET_FONT:
765                 if (cur.current_font.shape() == Font::ITALIC_SHAPE)
766                         cur.message(from_ascii("E"));
767                 else if (cur.current_font.shape() == Font::SMALLCAPS_SHAPE)
768                         cur.message(from_ascii("N"));
769                 else
770                         cur.message(from_ascii("0"));
771                 break;
772
773         case LFUN_SERVER_GET_LAYOUT:
774                 cur.message(cur.paragraph().layout()->name());
775                 break;
776
777         case LFUN_LAYOUT: {
778                 docstring layout = cmd.argument();
779                 LYXERR(Debug::INFO) << "LFUN_LAYOUT: (arg) " << to_utf8(layout) << endl;
780
781                 docstring const old_layout = cur.paragraph().layout()->name();
782
783                 // Derive layout number from given argument (string)
784                 // and current buffer's textclass (number)
785                 TextClass const & tclass = bv->buffer().params().getTextClass();
786                 if (layout.empty())
787                         layout = tclass.defaultLayoutName();
788                 bool hasLayout = tclass.hasLayout(layout);
789
790                 // If the entry is obsolete, use the new one instead.
791                 if (hasLayout) {
792                         docstring const & obs = tclass[layout]->obsoleted_by();
793                         if (!obs.empty())
794                                 layout = obs;
795                 }
796
797                 if (!hasLayout) {
798                         cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
799                                 from_utf8(N_(" not known")));
800                         break;
801                 }
802
803                 bool change_layout = (old_layout != layout);
804
805                 if (!change_layout && cur.selection() &&
806                         cur.selBegin().pit() != cur.selEnd().pit())
807                 {
808                         pit_type spit = cur.selBegin().pit();
809                         pit_type epit = cur.selEnd().pit() + 1;
810                         while (spit != epit) {
811                                 if (pars_[spit].layout()->name() != old_layout) {
812                                         change_layout = true;
813                                         break;
814                                 }
815                                 ++spit;
816                         }
817                 }
818
819                 if (change_layout)
820                         setLayout(cur, layout);
821
822                 break;
823         }
824
825         case LFUN_CLIPBOARD_PASTE:
826                 cur.clearSelection();
827                 pasteClipboard(cur, bv->buffer().errorList("Paste"),
828                                cmd.argument() == "paragraph");
829                 bv->buffer().errors("Paste");
830                 break;
831
832         case LFUN_PRIMARY_SELECTION_PASTE:
833                 pasteString(cur, theSelection().get(),
834                             cmd.argument() == "paragraph");
835                 break;
836
837         case LFUN_UNICODE_INSERT: {
838                 if (cmd.argument().empty())
839                         break;
840                 docstring hexstring = cmd.argument();
841                 if (lyx::support::isHex(hexstring)) {
842                         char_type c = lyx::support::hexToInt(hexstring);
843                         if (c >= 32 && c < 0x10ffff) {
844                                 lyxerr << "Inserting c: " << c << endl;
845                                 docstring s = docstring(1, c);
846                                 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
847                         }
848                 }
849                 break;
850         }
851
852         case LFUN_QUOTE_INSERT: {
853                 Paragraph & par = cur.paragraph();
854                 pos_type pos = cur.pos();
855                 BufferParams const & bufparams = bv->buffer().params();
856                 LayoutPtr const & style = par.layout();
857                 if (!style->pass_thru
858                     && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
859                         // this avoids a double undo
860                         // FIXME: should not be needed, ideally
861                         if (!cur.selection())
862                                 recordUndo(cur);
863                         cap::replaceSelection(cur);
864                         pos = cur.pos();
865                         char_type c;
866                         if (pos == 0)
867                                 c = ' ';
868                         else if (cur.prevInset() && cur.prevInset()->isSpace())
869                                 c = ' ';
870                         else
871                                 c = par.getChar(pos - 1);
872                         string arg = to_utf8(cmd.argument());
873                         if (arg == "single")
874                                 cur.insert(new InsetQuotes(c,
875                                     bufparams.quotes_language,
876                                     InsetQuotes::SingleQ));
877                         else
878                                 cur.insert(new InsetQuotes(c,
879                                     bufparams.quotes_language,
880                                     InsetQuotes::DoubleQ));
881                         cur.posRight();
882                 }
883                 else
884                         lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
885                 break;
886         }
887
888         case LFUN_DATE_INSERT: {
889                 string const format = cmd.argument().empty()
890                         ? lyxrc.date_insert_format : to_utf8(cmd.argument());
891                 string const time = formatted_time(current_time(), format);
892                 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
893                 break;
894         }
895
896         case LFUN_MOUSE_TRIPLE:
897                 if (cmd.button() == mouse_button::button1) {
898                         tm.cursorHome(cur);
899                         cur.resetAnchor();
900                         tm.cursorEnd(cur);
901                         cur.setSelection();
902                         bv->cursor() = cur;
903                 }
904                 break;
905
906         case LFUN_MOUSE_DOUBLE:
907                 if (cmd.button() == mouse_button::button1) {
908                         selectWord(cur, WHOLE_WORD_STRICT);
909                         bv->cursor() = cur;
910                 }
911                 break;
912
913         // Single-click on work area
914         case LFUN_MOUSE_PRESS: {
915                 // Right click on a footnote flag opens float menu
916                 if (cmd.button() == mouse_button::button3)
917                         cur.clearSelection();
918
919                 // Set the cursor
920                 bool update = bv->mouseSetCursor(cur);
921
922                 // Insert primary selection with middle mouse
923                 // if there is a local selection in the current buffer,
924                 // insert this
925                 if (cmd.button() == mouse_button::button2) {
926                         if (cap::selection()) {
927                                 // Copy the selection buffer to the clipboard
928                                 // stack, because we want it to appear in the
929                                 // "Edit->Paste recent" menu.
930                                 cap::copySelectionToStack();
931
932                                 cap::pasteSelection(bv->cursor(), 
933                                                     bv->buffer().errorList("Paste"));
934                                 bv->buffer().errors("Paste");
935                                 bv->buffer().markDirty();
936                                 finishUndo();
937                         } else {
938                                 lyx::dispatch(FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph"));
939                         }
940                 }
941
942                 // we have to update after dEPM triggered
943                 if (!update && cmd.button() == mouse_button::button1) {
944                         needsUpdate = false;
945                         cur.noUpdate();
946                 }
947
948                 break;
949         }
950
951         case LFUN_MOUSE_MOTION: {
952                 // Only use motion with button 1
953                 //if (cmd.button() != mouse_button::button1)
954                 //      return false;
955
956                 // ignore motions deeper nested than the real anchor
957                 Cursor & bvcur = cur.bv().cursor();
958                 if (bvcur.anchor_.hasPart(cur)) {
959                         CursorSlice old = bvcur.top();
960
961                         int const wh = bv->workHeight();
962                         int const y = std::max(0, std::min(wh - 1, cmd.y));
963
964                         tm.setCursorFromCoordinates(cur, cmd.x, y);
965                         cur.setTargetX(cmd.x);
966                         if (cmd.y >= wh)
967                                 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
968                         else if (cmd.y < 0)
969                                 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
970                         // This is to allow jumping over large insets
971                         if (cur.top() == old) {
972                                 if (cmd.y >= wh)
973                                         lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
974                                 else if (cmd.y < 0)
975                                         lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
976                         }
977
978                         if (cur.top() == old)
979                                 cur.noUpdate();
980                         else {
981                                 // don't set anchor_
982                                 bvcur.setCursor(cur);
983                                 bvcur.selection() = true;
984                                 //lyxerr << "MOTION: " << bv->cursor() << endl;
985                         }
986
987                 } else
988                         cur.undispatched();
989                 break;
990         }
991
992         case LFUN_MOUSE_RELEASE: {
993                 if (cmd.button() == mouse_button::button2)
994                         break;
995
996                 if (cmd.button() == mouse_button::button1) {
997                         // if there is new selection, update persistent
998                         // selection, otherwise, single click does not
999                         // clear persistent selection buffer
1000                         if (cur.selection()) {
1001                                 // finish selection
1002                                 // if double click, cur is moved to the end of word by selectWord
1003                                 // but bvcur is current mouse position
1004                                 Cursor & bvcur = cur.bv().cursor();
1005                                 bvcur.selection() = true;
1006                         }
1007                         needsUpdate = false;
1008                         cur.noUpdate();
1009                 }
1010
1011                 break;
1012         }
1013
1014         case LFUN_SELF_INSERT: {
1015                 if (cmd.argument().empty())
1016                         break;
1017
1018                 // Automatically delete the currently selected
1019                 // text and replace it with what is being
1020                 // typed in now. Depends on lyxrc settings
1021                 // "auto_region_delete", which defaults to
1022                 // true (on).
1023
1024                 if (lyxrc.auto_region_delete && cur.selection()) {
1025                         cutSelection(cur, false, false);
1026                         // When change tracking is set to off, the metrics update
1027                         // mechanism correctly detects if a full update is needed or not.
1028                         // This detection fails when a selection spans multiple rows and
1029                         // change tracking is enabled because the paragraph metrics stays
1030                         // the same. In this case, we force the full update:
1031                         // (see http://bugzilla.lyx.org/show_bug.cgi?id=3992)
1032                         if (cur.buffer().params().trackChanges)
1033                                 cur.updateFlags(Update::Force);
1034                 }
1035
1036                 cur.clearSelection();
1037                 Font const old_font = cur.real_current_font;
1038
1039                 docstring::const_iterator cit = cmd.argument().begin();
1040                 docstring::const_iterator end = cmd.argument().end();
1041                 for (; cit != end; ++cit)
1042                         bv->translateAndInsert(*cit, this, cur);
1043
1044                 cur.resetAnchor();
1045                 moveCursor(cur, false);
1046                 break;
1047         }
1048
1049         case LFUN_URL_INSERT: {
1050                 InsetCommandParams p("url");
1051                 docstring content;
1052                 if (cur.selection()) {
1053                         content = cur.selectionAsString(false);
1054                         cutSelection(cur, true, false);
1055                 }
1056                 p["target"] = (cmd.argument().empty()) ?
1057                         content : cmd.argument();
1058                 string const data = InsetCommandMailer::params2string("url", p);
1059                 if (p["target"].empty()) {
1060                         bv->showInsetDialog("url", data, 0);
1061                 } else {
1062                         FuncRequest fr(LFUN_INSET_INSERT, data);
1063                         dispatch(cur, fr);
1064                 }
1065                 break;
1066         }
1067
1068         case LFUN_HTML_INSERT: {
1069                 InsetCommandParams p("htmlurl");
1070                 docstring content;
1071                 if (cur.selection()) {
1072                         content = cur.selectionAsString(false);
1073                         cutSelection(cur, true, false);
1074                 }
1075                 p["target"] = (cmd.argument().empty()) ?
1076                         content : cmd.argument();
1077                 string const data = InsetCommandMailer::params2string("url", p);
1078                 if (p["target"].empty()) {
1079                         bv->showInsetDialog("url", data, 0);
1080                 } else {
1081                         FuncRequest fr(LFUN_INSET_INSERT, data);
1082                         dispatch(cur, fr);
1083                 }
1084                 break;
1085         }
1086
1087         case LFUN_LABEL_INSERT: {
1088                 InsetCommandParams p("label");
1089                 // Try to generate a valid label
1090                 p["name"] = (cmd.argument().empty()) ?
1091                         cur.getPossibleLabel() :
1092                         cmd.argument();
1093                 string const data = InsetCommandMailer::params2string("label", p);
1094
1095                 if (cmd.argument().empty()) {
1096                         bv->showInsetDialog("label", data, 0);
1097                 } else {
1098                         FuncRequest fr(LFUN_INSET_INSERT, data);
1099                         dispatch(cur, fr);
1100                 }
1101                 break;
1102         }
1103
1104
1105 #if 0
1106         case LFUN_LIST_INSERT:
1107         case LFUN_THEOREM_INSERT:
1108 #endif
1109         case LFUN_CAPTION_INSERT:
1110                 // Open the inset, and move the current selection
1111                 // inside it.
1112                 doInsertInset(cur, this, cmd, true, true);
1113                 cur.posRight();
1114                 updateLabels(bv->buffer());
1115                 break;
1116         case LFUN_NOTE_INSERT:
1117         case LFUN_FLEX_INSERT:
1118         case LFUN_BOX_INSERT:
1119         case LFUN_BRANCH_INSERT:
1120         case LFUN_BIBITEM_INSERT:
1121         case LFUN_ERT_INSERT:
1122         case LFUN_LISTING_INSERT:
1123         case LFUN_FOOTNOTE_INSERT:
1124         case LFUN_MARGINALNOTE_INSERT:
1125         case LFUN_OPTIONAL_INSERT:
1126         case LFUN_ENVIRONMENT_INSERT:
1127                 // Open the inset, and move the current selection
1128                 // inside it.
1129                 doInsertInset(cur, this, cmd, true, true);
1130                 cur.posRight();
1131                 break;
1132
1133         case LFUN_TABULAR_INSERT:
1134                 // if there were no arguments, just open the dialog
1135                 if (doInsertInset(cur, this, cmd, false, true))
1136                         cur.posRight();
1137                 else
1138                         bv->showDialog("tabularcreate");
1139
1140                 break;
1141
1142         case LFUN_FLOAT_INSERT:
1143         case LFUN_FLOAT_WIDE_INSERT:
1144         case LFUN_WRAP_INSERT: {
1145                 bool content = cur.selection();  // will some text be moved into the inset?
1146
1147                 doInsertInset(cur, this, cmd, true, true);
1148                 cur.posRight();
1149                 ParagraphList & pars = cur.text()->paragraphs();
1150
1151                 TextClass const & tclass = bv->buffer().params().getTextClass();
1152
1153                 // add a separate paragraph for the caption inset
1154                 pars.push_back(Paragraph());
1155                 pars.back().setInsetOwner(pars[0].inInset());
1156                 pars.back().layout(tclass.defaultLayout());
1157
1158                 int cap_pit = pars.size() - 1;
1159
1160                 // if an empty inset was created, we create an additional empty
1161                 // paragraph at the bottom so that the user can choose where to put
1162                 // the graphics (or table).
1163                 if (!content) {
1164                         pars.push_back(Paragraph());
1165                         pars.back().setInsetOwner(pars[0].inInset());
1166                         pars.back().layout(tclass.defaultLayout());
1167
1168                 }
1169
1170                 // reposition the cursor to the caption
1171                 cur.pit() = cap_pit;
1172                 cur.pos() = 0;
1173                 // FIXME: This Text/Cursor dispatch handling is a mess!
1174                 // We cannot use Cursor::dispatch here it needs access to up to
1175                 // date metrics.
1176                 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1177                 cur.text()->dispatch(cur, cmd_caption);
1178                 cur.updateFlags(Update::Force);
1179                 // FIXME: When leaving the Float (or Wrap) inset we should
1180                 // delete any empty paragraph left above or below the
1181                 // caption.
1182                 break;
1183         }
1184
1185         case LFUN_INDEX_INSERT:
1186                 doInsertInset(cur, this, cmd, true, true);
1187                 cur.posRight();
1188                 break;
1189         case LFUN_NOMENCL_INSERT: {
1190                 Inset * inset = createInset(&cur.bv(), cmd);
1191                 if (!inset)
1192                         break;
1193                 recordUndo(cur);
1194                 cur.clearSelection();
1195                 insertInset(cur, inset);
1196                 // Show the dialog for the nomenclature entry, since the
1197                 // description entry still needs to be filled in.
1198                 if (cmd.action == LFUN_NOMENCL_INSERT)
1199                         inset->edit(cur, true);
1200                 cur.posRight();
1201                 break;
1202         }
1203
1204         case LFUN_INDEX_PRINT:
1205         case LFUN_NOMENCL_PRINT:
1206         case LFUN_TOC_INSERT:
1207         case LFUN_HFILL_INSERT:
1208         case LFUN_LINE_INSERT:
1209         case LFUN_PAGEBREAK_INSERT:
1210         case LFUN_CLEARPAGE_INSERT:
1211         case LFUN_CLEARDOUBLEPAGE_INSERT:
1212                 // do nothing fancy
1213                 doInsertInset(cur, this, cmd, false, false);
1214                 cur.posRight();
1215                 break;
1216
1217         case LFUN_DEPTH_DECREMENT:
1218                 changeDepth(cur, DEC_DEPTH);
1219                 break;
1220
1221         case LFUN_DEPTH_INCREMENT:
1222                 changeDepth(cur, INC_DEPTH);
1223                 break;
1224
1225         case LFUN_MATH_DISPLAY:
1226                 mathDispatch(cur, cmd, true);
1227                 break;
1228
1229         case LFUN_MATH_IMPORT_SELECTION:
1230         case LFUN_MATH_MODE:
1231                 if (cmd.argument() == "on")
1232                         // don't pass "on" as argument
1233                         mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1234                 else
1235                         mathDispatch(cur, cmd, false);
1236                 break;
1237
1238         case LFUN_MATH_MACRO:
1239                 if (cmd.argument().empty())
1240                         cur.errorMessage(from_utf8(N_("Missing argument")));
1241                 else {
1242                         string s = to_utf8(cmd.argument());
1243                         string const s1 = token(s, ' ', 1);
1244                         int const nargs = s1.empty() ? 0 : convert<int>(s1);
1245                         string const s2 = token(s, ' ', 2);
1246                         string const type = s2.empty() ? "newcommand" : s2;
1247                         cur.insert(new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, from_utf8(type)));
1248                         //cur.nextInset()->edit(cur, true);
1249                 }
1250                 break;
1251
1252         // passthrough hat and underscore outside mathed:
1253         case LFUN_MATH_SUBSCRIPT:
1254                 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1255                 break;
1256         case LFUN_MATH_SUPERSCRIPT:
1257                 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1258                 break;
1259
1260         case LFUN_MATH_INSERT:
1261         case LFUN_MATH_MATRIX:
1262         case LFUN_MATH_DELIM:
1263         case LFUN_MATH_BIGDELIM: {
1264                 if (cur.selection())
1265                         cur.clearSelection();
1266                 // FIXME: instead of the above, this one
1267                 // should be used (but it asserts with Bidi enabled)
1268                 // cf. http://bugzilla.lyx.org/show_bug.cgi?id=4055
1269                 // cap::replaceSelection(cur);
1270                 cur.insert(new InsetMathHull(hullSimple));
1271                 checkAndActivateInset(cur, true);
1272                 BOOST_ASSERT(cur.inMathed());
1273                 cur.dispatch(cmd);
1274                 break;
1275         }
1276
1277         case LFUN_FONT_EMPH: {
1278                 Font font(Font::ALL_IGNORE);
1279                 font.setEmph(Font::TOGGLE);
1280                 toggleAndShow(cur, this, font);
1281                 break;
1282         }
1283
1284         case LFUN_FONT_BOLD: {
1285                 Font font(Font::ALL_IGNORE);
1286                 font.setSeries(Font::BOLD_SERIES);
1287                 toggleAndShow(cur, this, font);
1288                 break;
1289         }
1290
1291         case LFUN_FONT_NOUN: {
1292                 Font font(Font::ALL_IGNORE);
1293                 font.setNoun(Font::TOGGLE);
1294                 toggleAndShow(cur, this, font);
1295                 break;
1296         }
1297
1298         case LFUN_FONT_TYPEWRITER: {
1299                 Font font(Font::ALL_IGNORE);
1300                 font.setFamily(Font::TYPEWRITER_FAMILY); // no good
1301                 toggleAndShow(cur, this, font);
1302                 break;
1303         }
1304
1305         case LFUN_FONT_SANS: {
1306                 Font font(Font::ALL_IGNORE);
1307                 font.setFamily(Font::SANS_FAMILY);
1308                 toggleAndShow(cur, this, font);
1309                 break;
1310         }
1311
1312         case LFUN_FONT_ROMAN: {
1313                 Font font(Font::ALL_IGNORE);
1314                 font.setFamily(Font::ROMAN_FAMILY);
1315                 toggleAndShow(cur, this, font);
1316                 break;
1317         }
1318
1319         case LFUN_FONT_DEFAULT: {
1320                 Font font(Font::ALL_INHERIT, ignore_language);
1321                 toggleAndShow(cur, this, font);
1322                 break;
1323         }
1324
1325         case LFUN_FONT_UNDERLINE: {
1326                 Font font(Font::ALL_IGNORE);
1327                 font.setUnderbar(Font::TOGGLE);
1328                 toggleAndShow(cur, this, font);
1329                 break;
1330         }
1331
1332         case LFUN_FONT_SIZE: {
1333                 Font font(Font::ALL_IGNORE);
1334                 font.setLyXSize(to_utf8(cmd.argument()));
1335                 toggleAndShow(cur, this, font);
1336                 break;
1337         }
1338
1339         case LFUN_LANGUAGE: {
1340                 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1341                 if (!lang)
1342                         break;
1343                 Font font(Font::ALL_IGNORE);
1344                 font.setLanguage(lang);
1345                 toggleAndShow(cur, this, font);
1346                 break;
1347         }
1348
1349         case LFUN_FONT_FREE_APPLY:
1350                 toggleAndShow(cur, this, freefont, toggleall);
1351                 cur.message(_("Character set"));
1352                 break;
1353
1354         // Set the freefont using the contents of \param data dispatched from
1355         // the frontends and apply it at the current cursor location.
1356         case LFUN_FONT_FREE_UPDATE: {
1357                 Font font;
1358                 bool toggle;
1359                 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1360                         freefont = font;
1361                         toggleall = toggle;
1362                         toggleAndShow(cur, this, freefont, toggleall);
1363                         cur.message(_("Character set"));
1364                 }
1365                 break;
1366         }
1367
1368         case LFUN_FINISHED_LEFT:
1369                 LYXERR(Debug::DEBUG) << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1370                 if (reverseDirectionNeeded(cur)) {
1371                         ++cur.pos();
1372                         cur.setCurrentFont();
1373                 }
1374                 break;
1375
1376         case LFUN_FINISHED_RIGHT:
1377                 LYXERR(Debug::DEBUG) << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1378                 if (!reverseDirectionNeeded(cur)) {
1379                         ++cur.pos();
1380                         cur.setCurrentFont();
1381                 }
1382                 break;
1383
1384         case LFUN_LAYOUT_PARAGRAPH: {
1385                 string data;
1386                 params2string(cur.paragraph(), data);
1387                 data = "show\n" + data;
1388                 bv->showDialogWithData("paragraph", data);
1389                 break;
1390         }
1391
1392         case LFUN_PARAGRAPH_UPDATE: {
1393                 string data;
1394                 params2string(cur.paragraph(), data);
1395
1396                 // Will the paragraph accept changes from the dialog?
1397                 bool const accept = !cur.inset().forceDefaultParagraphs(cur.idx());
1398
1399                 data = "update " + convert<string>(accept) + '\n' + data;
1400                 bv->updateDialog("paragraph", data);
1401                 break;
1402         }
1403
1404         case LFUN_ACCENT_UMLAUT:
1405         case LFUN_ACCENT_CIRCUMFLEX:
1406         case LFUN_ACCENT_GRAVE:
1407         case LFUN_ACCENT_ACUTE:
1408         case LFUN_ACCENT_TILDE:
1409         case LFUN_ACCENT_CEDILLA:
1410         case LFUN_ACCENT_MACRON:
1411         case LFUN_ACCENT_DOT:
1412         case LFUN_ACCENT_UNDERDOT:
1413         case LFUN_ACCENT_UNDERBAR:
1414         case LFUN_ACCENT_CARON:
1415         case LFUN_ACCENT_SPECIAL_CARON:
1416         case LFUN_ACCENT_BREVE:
1417         case LFUN_ACCENT_TIE:
1418         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1419         case LFUN_ACCENT_CIRCLE:
1420         case LFUN_ACCENT_OGONEK:
1421                 theLyXFunc().handleKeyFunc(cmd.action);
1422                 if (!cmd.argument().empty())
1423                         // FIXME: Are all these characters encoded in one byte in utf8?
1424                         bv->translateAndInsert(cmd.argument()[0], this, cur);
1425                 break;
1426
1427         case LFUN_FLOAT_LIST: {
1428                 TextClass const & tclass = bv->buffer().params().getTextClass();
1429                 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1430                         recordUndo(cur);
1431                         if (cur.selection())
1432                                 cutSelection(cur, true, false);
1433                         breakParagraph(cur);
1434
1435                         if (cur.lastpos() != 0) {
1436                                 cursorLeft(cur);
1437                                 breakParagraph(cur);
1438                         }
1439
1440                         setLayout(cur, tclass.defaultLayoutName());
1441                         ParagraphParameters p;
1442                         setParagraphs(cur, p);
1443                         insertInset(cur, new InsetFloatList(to_utf8(cmd.argument())));
1444                         cur.posRight();
1445                 } else {
1446                         lyxerr << "Non-existent float type: "
1447                                << to_utf8(cmd.argument()) << endl;
1448                 }
1449                 break;
1450         }
1451
1452         case LFUN_CHANGE_ACCEPT: {
1453                 acceptOrRejectChanges(cur, ACCEPT);
1454                 break;
1455         }
1456
1457         case LFUN_CHANGE_REJECT: {
1458                 acceptOrRejectChanges(cur, REJECT);
1459                 break;
1460         }
1461
1462         case LFUN_THESAURUS_ENTRY: {
1463                 docstring arg = cmd.argument();
1464                 if (arg.empty()) {
1465                         arg = cur.selectionAsString(false);
1466                         // FIXME
1467                         if (arg.size() > 100 || arg.empty()) {
1468                                 // Get word or selection
1469                                 selectWordWhenUnderCursor(cur, WHOLE_WORD);
1470                                 arg = cur.selectionAsString(false);
1471                         }
1472                 }
1473                 bv->showDialogWithData("thesaurus", to_utf8(arg));
1474                 break;
1475         }
1476
1477         case LFUN_PARAGRAPH_PARAMS_APPLY: {
1478                 // Given data, an encoding of the ParagraphParameters
1479                 // generated in the Paragraph dialog, this function sets
1480                 // the current paragraph, or currently selected paragraphs,
1481                 // appropriately. 
1482                 // NOTE: This function overrides all existing settings.
1483                 setParagraphs(cur, cmd.argument());
1484                 cur.message(_("Paragraph layout set"));
1485                 break;
1486         }
1487         
1488         case LFUN_PARAGRAPH_PARAMS: {
1489                 // Given data, an encoding of the ParagraphParameters as we'd
1490                 // find them in a LyX file, this function modifies the current paragraph, 
1491                 // or currently selected paragraphs. 
1492                 // NOTE: This function only modifies, and does not override, existing
1493                 // settings.
1494                 setParagraphs(cur, cmd.argument(), true);
1495                 cur.message(_("Paragraph layout set"));
1496                 break;
1497         }
1498
1499         case LFUN_ESCAPE:
1500                 if (cur.selection()) {
1501                         cur.selection() = false;
1502                 } else {
1503                         cur.undispatched();
1504                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1505                 }
1506                 break;
1507
1508         default:
1509                 LYXERR(Debug::ACTION)
1510                         << BOOST_CURRENT_FUNCTION
1511                         << ": Command " << cmd
1512                         << " not DISPATCHED by Text" << endl;
1513                 cur.undispatched();
1514                 break;
1515         }
1516
1517         needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
1518
1519         // FIXME: The cursor flag is reset two lines below
1520         // so we need to check here if some of the LFUN did touch that.
1521         // for now only Text::erase() and Text::backspace() do that.
1522         // The plan is to verify all the LFUNs and then to remove this
1523         // singleParUpdate boolean altogether.
1524         if (cur.result().update() & Update::Force) {
1525                 singleParUpdate = false;
1526                 needsUpdate = true;
1527         }
1528
1529         // FIXME: the following code should go in favor of fine grained
1530         // update flag treatment.
1531         if (singleParUpdate) {
1532                 // Inserting characters does not change par height
1533                 ParagraphMetrics const & pms
1534                         = cur.bv().parMetrics(cur.bottom().text(), cur.bottom().pit());
1535                 if (pms.dim().height()
1536                     == olddim.height()) {
1537                         // if so, update _only_ this paragraph
1538                         cur.updateFlags(Update::SinglePar |
1539                                 Update::FitCursor |
1540                                 Update::MultiParSel);
1541                         return;
1542                 } else
1543                         needsUpdate = true;
1544         }
1545
1546         if (!needsUpdate
1547             && &oldTopSlice.inset() == &cur.inset()
1548             && oldTopSlice.idx() == cur.idx()
1549             && !sel // sel is a backup of cur.selection() at the biginning of the function.
1550             && !cur.selection())
1551                 // FIXME: it would be better if we could just do this
1552                 //
1553                 //if (cur.result().update() != Update::FitCursor)
1554                 //      cur.noUpdate();
1555                 //
1556                 // But some LFUNs do not set Update::FitCursor when needed, so we
1557                 // do it for all. This is not very harmfull as FitCursor will provoke
1558                 // a full redraw only if needed but still, a proper review of all LFUN
1559                 // should be done and this needsUpdate boolean can then be removed.
1560                 cur.updateFlags(Update::FitCursor);
1561         else
1562                 cur.updateFlags(Update::Force | Update::FitCursor);
1563 }
1564
1565
1566 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
1567                         FuncStatus & flag) const
1568 {
1569         BOOST_ASSERT(cur.text() == this);
1570
1571         Font const & font = cur.real_current_font;
1572         bool enable = true;
1573         Inset::Code code = Inset::NO_CODE;
1574
1575         switch (cmd.action) {
1576
1577         case LFUN_DEPTH_DECREMENT:
1578                 enable = changeDepthAllowed(cur, DEC_DEPTH);
1579                 break;
1580
1581         case LFUN_DEPTH_INCREMENT:
1582                 enable = changeDepthAllowed(cur, INC_DEPTH);
1583                 break;
1584
1585         case LFUN_APPENDIX:
1586                 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1587                 return true;
1588
1589         case LFUN_BIBITEM_INSERT:
1590                 enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO
1591                           && cur.pos() == 0);
1592                 break;
1593
1594         case LFUN_DIALOG_SHOW_NEW_INSET:
1595                 if (cmd.argument() == "bibitem")
1596                         code = Inset::BIBITEM_CODE;
1597                 else if (cmd.argument() == "bibtex")
1598                         code = Inset::BIBTEX_CODE;
1599                 else if (cmd.argument() == "box")
1600                         code = Inset::BOX_CODE;
1601                 else if (cmd.argument() == "branch")
1602                         code = Inset::BRANCH_CODE;
1603                 else if (cmd.argument() == "citation")
1604                         code = Inset::CITE_CODE;
1605                 else if (cmd.argument() == "ert")
1606                         code = Inset::ERT_CODE;
1607                 else if (cmd.argument() == "external")
1608                         code = Inset::EXTERNAL_CODE;
1609                 else if (cmd.argument() == "float")
1610                         code = Inset::FLOAT_CODE;
1611                 else if (cmd.argument() == "graphics")
1612                         code = Inset::GRAPHICS_CODE;
1613                 else if (cmd.argument() == "include")
1614                         code = Inset::INCLUDE_CODE;
1615                 else if (cmd.argument() == "index")
1616                         code = Inset::INDEX_CODE;
1617                 else if (cmd.argument() == "nomenclature")
1618                         code = Inset::NOMENCL_CODE;
1619                 else if (cmd.argument() == "label")
1620                         code = Inset::LABEL_CODE;
1621                 else if (cmd.argument() == "note")
1622                         code = Inset::NOTE_CODE;
1623                 else if (cmd.argument() == "ref")
1624                         code = Inset::REF_CODE;
1625                 else if (cmd.argument() == "toc")
1626                         code = Inset::TOC_CODE;
1627                 else if (cmd.argument() == "url")
1628                         code = Inset::URL_CODE;
1629                 else if (cmd.argument() == "vspace")
1630                         code = Inset::VSPACE_CODE;
1631                 else if (cmd.argument() == "wrap")
1632                         code = Inset::WRAP_CODE;
1633                 else if (cmd.argument() == "listings")
1634                         code = Inset::LISTINGS_CODE;
1635                 break;
1636
1637         case LFUN_ERT_INSERT:
1638                 code = Inset::ERT_CODE;
1639                 break;
1640         case LFUN_LISTING_INSERT:
1641             code = Inset::LISTINGS_CODE;
1642                 break;
1643         case LFUN_FOOTNOTE_INSERT:
1644                 code = Inset::FOOT_CODE;
1645                 break;
1646         case LFUN_TABULAR_INSERT:
1647                 code = Inset::TABULAR_CODE;
1648                 break;
1649         case LFUN_MARGINALNOTE_INSERT:
1650                 code = Inset::MARGIN_CODE;
1651                 break;
1652         case LFUN_FLOAT_INSERT:
1653         case LFUN_FLOAT_WIDE_INSERT:
1654                 code = Inset::FLOAT_CODE;
1655                 break;
1656         case LFUN_WRAP_INSERT:
1657                 code = Inset::WRAP_CODE;
1658                 break;
1659         case LFUN_FLOAT_LIST:
1660                 code = Inset::FLOAT_LIST_CODE;
1661                 break;
1662 #if 0
1663         case LFUN_LIST_INSERT:
1664                 code = Inset::LIST_CODE;
1665                 break;
1666         case LFUN_THEOREM_INSERT:
1667                 code = Inset::THEOREM_CODE;
1668                 break;
1669 #endif
1670         case LFUN_CAPTION_INSERT:
1671                 code = Inset::CAPTION_CODE;
1672                 break;
1673         case LFUN_NOTE_INSERT:
1674                 code = Inset::NOTE_CODE;
1675                 break;
1676         case LFUN_FLEX_INSERT: {
1677                 code = Inset::FLEX_CODE;
1678                 string s = cmd.getArg(0);
1679                 InsetLayout il =  cur.buffer().params().getTextClass().insetlayout(from_utf8(s));
1680                 if (il.lyxtype != "charstyle" &&
1681                     il.lyxtype != "custom" &&
1682                     il.lyxtype != "element")
1683                         enable = false;
1684                 break;
1685                 }
1686         case LFUN_BOX_INSERT:
1687                 code = Inset::BOX_CODE;
1688                 break;
1689         case LFUN_BRANCH_INSERT:
1690                 code = Inset::BRANCH_CODE;
1691                 if (cur.buffer().getMasterBuffer()->params().branchlist().empty())
1692                         enable = false;
1693                 break;
1694         case LFUN_LABEL_INSERT:
1695                 code = Inset::LABEL_CODE;
1696                 break;
1697         case LFUN_OPTIONAL_INSERT:
1698                 code = Inset::OPTARG_CODE;
1699                 enable = numberOfOptArgs(cur.paragraph())
1700                         < cur.paragraph().layout()->optionalargs;
1701                 break;
1702         case LFUN_ENVIRONMENT_INSERT:
1703                 code = Inset::BOX_CODE;
1704                 break;
1705         case LFUN_INDEX_INSERT:
1706                 code = Inset::INDEX_CODE;
1707                 break;
1708         case LFUN_INDEX_PRINT:
1709                 code = Inset::INDEX_PRINT_CODE;
1710                 break;
1711         case LFUN_NOMENCL_INSERT:
1712                 code = Inset::NOMENCL_CODE;
1713                 break;
1714         case LFUN_NOMENCL_PRINT:
1715                 code = Inset::NOMENCL_PRINT_CODE;
1716                 break;
1717         case LFUN_TOC_INSERT:
1718                 code = Inset::TOC_CODE;
1719                 break;
1720         case LFUN_HTML_INSERT:
1721         case LFUN_URL_INSERT:
1722                 code = Inset::URL_CODE;
1723                 break;
1724         case LFUN_QUOTE_INSERT:
1725                 // always allow this, since we will inset a raw quote
1726                 // if an inset is not allowed.
1727                 break;
1728         case LFUN_HYPHENATION_POINT_INSERT:
1729         case LFUN_LIGATURE_BREAK_INSERT:
1730         case LFUN_HFILL_INSERT:
1731         case LFUN_MENU_SEPARATOR_INSERT:
1732         case LFUN_DOTS_INSERT:
1733         case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
1734                 code = Inset::SPECIALCHAR_CODE;
1735                 break;
1736         case LFUN_SPACE_INSERT:
1737                 // slight hack: we know this is allowed in math mode
1738                 if (cur.inTexted())
1739                         code = Inset::SPACE_CODE;
1740                 break;
1741
1742         case LFUN_INSET_MODIFY:
1743                 // We need to disable this, because we may get called for a
1744                 // tabular cell via
1745                 // InsetTabular::getStatus() -> InsetText::getStatus()
1746                 // and we don't handle LFUN_INSET_MODIFY.
1747                 enable = false;
1748                 break;
1749
1750         case LFUN_FONT_EMPH:
1751                 flag.setOnOff(font.emph() == Font::ON);
1752                 return true;
1753
1754         case LFUN_FONT_NOUN:
1755                 flag.setOnOff(font.noun() == Font::ON);
1756                 return true;
1757
1758         case LFUN_FONT_BOLD:
1759                 flag.setOnOff(font.series() == Font::BOLD_SERIES);
1760                 return true;
1761
1762         case LFUN_FONT_SANS:
1763                 flag.setOnOff(font.family() == Font::SANS_FAMILY);
1764                 return true;
1765
1766         case LFUN_FONT_ROMAN:
1767                 flag.setOnOff(font.family() == Font::ROMAN_FAMILY);
1768                 return true;
1769
1770         case LFUN_FONT_TYPEWRITER:
1771                 flag.setOnOff(font.family() == Font::TYPEWRITER_FAMILY);
1772                 return true;
1773
1774         case LFUN_CUT:
1775         case LFUN_COPY:
1776                 enable = cur.selection();
1777                 break;
1778
1779         case LFUN_PASTE:
1780                 if (cmd.argument().empty()) {
1781                         if (theClipboard().isInternal())
1782                                 enable = cap::numberOfSelections() > 0;
1783                         else
1784                                 enable = !theClipboard().empty();
1785                 } else {
1786                         string const arg = to_utf8(cmd.argument());
1787                         if (isStrUnsignedInt(arg)) {
1788                                 unsigned int n = convert<unsigned int>(arg);
1789                                 enable = cap::numberOfSelections() > n;
1790                         } else
1791                                 // unknown argument
1792                                 enable = false;
1793                 }
1794                 break;
1795
1796         case LFUN_CLIPBOARD_PASTE:
1797                 enable = !theClipboard().empty();
1798                 break;
1799
1800         case LFUN_PRIMARY_SELECTION_PASTE:
1801                 enable = cur.selection() || !theSelection().empty();
1802                 break;
1803
1804         case LFUN_PARAGRAPH_MOVE_UP:
1805                 enable = cur.pit() > 0 && !cur.selection();
1806                 break;
1807
1808         case LFUN_PARAGRAPH_MOVE_DOWN:
1809                 enable = cur.pit() < cur.lastpit() && !cur.selection();
1810                 break;
1811
1812         case LFUN_INSET_DISSOLVE:
1813                 enable = !isMainText(cur.bv().buffer()) && cur.inset().nargs() == 1;
1814                 break;
1815
1816         case LFUN_CHANGE_ACCEPT:
1817         case LFUN_CHANGE_REJECT:
1818                 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
1819                 // In principle, these LFUNs should only be enabled if there
1820                 // is a change at the current position/in the current selection.
1821                 // However, without proper optimizations, this will inevitably
1822                 // result in unacceptable performance - just imagine a user who
1823                 // wants to select the complete content of a long document.
1824                 enable = true;
1825                 break;
1826
1827         case LFUN_WORD_DELETE_FORWARD:
1828         case LFUN_WORD_DELETE_BACKWARD:
1829         case LFUN_LINE_DELETE:
1830         case LFUN_WORD_FORWARD:
1831         case LFUN_WORD_BACKWARD:
1832         case LFUN_CHAR_FORWARD:
1833         case LFUN_CHAR_FORWARD_SELECT:
1834         case LFUN_CHAR_BACKWARD:
1835         case LFUN_CHAR_BACKWARD_SELECT:
1836         case LFUN_UP:
1837         case LFUN_UP_SELECT:
1838         case LFUN_DOWN:
1839         case LFUN_DOWN_SELECT:
1840         case LFUN_PARAGRAPH_UP_SELECT:
1841         case LFUN_PARAGRAPH_DOWN_SELECT:
1842         case LFUN_SCREEN_UP_SELECT:
1843         case LFUN_SCREEN_DOWN_SELECT:
1844         case LFUN_LINE_BEGIN_SELECT:
1845         case LFUN_LINE_END_SELECT:
1846         case LFUN_WORD_FORWARD_SELECT:
1847         case LFUN_WORD_BACKWARD_SELECT:
1848         case LFUN_WORD_SELECT:
1849         case LFUN_PARAGRAPH_UP:
1850         case LFUN_PARAGRAPH_DOWN:
1851         case LFUN_LINE_BEGIN:
1852         case LFUN_LINE_END:
1853         case LFUN_BREAK_LINE:
1854         case LFUN_CHAR_DELETE_FORWARD:
1855         case LFUN_DELETE_FORWARD_SKIP:
1856         case LFUN_CHAR_DELETE_BACKWARD:
1857         case LFUN_DELETE_BACKWARD_SKIP:
1858         case LFUN_BREAK_PARAGRAPH:
1859         case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
1860         case LFUN_BREAK_PARAGRAPH_SKIP:
1861         case LFUN_PARAGRAPH_SPACING:
1862         case LFUN_INSET_INSERT:
1863         case LFUN_WORD_UPCASE:
1864         case LFUN_WORD_LOWCASE:
1865         case LFUN_WORD_CAPITALIZE:
1866         case LFUN_CHARS_TRANSPOSE:
1867         case LFUN_SERVER_GET_XY:
1868         case LFUN_SERVER_SET_XY:
1869         case LFUN_SERVER_GET_FONT:
1870         case LFUN_SERVER_GET_LAYOUT:
1871         case LFUN_LAYOUT:
1872         case LFUN_DATE_INSERT:
1873         case LFUN_SELF_INSERT:
1874         case LFUN_LINE_INSERT:
1875         case LFUN_PAGEBREAK_INSERT:
1876         case LFUN_CLEARPAGE_INSERT:
1877         case LFUN_CLEARDOUBLEPAGE_INSERT:
1878         case LFUN_MATH_DISPLAY:
1879         case LFUN_MATH_IMPORT_SELECTION:
1880         case LFUN_MATH_MODE:
1881         case LFUN_MATH_MACRO:
1882         case LFUN_MATH_MATRIX:
1883         case LFUN_MATH_DELIM:
1884         case LFUN_MATH_BIGDELIM:
1885         case LFUN_MATH_INSERT:
1886         case LFUN_MATH_SUBSCRIPT:
1887         case LFUN_MATH_SUPERSCRIPT:
1888         case LFUN_FONT_DEFAULT:
1889         case LFUN_FONT_UNDERLINE:
1890         case LFUN_FONT_SIZE:
1891         case LFUN_LANGUAGE:
1892         case LFUN_FONT_FREE_APPLY:
1893         case LFUN_FONT_FREE_UPDATE:
1894         case LFUN_LAYOUT_PARAGRAPH:
1895         case LFUN_PARAGRAPH_UPDATE:
1896         case LFUN_ACCENT_UMLAUT:
1897         case LFUN_ACCENT_CIRCUMFLEX:
1898         case LFUN_ACCENT_GRAVE:
1899         case LFUN_ACCENT_ACUTE:
1900         case LFUN_ACCENT_TILDE:
1901         case LFUN_ACCENT_CEDILLA:
1902         case LFUN_ACCENT_MACRON:
1903         case LFUN_ACCENT_DOT:
1904         case LFUN_ACCENT_UNDERDOT:
1905         case LFUN_ACCENT_UNDERBAR:
1906         case LFUN_ACCENT_CARON:
1907         case LFUN_ACCENT_SPECIAL_CARON:
1908         case LFUN_ACCENT_BREVE:
1909         case LFUN_ACCENT_TIE:
1910         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1911         case LFUN_ACCENT_CIRCLE:
1912         case LFUN_ACCENT_OGONEK:
1913         case LFUN_THESAURUS_ENTRY:
1914         case LFUN_PARAGRAPH_PARAMS_APPLY:
1915         case LFUN_PARAGRAPH_PARAMS:
1916         case LFUN_ESCAPE:
1917         case LFUN_BUFFER_END:
1918         case LFUN_BUFFER_BEGIN:
1919         case LFUN_BUFFER_BEGIN_SELECT:
1920         case LFUN_BUFFER_END_SELECT:
1921         case LFUN_UNICODE_INSERT:
1922                 // these are handled in our dispatch()
1923                 enable = true;
1924                 break;
1925
1926         default:
1927                 return false;
1928         }
1929
1930         if (code != Inset::NO_CODE
1931             && (cur.empty() || !cur.inset().insetAllowed(code)))
1932                 enable = false;
1933
1934         flag.enabled(enable);
1935         return true;
1936 }
1937
1938
1939 void Text::pasteString(Cursor & cur, docstring const & clip,
1940                 bool asParagraphs)
1941 {
1942         cur.clearSelection();
1943         if (!clip.empty()) {
1944                 recordUndo(cur);
1945                 if (asParagraphs)
1946                         insertStringAsParagraphs(cur, clip);
1947                 else
1948                         insertStringAsLines(cur, clip);
1949         }
1950 }
1951
1952 } // namespace lyx