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