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