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