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