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