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