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