]> git.lyx.org Git - lyx.git/blob - src/Text3.cpp
Further cleanup of InsetFlex, InsetCollapsable and InsetLayout:
[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         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, 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                         cutSelection(cur, false, false);
1087                         // When change tracking is set to off, the metrics update
1088                         // mechanism correctly detects if a full update is needed or not.
1089                         // This detection fails when a selection spans multiple rows and
1090                         // change tracking is enabled because the paragraph metrics stays
1091                         // the same. In this case, we force the full update:
1092                         // (see http://bugzilla.lyx.org/show_bug.cgi?id=3992)
1093                         if (cur.buffer().params().trackChanges)
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(), 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         case LFUN_NOMENCL_INSERT: {
1248                 Inset * inset = createInset(&cur.bv(), cmd);
1249                 if (!inset)
1250                         break;
1251                 cur.recordUndo();
1252                 cur.clearSelection();
1253                 insertInset(cur, inset);
1254                 // Show the dialog for the nomenclature entry, since the
1255                 // description entry still needs to be filled in.
1256                 if (cmd.action == LFUN_NOMENCL_INSERT)
1257                         inset->edit(cur, true);
1258                 cur.posRight();
1259                 break;
1260         }
1261
1262         case LFUN_INDEX_PRINT:
1263         case LFUN_NOMENCL_PRINT:
1264         case LFUN_TOC_INSERT:
1265         case LFUN_HFILL_INSERT:
1266         case LFUN_LINE_INSERT:
1267         case LFUN_PAGEBREAK_INSERT:
1268         case LFUN_CLEARPAGE_INSERT:
1269         case LFUN_CLEARDOUBLEPAGE_INSERT:
1270                 // do nothing fancy
1271                 doInsertInset(cur, this, cmd, false, false);
1272                 cur.posRight();
1273                 break;
1274
1275         case LFUN_DEPTH_DECREMENT:
1276                 changeDepth(cur, DEC_DEPTH);
1277                 break;
1278
1279         case LFUN_DEPTH_INCREMENT:
1280                 changeDepth(cur, INC_DEPTH);
1281                 break;
1282
1283         case LFUN_MATH_DISPLAY:
1284                 mathDispatch(cur, cmd, true);
1285                 break;
1286
1287         case LFUN_MATH_IMPORT_SELECTION:
1288         case LFUN_MATH_MODE:
1289                 if (cmd.argument() == "on")
1290                         // don't pass "on" as argument
1291                         mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1292                 else
1293                         mathDispatch(cur, cmd, false);
1294                 break;
1295
1296         case LFUN_MATH_MACRO:
1297                 if (cmd.argument().empty())
1298                         cur.errorMessage(from_utf8(N_("Missing argument")));
1299                 else {
1300                         string s = to_utf8(cmd.argument());
1301                         string const s1 = token(s, ' ', 1);
1302                         int const nargs = s1.empty() ? 0 : convert<int>(s1);
1303                         string const s2 = token(s, ' ', 2);
1304                         string const type = s2.empty() ? "newcommand" : s2;
1305                         cur.insert(new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, false, from_utf8(type)));
1306                         //cur.nextInset()->edit(cur, true);
1307                 }
1308                 break;
1309
1310         // passthrough hat and underscore outside mathed:
1311         case LFUN_MATH_SUBSCRIPT:
1312                 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1313                 break;
1314         case LFUN_MATH_SUPERSCRIPT:
1315                 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1316                 break;
1317
1318         case LFUN_MATH_INSERT:
1319         case LFUN_MATH_MATRIX:
1320         case LFUN_MATH_DELIM:
1321         case LFUN_MATH_BIGDELIM: {
1322                 if (cur.selection())
1323                         cur.clearSelection();
1324                 // FIXME: instead of the above, this one
1325                 // should be used (but it asserts with Bidi enabled)
1326                 // cf. http://bugzilla.lyx.org/show_bug.cgi?id=4055
1327                 // cap::replaceSelection(cur);
1328                 cur.insert(new InsetMathHull(hullSimple));
1329                 checkAndActivateInset(cur, true);
1330                 BOOST_ASSERT(cur.inMathed());
1331                 cur.dispatch(cmd);
1332                 break;
1333         }
1334
1335         case LFUN_FONT_EMPH: {
1336                 Font font(ignore_font);
1337                 font.fontInfo().setEmph(FONT_TOGGLE);
1338                 toggleAndShow(cur, this, font);
1339                 break;
1340         }
1341
1342         case LFUN_FONT_BOLD: {
1343                 Font font(ignore_font);
1344                 font.fontInfo().setSeries(BOLD_SERIES);
1345                 toggleAndShow(cur, this, font);
1346                 break;
1347         }
1348
1349         case LFUN_FONT_NOUN: {
1350                 Font font(ignore_font);
1351                 font.fontInfo().setNoun(FONT_TOGGLE);
1352                 toggleAndShow(cur, this, font);
1353                 break;
1354         }
1355
1356         case LFUN_FONT_TYPEWRITER: {
1357                 Font font(ignore_font);
1358                 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1359                 toggleAndShow(cur, this, font);
1360                 break;
1361         }
1362
1363         case LFUN_FONT_SANS: {
1364                 Font font(ignore_font);
1365                 font.fontInfo().setFamily(SANS_FAMILY);
1366                 toggleAndShow(cur, this, font);
1367                 break;
1368         }
1369
1370         case LFUN_FONT_ROMAN: {
1371                 Font font(ignore_font);
1372                 font.fontInfo().setFamily(ROMAN_FAMILY);
1373                 toggleAndShow(cur, this, font);
1374                 break;
1375         }
1376
1377         case LFUN_FONT_DEFAULT: {
1378                 Font font(inherit_font, ignore_language);
1379                 toggleAndShow(cur, this, font);
1380                 break;
1381         }
1382
1383         case LFUN_FONT_UNDERLINE: {
1384                 Font font(ignore_font);
1385                 font.fontInfo().setUnderbar(FONT_TOGGLE);
1386                 toggleAndShow(cur, this, font);
1387                 break;
1388         }
1389
1390         case LFUN_FONT_SIZE: {
1391                 Font font(ignore_font);
1392                 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1393                 toggleAndShow(cur, this, font);
1394                 break;
1395         }
1396
1397         case LFUN_LANGUAGE: {
1398                 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1399                 if (!lang)
1400                         break;
1401                 Font font(ignore_font);
1402                 font.setLanguage(lang);
1403                 toggleAndShow(cur, this, font);
1404                 break;
1405         }
1406
1407         case LFUN_FONT_FREE_APPLY:
1408                 toggleAndShow(cur, this, freefont, toggleall);
1409                 cur.message(_("Character set"));
1410                 break;
1411
1412         // Set the freefont using the contents of \param data dispatched from
1413         // the frontends and apply it at the current cursor location.
1414         case LFUN_FONT_FREE_UPDATE: {
1415                 Font font;
1416                 bool toggle;
1417                 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1418                         freefont = font;
1419                         toggleall = toggle;
1420                         toggleAndShow(cur, this, freefont, toggleall);
1421                         cur.message(_("Character set"));
1422                 } else {
1423                         lyxerr << "Argument not ok";
1424                 }
1425                 break;
1426         }
1427
1428         case LFUN_FINISHED_LEFT:
1429                 LYXERR(Debug::DEBUG) << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1430                 if (reverseDirectionNeeded(cur)) {
1431                         ++cur.pos();
1432                         cur.setCurrentFont();
1433                 }
1434                 break;
1435
1436         case LFUN_FINISHED_RIGHT:
1437                 LYXERR(Debug::DEBUG) << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1438                 if (!reverseDirectionNeeded(cur)) {
1439                         ++cur.pos();
1440                         cur.setCurrentFont();
1441                 }
1442                 break;
1443
1444         case LFUN_FINISHED_BACKWARD:
1445                 LYXERR(Debug::DEBUG) << "handle LFUN_FINISHED_BACKWARD:\n" << cur << endl;
1446                 break;
1447
1448         case LFUN_FINISHED_FORWARD:
1449                 LYXERR(Debug::DEBUG) << "handle LFUN_FINISHED_FORWARD:\n" << cur << endl;
1450                 ++cur.pos();
1451                 cur.setCurrentFont();
1452                 break;
1453
1454         case LFUN_LAYOUT_PARAGRAPH: {
1455                 string data;
1456                 params2string(cur.paragraph(), data);
1457                 data = "show\n" + data;
1458                 bv->showDialogWithData("paragraph", data);
1459                 break;
1460         }
1461
1462         case LFUN_PARAGRAPH_UPDATE: {
1463                 string data;
1464                 params2string(cur.paragraph(), data);
1465
1466                 // Will the paragraph accept changes from the dialog?
1467                 bool const accept = !cur.inset().forceDefaultParagraphs(cur.idx());
1468
1469                 data = "update " + convert<string>(accept) + '\n' + data;
1470                 bv->updateDialog("paragraph", data);
1471                 break;
1472         }
1473
1474         case LFUN_ACCENT_UMLAUT:
1475         case LFUN_ACCENT_CIRCUMFLEX:
1476         case LFUN_ACCENT_GRAVE:
1477         case LFUN_ACCENT_ACUTE:
1478         case LFUN_ACCENT_TILDE:
1479         case LFUN_ACCENT_CEDILLA:
1480         case LFUN_ACCENT_MACRON:
1481         case LFUN_ACCENT_DOT:
1482         case LFUN_ACCENT_UNDERDOT:
1483         case LFUN_ACCENT_UNDERBAR:
1484         case LFUN_ACCENT_CARON:
1485         case LFUN_ACCENT_SPECIAL_CARON:
1486         case LFUN_ACCENT_BREVE:
1487         case LFUN_ACCENT_TIE:
1488         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1489         case LFUN_ACCENT_CIRCLE:
1490         case LFUN_ACCENT_OGONEK:
1491                 theLyXFunc().handleKeyFunc(cmd.action);
1492                 if (!cmd.argument().empty())
1493                         // FIXME: Are all these characters encoded in one byte in utf8?
1494                         bv->translateAndInsert(cmd.argument()[0], this, cur);
1495                 break;
1496
1497         case LFUN_FLOAT_LIST: {
1498                 TextClass const & tclass = bv->buffer().params().getTextClass();
1499                 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1500                         cur.recordUndo();
1501                         if (cur.selection())
1502                                 cutSelection(cur, true, false);
1503                         breakParagraph(cur);
1504
1505                         if (cur.lastpos() != 0) {
1506                                 cursorBackward(cur);
1507                                 breakParagraph(cur);
1508                         }
1509
1510                         setLayout(cur, tclass.defaultLayoutName());
1511                         ParagraphParameters p;
1512                         setParagraphs(cur, p);
1513                         insertInset(cur, new InsetFloatList(to_utf8(cmd.argument())));
1514                         cur.posRight();
1515                 } else {
1516                         lyxerr << "Non-existent float type: "
1517                                << to_utf8(cmd.argument()) << endl;
1518                 }
1519                 break;
1520         }
1521
1522         case LFUN_CHANGE_ACCEPT: {
1523                 acceptOrRejectChanges(cur, ACCEPT);
1524                 break;
1525         }
1526
1527         case LFUN_CHANGE_REJECT: {
1528                 acceptOrRejectChanges(cur, REJECT);
1529                 break;
1530         }
1531
1532         case LFUN_THESAURUS_ENTRY: {
1533                 docstring arg = cmd.argument();
1534                 if (arg.empty()) {
1535                         arg = cur.selectionAsString(false);
1536                         // FIXME
1537                         if (arg.size() > 100 || arg.empty()) {
1538                                 // Get word or selection
1539                                 selectWordWhenUnderCursor(cur, WHOLE_WORD);
1540                                 arg = cur.selectionAsString(false);
1541                         }
1542                 }
1543                 bv->showDialogWithData("thesaurus", to_utf8(arg));
1544                 break;
1545         }
1546
1547         case LFUN_PARAGRAPH_PARAMS_APPLY: {
1548                 // Given data, an encoding of the ParagraphParameters
1549                 // generated in the Paragraph dialog, this function sets
1550                 // the current paragraph, or currently selected paragraphs,
1551                 // appropriately. 
1552                 // NOTE: This function overrides all existing settings.
1553                 setParagraphs(cur, cmd.argument());
1554                 cur.message(_("Paragraph layout set"));
1555                 break;
1556         }
1557         
1558         case LFUN_PARAGRAPH_PARAMS: {
1559                 // Given data, an encoding of the ParagraphParameters as we'd
1560                 // find them in a LyX file, this function modifies the current paragraph, 
1561                 // or currently selected paragraphs. 
1562                 // NOTE: This function only modifies, and does not override, existing
1563                 // settings.
1564                 setParagraphs(cur, cmd.argument(), true);
1565                 cur.message(_("Paragraph layout set"));
1566                 break;
1567         }
1568
1569         case LFUN_ESCAPE:
1570                 if (cur.selection()) {
1571                         cur.selection() = false;
1572                 } else {
1573                         cur.undispatched();
1574                         // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
1575                         // correct, but I'm not 100% sure -- dov, 071019
1576                         cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1577                 }
1578                 break;
1579
1580         default:
1581                 LYXERR(Debug::ACTION)
1582                         << BOOST_CURRENT_FUNCTION
1583                         << ": Command " << cmd
1584                         << " not DISPATCHED by Text" << endl;
1585                 cur.undispatched();
1586                 break;
1587         }
1588
1589         needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
1590
1591         // FIXME: The cursor flag is reset two lines below
1592         // so we need to check here if some of the LFUN did touch that.
1593         // for now only Text::erase() and Text::backspace() do that.
1594         // The plan is to verify all the LFUNs and then to remove this
1595         // singleParUpdate boolean altogether.
1596         if (cur.result().update() & Update::Force) {
1597                 singleParUpdate = false;
1598                 needsUpdate = true;
1599         }
1600
1601         // FIXME: the following code should go in favor of fine grained
1602         // update flag treatment.
1603         if (singleParUpdate) {
1604                 // Inserting characters does not change par height
1605                 ParagraphMetrics const & pms
1606                         = cur.bv().parMetrics(cur.bottom().text(), cur.bottom().pit());
1607                 if (pms.dim().height()
1608                     == olddim.height()) {
1609                         // if so, update _only_ this paragraph
1610                         cur.updateFlags(Update::SinglePar |
1611                                 Update::FitCursor |
1612                                 Update::MultiParSel);
1613                         return;
1614                 } else
1615                         needsUpdate = true;
1616         }
1617
1618         if (!needsUpdate
1619             && &oldTopSlice.inset() == &cur.inset()
1620             && oldTopSlice.idx() == cur.idx()
1621             && !sel // sel is a backup of cur.selection() at the biginning of the function.
1622             && !cur.selection())
1623                 // FIXME: it would be better if we could just do this
1624                 //
1625                 //if (cur.result().update() != Update::FitCursor)
1626                 //      cur.noUpdate();
1627                 //
1628                 // But some LFUNs do not set Update::FitCursor when needed, so we
1629                 // do it for all. This is not very harmfull as FitCursor will provoke
1630                 // a full redraw only if needed but still, a proper review of all LFUN
1631                 // should be done and this needsUpdate boolean can then be removed.
1632                 cur.updateFlags(Update::FitCursor);
1633         else
1634                 cur.updateFlags(Update::Force | Update::FitCursor);
1635 }
1636
1637
1638 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
1639                         FuncStatus & flag) const
1640 {
1641         BOOST_ASSERT(cur.text() == this);
1642
1643         Font const & font = cur.real_current_font;
1644         FontInfo const & fontinfo = font.fontInfo();
1645         bool enable = true;
1646         InsetCode code = NO_CODE;
1647
1648         switch (cmd.action) {
1649
1650         case LFUN_DEPTH_DECREMENT:
1651                 enable = changeDepthAllowed(cur, DEC_DEPTH);
1652                 break;
1653
1654         case LFUN_DEPTH_INCREMENT:
1655                 enable = changeDepthAllowed(cur, INC_DEPTH);
1656                 break;
1657
1658         case LFUN_APPENDIX:
1659                 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1660                 return true;
1661
1662         case LFUN_BIBITEM_INSERT:
1663                 enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO
1664                           && cur.pos() == 0);
1665                 break;
1666
1667         case LFUN_DIALOG_SHOW_NEW_INSET:
1668                 if (cmd.argument() == "bibitem")
1669                         code = BIBITEM_CODE;
1670                 else if (cmd.argument() == "bibtex")
1671                         code = BIBTEX_CODE;
1672                 else if (cmd.argument() == "box")
1673                         code = BOX_CODE;
1674                 else if (cmd.argument() == "branch")
1675                         code = BRANCH_CODE;
1676                 else if (cmd.argument() == "citation")
1677                         code = CITE_CODE;
1678                 else if (cmd.argument() == "ert")
1679                         code = ERT_CODE;
1680                 else if (cmd.argument() == "external")
1681                         code = EXTERNAL_CODE;
1682                 else if (cmd.argument() == "float")
1683                         code = FLOAT_CODE;
1684                 else if (cmd.argument() == "graphics")
1685                         code = GRAPHICS_CODE;
1686                 else if (cmd.argument() == "href")
1687                         code = HYPERLINK_CODE;
1688                 else if (cmd.argument() == "include")
1689                         code = INCLUDE_CODE;
1690                 else if (cmd.argument() == "index")
1691                         code = INDEX_CODE;
1692                 else if (cmd.argument() == "nomenclature")
1693                         code = NOMENCL_CODE;
1694                 else if (cmd.argument() == "label")
1695                         code = LABEL_CODE;
1696                 else if (cmd.argument() == "note")
1697                         code = NOTE_CODE;
1698                 else if (cmd.argument() == "ref")
1699                         code = REF_CODE;
1700                 else if (cmd.argument() == "toc")
1701                         code = TOC_CODE;
1702                 else if (cmd.argument() == "vspace")
1703                         code = VSPACE_CODE;
1704                 else if (cmd.argument() == "wrap")
1705                         code = WRAP_CODE;
1706                 else if (cmd.argument() == "listings")
1707                         code = LISTINGS_CODE;
1708                 break;
1709
1710         case LFUN_ERT_INSERT:
1711                 code = ERT_CODE;
1712                 break;
1713         case LFUN_LISTING_INSERT:
1714             code = LISTINGS_CODE;
1715                 break;
1716         case LFUN_FOOTNOTE_INSERT:
1717                 code = FOOT_CODE;
1718                 break;
1719         case LFUN_TABULAR_INSERT:
1720                 code = TABULAR_CODE;
1721                 break;
1722         case LFUN_MARGINALNOTE_INSERT:
1723                 code = MARGIN_CODE;
1724                 break;
1725         case LFUN_FLOAT_INSERT:
1726         case LFUN_FLOAT_WIDE_INSERT:
1727                 code = FLOAT_CODE;
1728                 break;
1729         case LFUN_WRAP_INSERT:
1730                 code = WRAP_CODE;
1731                 break;
1732         case LFUN_FLOAT_LIST:
1733                 code = FLOAT_LIST_CODE;
1734                 break;
1735 #if 0
1736         case LFUN_LIST_INSERT:
1737                 code = LIST_CODE;
1738                 break;
1739         case LFUN_THEOREM_INSERT:
1740                 code = THEOREM_CODE;
1741                 break;
1742 #endif
1743         case LFUN_CAPTION_INSERT:
1744                 code = CAPTION_CODE;
1745                 break;
1746         case LFUN_NOTE_INSERT:
1747                 code = NOTE_CODE;
1748                 break;
1749         case LFUN_FLEX_INSERT: {
1750                 code = FLEX_CODE;
1751                 string s = cmd.getArg(0);
1752                 InsetLayout il =  cur.buffer().params().getTextClass().insetlayout(from_utf8(s));
1753                 if (il.lyxtype != "charstyle" &&
1754                     il.lyxtype != "custom" &&
1755                     il.lyxtype != "element")
1756                         enable = false;
1757                 break;
1758                 }
1759         case LFUN_BOX_INSERT:
1760                 code = BOX_CODE;
1761                 break;
1762         case LFUN_BRANCH_INSERT:
1763                 code = BRANCH_CODE;
1764                 if (cur.buffer().masterBuffer()->params().branchlist().empty())
1765                         enable = false;
1766                 break;
1767         case LFUN_LABEL_INSERT:
1768                 code = LABEL_CODE;
1769                 break;
1770         case LFUN_INFO_INSERT:
1771                 code = INFO_CODE;
1772                 break;
1773         case LFUN_OPTIONAL_INSERT:
1774                 code = OPTARG_CODE;
1775                 enable = cur.paragraph().insetList().count(OPTARG_CODE)
1776                         < cur.paragraph().layout()->optionalargs;
1777                 break;
1778         case LFUN_ENVIRONMENT_INSERT:
1779                 code = BOX_CODE;
1780                 break;
1781         case LFUN_INDEX_INSERT:
1782                 code = INDEX_CODE;
1783                 break;
1784         case LFUN_INDEX_PRINT:
1785                 code = INDEX_PRINT_CODE;
1786                 break;
1787         case LFUN_NOMENCL_INSERT:
1788                 code = NOMENCL_CODE;
1789                 break;
1790         case LFUN_NOMENCL_PRINT:
1791                 code = NOMENCL_PRINT_CODE;
1792                 break;
1793         case LFUN_TOC_INSERT:
1794                 code = TOC_CODE;
1795                 break;
1796         case LFUN_HYPERLINK_INSERT:
1797                 code = HYPERLINK_CODE;
1798                 break;
1799         case LFUN_QUOTE_INSERT:
1800                 // always allow this, since we will inset a raw quote
1801                 // if an inset is not allowed.
1802                 break;
1803         case LFUN_HYPHENATION_POINT_INSERT:
1804         case LFUN_LIGATURE_BREAK_INSERT:
1805         case LFUN_HFILL_INSERT:
1806         case LFUN_MENU_SEPARATOR_INSERT:
1807         case LFUN_DOTS_INSERT:
1808         case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
1809                 code = SPECIALCHAR_CODE;
1810                 break;
1811         case LFUN_SPACE_INSERT:
1812                 // slight hack: we know this is allowed in math mode
1813                 if (cur.inTexted())
1814                         code = SPACE_CODE;
1815                 break;
1816
1817         case LFUN_INSET_MODIFY:
1818                 // We need to disable this, because we may get called for a
1819                 // tabular cell via
1820                 // InsetTabular::getStatus() -> InsetText::getStatus()
1821                 // and we don't handle LFUN_INSET_MODIFY.
1822                 enable = false;
1823                 break;
1824
1825         case LFUN_FONT_EMPH:
1826                 flag.setOnOff(fontinfo.emph() == FONT_ON);
1827                 return true;
1828
1829         case LFUN_FONT_NOUN:
1830                 flag.setOnOff(fontinfo.noun() == FONT_ON);
1831                 return true;
1832
1833         case LFUN_FONT_BOLD:
1834                 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
1835                 return true;
1836
1837         case LFUN_FONT_SANS:
1838                 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
1839                 return true;
1840
1841         case LFUN_FONT_ROMAN:
1842                 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
1843                 return true;
1844
1845         case LFUN_FONT_TYPEWRITER:
1846                 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
1847                 return true;
1848
1849         case LFUN_CUT:
1850         case LFUN_COPY:
1851                 enable = cur.selection();
1852                 break;
1853
1854         case LFUN_PASTE:
1855                 if (cmd.argument().empty()) {
1856                         if (theClipboard().isInternal())
1857                                 enable = cap::numberOfSelections() > 0;
1858                         else
1859                                 enable = !theClipboard().empty();
1860                 } else {
1861                         string const arg = to_utf8(cmd.argument());
1862                         if (isStrUnsignedInt(arg)) {
1863                                 unsigned int n = convert<unsigned int>(arg);
1864                                 enable = cap::numberOfSelections() > n;
1865                         } else
1866                                 // unknown argument
1867                                 enable = false;
1868                 }
1869                 break;
1870
1871         case LFUN_CLIPBOARD_PASTE:
1872                 enable = !theClipboard().empty();
1873                 break;
1874
1875         case LFUN_PRIMARY_SELECTION_PASTE:
1876                 enable = cur.selection() || !theSelection().empty();
1877                 break;
1878
1879         case LFUN_PARAGRAPH_MOVE_UP:
1880                 enable = cur.pit() > 0 && !cur.selection();
1881                 break;
1882
1883         case LFUN_PARAGRAPH_MOVE_DOWN:
1884                 enable = cur.pit() < cur.lastpit() && !cur.selection();
1885                 break;
1886
1887         case LFUN_INSET_DISSOLVE:
1888                 if (!cmd.argument().empty()) {
1889                         InsetLayout il = cur.inset().getLayout(cur.buffer().params());
1890                         enable = cur.inset().lyxCode() == FLEX_CODE
1891                                  && il.lyxtype == to_utf8(cmd.argument());
1892                 } else {
1893                         enable = !isMainText(cur.bv().buffer()) 
1894                                  && cur.inset().nargs() == 1;
1895                 }
1896                 break;
1897
1898         case LFUN_CHANGE_ACCEPT:
1899         case LFUN_CHANGE_REJECT:
1900                 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
1901                 // In principle, these LFUNs should only be enabled if there
1902                 // is a change at the current position/in the current selection.
1903                 // However, without proper optimizations, this will inevitably
1904                 // result in unacceptable performance - just imagine a user who
1905                 // wants to select the complete content of a long document.
1906                 enable = true;
1907                 break;
1908
1909         case LFUN_WORD_DELETE_FORWARD:
1910         case LFUN_WORD_DELETE_BACKWARD:
1911         case LFUN_LINE_DELETE:
1912         case LFUN_WORD_FORWARD:
1913         case LFUN_WORD_BACKWARD:
1914         case LFUN_WORD_RIGHT:
1915         case LFUN_WORD_LEFT:
1916         case LFUN_CHAR_FORWARD:
1917         case LFUN_CHAR_FORWARD_SELECT:
1918         case LFUN_CHAR_BACKWARD:
1919         case LFUN_CHAR_BACKWARD_SELECT:
1920         case LFUN_CHAR_LEFT:
1921         case LFUN_CHAR_LEFT_SELECT:
1922         case LFUN_CHAR_RIGHT:
1923         case LFUN_CHAR_RIGHT_SELECT:
1924         case LFUN_UP:
1925         case LFUN_UP_SELECT:
1926         case LFUN_DOWN:
1927         case LFUN_DOWN_SELECT:
1928         case LFUN_PARAGRAPH_UP_SELECT:
1929         case LFUN_PARAGRAPH_DOWN_SELECT:
1930         case LFUN_SCREEN_UP_SELECT:
1931         case LFUN_SCREEN_DOWN_SELECT:
1932         case LFUN_LINE_BEGIN_SELECT:
1933         case LFUN_LINE_END_SELECT:
1934         case LFUN_WORD_FORWARD_SELECT:
1935         case LFUN_WORD_BACKWARD_SELECT:
1936         case LFUN_WORD_RIGHT_SELECT:
1937         case LFUN_WORD_LEFT_SELECT:
1938         case LFUN_WORD_SELECT:
1939         case LFUN_PARAGRAPH_UP:
1940         case LFUN_PARAGRAPH_DOWN:
1941         case LFUN_LINE_BEGIN:
1942         case LFUN_LINE_END:
1943         case LFUN_BREAK_LINE:
1944         case LFUN_CHAR_DELETE_FORWARD:
1945         case LFUN_DELETE_FORWARD_SKIP:
1946         case LFUN_CHAR_DELETE_BACKWARD:
1947         case LFUN_DELETE_BACKWARD_SKIP:
1948         case LFUN_BREAK_PARAGRAPH:
1949         case LFUN_BREAK_PARAGRAPH_SKIP:
1950         case LFUN_PARAGRAPH_SPACING:
1951         case LFUN_INSET_INSERT:
1952         case LFUN_WORD_UPCASE:
1953         case LFUN_WORD_LOWCASE:
1954         case LFUN_WORD_CAPITALIZE:
1955         case LFUN_CHARS_TRANSPOSE:
1956         case LFUN_SERVER_GET_XY:
1957         case LFUN_SERVER_SET_XY:
1958         case LFUN_SERVER_GET_FONT:
1959         case LFUN_SERVER_GET_LAYOUT:
1960         case LFUN_LAYOUT:
1961         case LFUN_DATE_INSERT:
1962         case LFUN_SELF_INSERT:
1963         case LFUN_LINE_INSERT:
1964         case LFUN_PAGEBREAK_INSERT:
1965         case LFUN_CLEARPAGE_INSERT:
1966         case LFUN_CLEARDOUBLEPAGE_INSERT:
1967         case LFUN_MATH_DISPLAY:
1968         case LFUN_MATH_IMPORT_SELECTION:
1969         case LFUN_MATH_MODE:
1970         case LFUN_MATH_MACRO:
1971         case LFUN_MATH_MATRIX:
1972         case LFUN_MATH_DELIM:
1973         case LFUN_MATH_BIGDELIM:
1974         case LFUN_MATH_INSERT:
1975         case LFUN_MATH_SUBSCRIPT:
1976         case LFUN_MATH_SUPERSCRIPT:
1977         case LFUN_FONT_DEFAULT:
1978         case LFUN_FONT_UNDERLINE:
1979         case LFUN_FONT_SIZE:
1980         case LFUN_LANGUAGE:
1981         case LFUN_FONT_FREE_APPLY:
1982         case LFUN_FONT_FREE_UPDATE:
1983         case LFUN_LAYOUT_PARAGRAPH:
1984         case LFUN_PARAGRAPH_UPDATE:
1985         case LFUN_ACCENT_UMLAUT:
1986         case LFUN_ACCENT_CIRCUMFLEX:
1987         case LFUN_ACCENT_GRAVE:
1988         case LFUN_ACCENT_ACUTE:
1989         case LFUN_ACCENT_TILDE:
1990         case LFUN_ACCENT_CEDILLA:
1991         case LFUN_ACCENT_MACRON:
1992         case LFUN_ACCENT_DOT:
1993         case LFUN_ACCENT_UNDERDOT:
1994         case LFUN_ACCENT_UNDERBAR:
1995         case LFUN_ACCENT_CARON:
1996         case LFUN_ACCENT_SPECIAL_CARON:
1997         case LFUN_ACCENT_BREVE:
1998         case LFUN_ACCENT_TIE:
1999         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2000         case LFUN_ACCENT_CIRCLE:
2001         case LFUN_ACCENT_OGONEK:
2002         case LFUN_THESAURUS_ENTRY:
2003         case LFUN_PARAGRAPH_PARAMS_APPLY:
2004         case LFUN_PARAGRAPH_PARAMS:
2005         case LFUN_ESCAPE:
2006         case LFUN_BUFFER_END:
2007         case LFUN_BUFFER_BEGIN:
2008         case LFUN_BUFFER_BEGIN_SELECT:
2009         case LFUN_BUFFER_END_SELECT:
2010         case LFUN_UNICODE_INSERT:
2011                 // these are handled in our dispatch()
2012                 enable = true;
2013                 break;
2014
2015         default:
2016                 return false;
2017         }
2018
2019         if (code != NO_CODE
2020             && (cur.empty() || !cur.inset().insetAllowed(code)))
2021                 enable = false;
2022
2023         flag.enabled(enable);
2024         return true;
2025 }
2026
2027
2028 void Text::pasteString(Cursor & cur, docstring const & clip,
2029                 bool asParagraphs)
2030 {
2031         cur.clearSelection();
2032         if (!clip.empty()) {
2033                 cur.recordUndo();
2034                 if (asParagraphs)
2035                         insertStringAsParagraphs(cur, clip);
2036                 else
2037                         insertStringAsLines(cur, clip);
2038         }
2039 }
2040
2041 } // namespace lyx