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