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