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