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