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