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