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