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