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