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