]> git.lyx.org Git - lyx.git/blob - src/Text3.cpp
Final fix to get unit with optional arg (value) working
[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/InsetCommand.h"
54 #include "insets/InsetFloatList.h"
55 #include "insets/InsetNewline.h"
56 #include "insets/InsetQuotes.h"
57 #include "insets/InsetSpecialChar.h"
58 #include "insets/InsetText.h"
59
60 #include "support/lstrings.h"
61 #include "support/lyxlib.h"
62 #include "support/convert.h"
63 #include "support/lyxtime.h"
64
65 #include "mathed/InsetMathHull.h"
66 #include "mathed/MathMacroTemplate.h"
67
68 #include <boost/current_function.hpp>
69
70 #include <clocale>
71 #include <sstream>
72
73 using std::endl;
74 using std::string;
75 using std::istringstream;
76 using std::ostringstream;
77
78 namespace lyx {
79
80 using cap::copySelection;
81 using cap::cutSelection;
82 using cap::pasteFromStack;
83 using cap::pasteClipboard;
84 using cap::replaceSelection;
85
86 using support::isStrUnsignedInt;
87 using support::token;
88
89 namespace frontend {
90 extern docstring current_layout;
91 }
92
93 // globals...
94 static Font freefont(Font::ALL_IGNORE);
95 static bool toggleall = false;
96
97 static void toggleAndShow(Cursor & cur, Text * text,
98         Font const & font, bool toggleall = true)
99 {
100         text->toggleFree(cur, font, toggleall);
101
102         if (font.language() != ignore_language ||
103                         font.number() != Font::IGNORE) {
104                 TextMetrics const & tm = cur.bv().textMetrics(text);
105                 if (cur.boundary() != tm.isRTLBoundary(cur.pit(),
106                                                                                                                 cur.pos(), cur.real_current_font))
107                         text->setCursor(cur, cur.pit(), cur.pos(),
108                                                                                         false, !cur.boundary());
109         }
110 }
111
112
113 static void moveCursor(Cursor & cur, bool selecting)
114 {
115         if (selecting || cur.mark())
116                 cur.setSelection();
117 }
118
119
120 static void finishChange(Cursor & cur, bool selecting)
121 {
122         finishUndo();
123         moveCursor(cur, selecting);
124 }
125
126
127 static void mathDispatch(Cursor & cur, FuncRequest const & cmd, bool display)
128 {
129         recordUndo(cur);
130         docstring sel = cur.selectionAsString(false);
131
132         // It may happen that sel is empty but there is a selection
133         replaceSelection(cur);
134
135         if (sel.empty()) {
136 #ifdef ENABLE_ASSERTIONS
137                 const int old_pos = cur.pos();
138 #endif
139                 cur.insert(new InsetMathHull(hullSimple));
140                 BOOST_ASSERT(old_pos == cur.pos());
141                 cur.nextInset()->edit(cur, true);
142                 // don't do that also for LFUN_MATH_MODE
143                 // unless you want end up with always changing
144                 // to mathrm when opening an inlined inset --
145                 // I really hate "LyXfunc overloading"...
146                 if (display)
147                         cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
148                 // Avoid an unnecessary undo step if cmd.argument
149                 // is empty
150                 if (!cmd.argument().empty())
151                         cur.dispatch(FuncRequest(LFUN_MATH_INSERT,
152                                                  cmd.argument()));
153         } else {
154                 // create a macro if we see "\\newcommand"
155                 // somewhere, and an ordinary formula
156                 // otherwise
157                 if (sel.find(from_ascii("\\newcommand")) == string::npos
158                                 && sel.find(from_ascii("\\def")) == string::npos)
159                 {
160                         InsetMathHull * formula = new InsetMathHull;
161                         istringstream is(to_utf8(sel));
162                         Lexer lex(0, 0);
163                         lex.setStream(is);
164                         formula->read(cur.buffer(), lex);
165                         if (formula->getType() == hullNone)
166                                 // Don't create pseudo formulas if
167                                 // delimiters are left out
168                                 formula->mutate(hullSimple);
169                         cur.insert(formula);
170                 } else {
171                         cur.insert(new MathMacroTemplate(sel));
172                 }
173         }
174         cur.message(from_utf8(N_("Math editor mode")));
175 }
176
177
178 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
179 {
180         recordUndo(cur);
181         cap::replaceSelection(cur);
182         cur.insert(new InsetSpecialChar(kind));
183         cur.posRight();
184 }
185
186
187 static bool doInsertInset(Cursor & cur, Text * text,
188         FuncRequest const & cmd, bool edit, bool pastesel)
189 {
190         Inset * inset = createInset(&cur.bv(), cmd);
191         if (!inset)
192                 return false;
193
194         recordUndo(cur);
195         bool gotsel = false;
196         if (cur.selection()) {
197                 lyx::dispatch(FuncRequest(LFUN_CUT));
198                 gotsel = true;
199         }
200         text->insertInset(cur, inset);
201
202         if (edit)
203                 inset->edit(cur, true);
204
205         if (gotsel && pastesel) {
206                 lyx::dispatch(FuncRequest(LFUN_PASTE, "0"));
207                 // reset first par to default
208                 if (cur.lastpit() != 0 || cur.lastpos() != 0) {
209                         LayoutPtr const layout =
210                                 cur.buffer().params().getTextClass().defaultLayout();
211                         cur.text()->paragraphs().begin()->layout(layout);
212                 }
213         }
214         return true;
215 }
216
217
218 string const freefont2string()
219 {
220         return freefont.toString(toggleall);
221 }
222
223
224 void Text::number(Cursor & cur)
225 {
226         Font font(Font::ALL_IGNORE);
227         font.setNumber(Font::TOGGLE);
228         toggleAndShow(cur, this, font);
229 }
230
231
232 bool Text::isRTL(Buffer const & buffer, Paragraph const & par) const
233 {
234         return par.isRTL(buffer.params());
235 }
236
237
238 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
239 {
240         LYXERR(Debug::ACTION) << "Text::dispatch: cmd: " << cmd << endl;
241
242         // FIXME: We use the update flag to indicates wether a singlePar or a
243         // full screen update is needed. We reset it here but shall we restore it
244         // at the end?
245         cur.noUpdate();
246
247         BOOST_ASSERT(cur.text() == this);
248         BufferView * bv = &cur.bv();
249         TextMetrics & tm = cur.bv().textMetrics(this);
250         CursorSlice oldTopSlice = cur.top();
251         bool oldBoundary = cur.boundary();
252         bool sel = cur.selection();
253         // Signals that, even if needsUpdate == false, an update of the
254         // cursor paragraph is required
255         bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
256                 LyXAction::SingleParUpdate);
257         // Signals that a full-screen update is required
258         bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
259                 LyXAction::NoUpdate) || singleParUpdate);
260         // Remember the old paragraph metric (_outer_ paragraph!)
261         ParagraphMetrics const & pm = cur.bv().parMetrics(
262                 cur.bottom().text(), cur.bottom().pit());
263         Dimension olddim = pm.dim();
264
265         switch (cmd.action) {
266
267         case LFUN_PARAGRAPH_MOVE_DOWN: {
268                 pit_type const pit = cur.pit();
269                 recUndo(cur, pit, pit + 1);
270                 finishUndo();
271                 std::swap(pars_[pit], pars_[pit + 1]);
272                 updateLabels(cur.buffer());
273                 needsUpdate = true;
274                 ++cur.pit();
275                 break;
276         }
277
278         case LFUN_PARAGRAPH_MOVE_UP: {
279                 pit_type const pit = cur.pit();
280                 recUndo(cur, pit - 1, pit);
281                 finishUndo();
282                 std::swap(pars_[pit], pars_[pit - 1]);
283                 updateLabels(cur.buffer());
284                 --cur.pit();
285                 needsUpdate = true;
286                 break;
287         }
288
289         case LFUN_APPENDIX: {
290                 Paragraph & par = cur.paragraph();
291                 bool start = !par.params().startOfAppendix();
292
293 // FIXME: The code below only makes sense at top level.
294 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
295                 // ensure that we have only one start_of_appendix in this document
296                 // FIXME: this don't work for multipart document!
297                 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
298                         if (pars_[tmp].params().startOfAppendix()) {
299                                 recUndo(cur, tmp);
300                                 pars_[tmp].params().startOfAppendix(false);
301                                 break;
302                         }
303                 }
304
305                 recordUndo(cur);
306                 par.params().startOfAppendix(start);
307
308                 // we can set the refreshing parameters now
309                 updateLabels(cur.buffer());
310                 break;
311         }
312
313         case LFUN_WORD_DELETE_FORWARD:
314                 if (cur.selection()) {
315                         cutSelection(cur, true, false);
316                 } else
317                         deleteWordForward(cur);
318                 finishChange(cur, false);
319                 break;
320
321         case LFUN_WORD_DELETE_BACKWARD:
322                 if (cur.selection()) {
323                         cutSelection(cur, true, false);
324                 } else
325                         deleteWordBackward(cur);
326                 finishChange(cur, false);
327                 break;
328
329         case LFUN_LINE_DELETE:
330                 if (cur.selection()) {
331                         cutSelection(cur, true, false);
332                 } else
333                         tm.deleteLineForward(cur);
334                 finishChange(cur, false);
335                 break;
336
337         case LFUN_BUFFER_BEGIN:
338         case LFUN_BUFFER_BEGIN_SELECT:
339                 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_BEGIN_SELECT);
340                 if (cur.depth() == 1) {
341                         needsUpdate |= cursorTop(cur);
342                 } else {
343                         cur.undispatched();
344                 }
345                 break;
346
347         case LFUN_BUFFER_END:
348         case LFUN_BUFFER_END_SELECT:
349                 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_END_SELECT);
350                 if (cur.depth() == 1) {
351                         needsUpdate |= cursorBottom(cur);
352                 } else {
353                         cur.undispatched();
354                 }
355                 break;
356
357         case LFUN_CHAR_FORWARD:
358         case LFUN_CHAR_FORWARD_SELECT:
359                 //lyxerr << BOOST_CURRENT_FUNCTION
360                 //       << " LFUN_CHAR_FORWARD[SEL]:\n" << cur << endl;
361                 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
362                 if (reverseDirectionNeeded(cur))
363                         needsUpdate |= cursorLeft(cur);
364                 else
365                         needsUpdate |= cursorRight(cur);
366
367                 if (!needsUpdate && oldTopSlice == cur.top()
368                                 && cur.boundary() == oldBoundary) {
369                         cur.undispatched();
370                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
371                 }
372                 break;
373
374         case LFUN_CHAR_BACKWARD:
375         case LFUN_CHAR_BACKWARD_SELECT:
376                 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
377                 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
378                 if (reverseDirectionNeeded(cur))
379                         needsUpdate |= cursorRight(cur);
380                 else
381                         needsUpdate |= cursorLeft(cur);
382
383                 if (!needsUpdate && oldTopSlice == cur.top()
384                         && cur.boundary() == oldBoundary) {
385                         cur.undispatched();
386                         cmd = FuncRequest(LFUN_FINISHED_LEFT);
387                 }
388                 break;
389
390         case LFUN_UP_SELECT:
391         case LFUN_DOWN_SELECT:
392         case LFUN_UP:
393         case LFUN_DOWN: {
394                 // stop/start the selection
395                 bool select = cmd.action == LFUN_DOWN_SELECT ||
396                         cmd.action == LFUN_UP_SELECT;
397                 cur.selHandle(select);
398                 
399                 // move cursor up/down
400                 bool up = cmd.action == LFUN_UP_SELECT || cmd.action == LFUN_UP;
401                 bool const successful = cur.upDownInText(up, needsUpdate);
402                 if (successful) {
403                         // notify insets which were left and get their update flags 
404                         notifyCursorLeaves(cur.beforeDispatchCursor(), cur);
405                         cur.fixIfBroken();
406                         
407                         // redraw if you leave mathed (for the decorations)
408                         needsUpdate |= cur.beforeDispatchCursor().inMathed();
409                 } else
410                         cur.undispatched();
411                 
412                 break;
413         }
414
415         case LFUN_PARAGRAPH_UP:
416         case LFUN_PARAGRAPH_UP_SELECT:
417                 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
418                 needsUpdate |= cursorUpParagraph(cur);
419                 break;
420
421         case LFUN_PARAGRAPH_DOWN:
422         case LFUN_PARAGRAPH_DOWN_SELECT:
423                 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
424                 needsUpdate |= cursorDownParagraph(cur);
425                 break;
426
427         case LFUN_SCREEN_UP_SELECT:
428                 needsUpdate |= cur.selHandle(true);
429                 if (cur.pit() == 0 && cur.textRow().pos() == 0)
430                         cur.undispatched();
431                 else {
432                         tm.cursorPrevious(cur);
433                 }
434                 break;
435
436         case LFUN_SCREEN_DOWN_SELECT:
437                 needsUpdate |= cur.selHandle(true);
438                 if (cur.pit() == cur.lastpit()
439                           && cur.textRow().endpos() == cur.lastpos())
440                         cur.undispatched();
441                 else {
442                         tm.cursorNext(cur);
443                 }
444                 break;
445
446         case LFUN_LINE_BEGIN:
447         case LFUN_LINE_BEGIN_SELECT:
448                 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_BEGIN_SELECT);
449                 needsUpdate |= tm.cursorHome(cur);
450                 break;
451
452         case LFUN_LINE_END:
453         case LFUN_LINE_END_SELECT:
454                 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
455                 needsUpdate |= tm.cursorEnd(cur);
456                 break;
457
458         case LFUN_WORD_FORWARD:
459         case LFUN_WORD_FORWARD_SELECT:
460                 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT);
461                 if (reverseDirectionNeeded(cur))
462                         needsUpdate |= cursorLeftOneWord(cur);
463                 else
464                         needsUpdate |= cursorRightOneWord(cur);
465                 break;
466
467         case LFUN_WORD_BACKWARD:
468         case LFUN_WORD_BACKWARD_SELECT:
469                 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT);
470                 if (reverseDirectionNeeded(cur))
471                         needsUpdate |= cursorRightOneWord(cur);
472                 else
473                         needsUpdate |= cursorLeftOneWord(cur);
474                 break;
475
476         case LFUN_WORD_SELECT: {
477                 selectWord(cur, WHOLE_WORD);
478                 finishChange(cur, true);
479                 break;
480         }
481
482         case LFUN_BREAK_LINE: {
483                 // Not allowed by LaTeX (labels or empty par)
484                 if (cur.pos() > cur.paragraph().beginOfBody()) {
485                         // this avoids a double undo
486                         // FIXME: should not be needed, ideally
487                         if (!cur.selection())
488                                 recordUndo(cur);
489                         cap::replaceSelection(cur);
490                         cur.insert(new InsetNewline);
491                         cur.posRight();
492                         moveCursor(cur, false);
493                 }
494                 break;
495         }
496
497         case LFUN_CHAR_DELETE_FORWARD:
498                 if (!cur.selection()) {
499                         if (cur.pos() == cur.paragraph().size())
500                                 // Par boundary, force full-screen update
501                                 singleParUpdate = false;
502                         needsUpdate |= erase(cur);
503                         cur.resetAnchor();
504                         // It is possible to make it a lot faster still
505                         // just comment out the line below...
506                 } else {
507                         cutSelection(cur, true, false);
508                         singleParUpdate = false;
509                 }
510                 moveCursor(cur, false);
511                 break;
512
513         case LFUN_DELETE_FORWARD_SKIP:
514                 // Reverse the effect of LFUN_BREAK_PARAGRAPH_SKIP.
515                 if (!cur.selection()) {
516                         if (cur.pos() == cur.lastpos()) {
517                                 cursorRight(cur);
518                                 cursorLeft(cur);
519                         }
520                         erase(cur);
521                         cur.resetAnchor();
522                 } else {
523                         cutSelection(cur, true, false);
524                 }
525                 break;
526
527
528         case LFUN_CHAR_DELETE_BACKWARD:
529                 if (!cur.selection()) {
530                         if (bv->getIntl().getTransManager().backspace()) {
531                                 // Par boundary, full-screen update
532                                 if (cur.pos() == 0)
533                                         singleParUpdate = false;
534                                 needsUpdate |= backspace(cur);
535                                 cur.resetAnchor();
536                                 // It is possible to make it a lot faster still
537                                 // just comment out the line below...
538                         }
539                 } else {
540                         cutSelection(cur, true, false);
541                         singleParUpdate = false;
542                 }
543                 break;
544
545         case LFUN_DELETE_BACKWARD_SKIP:
546                 // Reverse the effect of LFUN_BREAK_PARAGRAPH_SKIP.
547                 if (!cur.selection()) {
548                         // FIXME: look here
549                         //CursorSlice cur = cursor();
550                         backspace(cur);
551                         //anchor() = cur;
552                 } else {
553                         cutSelection(cur, true, false);
554                 }
555                 break;
556
557         case LFUN_BREAK_PARAGRAPH:
558                 cap::replaceSelection(cur);
559                 breakParagraph(cur, false);
560                 cur.resetAnchor();
561                 break;
562
563         case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
564                 cap::replaceSelection(cur);
565                 breakParagraph(cur, true);
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                 // Derive layout number from given argument (string)
776                 // and current buffer's textclass (number)
777                 TextClass const & tclass = bv->buffer().params().getTextClass();
778                 if (layout.empty())
779                         layout = tclass.defaultLayoutName();
780                 bool hasLayout = tclass.hasLayout(layout);
781
782                 // If the entry is obsolete, use the new one instead.
783                 if (hasLayout) {
784                         docstring const & obs = tclass[layout]->obsoleted_by();
785                         if (!obs.empty())
786                                 layout = obs;
787                 }
788
789                 if (!hasLayout) {
790                         cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
791                                 from_utf8(N_(" not known")));
792                         break;
793                 }
794
795                 bool change_layout = (frontend::current_layout != layout);
796
797                 if (!change_layout && cur.selection() &&
798                         cur.selBegin().pit() != cur.selEnd().pit())
799                 {
800                         pit_type spit = cur.selBegin().pit();
801                         pit_type epit = cur.selEnd().pit() + 1;
802                         while (spit != epit) {
803                                 if (pars_[spit].layout()->name() != frontend::current_layout) {
804                                         change_layout = true;
805                                         break;
806                                 }
807                                 ++spit;
808                         }
809                 }
810
811                 if (change_layout) {
812                         setLayout(cur, layout);
813                         // inform the GUI that the layout has changed.
814                         bv->layoutChanged(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         case LFUN_NOMENCL_INSERT: {
1181                 Inset * inset = createInset(&cur.bv(), cmd);
1182                 if (!inset)
1183                         break;
1184                 recordUndo(cur);
1185                 cur.clearSelection();
1186                 insertInset(cur, inset);
1187                 // Show the dialog for the nomenclature entry, since the
1188                 // description entry still needs to be filled in.
1189                 if (cmd.action == LFUN_NOMENCL_INSERT)
1190                         inset->edit(cur, true);
1191                 cur.posRight();
1192                 break;
1193         }
1194
1195         case LFUN_INDEX_PRINT:
1196         case LFUN_NOMENCL_PRINT:
1197         case LFUN_TOC_INSERT:
1198         case LFUN_HFILL_INSERT:
1199         case LFUN_LINE_INSERT:
1200         case LFUN_PAGEBREAK_INSERT:
1201         case LFUN_CLEARPAGE_INSERT:
1202         case LFUN_CLEARDOUBLEPAGE_INSERT:
1203                 // do nothing fancy
1204                 doInsertInset(cur, this, cmd, false, false);
1205                 cur.posRight();
1206                 break;
1207
1208         case LFUN_DEPTH_DECREMENT:
1209                 changeDepth(cur, DEC_DEPTH);
1210                 break;
1211
1212         case LFUN_DEPTH_INCREMENT:
1213                 changeDepth(cur, INC_DEPTH);
1214                 break;
1215
1216         case LFUN_MATH_DISPLAY:
1217                 mathDispatch(cur, cmd, true);
1218                 break;
1219
1220         case LFUN_MATH_IMPORT_SELECTION:
1221         case LFUN_MATH_MODE:
1222                 if (cmd.argument() == "on")
1223                         // don't pass "on" as argument
1224                         mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1225                 else
1226                         mathDispatch(cur, cmd, false);
1227                 break;
1228
1229         case LFUN_MATH_MACRO:
1230                 if (cmd.argument().empty())
1231                         cur.errorMessage(from_utf8(N_("Missing argument")));
1232                 else {
1233                         string s = to_utf8(cmd.argument());
1234                         string const s1 = token(s, ' ', 1);
1235                         int const nargs = s1.empty() ? 0 : convert<int>(s1);
1236                         string const s2 = token(s, ' ', 2);
1237                         string const type = s2.empty() ? "newcommand" : s2;
1238                         cur.insert(new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, from_utf8(type)));
1239                         //cur.nextInset()->edit(cur, true);
1240                 }
1241                 break;
1242
1243         // passthrough hat and underscore outside mathed:
1244         case LFUN_MATH_SUBSCRIPT:
1245                 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1246                 break;
1247         case LFUN_MATH_SUPERSCRIPT:
1248                 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1249                 break;
1250
1251         case LFUN_MATH_INSERT:
1252         case LFUN_MATH_MATRIX:
1253         case LFUN_MATH_DELIM:
1254         case LFUN_MATH_BIGDELIM: {
1255                 if (cur.selection())
1256                         cur.clearSelection();
1257                 // FIXME: instead of the above, this one
1258                 // should be used (but it asserts with Bidi enabled)
1259                 // cf. http://bugzilla.lyx.org/show_bug.cgi?id=4055
1260                 // cap::replaceSelection(cur);
1261                 cur.insert(new InsetMathHull(hullSimple));
1262                 checkAndActivateInset(cur, true);
1263                 BOOST_ASSERT(cur.inMathed());
1264                 cur.dispatch(cmd);
1265                 break;
1266         }
1267
1268         case LFUN_FONT_EMPH: {
1269                 Font font(Font::ALL_IGNORE);
1270                 font.setEmph(Font::TOGGLE);
1271                 toggleAndShow(cur, this, font);
1272                 break;
1273         }
1274
1275         case LFUN_FONT_BOLD: {
1276                 Font font(Font::ALL_IGNORE);
1277                 font.setSeries(Font::BOLD_SERIES);
1278                 toggleAndShow(cur, this, font);
1279                 break;
1280         }
1281
1282         case LFUN_FONT_NOUN: {
1283                 Font font(Font::ALL_IGNORE);
1284                 font.setNoun(Font::TOGGLE);
1285                 toggleAndShow(cur, this, font);
1286                 break;
1287         }
1288
1289         case LFUN_FONT_TYPEWRITER: {
1290                 Font font(Font::ALL_IGNORE);
1291                 font.setFamily(Font::TYPEWRITER_FAMILY); // no good
1292                 toggleAndShow(cur, this, font);
1293                 break;
1294         }
1295
1296         case LFUN_FONT_SANS: {
1297                 Font font(Font::ALL_IGNORE);
1298                 font.setFamily(Font::SANS_FAMILY);
1299                 toggleAndShow(cur, this, font);
1300                 break;
1301         }
1302
1303         case LFUN_FONT_ROMAN: {
1304                 Font font(Font::ALL_IGNORE);
1305                 font.setFamily(Font::ROMAN_FAMILY);
1306                 toggleAndShow(cur, this, font);
1307                 break;
1308         }
1309
1310         case LFUN_FONT_DEFAULT: {
1311                 Font font(Font::ALL_INHERIT, ignore_language);
1312                 toggleAndShow(cur, this, font);
1313                 break;
1314         }
1315
1316         case LFUN_FONT_UNDERLINE: {
1317                 Font font(Font::ALL_IGNORE);
1318                 font.setUnderbar(Font::TOGGLE);
1319                 toggleAndShow(cur, this, font);
1320                 break;
1321         }
1322
1323         case LFUN_FONT_SIZE: {
1324                 Font font(Font::ALL_IGNORE);
1325                 font.setLyXSize(to_utf8(cmd.argument()));
1326                 toggleAndShow(cur, this, font);
1327                 break;
1328         }
1329
1330         case LFUN_LANGUAGE: {
1331                 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1332                 if (!lang)
1333                         break;
1334                 Font font(Font::ALL_IGNORE);
1335                 font.setLanguage(lang);
1336                 toggleAndShow(cur, this, font);
1337                 break;
1338         }
1339
1340         case LFUN_FONT_FREE_APPLY:
1341                 toggleAndShow(cur, this, freefont, toggleall);
1342                 cur.message(_("Character set"));
1343                 break;
1344
1345         // Set the freefont using the contents of \param data dispatched from
1346         // the frontends and apply it at the current cursor location.
1347         case LFUN_FONT_FREE_UPDATE: {
1348                 Font font;
1349                 bool toggle;
1350                 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1351                         freefont = font;
1352                         toggleall = toggle;
1353                         toggleAndShow(cur, this, freefont, toggleall);
1354                         cur.message(_("Character set"));
1355                 }
1356                 break;
1357         }
1358
1359         case LFUN_FINISHED_LEFT:
1360                 LYXERR(Debug::DEBUG) << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1361                 if (reverseDirectionNeeded(cur)) {
1362                         ++cur.pos();
1363                         cur.setCurrentFont();
1364                 }
1365                 break;
1366
1367         case LFUN_FINISHED_RIGHT:
1368                 LYXERR(Debug::DEBUG) << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1369                 if (!reverseDirectionNeeded(cur)) {
1370                         ++cur.pos();
1371                         cur.setCurrentFont();
1372                 }
1373                 break;
1374
1375         case LFUN_LAYOUT_PARAGRAPH: {
1376                 string data;
1377                 params2string(cur.paragraph(), data);
1378                 data = "show\n" + data;
1379                 bv->showDialogWithData("paragraph", data);
1380                 break;
1381         }
1382
1383         case LFUN_PARAGRAPH_UPDATE: {
1384                 string data;
1385                 params2string(cur.paragraph(), data);
1386
1387                 // Will the paragraph accept changes from the dialog?
1388                 bool const accept = !cur.inset().forceDefaultParagraphs(cur.idx());
1389
1390                 data = "update " + convert<string>(accept) + '\n' + data;
1391                 bv->updateDialog("paragraph", data);
1392                 break;
1393         }
1394
1395         case LFUN_ACCENT_UMLAUT:
1396         case LFUN_ACCENT_CIRCUMFLEX:
1397         case LFUN_ACCENT_GRAVE:
1398         case LFUN_ACCENT_ACUTE:
1399         case LFUN_ACCENT_TILDE:
1400         case LFUN_ACCENT_CEDILLA:
1401         case LFUN_ACCENT_MACRON:
1402         case LFUN_ACCENT_DOT:
1403         case LFUN_ACCENT_UNDERDOT:
1404         case LFUN_ACCENT_UNDERBAR:
1405         case LFUN_ACCENT_CARON:
1406         case LFUN_ACCENT_SPECIAL_CARON:
1407         case LFUN_ACCENT_BREVE:
1408         case LFUN_ACCENT_TIE:
1409         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1410         case LFUN_ACCENT_CIRCLE:
1411         case LFUN_ACCENT_OGONEK:
1412                 theLyXFunc().handleKeyFunc(cmd.action);
1413                 if (!cmd.argument().empty())
1414                         // FIXME: Are all these characters encoded in one byte in utf8?
1415                         bv->translateAndInsert(cmd.argument()[0], this, cur);
1416                 break;
1417
1418         case LFUN_FLOAT_LIST: {
1419                 TextClass const & tclass = bv->buffer().params().getTextClass();
1420                 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1421                         recordUndo(cur);
1422                         if (cur.selection())
1423                                 cutSelection(cur, true, false);
1424                         breakParagraph(cur);
1425
1426                         if (cur.lastpos() != 0) {
1427                                 cursorLeft(cur);
1428                                 breakParagraph(cur);
1429                         }
1430
1431                         setLayout(cur, tclass.defaultLayoutName());
1432                         ParagraphParameters p;
1433                         setParagraphs(cur, p);
1434                         insertInset(cur, new InsetFloatList(to_utf8(cmd.argument())));
1435                         cur.posRight();
1436                 } else {
1437                         lyxerr << "Non-existent float type: "
1438                                << to_utf8(cmd.argument()) << endl;
1439                 }
1440                 break;
1441         }
1442
1443         case LFUN_CHANGE_ACCEPT: {
1444                 acceptOrRejectChanges(cur, ACCEPT);
1445                 break;
1446         }
1447
1448         case LFUN_CHANGE_REJECT: {
1449                 acceptOrRejectChanges(cur, REJECT);
1450                 break;
1451         }
1452
1453         case LFUN_THESAURUS_ENTRY: {
1454                 docstring arg = cmd.argument();
1455                 if (arg.empty()) {
1456                         arg = cur.selectionAsString(false);
1457                         // FIXME
1458                         if (arg.size() > 100 || arg.empty()) {
1459                                 // Get word or selection
1460                                 selectWordWhenUnderCursor(cur, WHOLE_WORD);
1461                                 arg = cur.selectionAsString(false);
1462                         }
1463                 }
1464                 bv->showDialogWithData("thesaurus", to_utf8(arg));
1465                 break;
1466         }
1467
1468         case LFUN_PARAGRAPH_PARAMS_APPLY: {
1469                 // Given data, an encoding of the ParagraphParameters
1470                 // generated in the Paragraph dialog, this function sets
1471                 // the current paragraph, or currently selected paragraphs,
1472                 // appropriately. 
1473                 // NOTE: This function overrides all existing settings.
1474                 setParagraphs(cur, cmd.argument());
1475                 cur.message(_("Paragraph layout set"));
1476                 break;
1477         }
1478         
1479         case LFUN_PARAGRAPH_PARAMS: {
1480                 // Given data, an encoding of the ParagraphParameters as we'd
1481                 // find them in a LyX file, this function modifies the current paragraph, 
1482                 // or currently selected paragraphs. 
1483                 // NOTE: This function only modifies, and does not override, existing
1484                 // settings.
1485                 setParagraphs(cur, cmd.argument(), true);
1486                 cur.message(_("Paragraph layout set"));
1487                 break;
1488         }
1489
1490         case LFUN_ESCAPE:
1491                 if (cur.selection()) {
1492                         cur.selection() = false;
1493                 } else {
1494                         cur.undispatched();
1495                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1496                 }
1497                 break;
1498
1499         default:
1500                 LYXERR(Debug::ACTION)
1501                         << BOOST_CURRENT_FUNCTION
1502                         << ": Command " << cmd
1503                         << " not DISPATCHED by Text" << endl;
1504                 cur.undispatched();
1505                 break;
1506         }
1507
1508         needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
1509
1510         // FIXME: The cursor flag is reset two lines below
1511         // so we need to check here if some of the LFUN did touch that.
1512         // for now only Text::erase() and Text::backspace() do that.
1513         // The plan is to verify all the LFUNs and then to remove this
1514         // singleParUpdate boolean altogether.
1515         if (cur.result().update() & Update::Force) {
1516                 singleParUpdate = false;
1517                 needsUpdate = true;
1518         }
1519
1520         // FIXME: the following code should go in favor of fine grained
1521         // update flag treatment.
1522         if (singleParUpdate) {
1523                 // Inserting characters does not change par height
1524                 ParagraphMetrics const & pms
1525                         = cur.bv().parMetrics(cur.bottom().text(), cur.bottom().pit());
1526                 if (pms.dim().height()
1527                     == olddim.height()) {
1528                         // if so, update _only_ this paragraph
1529                         cur.updateFlags(Update::SinglePar |
1530                                 Update::FitCursor |
1531                                 Update::MultiParSel);
1532                         return;
1533                 } else
1534                         needsUpdate = true;
1535         }
1536
1537         if (!needsUpdate
1538             && &oldTopSlice.inset() == &cur.inset()
1539             && oldTopSlice.idx() == cur.idx()
1540             && !sel // sel is a backup of cur.selection() at the biginning of the function.
1541             && !cur.selection())
1542                 // FIXME: it would be better if we could just do this
1543                 //
1544                 //if (cur.result().update() != Update::FitCursor)
1545                 //      cur.noUpdate();
1546                 //
1547                 // But some LFUNs do not set Update::FitCursor when needed, so we
1548                 // do it for all. This is not very harmfull as FitCursor will provoke
1549                 // a full redraw only if needed but still, a proper review of all LFUN
1550                 // should be done and this needsUpdate boolean can then be removed.
1551                 cur.updateFlags(Update::FitCursor);
1552         else
1553                 cur.updateFlags(Update::Force | Update::FitCursor);
1554 }
1555
1556
1557 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
1558                         FuncStatus & flag) const
1559 {
1560         BOOST_ASSERT(cur.text() == this);
1561
1562         Font const & font = cur.real_current_font;
1563         bool enable = true;
1564         Inset::Code code = Inset::NO_CODE;
1565
1566         switch (cmd.action) {
1567
1568         case LFUN_DEPTH_DECREMENT:
1569                 enable = changeDepthAllowed(cur, DEC_DEPTH);
1570                 break;
1571
1572         case LFUN_DEPTH_INCREMENT:
1573                 enable = changeDepthAllowed(cur, INC_DEPTH);
1574                 break;
1575
1576         case LFUN_APPENDIX:
1577                 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1578                 return true;
1579
1580         case LFUN_BIBITEM_INSERT:
1581                 enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO
1582                           && cur.pos() == 0);
1583                 break;
1584
1585         case LFUN_DIALOG_SHOW_NEW_INSET:
1586                 if (cmd.argument() == "bibitem")
1587                         code = Inset::BIBITEM_CODE;
1588                 else if (cmd.argument() == "bibtex")
1589                         code = Inset::BIBTEX_CODE;
1590                 else if (cmd.argument() == "box")
1591                         code = Inset::BOX_CODE;
1592                 else if (cmd.argument() == "branch")
1593                         code = Inset::BRANCH_CODE;
1594                 else if (cmd.argument() == "citation")
1595                         code = Inset::CITE_CODE;
1596                 else if (cmd.argument() == "ert")
1597                         code = Inset::ERT_CODE;
1598                 else if (cmd.argument() == "external")
1599                         code = Inset::EXTERNAL_CODE;
1600                 else if (cmd.argument() == "float")
1601                         code = Inset::FLOAT_CODE;
1602                 else if (cmd.argument() == "graphics")
1603                         code = Inset::GRAPHICS_CODE;
1604                 else if (cmd.argument() == "include")
1605                         code = Inset::INCLUDE_CODE;
1606                 else if (cmd.argument() == "index")
1607                         code = Inset::INDEX_CODE;
1608                 else if (cmd.argument() == "nomenclature")
1609                         code = Inset::NOMENCL_CODE;
1610                 else if (cmd.argument() == "label")
1611                         code = Inset::LABEL_CODE;
1612                 else if (cmd.argument() == "note")
1613                         code = Inset::NOTE_CODE;
1614                 else if (cmd.argument() == "ref")
1615                         code = Inset::REF_CODE;
1616                 else if (cmd.argument() == "toc")
1617                         code = Inset::TOC_CODE;
1618                 else if (cmd.argument() == "url")
1619                         code = Inset::URL_CODE;
1620                 else if (cmd.argument() == "vspace")
1621                         code = Inset::VSPACE_CODE;
1622                 else if (cmd.argument() == "wrap")
1623                         code = Inset::WRAP_CODE;
1624                 else if (cmd.argument() == "listings")
1625                         code = Inset::LISTINGS_CODE;
1626                 break;
1627
1628         case LFUN_ERT_INSERT:
1629                 code = Inset::ERT_CODE;
1630                 break;
1631         case LFUN_LISTING_INSERT:
1632             code = Inset::LISTINGS_CODE;
1633                 break;
1634         case LFUN_FOOTNOTE_INSERT:
1635                 code = Inset::FOOT_CODE;
1636                 break;
1637         case LFUN_TABULAR_INSERT:
1638                 code = Inset::TABULAR_CODE;
1639                 break;
1640         case LFUN_MARGINALNOTE_INSERT:
1641                 code = Inset::MARGIN_CODE;
1642                 break;
1643         case LFUN_FLOAT_INSERT:
1644         case LFUN_FLOAT_WIDE_INSERT:
1645                 code = Inset::FLOAT_CODE;
1646                 break;
1647         case LFUN_WRAP_INSERT:
1648                 code = Inset::WRAP_CODE;
1649                 break;
1650         case LFUN_FLOAT_LIST:
1651                 code = Inset::FLOAT_LIST_CODE;
1652                 break;
1653 #if 0
1654         case LFUN_LIST_INSERT:
1655                 code = Inset::LIST_CODE;
1656                 break;
1657         case LFUN_THEOREM_INSERT:
1658                 code = Inset::THEOREM_CODE;
1659                 break;
1660 #endif
1661         case LFUN_CAPTION_INSERT:
1662                 code = Inset::CAPTION_CODE;
1663                 break;
1664         case LFUN_NOTE_INSERT:
1665                 code = Inset::NOTE_CODE;
1666                 break;
1667         case LFUN_FLEX_INSERT: {
1668                 code = Inset::FLEX_CODE;
1669                 string s = cmd.getArg(0);
1670                 InsetLayout il =  cur.buffer().params().getTextClass().insetlayout(from_utf8(s));
1671                 if (il.lyxtype != "charstyle" &&
1672                     il.lyxtype != "custom" &&
1673                     il.lyxtype != "element")
1674                         enable = false;
1675                 break;
1676                 }
1677         case LFUN_BOX_INSERT:
1678                 code = Inset::BOX_CODE;
1679                 break;
1680         case LFUN_BRANCH_INSERT:
1681                 code = Inset::BRANCH_CODE;
1682                 if (cur.buffer().getMasterBuffer()->params().branchlist().empty())
1683                         enable = false;
1684                 break;
1685         case LFUN_LABEL_INSERT:
1686                 code = Inset::LABEL_CODE;
1687                 break;
1688         case LFUN_OPTIONAL_INSERT:
1689                 code = Inset::OPTARG_CODE;
1690                 enable = numberOfOptArgs(cur.paragraph())
1691                         < cur.paragraph().layout()->optionalargs;
1692                 break;
1693         case LFUN_ENVIRONMENT_INSERT:
1694                 code = Inset::BOX_CODE;
1695                 break;
1696         case LFUN_INDEX_INSERT:
1697                 code = Inset::INDEX_CODE;
1698                 break;
1699         case LFUN_INDEX_PRINT:
1700                 code = Inset::INDEX_PRINT_CODE;
1701                 break;
1702         case LFUN_NOMENCL_INSERT:
1703                 code = Inset::NOMENCL_CODE;
1704                 break;
1705         case LFUN_NOMENCL_PRINT:
1706                 code = Inset::NOMENCL_PRINT_CODE;
1707                 break;
1708         case LFUN_TOC_INSERT:
1709                 code = Inset::TOC_CODE;
1710                 break;
1711         case LFUN_HTML_INSERT:
1712         case LFUN_URL_INSERT:
1713                 code = Inset::URL_CODE;
1714                 break;
1715         case LFUN_QUOTE_INSERT:
1716                 // always allow this, since we will inset a raw quote
1717                 // if an inset is not allowed.
1718                 break;
1719         case LFUN_HYPHENATION_POINT_INSERT:
1720         case LFUN_LIGATURE_BREAK_INSERT:
1721         case LFUN_HFILL_INSERT:
1722         case LFUN_MENU_SEPARATOR_INSERT:
1723         case LFUN_DOTS_INSERT:
1724         case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
1725                 code = Inset::SPECIALCHAR_CODE;
1726                 break;
1727         case LFUN_SPACE_INSERT:
1728                 // slight hack: we know this is allowed in math mode
1729                 if (cur.inTexted())
1730                         code = Inset::SPACE_CODE;
1731                 break;
1732
1733         case LFUN_INSET_MODIFY:
1734                 // We need to disable this, because we may get called for a
1735                 // tabular cell via
1736                 // InsetTabular::getStatus() -> InsetText::getStatus()
1737                 // and we don't handle LFUN_INSET_MODIFY.
1738                 enable = false;
1739                 break;
1740
1741         case LFUN_FONT_EMPH:
1742                 flag.setOnOff(font.emph() == Font::ON);
1743                 return true;
1744
1745         case LFUN_FONT_NOUN:
1746                 flag.setOnOff(font.noun() == Font::ON);
1747                 return true;
1748
1749         case LFUN_FONT_BOLD:
1750                 flag.setOnOff(font.series() == Font::BOLD_SERIES);
1751                 return true;
1752
1753         case LFUN_FONT_SANS:
1754                 flag.setOnOff(font.family() == Font::SANS_FAMILY);
1755                 return true;
1756
1757         case LFUN_FONT_ROMAN:
1758                 flag.setOnOff(font.family() == Font::ROMAN_FAMILY);
1759                 return true;
1760
1761         case LFUN_FONT_TYPEWRITER:
1762                 flag.setOnOff(font.family() == Font::TYPEWRITER_FAMILY);
1763                 return true;
1764
1765         case LFUN_CUT:
1766         case LFUN_COPY:
1767                 enable = cur.selection();
1768                 break;
1769
1770         case LFUN_PASTE:
1771                 if (cmd.argument().empty()) {
1772                         if (theClipboard().isInternal())
1773                                 enable = cap::numberOfSelections() > 0;
1774                         else
1775                                 enable = !theClipboard().empty();
1776                 } else {
1777                         string const arg = to_utf8(cmd.argument());
1778                         if (isStrUnsignedInt(arg)) {
1779                                 unsigned int n = convert<unsigned int>(arg);
1780                                 enable = cap::numberOfSelections() > n;
1781                         } else
1782                                 // unknown argument
1783                                 enable = false;
1784                 }
1785                 break;
1786
1787         case LFUN_CLIPBOARD_PASTE:
1788                 enable = !theClipboard().empty();
1789                 break;
1790
1791         case LFUN_PRIMARY_SELECTION_PASTE:
1792                 enable = cur.selection() || !theSelection().empty();
1793                 break;
1794
1795         case LFUN_PARAGRAPH_MOVE_UP:
1796                 enable = cur.pit() > 0 && !cur.selection();
1797                 break;
1798
1799         case LFUN_PARAGRAPH_MOVE_DOWN:
1800                 enable = cur.pit() < cur.lastpit() && !cur.selection();
1801                 break;
1802
1803         case LFUN_INSET_DISSOLVE:
1804                 enable = !isMainText(cur.bv().buffer()) && cur.inset().nargs() == 1;
1805                 break;
1806
1807         case LFUN_CHANGE_ACCEPT:
1808         case LFUN_CHANGE_REJECT:
1809                 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
1810                 // In principle, these LFUNs should only be enabled if there
1811                 // is a change at the current position/in the current selection.
1812                 // However, without proper optimizations, this will inevitably
1813                 // result in unacceptable performance - just imagine a user who
1814                 // wants to select the complete content of a long document.
1815                 enable = true;
1816                 break;
1817
1818         case LFUN_WORD_DELETE_FORWARD:
1819         case LFUN_WORD_DELETE_BACKWARD:
1820         case LFUN_LINE_DELETE:
1821         case LFUN_WORD_FORWARD:
1822         case LFUN_WORD_BACKWARD:
1823         case LFUN_CHAR_FORWARD:
1824         case LFUN_CHAR_FORWARD_SELECT:
1825         case LFUN_CHAR_BACKWARD:
1826         case LFUN_CHAR_BACKWARD_SELECT:
1827         case LFUN_UP:
1828         case LFUN_UP_SELECT:
1829         case LFUN_DOWN:
1830         case LFUN_DOWN_SELECT:
1831         case LFUN_PARAGRAPH_UP_SELECT:
1832         case LFUN_PARAGRAPH_DOWN_SELECT:
1833         case LFUN_SCREEN_UP_SELECT:
1834         case LFUN_SCREEN_DOWN_SELECT:
1835         case LFUN_LINE_BEGIN_SELECT:
1836         case LFUN_LINE_END_SELECT:
1837         case LFUN_WORD_FORWARD_SELECT:
1838         case LFUN_WORD_BACKWARD_SELECT:
1839         case LFUN_WORD_SELECT:
1840         case LFUN_PARAGRAPH_UP:
1841         case LFUN_PARAGRAPH_DOWN:
1842         case LFUN_LINE_BEGIN:
1843         case LFUN_LINE_END:
1844         case LFUN_BREAK_LINE:
1845         case LFUN_CHAR_DELETE_FORWARD:
1846         case LFUN_DELETE_FORWARD_SKIP:
1847         case LFUN_CHAR_DELETE_BACKWARD:
1848         case LFUN_DELETE_BACKWARD_SKIP:
1849         case LFUN_BREAK_PARAGRAPH:
1850         case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
1851         case LFUN_BREAK_PARAGRAPH_SKIP:
1852         case LFUN_PARAGRAPH_SPACING:
1853         case LFUN_INSET_INSERT:
1854         case LFUN_WORD_UPCASE:
1855         case LFUN_WORD_LOWCASE:
1856         case LFUN_WORD_CAPITALIZE:
1857         case LFUN_CHARS_TRANSPOSE:
1858         case LFUN_SERVER_GET_XY:
1859         case LFUN_SERVER_SET_XY:
1860         case LFUN_SERVER_GET_FONT:
1861         case LFUN_SERVER_GET_LAYOUT:
1862         case LFUN_LAYOUT:
1863         case LFUN_DATE_INSERT:
1864         case LFUN_SELF_INSERT:
1865         case LFUN_LINE_INSERT:
1866         case LFUN_PAGEBREAK_INSERT:
1867         case LFUN_CLEARPAGE_INSERT:
1868         case LFUN_CLEARDOUBLEPAGE_INSERT:
1869         case LFUN_MATH_DISPLAY:
1870         case LFUN_MATH_IMPORT_SELECTION:
1871         case LFUN_MATH_MODE:
1872         case LFUN_MATH_MACRO:
1873         case LFUN_MATH_MATRIX:
1874         case LFUN_MATH_DELIM:
1875         case LFUN_MATH_BIGDELIM:
1876         case LFUN_MATH_INSERT:
1877         case LFUN_MATH_SUBSCRIPT:
1878         case LFUN_MATH_SUPERSCRIPT:
1879         case LFUN_FONT_DEFAULT:
1880         case LFUN_FONT_UNDERLINE:
1881         case LFUN_FONT_SIZE:
1882         case LFUN_LANGUAGE:
1883         case LFUN_FONT_FREE_APPLY:
1884         case LFUN_FONT_FREE_UPDATE:
1885         case LFUN_LAYOUT_PARAGRAPH:
1886         case LFUN_PARAGRAPH_UPDATE:
1887         case LFUN_ACCENT_UMLAUT:
1888         case LFUN_ACCENT_CIRCUMFLEX:
1889         case LFUN_ACCENT_GRAVE:
1890         case LFUN_ACCENT_ACUTE:
1891         case LFUN_ACCENT_TILDE:
1892         case LFUN_ACCENT_CEDILLA:
1893         case LFUN_ACCENT_MACRON:
1894         case LFUN_ACCENT_DOT:
1895         case LFUN_ACCENT_UNDERDOT:
1896         case LFUN_ACCENT_UNDERBAR:
1897         case LFUN_ACCENT_CARON:
1898         case LFUN_ACCENT_SPECIAL_CARON:
1899         case LFUN_ACCENT_BREVE:
1900         case LFUN_ACCENT_TIE:
1901         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1902         case LFUN_ACCENT_CIRCLE:
1903         case LFUN_ACCENT_OGONEK:
1904         case LFUN_THESAURUS_ENTRY:
1905         case LFUN_PARAGRAPH_PARAMS_APPLY:
1906         case LFUN_PARAGRAPH_PARAMS:
1907         case LFUN_ESCAPE:
1908         case LFUN_BUFFER_END:
1909         case LFUN_BUFFER_BEGIN:
1910         case LFUN_BUFFER_BEGIN_SELECT:
1911         case LFUN_BUFFER_END_SELECT:
1912         case LFUN_UNICODE_INSERT:
1913                 // these are handled in our dispatch()
1914                 enable = true;
1915                 break;
1916
1917         default:
1918                 return false;
1919         }
1920
1921         if (code != Inset::NO_CODE
1922             && (cur.empty() || !cur.inset().insetAllowed(code)))
1923                 enable = false;
1924
1925         flag.enabled(enable);
1926         return true;
1927 }
1928
1929
1930 void Text::pasteString(Cursor & cur, docstring const & clip,
1931                 bool asParagraphs)
1932 {
1933         cur.clearSelection();
1934         if (!clip.empty()) {
1935                 recordUndo(cur);
1936                 if (asParagraphs)
1937                         insertStringAsParagraphs(cur, clip);
1938                 else
1939                         insertStringAsLines(cur, clip);
1940         }
1941 }
1942
1943 } // namespace lyx