]> git.lyx.org Git - lyx.git/blob - src/Text3.cpp
Use home made code for the bool facet.
[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                 cap::saveSelection(bv->cursor());
1019                 // Right click on a footnote flag opens float menu
1020                 if (cmd.button() == mouse_button::button3)
1021                         cur.clearSelection();
1022
1023                 // Middle button press pastes if we have a selection
1024                 // We do this here as if the selection was inside an inset
1025                 // it could get cleared on the unlocking of the inset so
1026                 // we have to check this first
1027                 bool paste_internally = false;
1028                 if (cmd.button() == mouse_button::button2 && cap::selection()) {
1029                         // Copy the selection buffer to the clipboard
1030                         // stack, because we want it to appear in the
1031                         // "Edit->Paste recent" menu.
1032                         cap::copySelectionToStack();
1033                         paste_internally = true;
1034                 }
1035
1036                 // Insert primary selection with middle mouse
1037                 // if there is a local selection in the current buffer,
1038                 // insert this
1039                 if (cmd.button() == mouse_button::button2) {
1040                         if (paste_internally) {
1041                                 cap::pasteSelection(cur, bv->buffer()->errorList("Paste"));
1042                                 bv->buffer()->errors("Paste");
1043                                 cur.clearSelection(); // bug 393
1044                                 bv->buffer()->markDirty();
1045                                 finishUndo();
1046                         } else {
1047                                 bv->mouseSetCursor(cur);
1048                                 lyx::dispatch(FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph"));
1049                         }
1050                 }
1051
1052                 // we have to update after dePM triggered
1053                 bool update = bv->mouseSetCursor(cur);
1054
1055                 if (!update && cmd.button() == mouse_button::button1) {
1056                         needsUpdate = false;
1057                         cur.noUpdate();
1058                 }
1059
1060                 break;
1061         }
1062
1063         case LFUN_MOUSE_MOTION: {
1064                 // Only use motion with button 1
1065                 //if (cmd.button() != mouse_button::button1)
1066                 //      return false;
1067
1068                 // ignore motions deeper nested than the real anchor
1069                 Cursor & bvcur = cur.bv().cursor();
1070                 if (bvcur.anchor_.hasPart(cur)) {
1071                         CursorSlice old = bvcur.top();
1072
1073                         int const wh = bv->workHeight();
1074                         int const y = std::max(0, std::min(wh - 1, cmd.y));
1075
1076                         setCursorFromCoordinates(cur, cmd.x, y);
1077                         cur.setTargetX(cmd.x);
1078                         if (cmd.y >= wh)
1079                                 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1080                         else if (cmd.y < 0)
1081                                 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1082                         // This is to allow jumping over large insets
1083                         if (cur.top() == old) {
1084                                 if (cmd.y >= wh)
1085                                         lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1086                                 else if (cmd.y < 0)
1087                                         lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1088                         }
1089
1090                         if (cur.top() == old)
1091                                 cur.noUpdate();
1092                         else {
1093                                 // don't set anchor_
1094                                 bvcur.setCursor(cur);
1095                                 bvcur.selection() = true;
1096                                 //lyxerr << "MOTION: " << bv->cursor() << endl;
1097                         }
1098
1099                 } else
1100                         cur.undispatched();
1101                 break;
1102         }
1103
1104         case LFUN_MOUSE_RELEASE: {
1105                 if (cmd.button() == mouse_button::button2)
1106                         break;
1107
1108                 if (cmd.button() == mouse_button::button1) {
1109                         // if there is new selection, update persistent
1110                         // selection, otherwise, single click does not
1111                         // clear persistent selection buffer
1112                         if (cur.selection()) {
1113                                 // finish selection
1114                                 // if double click, cur is moved to the end of word by selectWord
1115                                 // but bvcur is current mouse position
1116                                 Cursor & bvcur = cur.bv().cursor();
1117                                 bvcur.selection() = true;
1118                         }
1119                         needsUpdate = false;
1120                         cur.noUpdate();
1121                 }
1122
1123                 break;
1124         }
1125
1126         case LFUN_SELF_INSERT: {
1127                 if (cmd.argument().empty())
1128                         break;
1129
1130                 // Automatically delete the currently selected
1131                 // text and replace it with what is being
1132                 // typed in now. Depends on lyxrc settings
1133                 // "auto_region_delete", which defaults to
1134                 // true (on).
1135
1136                 if (lyxrc.auto_region_delete && cur.selection()) {
1137                         cutSelection(cur, false, false);
1138                         // When change tracking is set to off, the metrics update
1139                         // mechanism correctly detects if a full update is needed or not.
1140                         // This detection fails when a selection spans multiple rows and
1141                         // change tracking is enabled because the paragraph metrics stays
1142                         // the same. In this case, we force the full update:
1143                         // (see http://bugzilla.lyx.org/show_bug.cgi?id=3992)
1144                         if (cur.buffer().params().trackChanges)
1145                                 cur.updateFlags(Update::Force);
1146                 }
1147
1148                 cur.clearSelection();
1149                 Font const old_font = real_current_font;
1150
1151                 docstring::const_iterator cit = cmd.argument().begin();
1152                 docstring::const_iterator end = cmd.argument().end();
1153                 for (; cit != end; ++cit)
1154                         bv->translateAndInsert(*cit, this, cur);
1155
1156                 cur.resetAnchor();
1157                 moveCursor(cur, false);
1158                 break;
1159         }
1160
1161         case LFUN_URL_INSERT: {
1162                 InsetCommandParams p("url");
1163                 docstring content;
1164                 if (cur.selection()) {
1165                         content = cur.selectionAsString(false);
1166                         cutSelection(cur, true, false);
1167                 }
1168                 p["target"] = (cmd.argument().empty()) ?
1169                         content : cmd.argument();
1170                 string const data = InsetCommandMailer::params2string("url", p);
1171                 if (p["target"].empty()) {
1172                         bv->showInsetDialog("url", data, 0);
1173                 } else {
1174                         FuncRequest fr(LFUN_INSET_INSERT, data);
1175                         dispatch(cur, fr);
1176                 }
1177                 break;
1178         }
1179
1180         case LFUN_HTML_INSERT: {
1181                 InsetCommandParams p("htmlurl");
1182                 docstring content;
1183                 if (cur.selection()) {
1184                         content = cur.selectionAsString(false);
1185                         cutSelection(cur, true, false);
1186                 }
1187                 p["target"] = (cmd.argument().empty()) ?
1188                         content : cmd.argument();
1189                 string const data = InsetCommandMailer::params2string("url", p);
1190                 if (p["target"].empty()) {
1191                         bv->showInsetDialog("url", data, 0);
1192                 } else {
1193                         FuncRequest fr(LFUN_INSET_INSERT, data);
1194                         dispatch(cur, fr);
1195                 }
1196                 break;
1197         }
1198
1199         case LFUN_LABEL_INSERT: {
1200                 InsetCommandParams p("label");
1201                 // Try to generate a valid label
1202                 p["name"] = (cmd.argument().empty()) ?
1203                         cur.getPossibleLabel() :
1204                         cmd.argument();
1205                 string const data = InsetCommandMailer::params2string("label", p);
1206
1207                 if (cmd.argument().empty()) {
1208                         bv->showInsetDialog("label", data, 0);
1209                 } else {
1210                         FuncRequest fr(LFUN_INSET_INSERT, data);
1211                         dispatch(cur, fr);
1212                 }
1213                 break;
1214         }
1215
1216
1217 #if 0
1218         case LFUN_LIST_INSERT:
1219         case LFUN_THEOREM_INSERT:
1220 #endif
1221         case LFUN_CAPTION_INSERT:
1222                 // Open the inset, and move the current selection
1223                 // inside it.
1224                 doInsertInset(cur, this, cmd, true, true);
1225                 cur.posRight();
1226                 updateLabels(*bv->buffer());
1227                 break;
1228         case LFUN_NOTE_INSERT:
1229         case LFUN_CHARSTYLE_INSERT:
1230         case LFUN_BOX_INSERT:
1231         case LFUN_BRANCH_INSERT:
1232         case LFUN_BIBITEM_INSERT:
1233         case LFUN_ERT_INSERT:
1234         case LFUN_LISTING_INSERT:
1235         case LFUN_FOOTNOTE_INSERT:
1236         case LFUN_MARGINALNOTE_INSERT:
1237         case LFUN_OPTIONAL_INSERT:
1238         case LFUN_ENVIRONMENT_INSERT:
1239                 // Open the inset, and move the current selection
1240                 // inside it.
1241                 doInsertInset(cur, this, cmd, true, true);
1242                 cur.posRight();
1243                 break;
1244
1245         case LFUN_TABULAR_INSERT:
1246                 // if there were no arguments, just open the dialog
1247                 if (doInsertInset(cur, this, cmd, false, true))
1248                         cur.posRight();
1249                 else
1250                         bv->showDialog("tabularcreate");
1251
1252                 break;
1253
1254         case LFUN_FLOAT_INSERT:
1255         case LFUN_FLOAT_WIDE_INSERT:
1256         case LFUN_WRAP_INSERT: {
1257                 bool content = cur.selection();  // will some text be moved into the inset?
1258
1259                 doInsertInset(cur, this, cmd, true, true);
1260                 cur.posRight();
1261                 ParagraphList & pars = cur.text()->paragraphs();
1262
1263                 TextClass const & tclass = bv->buffer()->params().getTextClass();
1264
1265                 // add a separate paragraph for the caption inset
1266                 pars.push_back(Paragraph());
1267                 pars.back().setInsetOwner(pars[0].inInset());
1268                 pars.back().layout(tclass.defaultLayout());
1269
1270                 int cap_pit = pars.size() - 1;
1271
1272                 // if an empty inset was created, we create an additional empty
1273                 // paragraph at the bottom so that the user can choose where to put
1274                 // the graphics (or table).
1275                 if (!content) {
1276                         pars.push_back(Paragraph());
1277                         pars.back().setInsetOwner(pars[0].inInset());
1278                         pars.back().layout(tclass.defaultLayout());
1279
1280                 }
1281
1282                 // reposition the cursor to the caption
1283                 cur.pit() = cap_pit;
1284                 cur.pos() = 0;
1285                 cur.dispatch(FuncRequest(LFUN_CAPTION_INSERT));
1286                 // FIXME: When leaving the Float (or Wrap) inset we should
1287                 // delete any empty paragraph left above or below the
1288                 // caption.
1289                 break;
1290         }
1291
1292         case LFUN_INDEX_INSERT:
1293         case LFUN_NOMENCL_INSERT: {
1294                 Inset * inset = createInset(&cur.bv(), cmd);
1295                 if (!inset)
1296                         break;
1297                 recordUndo(cur);
1298                 cur.clearSelection();
1299                 insertInset(cur, inset);
1300                 inset->edit(cur, true);
1301                 // Show the dialog for the nomenclature entry, since the
1302                 // description entry still needs to be filled in.
1303                 if (cmd.action == LFUN_NOMENCL_INSERT)
1304                         InsetCommandMailer("nomenclature",
1305                                 *reinterpret_cast<InsetCommand *>(inset)).showDialog(&cur.bv());
1306                 cur.posRight();
1307                 break;
1308         }
1309
1310         case LFUN_INDEX_PRINT:
1311         case LFUN_NOMENCL_PRINT:
1312         case LFUN_TOC_INSERT:
1313         case LFUN_HFILL_INSERT:
1314         case LFUN_LINE_INSERT:
1315         case LFUN_PAGEBREAK_INSERT:
1316         case LFUN_CLEARPAGE_INSERT:
1317         case LFUN_CLEARDOUBLEPAGE_INSERT:
1318                 // do nothing fancy
1319                 doInsertInset(cur, this, cmd, false, false);
1320                 cur.posRight();
1321                 break;
1322
1323         case LFUN_DEPTH_DECREMENT:
1324                 changeDepth(cur, DEC_DEPTH);
1325                 break;
1326
1327         case LFUN_DEPTH_INCREMENT:
1328                 changeDepth(cur, INC_DEPTH);
1329                 break;
1330
1331         case LFUN_MATH_DISPLAY:
1332                 mathDispatch(cur, cmd, true);
1333                 break;
1334
1335         case LFUN_MATH_IMPORT_SELECTION:
1336         case LFUN_MATH_MODE:
1337                 if (cmd.argument() == "on")
1338                         // don't pass "on" as argument
1339                         mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1340                 else
1341                         mathDispatch(cur, cmd, false);
1342                 break;
1343
1344         case LFUN_MATH_MACRO:
1345                 if (cmd.argument().empty())
1346                         cur.errorMessage(from_utf8(N_("Missing argument")));
1347                 else {
1348                         string s = to_utf8(cmd.argument());
1349                         string const s1 = token(s, ' ', 1);
1350                         int const nargs = s1.empty() ? 0 : convert<int>(s1);
1351                         string const s2 = token(s, ' ', 2);
1352                         string const type = s2.empty() ? "newcommand" : s2;
1353                         cur.insert(new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, from_utf8(type)));
1354                         //cur.nextInset()->edit(cur, true);
1355                 }
1356                 break;
1357
1358         // passthrough hat and underscore outside mathed:
1359         case LFUN_MATH_SUBSCRIPT:
1360                 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1361                 break;
1362         case LFUN_MATH_SUPERSCRIPT:
1363                 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1364                 break;
1365
1366         case LFUN_MATH_INSERT:
1367         case LFUN_MATH_MATRIX:
1368         case LFUN_MATH_DELIM:
1369         case LFUN_MATH_BIGDELIM: {
1370                 if (cur.selection())
1371                         cur.clearSelection();
1372                 // FIXME: instead of the above, this one
1373                 // should be used (but it asserts with Bidi enabled)
1374                 // cf. http://bugzilla.lyx.org/show_bug.cgi?id=4055
1375                 // cap::replaceSelection(cur);
1376                 cur.insert(new InsetMathHull(hullSimple));
1377                 checkAndActivateInset(cur, true);
1378                 BOOST_ASSERT(cur.inMathed());
1379                 cur.dispatch(cmd);
1380                 break;
1381         }
1382
1383         case LFUN_FONT_EMPH: {
1384                 Font font(Font::ALL_IGNORE);
1385                 font.setEmph(Font::TOGGLE);
1386                 toggleAndShow(cur, this, font);
1387                 break;
1388         }
1389
1390         case LFUN_FONT_BOLD: {
1391                 Font font(Font::ALL_IGNORE);
1392                 font.setSeries(Font::BOLD_SERIES);
1393                 toggleAndShow(cur, this, font);
1394                 break;
1395         }
1396
1397         case LFUN_FONT_NOUN: {
1398                 Font font(Font::ALL_IGNORE);
1399                 font.setNoun(Font::TOGGLE);
1400                 toggleAndShow(cur, this, font);
1401                 break;
1402         }
1403
1404         case LFUN_FONT_CODE: {
1405                 Font font(Font::ALL_IGNORE);
1406                 font.setFamily(Font::TYPEWRITER_FAMILY); // no good
1407                 toggleAndShow(cur, this, font);
1408                 break;
1409         }
1410
1411         case LFUN_FONT_SANS: {
1412                 Font font(Font::ALL_IGNORE);
1413                 font.setFamily(Font::SANS_FAMILY);
1414                 toggleAndShow(cur, this, font);
1415                 break;
1416         }
1417
1418         case LFUN_FONT_ROMAN: {
1419                 Font font(Font::ALL_IGNORE);
1420                 font.setFamily(Font::ROMAN_FAMILY);
1421                 toggleAndShow(cur, this, font);
1422                 break;
1423         }
1424
1425         case LFUN_FONT_DEFAULT: {
1426                 Font font(Font::ALL_INHERIT, ignore_language);
1427                 toggleAndShow(cur, this, font);
1428                 break;
1429         }
1430
1431         case LFUN_FONT_UNDERLINE: {
1432                 Font font(Font::ALL_IGNORE);
1433                 font.setUnderbar(Font::TOGGLE);
1434                 toggleAndShow(cur, this, font);
1435                 break;
1436         }
1437
1438         case LFUN_FONT_SIZE: {
1439                 Font font(Font::ALL_IGNORE);
1440                 font.setLyXSize(to_utf8(cmd.argument()));
1441                 toggleAndShow(cur, this, font);
1442                 break;
1443         }
1444
1445         case LFUN_LANGUAGE: {
1446                 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1447                 if (!lang)
1448                         break;
1449                 Font font(Font::ALL_IGNORE);
1450                 font.setLanguage(lang);
1451                 toggleAndShow(cur, this, font);
1452                 break;
1453         }
1454
1455         case LFUN_FONT_FREE_APPLY:
1456                 toggleAndShow(cur, this, freefont, toggleall);
1457                 cur.message(_("Character set"));
1458                 break;
1459
1460         // Set the freefont using the contents of \param data dispatched from
1461         // the frontends and apply it at the current cursor location.
1462         case LFUN_FONT_FREE_UPDATE: {
1463                 Font font;
1464                 bool toggle;
1465                 if (bv_funcs::string2font(to_utf8(cmd.argument()), font, toggle)) {
1466                         freefont = font;
1467                         toggleall = toggle;
1468                         toggleAndShow(cur, this, freefont, toggleall);
1469                         cur.message(_("Character set"));
1470                 }
1471                 break;
1472         }
1473
1474         case LFUN_FINISHED_LEFT:
1475                 LYXERR(Debug::DEBUG) << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1476                 if (reverseDirectionNeeded(cur))
1477                         ++cur.pos();
1478                 break;
1479
1480         case LFUN_FINISHED_RIGHT:
1481                 LYXERR(Debug::DEBUG) << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1482                 if (!reverseDirectionNeeded(cur))
1483                         ++cur.pos();
1484                 break;
1485
1486         case LFUN_LAYOUT_PARAGRAPH: {
1487                 string data;
1488                 params2string(cur.paragraph(), data);
1489                 data = "show\n" + data;
1490                 bv->showDialogWithData("paragraph", data);
1491                 break;
1492         }
1493
1494         case LFUN_PARAGRAPH_UPDATE: {
1495                 string data;
1496                 params2string(cur.paragraph(), data);
1497
1498                 // Will the paragraph accept changes from the dialog?
1499                 bool const accept = !cur.inset().forceDefaultParagraphs(cur.idx());
1500
1501                 data = "update " + convert<string>(accept) + '\n' + data;
1502                 bv->updateDialog("paragraph", data);
1503                 break;
1504         }
1505
1506         case LFUN_ACCENT_UMLAUT:
1507         case LFUN_ACCENT_CIRCUMFLEX:
1508         case LFUN_ACCENT_GRAVE:
1509         case LFUN_ACCENT_ACUTE:
1510         case LFUN_ACCENT_TILDE:
1511         case LFUN_ACCENT_CEDILLA:
1512         case LFUN_ACCENT_MACRON:
1513         case LFUN_ACCENT_DOT:
1514         case LFUN_ACCENT_UNDERDOT:
1515         case LFUN_ACCENT_UNDERBAR:
1516         case LFUN_ACCENT_CARON:
1517         case LFUN_ACCENT_SPECIAL_CARON:
1518         case LFUN_ACCENT_BREVE:
1519         case LFUN_ACCENT_TIE:
1520         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1521         case LFUN_ACCENT_CIRCLE:
1522         case LFUN_ACCENT_OGONEK:
1523                 theLyXFunc().handleKeyFunc(cmd.action);
1524                 if (!cmd.argument().empty())
1525                         // FIXME: Are all these characters encoded in one byte in utf8?
1526                         bv->translateAndInsert(cmd.argument()[0], this, cur);
1527                 break;
1528
1529         case LFUN_FLOAT_LIST: {
1530                 TextClass const & tclass = bv->buffer()->params().getTextClass();
1531                 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1532                         recordUndo(cur);
1533                         if (cur.selection())
1534                                 cutSelection(cur, true, false);
1535                         breakParagraph(cur);
1536
1537                         if (cur.lastpos() != 0) {
1538                                 cursorLeft(cur);
1539                                 breakParagraph(cur);
1540                         }
1541
1542                         setLayout(cur, tclass.defaultLayoutName());
1543                         setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, docstring(), 0);
1544                         insertInset(cur, new InsetFloatList(to_utf8(cmd.argument())));
1545                         cur.posRight();
1546                 } else {
1547                         lyxerr << "Non-existent float type: "
1548                                << to_utf8(cmd.argument()) << endl;
1549                 }
1550                 break;
1551         }
1552
1553         case LFUN_CHANGE_ACCEPT: {
1554                 acceptOrRejectChanges(cur, ACCEPT);
1555                 break;
1556         }
1557
1558         case LFUN_CHANGE_REJECT: {
1559                 acceptOrRejectChanges(cur, REJECT);
1560                 break;
1561         }
1562
1563         case LFUN_THESAURUS_ENTRY: {
1564                 docstring arg = cmd.argument();
1565                 if (arg.empty()) {
1566                         arg = cur.selectionAsString(false);
1567                         // FIXME
1568                         if (arg.size() > 100 || arg.empty()) {
1569                                 // Get word or selection
1570                                 selectWordWhenUnderCursor(cur, WHOLE_WORD);
1571                                 arg = cur.selectionAsString(false);
1572                         }
1573                 }
1574                 bv->showDialogWithData("thesaurus", to_utf8(arg));
1575                 break;
1576         }
1577
1578         case LFUN_PARAGRAPH_PARAMS_APPLY: {
1579                 // Given data, an encoding of the ParagraphParameters
1580                 // generated in the Paragraph dialog, this function sets
1581                 // the current paragraph appropriately.
1582                 istringstream is(to_utf8(cmd.argument()));
1583                 Lexer lex(0, 0);
1584                 lex.setStream(is);
1585                 ParagraphParameters params;
1586                 params.read(lex);
1587                 setParagraph(cur,
1588                              params.spacing(),
1589                              params.align(),
1590                              params.labelWidthString(),
1591                              params.noindent());
1592                 cur.message(_("Paragraph layout set"));
1593                 break;
1594         }
1595
1596         case LFUN_ESCAPE:
1597                 if (cur.selection()) {
1598                         cur.selection() = false;
1599                 } else {
1600                         cur.undispatched();
1601                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1602                 }
1603                 break;
1604
1605         default:
1606                 LYXERR(Debug::ACTION)
1607                         << BOOST_CURRENT_FUNCTION
1608                         << ": Command " << cmd
1609                         << " not DISPATCHED by Text" << endl;
1610                 cur.undispatched();
1611                 break;
1612         }
1613
1614         needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
1615         theSelection().haveSelection(bv->cursor().selection());
1616
1617         // FIXME: The cursor flag is reset two lines below
1618         // so we need to check here if some of the LFUN did touch that.
1619         // for now only Text::erase() and Text::backspace() do that.
1620         // The plan is to verify all the LFUNs and then to remove this
1621         // singleParUpdate boolean altogether.
1622         if (cur.result().update() & Update::Force) {
1623                 singleParUpdate = false;
1624                 needsUpdate = true;
1625         }
1626
1627         // FIXME: the following code should go in favor of fine grained
1628         // update flag treatment.
1629         if (singleParUpdate) {
1630                 // Inserting characters does not change par height
1631                 ParagraphMetrics const & pms
1632                         = cur.bv().parMetrics(cur.bottom().text(), cur.bottom().pit());
1633                 if (pms.dim().height()
1634                     == olddim.height()) {
1635                         // if so, update _only_ this paragraph
1636                         cur.updateFlags(Update::SinglePar |
1637                                 Update::FitCursor |
1638                                 Update::MultiParSel);
1639                         return;
1640                 } else
1641                         needsUpdate = true;
1642         }
1643
1644         if (!needsUpdate
1645             && &oldTopSlice.inset() == &cur.inset()
1646             && oldTopSlice.idx() == cur.idx()
1647             && !sel // sel is a backup of cur.selection() at the biginning of the function.
1648             && !cur.selection())
1649                 // FIXME: it would be better if we could just do this
1650                 //
1651                 //if (cur.result().update() != Update::FitCursor)
1652                 //      cur.noUpdate();
1653                 //
1654                 // But some LFUNs do not set Update::FitCursor when needed, so we
1655                 // do it for all. This is not very harmfull as FitCursor will provoke
1656                 // a full redraw only if needed but still, a proper review of all LFUN
1657                 // should be done and this needsUpdate boolean can then be removed.
1658                 cur.updateFlags(Update::FitCursor);
1659         else
1660                 cur.updateFlags(Update::Force | Update::FitCursor);
1661 }
1662
1663
1664 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
1665                         FuncStatus & flag) const
1666 {
1667         BOOST_ASSERT(cur.text() == this);
1668
1669         Font const & font = real_current_font;
1670         bool enable = true;
1671         Inset::Code code = Inset::NO_CODE;
1672
1673         switch (cmd.action) {
1674
1675         case LFUN_DEPTH_DECREMENT:
1676                 enable = changeDepthAllowed(cur, DEC_DEPTH);
1677                 break;
1678
1679         case LFUN_DEPTH_INCREMENT:
1680                 enable = changeDepthAllowed(cur, INC_DEPTH);
1681                 break;
1682
1683         case LFUN_APPENDIX:
1684                 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1685                 return true;
1686
1687         case LFUN_BIBITEM_INSERT:
1688                 enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO
1689                           && cur.pos() == 0);
1690                 break;
1691
1692         case LFUN_DIALOG_SHOW_NEW_INSET:
1693                 if (cmd.argument() == "bibitem")
1694                         code = Inset::BIBITEM_CODE;
1695                 else if (cmd.argument() == "bibtex")
1696                         code = Inset::BIBTEX_CODE;
1697                 else if (cmd.argument() == "box")
1698                         code = Inset::BOX_CODE;
1699                 else if (cmd.argument() == "branch")
1700                         code = Inset::BRANCH_CODE;
1701                 else if (cmd.argument() == "citation")
1702                         code = Inset::CITE_CODE;
1703                 else if (cmd.argument() == "ert")
1704                         code = Inset::ERT_CODE;
1705                 else if (cmd.argument() == "external")
1706                         code = Inset::EXTERNAL_CODE;
1707                 else if (cmd.argument() == "float")
1708                         code = Inset::FLOAT_CODE;
1709                 else if (cmd.argument() == "graphics")
1710                         code = Inset::GRAPHICS_CODE;
1711                 else if (cmd.argument() == "include")
1712                         code = Inset::INCLUDE_CODE;
1713                 else if (cmd.argument() == "index")
1714                         code = Inset::INDEX_CODE;
1715                 else if (cmd.argument() == "nomenclature")
1716                         code = Inset::NOMENCL_CODE;
1717                 else if (cmd.argument() == "label")
1718                         code = Inset::LABEL_CODE;
1719                 else if (cmd.argument() == "note")
1720                         code = Inset::NOTE_CODE;
1721                 else if (cmd.argument() == "ref")
1722                         code = Inset::REF_CODE;
1723                 else if (cmd.argument() == "toc")
1724                         code = Inset::TOC_CODE;
1725                 else if (cmd.argument() == "url")
1726                         code = Inset::URL_CODE;
1727                 else if (cmd.argument() == "vspace")
1728                         code = Inset::VSPACE_CODE;
1729                 else if (cmd.argument() == "wrap")
1730                         code = Inset::WRAP_CODE;
1731                 else if (cmd.argument() == "listings")
1732                         code = Inset::LISTINGS_CODE;
1733                 break;
1734
1735         case LFUN_ERT_INSERT:
1736                 code = Inset::ERT_CODE;
1737                 break;
1738         case LFUN_LISTING_INSERT:
1739             code = Inset::LISTINGS_CODE;
1740                 break;
1741         case LFUN_FOOTNOTE_INSERT:
1742                 code = Inset::FOOT_CODE;
1743                 break;
1744         case LFUN_TABULAR_INSERT:
1745                 code = Inset::TABULAR_CODE;
1746                 break;
1747         case LFUN_MARGINALNOTE_INSERT:
1748                 code = Inset::MARGIN_CODE;
1749                 break;
1750         case LFUN_FLOAT_INSERT:
1751         case LFUN_FLOAT_WIDE_INSERT:
1752                 code = Inset::FLOAT_CODE;
1753                 break;
1754         case LFUN_WRAP_INSERT:
1755                 code = Inset::WRAP_CODE;
1756                 break;
1757         case LFUN_FLOAT_LIST:
1758                 code = Inset::FLOAT_LIST_CODE;
1759                 break;
1760 #if 0
1761         case LFUN_LIST_INSERT:
1762                 code = Inset::LIST_CODE;
1763                 break;
1764         case LFUN_THEOREM_INSERT:
1765                 code = Inset::THEOREM_CODE;
1766                 break;
1767 #endif
1768         case LFUN_CAPTION_INSERT:
1769                 code = Inset::CAPTION_CODE;
1770                 break;
1771         case LFUN_NOTE_INSERT:
1772                 code = Inset::NOTE_CODE;
1773                 break;
1774         case LFUN_CHARSTYLE_INSERT:
1775                 code = Inset::CHARSTYLE_CODE;
1776                 if (cur.buffer().params().getTextClass().charstyles().empty())
1777                         enable = false;
1778                 break;
1779         case LFUN_BOX_INSERT:
1780                 code = Inset::BOX_CODE;
1781                 break;
1782         case LFUN_BRANCH_INSERT:
1783                 code = Inset::BRANCH_CODE;
1784                 if (cur.buffer().getMasterBuffer()->params().branchlist().empty())
1785                         enable = false;
1786                 break;
1787         case LFUN_LABEL_INSERT:
1788                 code = Inset::LABEL_CODE;
1789                 break;
1790         case LFUN_OPTIONAL_INSERT:
1791                 code = Inset::OPTARG_CODE;
1792                 enable = numberOfOptArgs(cur.paragraph())
1793                         < cur.paragraph().layout()->optionalargs;
1794                 break;
1795         case LFUN_ENVIRONMENT_INSERT:
1796                 code = Inset::BOX_CODE;
1797                 break;
1798         case LFUN_INDEX_INSERT:
1799                 code = Inset::INDEX_CODE;
1800                 break;
1801         case LFUN_INDEX_PRINT:
1802                 code = Inset::INDEX_PRINT_CODE;
1803                 break;
1804         case LFUN_NOMENCL_INSERT:
1805                 code = Inset::NOMENCL_CODE;
1806                 break;
1807         case LFUN_NOMENCL_PRINT:
1808                 code = Inset::NOMENCL_PRINT_CODE;
1809                 break;
1810         case LFUN_TOC_INSERT:
1811                 code = Inset::TOC_CODE;
1812                 break;
1813         case LFUN_HTML_INSERT:
1814         case LFUN_URL_INSERT:
1815                 code = Inset::URL_CODE;
1816                 break;
1817         case LFUN_QUOTE_INSERT:
1818                 // always allow this, since we will inset a raw quote
1819                 // if an inset is not allowed.
1820                 break;
1821         case LFUN_HYPHENATION_POINT_INSERT:
1822         case LFUN_LIGATURE_BREAK_INSERT:
1823         case LFUN_HFILL_INSERT:
1824         case LFUN_MENU_SEPARATOR_INSERT:
1825         case LFUN_DOTS_INSERT:
1826         case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
1827                 code = Inset::SPECIALCHAR_CODE;
1828                 break;
1829         case LFUN_SPACE_INSERT:
1830                 // slight hack: we know this is allowed in math mode
1831                 if (cur.inTexted())
1832                         code = Inset::SPACE_CODE;
1833                 break;
1834
1835         case LFUN_INSET_MODIFY:
1836                 // We need to disable this, because we may get called for a
1837                 // tabular cell via
1838                 // InsetTabular::getStatus() -> InsetText::getStatus()
1839                 // and we don't handle LFUN_INSET_MODIFY.
1840                 enable = false;
1841                 break;
1842
1843         case LFUN_FONT_EMPH:
1844                 flag.setOnOff(font.emph() == Font::ON);
1845                 return true;
1846
1847         case LFUN_FONT_NOUN:
1848                 flag.setOnOff(font.noun() == Font::ON);
1849                 return true;
1850
1851         case LFUN_FONT_BOLD:
1852                 flag.setOnOff(font.series() == Font::BOLD_SERIES);
1853                 return true;
1854
1855         case LFUN_FONT_SANS:
1856                 flag.setOnOff(font.family() == Font::SANS_FAMILY);
1857                 return true;
1858
1859         case LFUN_FONT_ROMAN:
1860                 flag.setOnOff(font.family() == Font::ROMAN_FAMILY);
1861                 return true;
1862
1863         case LFUN_FONT_CODE:
1864                 flag.setOnOff(font.family() == Font::TYPEWRITER_FAMILY);
1865                 return true;
1866
1867         case LFUN_CUT:
1868         case LFUN_COPY:
1869                 enable = cur.selection();
1870                 break;
1871
1872         case LFUN_PASTE:
1873                 if (cmd.argument().empty()) {
1874                         if (theClipboard().isInternal())
1875                                 enable = cap::numberOfSelections() > 0;
1876                         else
1877                                 enable = !theClipboard().empty();
1878                 } else {
1879                         string const arg = to_utf8(cmd.argument());
1880                         if (isStrUnsignedInt(arg)) {
1881                                 unsigned int n = convert<unsigned int>(arg);
1882                                 enable = cap::numberOfSelections() > n;
1883                         } else
1884                                 // unknown argument
1885                                 enable = false;
1886                 }
1887                 break;
1888
1889         case LFUN_CLIPBOARD_PASTE:
1890                 enable = !theClipboard().empty();
1891                 break;
1892
1893         case LFUN_PRIMARY_SELECTION_PASTE:
1894                 enable = cur.selection() || !theSelection().empty();
1895                 break;
1896
1897         case LFUN_PARAGRAPH_MOVE_UP:
1898                 enable = cur.pit() > 0 && !cur.selection();
1899                 break;
1900
1901         case LFUN_PARAGRAPH_MOVE_DOWN:
1902                 enable = cur.pit() < cur.lastpit() && !cur.selection();
1903                 break;
1904
1905         case LFUN_INSET_DISSOLVE:
1906                 enable = !isMainText(*cur.bv().buffer()) && cur.inset().nargs() == 1;
1907                 break;
1908
1909         case LFUN_CHANGE_ACCEPT:
1910         case LFUN_CHANGE_REJECT:
1911                 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
1912                 // In principle, these LFUNs should only be enabled if there
1913                 // is a change at the current position/in the current selection.
1914                 // However, without proper optimizations, this will inevitably
1915                 // result in unacceptable performance - just imagine a user who
1916                 // wants to select the complete content of a long document.
1917                 enable = true;
1918                 break;
1919
1920         case LFUN_WORD_DELETE_FORWARD:
1921         case LFUN_WORD_DELETE_BACKWARD:
1922         case LFUN_LINE_DELETE:
1923         case LFUN_WORD_FORWARD:
1924         case LFUN_WORD_BACKWARD:
1925         case LFUN_CHAR_FORWARD:
1926         case LFUN_CHAR_FORWARD_SELECT:
1927         case LFUN_CHAR_BACKWARD:
1928         case LFUN_CHAR_BACKWARD_SELECT:
1929         case LFUN_UP:
1930         case LFUN_UP_SELECT:
1931         case LFUN_DOWN:
1932         case LFUN_DOWN_SELECT:
1933         case LFUN_PARAGRAPH_UP_SELECT:
1934         case LFUN_PARAGRAPH_DOWN_SELECT:
1935         case LFUN_SCREEN_UP_SELECT:
1936         case LFUN_SCREEN_DOWN_SELECT:
1937         case LFUN_LINE_BEGIN_SELECT:
1938         case LFUN_LINE_END_SELECT:
1939         case LFUN_WORD_FORWARD_SELECT:
1940         case LFUN_WORD_BACKWARD_SELECT:
1941         case LFUN_WORD_SELECT:
1942         case LFUN_PARAGRAPH_UP:
1943         case LFUN_PARAGRAPH_DOWN:
1944         case LFUN_SCREEN_UP:
1945         case LFUN_SCREEN_DOWN:
1946         case LFUN_LINE_BEGIN:
1947         case LFUN_LINE_END:
1948         case LFUN_BREAK_LINE:
1949         case LFUN_CHAR_DELETE_FORWARD:
1950         case LFUN_DELETE_FORWARD_SKIP:
1951         case LFUN_CHAR_DELETE_BACKWARD:
1952         case LFUN_DELETE_BACKWARD_SKIP:
1953         case LFUN_BREAK_PARAGRAPH:
1954         case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
1955         case LFUN_BREAK_PARAGRAPH_SKIP:
1956         case LFUN_PARAGRAPH_SPACING:
1957         case LFUN_INSET_INSERT:
1958         case LFUN_WORD_UPCASE:
1959         case LFUN_WORD_LOWCASE:
1960         case LFUN_WORD_CAPITALIZE:
1961         case LFUN_CHARS_TRANSPOSE:
1962         case LFUN_SERVER_GET_XY:
1963         case LFUN_SERVER_SET_XY:
1964         case LFUN_SERVER_GET_FONT:
1965         case LFUN_SERVER_GET_LAYOUT:
1966         case LFUN_LAYOUT:
1967         case LFUN_DATE_INSERT:
1968         case LFUN_SELF_INSERT:
1969         case LFUN_LINE_INSERT:
1970         case LFUN_PAGEBREAK_INSERT:
1971         case LFUN_CLEARPAGE_INSERT:
1972         case LFUN_CLEARDOUBLEPAGE_INSERT:
1973         case LFUN_MATH_DISPLAY:
1974         case LFUN_MATH_IMPORT_SELECTION:
1975         case LFUN_MATH_MODE:
1976         case LFUN_MATH_MACRO:
1977         case LFUN_MATH_MATRIX:
1978         case LFUN_MATH_DELIM:
1979         case LFUN_MATH_BIGDELIM:
1980         case LFUN_MATH_INSERT:
1981         case LFUN_MATH_SUBSCRIPT:
1982         case LFUN_MATH_SUPERSCRIPT:
1983         case LFUN_FONT_DEFAULT:
1984         case LFUN_FONT_UNDERLINE:
1985         case LFUN_FONT_SIZE:
1986         case LFUN_LANGUAGE:
1987         case LFUN_FONT_FREE_APPLY:
1988         case LFUN_FONT_FREE_UPDATE:
1989         case LFUN_LAYOUT_PARAGRAPH:
1990         case LFUN_PARAGRAPH_UPDATE:
1991         case LFUN_ACCENT_UMLAUT:
1992         case LFUN_ACCENT_CIRCUMFLEX:
1993         case LFUN_ACCENT_GRAVE:
1994         case LFUN_ACCENT_ACUTE:
1995         case LFUN_ACCENT_TILDE:
1996         case LFUN_ACCENT_CEDILLA:
1997         case LFUN_ACCENT_MACRON:
1998         case LFUN_ACCENT_DOT:
1999         case LFUN_ACCENT_UNDERDOT:
2000         case LFUN_ACCENT_UNDERBAR:
2001         case LFUN_ACCENT_CARON:
2002         case LFUN_ACCENT_SPECIAL_CARON:
2003         case LFUN_ACCENT_BREVE:
2004         case LFUN_ACCENT_TIE:
2005         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2006         case LFUN_ACCENT_CIRCLE:
2007         case LFUN_ACCENT_OGONEK:
2008         case LFUN_THESAURUS_ENTRY:
2009         case LFUN_PARAGRAPH_PARAMS_APPLY:
2010         case LFUN_ESCAPE:
2011         case LFUN_BUFFER_END:
2012         case LFUN_BUFFER_BEGIN:
2013         case LFUN_BUFFER_BEGIN_SELECT:
2014         case LFUN_BUFFER_END_SELECT:
2015         case LFUN_UNICODE_INSERT:
2016                 // these are handled in our dispatch()
2017                 enable = true;
2018                 break;
2019
2020         default:
2021                 return false;
2022         }
2023
2024         if (code != Inset::NO_CODE
2025             && (cur.empty() || !cur.inset().insetAllowed(code)))
2026                 enable = false;
2027
2028         flag.enabled(enable);
2029         return true;
2030 }
2031
2032
2033 void Text::pasteString(Cursor & cur, docstring const & clip,
2034                 bool asParagraphs)
2035 {
2036         cur.clearSelection();
2037         if (!clip.empty()) {
2038                 recordUndo(cur);
2039                 if (asParagraphs)
2040                         insertStringAsParagraphs(cur, clip);
2041                 else
2042                         insertStringAsLines(cur, clip);
2043         }
2044 }
2045
2046 } // namespace lyx