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