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