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