]> git.lyx.org Git - lyx.git/blob - src/Text3.cpp
make sure that error_ is correctly initialized
[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 "Changes.h"
29 #include "Cursor.h"
30 #include "CutAndPaste.h"
31 #include "DispatchResult.h"
32 #include "ErrorList.h"
33 #include "factory.h"
34 #include "FuncRequest.h"
35 #include "InsetList.h"
36 #include "Intl.h"
37 #include "Language.h"
38 #include "Layout.h"
39 #include "LyXAction.h"
40 #include "LyXFunc.h"
41 #include "Lexer.h"
42 #include "LyXRC.h"
43 #include "Paragraph.h"
44 #include "ParagraphParameters.h"
45 #include "TextClass.h"
46 #include "TextMetrics.h"
47 #include "VSpace.h"
48
49 #include "frontends/Application.h"
50 #include "frontends/Clipboard.h"
51 #include "frontends/Selection.h"
52
53 #include "insets/InsetCollapsable.h"
54 #include "insets/InsetCommand.h"
55 #include "insets/InsetExternal.h"
56 #include "insets/InsetFloat.h"
57 #include "insets/InsetFloatList.h"
58 #include "insets/InsetGraphics.h"
59 #include "insets/InsetGraphicsParams.h"
60 #include "insets/InsetNewline.h"
61 #include "insets/InsetQuotes.h"
62 #include "insets/InsetSpecialChar.h"
63 #include "insets/InsetText.h"
64 #include "insets/InsetWrap.h"
65
66 #include "support/convert.h"
67 #include "support/debug.h"
68 #include "support/gettext.h"
69 #include "support/lstrings.h"
70 #include "support/lyxtime.h"
71 #include "support/os.h"
72
73 #include "mathed/InsetMathHull.h"
74 #include "mathed/MathMacroTemplate.h"
75
76 #include <boost/next_prior.hpp>
77
78 #include <clocale>
79 #include <sstream>
80
81 using namespace std;
82 using namespace lyx::support;
83
84 namespace lyx {
85
86 using cap::copySelection;
87 using cap::cutSelection;
88 using cap::pasteFromStack;
89 using cap::pasteClipboardText;
90 using cap::pasteClipboardGraphics;
91 using cap::replaceSelection;
92 using cap::grabAndEraseSelection;
93 using cap::selClearOrDel;
94
95 // globals...
96 static Font freefont(ignore_font, ignore_language);
97 static bool toggleall = false;
98
99 static void toggleAndShow(Cursor & cur, Text * text,
100         Font const & font, bool toggleall = true)
101 {
102         text->toggleFree(cur, font, toggleall);
103
104         if (font.language() != ignore_language ||
105             font.fontInfo().number() != FONT_IGNORE) {
106                 TextMetrics const & tm = cur.bv().textMetrics(text);
107                 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
108                                                        cur.real_current_font))
109                         text->setCursor(cur, cur.pit(), cur.pos(),
110                                         false, !cur.boundary());
111         }
112 }
113
114
115 static void moveCursor(Cursor & cur, bool selecting)
116 {
117         if (selecting || cur.mark())
118                 cur.setSelection();
119 }
120
121
122 static void finishChange(Cursor & cur, bool selecting)
123 {
124         cur.finishUndo();
125         moveCursor(cur, selecting);
126 }
127
128
129 static void mathDispatch(Cursor & cur, FuncRequest const & cmd, bool display)
130 {
131         cur.recordUndo();
132         docstring sel = cur.selectionAsString(false);
133
134         // It may happen that sel is empty but there is a selection
135         replaceSelection(cur);
136
137         // Is this a valid formula?
138         bool valid = true;
139
140         if (sel.empty()) {
141 #ifdef ENABLE_ASSERTIONS
142                 const int old_pos = cur.pos();
143 #endif
144                 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
145 #ifdef ENABLE_ASSERTIONS
146                 LASSERT(old_pos == cur.pos(), /**/);
147 #endif
148                 cur.nextInset()->edit(cur, true);
149                 // don't do that also for LFUN_MATH_MODE
150                 // unless you want end up with always changing
151                 // to mathrm when opening an inlined inset --
152                 // I really hate "LyXfunc overloading"...
153                 if (display)
154                         cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
155                 // Avoid an unnecessary undo step if cmd.argument
156                 // is empty
157                 if (!cmd.argument().empty())
158                         cur.dispatch(FuncRequest(LFUN_MATH_INSERT,
159                                                  cmd.argument()));
160         } else {
161                 // create a macro if we see "\\newcommand"
162                 // somewhere, and an ordinary formula
163                 // otherwise
164                 if (sel.find(from_ascii("\\newcommand")) == string::npos
165                                 && sel.find(from_ascii("\\newlyxcommand")) == string::npos
166                                 && sel.find(from_ascii("\\def")) == string::npos)
167                 {
168                         InsetMathHull * formula = new InsetMathHull(cur.buffer());
169                         string const selstr = to_utf8(sel);
170                         istringstream is(selstr);
171                         Lexer lex;
172                         lex.setStream(is);
173                         if (!formula->readQuiet(lex)) {
174                                 // No valid formula, let's try with delims
175                                 is.str("$" + selstr + "$");
176                                 lex.setStream(is);
177                                 if (!formula->readQuiet(lex)) {
178                                         // Still not valid, leave it as is
179                                         valid = false;
180                                         delete formula;
181                                         cur.insert(sel);
182                                 } else
183                                         cur.insert(formula);
184                         } else
185                                 cur.insert(formula);
186                 } else {
187                         cur.insert(new MathMacroTemplate(cur.buffer(), sel));
188                 }
189         }
190         if (valid)
191                 cur.message(from_utf8(N_("Math editor mode")));
192         else
193                 cur.message(from_utf8(N_("No valid math formula")));
194 }
195
196
197 void regexpDispatch(Cursor & cur, FuncRequest const & cmd)
198 {
199         BOOST_ASSERT(cmd.action == LFUN_REGEXP_MODE);
200         if (cur.inRegexped()) {
201                 cur.message(_("Already in regexp mode"));
202                 return;
203         }
204         cur.recordUndo();
205         docstring sel = cur.selectionAsString(false);
206
207         // It may happen that sel is empty but there is a selection
208         replaceSelection(cur);
209
210         cur.insert(new InsetMathHull(cur.buffer(), hullRegexp));
211         cur.nextInset()->edit(cur, true);
212         cur.niceInsert(sel);
213
214         cur.message(_("Regexp editor mode"));
215 }
216
217
218 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
219 {
220         cur.recordUndo();
221         cap::replaceSelection(cur);
222         cur.insert(new InsetSpecialChar(kind));
223         cur.posForward();
224 }
225
226
227 static bool doInsertInset(Cursor & cur, Text * text,
228         FuncRequest const & cmd, bool edit, bool pastesel)
229 {
230         Buffer & buffer = cur.bv().buffer();
231         BufferParams const & bparams = buffer.params();
232         Inset * inset = createInset(&buffer, cmd);
233         if (!inset)
234                 return false;
235
236         if (InsetCollapsable * ci = inset->asInsetCollapsable())
237                 ci->setButtonLabel();
238
239         cur.recordUndo();
240         if (cmd.action == LFUN_INDEX_INSERT) {
241                 docstring ds = subst(text->getStringToIndex(cur), '\n', ' ');
242                 text->insertInset(cur, inset);
243                 if (edit)
244                         inset->edit(cur, true);
245                 // Now put this into inset
246                 cur.text()->insertStringAsLines(cur, ds, cur.current_font);
247                 cur.leaveInset(*inset);
248                 return true;
249         }
250
251         bool gotsel = false;
252         if (cur.selection()) {
253                 cutSelection(cur, false, pastesel);
254                 cur.clearSelection();
255                 gotsel = true;
256         }
257         text->insertInset(cur, inset);
258
259         if (edit)
260                 inset->edit(cur, true);
261
262         if (!gotsel || !pastesel)
263                 return true;
264
265         pasteFromStack(cur, cur.buffer()->errorList("Paste"), 0);
266         cur.buffer()->errors("Paste");
267         cur.clearSelection(); // bug 393
268         cur.finishUndo();
269         InsetText * insetText = dynamic_cast<InsetText *>(inset);
270         if (insetText) {
271                 insetText->fixParagraphsFont();
272                 if (!insetText->allowMultiPar() || cur.lastpit() == 0) {
273                         // reset first par to default
274                         cur.text()->paragraphs().begin()
275                                 ->setPlainOrDefaultLayout(bparams.documentClass());
276                         cur.pos() = 0;
277                         cur.pit() = 0;
278                         // Merge multiple paragraphs -- hack
279                         while (cur.lastpit() > 0)
280                                 mergeParagraph(bparams, cur.text()->paragraphs(), 0);
281                         cur.leaveInset(*inset);
282                 }
283         } else {
284                 cur.leaveInset(*inset);
285                 // reset surrounding par to default
286                 DocumentClass const & dc = bparams.documentClass();
287                 docstring const layoutname = inset->usePlainLayout()
288                         ? dc.plainLayoutName()
289                         : dc.defaultLayoutName();
290                 text->setLayout(cur, layoutname);
291         }
292         return true;
293 }
294
295
296 string const freefont2string()
297 {
298         return freefont.toString(toggleall);
299 }
300
301
302 /// the type of outline operation
303 enum OutlineOp {
304         OutlineUp, // Move this header with text down
305         OutlineDown,   // Move this header with text up
306         OutlineIn, // Make this header deeper
307         OutlineOut // Make this header shallower
308 };
309
310
311 static void outline(OutlineOp mode, Cursor & cur)
312 {
313         Buffer & buf = *cur.buffer();
314         pit_type & pit = cur.pit();
315         ParagraphList & pars = buf.text().paragraphs();
316         ParagraphList::iterator const bgn = pars.begin();
317         // The first paragraph of the area to be copied:
318         ParagraphList::iterator start = boost::next(bgn, pit);
319         // The final paragraph of area to be copied:
320         ParagraphList::iterator finish = start;
321         ParagraphList::iterator const end = pars.end();
322
323         DocumentClass const & tc = buf.params().documentClass();
324
325         int const thistoclevel = start->layout().toclevel;
326         int toclevel;
327
328         // Move out (down) from this section header
329         if (finish != end)
330                 ++finish;
331
332         // Seek the one (on same level) below
333         for (; finish != end; ++finish) {
334                 toclevel = finish->layout().toclevel;
335                 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
336                         break;
337         }
338
339         switch (mode) {
340                 case OutlineUp: {
341                         if (start == pars.begin())
342                                 // Nothing to move.
343                                 return;
344                         ParagraphList::iterator dest = start;
345                         // Move out (up) from this header
346                         if (dest == bgn)
347                                 return;
348                         // Search previous same-level header above
349                         do {
350                                 --dest;
351                                 toclevel = dest->layout().toclevel;
352                         } while(dest != bgn
353                                 && (toclevel == Layout::NOT_IN_TOC
354                                     || toclevel > thistoclevel));
355                         // Not found; do nothing
356                         if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
357                                 return;
358                         pit_type const newpit = distance(bgn, dest);
359                         pit_type const len = distance(start, finish);
360                         pit_type const deletepit = pit + len;
361                         buf.undo().recordUndo(cur, ATOMIC_UNDO, newpit, deletepit - 1);
362                         pars.splice(dest, start, finish);
363                         cur.pit() = newpit;
364                         break;
365                 }
366                 case OutlineDown: {
367                         if (finish == end)
368                                 // Nothing to move.
369                                 return;
370                         // Go one down from *this* header:
371                         ParagraphList::iterator dest = boost::next(finish, 1);
372                         // Go further down to find header to insert in front of:
373                         for (; dest != end; ++dest) {
374                                 toclevel = dest->layout().toclevel;
375                                 if (toclevel != Layout::NOT_IN_TOC
376                                       && toclevel <= thistoclevel)
377                                         break;
378                         }
379                         // One such was found:
380                         pit_type newpit = distance(bgn, dest);
381                         buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, newpit - 1);
382                         pit_type const len = distance(start, finish);
383                         pars.splice(dest, start, finish);
384                         cur.pit() = newpit - len;
385                         break;
386                 }
387                 case OutlineIn: {
388                         pit_type const len = distance(start, finish);
389                         buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
390                         for (; start != finish; ++start) {
391                                 toclevel = start->layout().toclevel;
392                                 if (toclevel == Layout::NOT_IN_TOC)
393                                         continue;
394                                 DocumentClass::const_iterator lit = tc.begin();
395                                 DocumentClass::const_iterator len = tc.end();
396                                 for (; lit != len; ++lit) {
397                                         if (lit->toclevel == toclevel + 1 &&
398                                             start->layout().labeltype == lit->labeltype) {
399                                                 start->setLayout(*lit);
400                                                 break;
401                                         }
402                                 }
403                         }
404                         break;
405                 }
406                 case OutlineOut: {
407                         pit_type const len = distance(start, finish);
408                         buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
409                         for (; start != finish; ++start) {
410                                 toclevel = start->layout().toclevel;
411                                 if (toclevel == Layout::NOT_IN_TOC)
412                                         continue;
413                                 DocumentClass::const_iterator lit = tc.begin();
414                                 DocumentClass::const_iterator len = tc.end();
415                                 for (; lit != len; ++lit) {
416                                         if (lit->toclevel == toclevel - 1 &&
417                                                 start->layout().labeltype == lit->labeltype) {
418                                                         start->setLayout(*lit);
419                                                         break;
420                                         }
421                                 }
422                         }
423                         break;
424                 }
425         }
426 }
427
428
429 void Text::number(Cursor & cur)
430 {
431         FontInfo font = ignore_font;
432         font.setNumber(FONT_TOGGLE);
433         toggleAndShow(cur, this, Font(font, ignore_language));
434 }
435
436
437 bool Text::isRTL(Paragraph const & par) const
438 {
439         Buffer const & buffer = owner_->buffer();
440         return par.isRTL(buffer.params());
441 }
442
443
444 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
445 {
446         LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
447
448         // Dispatch if the cursor is inside the text. It is not the
449         // case for context menus (bug 5797).
450         if (cur.text() != this) {
451                 cur.undispatched();
452                 return;
453         }
454
455         BufferView * bv = &cur.bv();
456         TextMetrics * tm = &bv->textMetrics(this);
457         if (!tm->contains(cur.pit())) {
458                 lyx::dispatch(FuncRequest(LFUN_SCREEN_SHOW_CURSOR));
459                 tm = &bv->textMetrics(this);
460         }
461
462         // FIXME: We use the update flag to indicates wether a singlePar or a
463         // full screen update is needed. We reset it here but shall we restore it
464         // at the end?
465         cur.noUpdate();
466
467         LASSERT(cur.text() == this, /**/);
468         CursorSlice oldTopSlice = cur.top();
469         bool oldBoundary = cur.boundary();
470         bool sel = cur.selection();
471         // Signals that, even if needsUpdate == false, an update of the
472         // cursor paragraph is required
473         bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
474                 LyXAction::SingleParUpdate);
475         // Signals that a full-screen update is required
476         bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
477                 LyXAction::NoUpdate) || singleParUpdate);
478
479         switch (cmd.action) {
480
481         case LFUN_PARAGRAPH_MOVE_DOWN: {
482                 pit_type const pit = cur.pit();
483                 recUndo(cur, pit, pit + 1);
484                 cur.finishUndo();
485                 pars_.swap(pit, pit + 1);
486                 cur.buffer()->updateLabels();
487                 needsUpdate = true;
488                 ++cur.pit();
489                 break;
490         }
491
492         case LFUN_PARAGRAPH_MOVE_UP: {
493                 pit_type const pit = cur.pit();
494                 recUndo(cur, pit - 1, pit);
495                 cur.finishUndo();
496                 pars_.swap(pit, pit - 1);
497                 cur.buffer()->updateLabels();
498                 --cur.pit();
499                 needsUpdate = true;
500                 break;
501         }
502
503         case LFUN_APPENDIX: {
504                 Paragraph & par = cur.paragraph();
505                 bool start = !par.params().startOfAppendix();
506
507 // FIXME: The code below only makes sense at top level.
508 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
509                 // ensure that we have only one start_of_appendix in this document
510                 // FIXME: this don't work for multipart document!
511                 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
512                         if (pars_[tmp].params().startOfAppendix()) {
513                                 recUndo(cur, tmp);
514                                 pars_[tmp].params().startOfAppendix(false);
515                                 break;
516                         }
517                 }
518
519                 cur.recordUndo();
520                 par.params().startOfAppendix(start);
521
522                 // we can set the refreshing parameters now
523                 cur.buffer()->updateLabels();
524                 break;
525         }
526
527         case LFUN_WORD_DELETE_FORWARD:
528                 if (cur.selection())
529                         cutSelection(cur, true, false);
530                 else
531                         deleteWordForward(cur);
532                 finishChange(cur, false);
533                 break;
534
535         case LFUN_WORD_DELETE_BACKWARD:
536                 if (cur.selection())
537                         cutSelection(cur, true, false);
538                 else
539                         deleteWordBackward(cur);
540                 finishChange(cur, false);
541                 break;
542
543         case LFUN_LINE_DELETE:
544                 if (cur.selection())
545                         cutSelection(cur, true, false);
546                 else
547                         tm->deleteLineForward(cur);
548                 finishChange(cur, false);
549                 break;
550
551         case LFUN_BUFFER_BEGIN:
552         case LFUN_BUFFER_BEGIN_SELECT:
553                 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_BEGIN_SELECT);
554                 if (cur.depth() == 1)
555                         needsUpdate |= cursorTop(cur);
556                 else
557                         cur.undispatched();
558                 cur.updateFlags(Update::FitCursor);
559                 break;
560
561         case LFUN_BUFFER_END:
562         case LFUN_BUFFER_END_SELECT:
563                 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_END_SELECT);
564                 if (cur.depth() == 1)
565                         needsUpdate |= cursorBottom(cur);
566                 else
567                         cur.undispatched();
568                 cur.updateFlags(Update::FitCursor);
569                 break;
570
571         case LFUN_INSET_BEGIN:
572         case LFUN_INSET_BEGIN_SELECT:
573                 needsUpdate |= cur.selHandle(cmd.action == LFUN_INSET_BEGIN_SELECT);
574                 if (cur.depth() == 1 || !cur.top().at_begin())
575                         needsUpdate |= cursorTop(cur);
576                 else
577                         cur.undispatched();
578                 cur.updateFlags(Update::FitCursor);
579                 break;
580
581         case LFUN_INSET_END:
582         case LFUN_INSET_END_SELECT:
583                 needsUpdate |= cur.selHandle(cmd.action == LFUN_INSET_END_SELECT);
584                 if (cur.depth() == 1 || !cur.top().at_end())
585                         needsUpdate |= cursorBottom(cur);
586                 else
587                         cur.undispatched();
588                 cur.updateFlags(Update::FitCursor);
589                 break;
590
591         case LFUN_INSET_SELECT_ALL:
592                 if (cur.depth() == 1 || !cur.selection() || !cur.selBegin().at_begin()
593                           || !cur.selEnd().at_end()) {
594                         needsUpdate |= cur.selHandle(false);
595                         needsUpdate |= cursorTop(cur);
596                         needsUpdate |= cur.selHandle(true);
597                         needsUpdate |= cursorBottom(cur);
598                 } else 
599                         cur.undispatched();
600                 cur.updateFlags(Update::FitCursor);
601                 break;
602
603         case LFUN_CHAR_FORWARD:
604         case LFUN_CHAR_FORWARD_SELECT:
605                 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
606                 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
607                 needsUpdate |= cursorForward(cur);
608
609                 if (!needsUpdate && oldTopSlice == cur.top()
610                                 && cur.boundary() == oldBoundary) {
611                         cur.undispatched();
612                         cmd = FuncRequest(LFUN_FINISHED_FORWARD);
613                 
614                         // we will probably be moving out the inset, so we should execute
615                         // the depm-mechanism, but only when the cursor has a place to 
616                         // go outside this inset, i.e. in a slice above.
617                         if (cur.depth() > 1 && cur.pos() == cur.lastpos() 
618                                   && cur.pit() == cur.lastpit()) {
619                                 // The cursor hasn't changed yet. To give the 
620                                 // DEPM the possibility of doing something we must
621                                 // provide it with two different cursors.
622                                 Cursor dummy = cur;
623                                 dummy.pos() = dummy.pit() = 0;
624                                 cur.bv().checkDepm(dummy, cur);
625                         }
626                 }
627                 break;
628
629         case LFUN_CHAR_BACKWARD:
630         case LFUN_CHAR_BACKWARD_SELECT:
631                 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
632                 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
633                 needsUpdate |= cursorBackward(cur);
634
635                 if (!needsUpdate && oldTopSlice == cur.top()
636                         && cur.boundary() == oldBoundary) {
637                         cur.undispatched();
638                         cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
639
640                         // we will probably be moving out the inset, so we should execute
641                         // the depm-mechanism, but only when the cursor has a place to 
642                         // go outside this inset, i.e. in a slice above.
643                         if (cur.depth() > 1 && cur.pos() == 0 && cur.pit() == 0) {
644                                 // The cursor hasn't changed yet. To give the 
645                                 // DEPM the possibility of doing something we must
646                                 // provide it with two different cursors.
647                                 Cursor dummy = cur;
648                                 dummy.pos() = cur.lastpos();
649                                 dummy.pit() = cur.lastpit();
650                                 cur.bv().checkDepm(dummy, cur);
651                         }
652                 }
653                 break;
654
655         case LFUN_CHAR_LEFT:
656         case LFUN_CHAR_LEFT_SELECT:
657                 if (lyxrc.visual_cursor) {
658                         needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_LEFT_SELECT);
659                         needsUpdate |= cursorVisLeft(cur);
660                         if (!needsUpdate && oldTopSlice == cur.top()
661                                         && cur.boundary() == oldBoundary) {
662                                 cur.undispatched();
663                                 cmd = FuncRequest(LFUN_FINISHED_LEFT);
664                         }
665                 } else {
666                         if (reverseDirectionNeeded(cur)) {
667                                 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
668                                         LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
669                         } else {
670                                 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
671                                         LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
672                         }
673                         dispatch(cur, cmd);
674                         return;
675                 }
676                 break;
677
678         case LFUN_CHAR_RIGHT:
679         case LFUN_CHAR_RIGHT_SELECT:
680                 if (lyxrc.visual_cursor) {
681                         needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_RIGHT_SELECT);
682                         needsUpdate |= cursorVisRight(cur);
683                         if (!needsUpdate && oldTopSlice == cur.top()
684                                         && cur.boundary() == oldBoundary) {
685                                 cur.undispatched();
686                                 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
687                         }
688                 } else {
689                         if (reverseDirectionNeeded(cur)) {
690                                 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
691                                         LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
692                         } else {
693                                 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
694                                         LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
695                         }
696                         dispatch(cur, cmd);
697                         return;
698                 }
699                 break;
700
701
702         case LFUN_UP_SELECT:
703         case LFUN_DOWN_SELECT:
704         case LFUN_UP:
705         case LFUN_DOWN: {
706                 // stop/start the selection
707                 bool select = cmd.action == LFUN_DOWN_SELECT ||
708                         cmd.action == LFUN_UP_SELECT;
709
710                 // move cursor up/down
711                 bool up = cmd.action == LFUN_UP_SELECT || cmd.action == LFUN_UP;
712                 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
713
714                 if (!atFirstOrLastRow) {
715                         needsUpdate |= cur.selHandle(select);   
716                         cur.selHandle(select);
717                         cur.upDownInText(up, needsUpdate);
718                         needsUpdate |= cur.beforeDispatchCursor().inMathed();
719                 } else {
720                         // if the cursor cannot be moved up or down do not remove
721                         // the selection right now, but wait for the next dispatch.
722                         if (select)
723                                 needsUpdate |= cur.selHandle(select);   
724                         cur.upDownInText(up, needsUpdate);
725                         cur.undispatched();
726                 }
727
728                 break;
729         }
730
731         case LFUN_PARAGRAPH_UP:
732         case LFUN_PARAGRAPH_UP_SELECT:
733                 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
734                 needsUpdate |= cursorUpParagraph(cur);
735                 break;
736
737         case LFUN_PARAGRAPH_DOWN:
738         case LFUN_PARAGRAPH_DOWN_SELECT:
739                 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
740                 needsUpdate |= cursorDownParagraph(cur);
741                 break;
742
743         case LFUN_LINE_BEGIN:
744         case LFUN_LINE_BEGIN_SELECT:
745                 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_BEGIN_SELECT);
746                 needsUpdate |= tm->cursorHome(cur);
747                 break;
748
749         case LFUN_LINE_END:
750         case LFUN_LINE_END_SELECT:
751                 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
752                 needsUpdate |= tm->cursorEnd(cur);
753                 break;
754
755         case LFUN_SECTION_SELECT: {
756                 Buffer const & buf = *cur.buffer();
757                 pit_type const pit = cur.pit();
758                 ParagraphList & pars = buf.text().paragraphs();
759                 ParagraphList::iterator bgn = pars.begin();
760                 // The first paragraph of the area to be selected:
761                 ParagraphList::iterator start = boost::next(bgn, pit);
762                 // The final paragraph of area to be selected:
763                 ParagraphList::iterator finish = start;
764                 ParagraphList::iterator end = pars.end();
765
766                 int const thistoclevel = start->layout().toclevel;
767                 if (thistoclevel == Layout::NOT_IN_TOC)
768                         break;
769
770                 cur.pos() = 0;
771                 Cursor const old_cur = cur;
772                 needsUpdate |= cur.selHandle(true);
773
774                 // Move out (down) from this section header
775                 if (finish != end)
776                         ++finish;
777
778                 // Seek the one (on same level) below
779                 for (; finish != end; ++finish, ++cur.pit()) {
780                         int const toclevel = finish->layout().toclevel;
781                         if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
782                                 break;
783                 }
784                 cur.pos() = cur.lastpos();
785                 
786                 needsUpdate |= cur != old_cur;
787                 break;
788         }
789
790         case LFUN_WORD_RIGHT:
791         case LFUN_WORD_RIGHT_SELECT:
792                 if (lyxrc.visual_cursor) {
793                         needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_RIGHT_SELECT);
794                         needsUpdate |= cursorVisRightOneWord(cur);
795                         if (!needsUpdate && oldTopSlice == cur.top()
796                                         && cur.boundary() == oldBoundary) {
797                                 cur.undispatched();
798                                 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
799                         }
800                 } else {
801                         if (reverseDirectionNeeded(cur)) {
802                                 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
803                                                 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
804                         } else {
805                                 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
806                                                 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
807                         }
808                         dispatch(cur, cmd);
809                         return;
810                 }
811                 break;
812
813         case LFUN_WORD_FORWARD:
814         case LFUN_WORD_FORWARD_SELECT:
815                 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT);
816                 needsUpdate |= cursorForwardOneWord(cur);
817                 break;
818
819         case LFUN_WORD_LEFT:
820         case LFUN_WORD_LEFT_SELECT:
821                 if (lyxrc.visual_cursor) {
822                         needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_LEFT_SELECT);
823                         needsUpdate |= cursorVisLeftOneWord(cur);
824                         if (!needsUpdate && oldTopSlice == cur.top()
825                                         && cur.boundary() == oldBoundary) {
826                                 cur.undispatched();
827                                 cmd = FuncRequest(LFUN_FINISHED_LEFT);
828                         }
829                 } else {
830                         if (reverseDirectionNeeded(cur)) {
831                                 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
832                                                 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
833                         } else {
834                                 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
835                                                 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
836                         }
837                         dispatch(cur, cmd);
838                         return;
839                 }
840                 break;
841
842         case LFUN_WORD_BACKWARD:
843         case LFUN_WORD_BACKWARD_SELECT:
844                 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT);
845                 needsUpdate |= cursorBackwardOneWord(cur);
846                 break;
847
848         case LFUN_WORD_SELECT: {
849                 selectWord(cur, WHOLE_WORD);
850                 finishChange(cur, true);
851                 break;
852         }
853
854         case LFUN_NEWLINE_INSERT: {
855                 InsetNewlineParams inp;
856                 docstring arg = cmd.argument();
857                 // this avoids a double undo
858                 // FIXME: should not be needed, ideally
859                 if (!cur.selection())
860                         cur.recordUndo();
861                 cap::replaceSelection(cur);
862                 if (arg == "linebreak")
863                         inp.kind = InsetNewlineParams::LINEBREAK;
864                 else
865                         inp.kind = InsetNewlineParams::NEWLINE;
866                 cur.insert(new InsetNewline(inp));
867                 cur.posForward();
868                 moveCursor(cur, false);
869                 break;
870         }
871
872         case LFUN_TAB_INSERT: {
873                 bool const multi_par_selection = cur.selection() &&
874                         cur.selBegin().pit() != cur.selEnd().pit();
875                 if (multi_par_selection) {
876                         // If there is a multi-paragraph selection, a tab is inserted
877                         // at the beginning of each paragraph.
878                         cur.recordUndoSelection();
879                         pit_type const pit_end = cur.selEnd().pit();
880                         for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
881                                 pars_[pit].insertChar(0, '\t', 
882                                                       bv->buffer().params().trackChanges);
883                                 // Update the selection pos to make sure the selection does not
884                                 // change as the inserted tab will increase the logical pos.
885                                 if (cur.anchor_.pit() == pit)
886                                         cur.anchor_.forwardPos();
887                                 if (cur.pit() == pit)
888                                         cur.forwardPos();
889                         }
890                         cur.finishUndo();
891                 } else {
892                         // Maybe we shouldn't allow tabs within a line, because they
893                         // are not (yet) aligned as one might do expect.
894                         FuncRequest cmd(LFUN_SELF_INSERT, from_ascii("\t"));
895                         dispatch(cur, cmd);     
896                 }
897                 break;
898         }
899
900         case LFUN_TAB_DELETE: {
901                 bool const tc = bv->buffer().params().trackChanges;
902                 if (cur.selection()) {
903                         // If there is a selection, a tab (if present) is removed from
904                         // the beginning of each paragraph.
905                         cur.recordUndoSelection();
906                         pit_type const pit_end = cur.selEnd().pit();
907                         for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
908                                 Paragraph & par = paragraphs()[pit];
909                                 if (par.getChar(0) == '\t') {
910                                         if (cur.pit() == pit)
911                                                 cur.posBackward();
912                                         if (cur.anchor_.pit() == pit && cur.anchor_.pos() > 0 )
913                                                 cur.anchor_.backwardPos();
914                                         
915                                         par.eraseChar(0, tc);
916                                 } else 
917                                         // If no tab was present, try to remove up to four spaces.
918                                         for (int n_spaces = 0;
919                                              par.getChar(0) == ' ' && n_spaces < 4; ++n_spaces) {
920                                                 if (cur.pit() == pit)
921                                                         cur.posBackward();
922                                                 if (cur.anchor_.pit() == pit && cur.anchor_.pos() > 0 )
923                                                         cur.anchor_.backwardPos();
924                                                 
925                                                 par.eraseChar(0, tc);
926                                         }
927                         }
928                         cur.finishUndo();
929                 } else {
930                         // If there is no selection, try to remove a tab or some spaces 
931                         // before the position of the cursor.
932                         Paragraph & par = paragraphs()[cur.pit()];
933                         pos_type const pos = cur.pos();
934                         
935                         if (pos == 0)
936                                 break;
937                         
938                         char_type const c = par.getChar(pos - 1);
939                         cur.recordUndo();
940                         if (c == '\t') {
941                                 cur.posBackward();
942                                 par.eraseChar(cur.pos(), tc);
943                         } else
944                                 for (int n_spaces = 0; 
945                                      cur.pos() > 0
946                                              && par.getChar(cur.pos() - 1) == ' ' 
947                                              && n_spaces < 4;
948                                      ++n_spaces) {
949                                         cur.posBackward();
950                                         par.eraseChar(cur.pos(), tc);
951                                 }
952                         cur.finishUndo();
953                 }
954                 break;
955         }
956
957         case LFUN_CHAR_DELETE_FORWARD:
958                 if (!cur.selection()) {
959                         if (cur.pos() == cur.paragraph().size())
960                                 // Par boundary, force full-screen update
961                                 singleParUpdate = false;
962                         needsUpdate |= erase(cur);
963                         cur.resetAnchor();
964                         // It is possible to make it a lot faster still
965                         // just comment out the line below...
966                 } else {
967                         cutSelection(cur, true, false);
968                         singleParUpdate = false;
969                 }
970                 moveCursor(cur, false);
971                 break;
972
973         case LFUN_CHAR_DELETE_BACKWARD:
974                 if (!cur.selection()) {
975                         if (bv->getIntl().getTransManager().backspace()) {
976                                 // Par boundary, full-screen update
977                                 if (cur.pos() == 0)
978                                         singleParUpdate = false;
979                                 needsUpdate |= backspace(cur);
980                                 cur.resetAnchor();
981                                 // It is possible to make it a lot faster still
982                                 // just comment out the line below...
983                         }
984                 } else {
985                         cutSelection(cur, true, false);
986                         singleParUpdate = false;
987                 }
988                 break;
989
990         case LFUN_BREAK_PARAGRAPH:
991                 cap::replaceSelection(cur);
992                 breakParagraph(cur, cmd.argument() == "inverse");
993                 cur.resetAnchor();
994                 break;
995
996         case LFUN_INSET_INSERT: {
997                 cur.recordUndo();
998
999                 // We have to avoid triggering InstantPreview loading
1000                 // before inserting into the document. See bug #5626.
1001                 bool loaded = bv->buffer().isFullyLoaded();
1002                 bv->buffer().setFullyLoaded(false);
1003                 Inset * inset = createInset(&bv->buffer(), cmd);
1004                 bv->buffer().setFullyLoaded(loaded);
1005
1006                 if (inset) {
1007                         // FIXME (Abdel 01/02/2006):
1008                         // What follows would be a partial fix for bug 2154:
1009                         //   http://www.lyx.org/trac/ticket/2154
1010                         // This automatically put the label inset _after_ a
1011                         // numbered section. It should be possible to extend the mechanism
1012                         // to any kind of LateX environement.
1013                         // The correct way to fix that bug would be at LateX generation.
1014                         // I'll let the code here for reference as it could be used for some
1015                         // other feature like "automatic labelling".
1016                         /*
1017                         Paragraph & par = pars_[cur.pit()];
1018                         if (inset->lyxCode() == LABEL_CODE
1019                                 && par.layout().labeltype == LABEL_COUNTER) {
1020                                 // Go to the end of the paragraph
1021                                 // Warning: Because of Change-Tracking, the last
1022                                 // position is 'size()' and not 'size()-1':
1023                                 cur.pos() = par.size();
1024                                 // Insert a new paragraph
1025                                 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
1026                                 dispatch(cur, fr);
1027                         }
1028                         */
1029                         if (cur.selection())
1030                                 cutSelection(cur, true, false);
1031                         cur.insert(inset);
1032                         cur.posForward();
1033
1034                         // trigger InstantPreview now
1035                         if (inset->lyxCode() == EXTERNAL_CODE) {
1036                                 InsetExternal & ins =
1037                                         static_cast<InsetExternal &>(*inset);
1038                                 ins.updatePreview();
1039                         }
1040                 }
1041
1042                 break;
1043         }
1044
1045         case LFUN_INSET_DISSOLVE: {
1046                 if (dissolveInset(cur))
1047                         needsUpdate = true;
1048                 break;
1049         }
1050
1051         case LFUN_SET_GRAPHICS_GROUP: {
1052                 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1053                 if (!ins)
1054                         break;
1055
1056                 cur.recordUndo();
1057
1058                 string id = to_utf8(cmd.argument());
1059                 string grp = graphics::getGroupParams(bv->buffer(), id);
1060                 InsetGraphicsParams tmp, inspar = ins->getParams();
1061
1062                 if (id.empty())
1063                         inspar.groupId = to_utf8(cmd.argument());
1064                 else {
1065                         InsetGraphics::string2params(grp, bv->buffer(), tmp);
1066                         tmp.filename = inspar.filename;
1067                         inspar = tmp;
1068                 }
1069
1070                 ins->setParams(inspar);
1071         }
1072
1073         case LFUN_SPACE_INSERT:
1074                 if (cur.paragraph().layout().free_spacing)
1075                         insertChar(cur, ' ');
1076                 else {
1077                         doInsertInset(cur, this, cmd, false, false);
1078                         cur.posForward();
1079                 }
1080                 moveCursor(cur, false);
1081                 break;
1082
1083         case LFUN_SPECIALCHAR_INSERT: {
1084                 string const name = to_utf8(cmd.argument());
1085                 if (name == "hyphenation")
1086                         specialChar(cur, InsetSpecialChar::HYPHENATION);
1087                 else if (name == "ligature-break")
1088                         specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1089                 else if (name == "slash")
1090                         specialChar(cur, InsetSpecialChar::SLASH);
1091                 else if (name == "nobreakdash")
1092                         specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1093                 else if (name == "dots")
1094                         specialChar(cur, InsetSpecialChar::LDOTS);
1095                 else if (name == "end-of-sentence")
1096                         specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1097                 else if (name == "menu-separator")
1098                         specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1099                 else if (name.empty())
1100                         lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1101                 else
1102                         lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1103                 break;
1104         }
1105
1106         case LFUN_WORD_UPCASE:
1107                 changeCase(cur, text_uppercase);
1108                 break;
1109
1110         case LFUN_WORD_LOWCASE:
1111                 changeCase(cur, text_lowercase);
1112                 break;
1113
1114         case LFUN_WORD_CAPITALIZE:
1115                 changeCase(cur, text_capitalization);
1116                 break;
1117
1118         case LFUN_CHARS_TRANSPOSE:
1119                 charsTranspose(cur);
1120                 break;
1121
1122         case LFUN_PASTE: {
1123                 cur.message(_("Paste"));
1124                 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), /**/);
1125                 cap::replaceSelection(cur);
1126
1127                 // without argument?
1128                 string const arg = to_utf8(cmd.argument());
1129                 if (arg.empty()) {
1130                         if (theClipboard().isInternal())
1131                                 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1132                         else if (theClipboard().hasGraphicsContents() 
1133                                      && !theClipboard().hasTextContents())
1134                                 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1135                         else
1136                                 pasteClipboardText(cur, bv->buffer().errorList("Paste"));
1137                 } else if (isStrUnsignedInt(arg)) {
1138                         // we have a numerical argument
1139                         pasteFromStack(cur, bv->buffer().errorList("Paste"),
1140                                        convert<unsigned int>(arg));
1141                 } else {
1142                         Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1143                         if (arg == "pdf")
1144                                 type = Clipboard::PdfGraphicsType;
1145                         else if (arg == "png")
1146                                 type = Clipboard::PngGraphicsType;
1147                         else if (arg == "jpeg")
1148                                 type = Clipboard::JpegGraphicsType;
1149                         else if (arg == "linkback")
1150                                 type = Clipboard::LinkBackGraphicsType;
1151                         else if (arg == "emf")
1152                                 type = Clipboard::EmfGraphicsType;
1153                         else if (arg == "wmf")
1154                                 type = Clipboard::WmfGraphicsType;
1155
1156                         else
1157                                 LASSERT(false, /**/);
1158
1159                         pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1160                 }
1161
1162                 bv->buffer().errors("Paste");
1163                 cur.clearSelection(); // bug 393
1164                 cur.finishUndo();
1165                 break;
1166         }
1167
1168         case LFUN_CUT:
1169                 cutSelection(cur, true, true);
1170                 cur.message(_("Cut"));
1171                 break;
1172
1173         case LFUN_COPY:
1174                 copySelection(cur);
1175                 cur.message(_("Copy"));
1176                 break;
1177
1178         case LFUN_SERVER_GET_XY:
1179                 cur.message(from_utf8(
1180                         convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1181                         + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1182                 break;
1183
1184         case LFUN_SERVER_SET_XY: {
1185                 int x = 0;
1186                 int y = 0;
1187                 istringstream is(to_utf8(cmd.argument()));
1188                 is >> x >> y;
1189                 if (!is)
1190                         lyxerr << "SETXY: Could not parse coordinates in '"
1191                                << to_utf8(cmd.argument()) << endl;
1192                 else
1193                         tm->setCursorFromCoordinates(cur, x, y);
1194                 break;
1195         }
1196
1197         case LFUN_SERVER_GET_LAYOUT:
1198                 cur.message(cur.paragraph().layout().name());
1199                 break;
1200
1201         case LFUN_LAYOUT: {
1202                 docstring layout = cmd.argument();
1203                 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1204
1205                 Paragraph const & para = cur.paragraph();
1206                 docstring const old_layout = para.layout().name();
1207                 DocumentClass const & tclass = bv->buffer().params().documentClass();
1208
1209                 if (layout.empty())
1210                         layout = tclass.defaultLayoutName();
1211
1212                 if (owner_->forcePlainLayout())
1213                         // in this case only the empty layout is allowed
1214                         layout = tclass.plainLayoutName();
1215                 else if (para.usePlainLayout()) {
1216                         // in this case, default layout maps to empty layout
1217                         if (layout == tclass.defaultLayoutName())
1218                                 layout = tclass.plainLayoutName();
1219                 } else {
1220                         // otherwise, the empty layout maps to the default
1221                         if (layout == tclass.plainLayoutName())
1222                                 layout = tclass.defaultLayoutName();
1223                 }
1224
1225                 bool hasLayout = tclass.hasLayout(layout);
1226
1227                 // If the entry is obsolete, use the new one instead.
1228                 if (hasLayout) {
1229                         docstring const & obs = tclass[layout].obsoleted_by();
1230                         if (!obs.empty())
1231                                 layout = obs;
1232                 }
1233
1234                 if (!hasLayout) {
1235                         cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1236                                 from_utf8(N_(" not known")));
1237                         break;
1238                 }
1239
1240                 bool change_layout = (old_layout != layout);
1241
1242                 if (!change_layout && cur.selection() &&
1243                         cur.selBegin().pit() != cur.selEnd().pit())
1244                 {
1245                         pit_type spit = cur.selBegin().pit();
1246                         pit_type epit = cur.selEnd().pit() + 1;
1247                         while (spit != epit) {
1248                                 if (pars_[spit].layout().name() != old_layout) {
1249                                         change_layout = true;
1250                                         break;
1251                                 }
1252                                 ++spit;
1253                         }
1254                 }
1255
1256                 if (change_layout)
1257                         setLayout(cur, layout);
1258
1259                 break;
1260         }
1261
1262         case LFUN_CLIPBOARD_PASTE:
1263                 cur.clearSelection();
1264                 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1265                                cmd.argument() == "paragraph");
1266                 bv->buffer().errors("Paste");
1267                 break;
1268
1269         case LFUN_PRIMARY_SELECTION_PASTE:
1270                 pasteString(cur, theSelection().get(),
1271                             cmd.argument() == "paragraph");
1272                 break;
1273
1274         case LFUN_SELECTION_PASTE:
1275                 // Copy the selection buffer to the clipboard stack,
1276                 // because we want it to appear in the "Edit->Paste
1277                 // recent" menu.
1278                 cap::copySelectionToStack();
1279                 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1280                 bv->buffer().errors("Paste");
1281                 break;
1282
1283         case LFUN_UNICODE_INSERT: {
1284                 if (cmd.argument().empty())
1285                         break;
1286                 docstring hexstring = cmd.argument();
1287                 if (isHex(hexstring)) {
1288                         char_type c = hexToInt(hexstring);
1289                         if (c >= 32 && c < 0x10ffff) {
1290                                 lyxerr << "Inserting c: " << c << endl;
1291                                 docstring s = docstring(1, c);
1292                                 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1293                         }
1294                 }
1295                 break;
1296         }
1297
1298         case LFUN_QUOTE_INSERT: {
1299                 Paragraph & par = cur.paragraph();
1300                 pos_type pos = cur.pos();
1301                 BufferParams const & bufparams = bv->buffer().params();
1302                 Layout const & style = par.layout();
1303                 InsetLayout const & ilayout = cur.inset().getLayout();
1304                 if (!style.pass_thru && !ilayout.isPassThru()
1305                     && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1306                         // this avoids a double undo
1307                         // FIXME: should not be needed, ideally
1308                         if (!cur.selection())
1309                                 cur.recordUndo();
1310                         cap::replaceSelection(cur);
1311                         pos = cur.pos();
1312                         char_type c;
1313                         if (pos == 0)
1314                                 c = ' ';
1315                         else if (cur.prevInset() && cur.prevInset()->isSpace())
1316                                 c = ' ';
1317                         else
1318                                 c = par.getChar(pos - 1);
1319                         string arg = to_utf8(cmd.argument());
1320                         cur.insert(new InsetQuotes(cur.buffer(), c, (arg == "single")
1321                                 ? InsetQuotes::SingleQuotes : InsetQuotes::DoubleQuotes));
1322                         cur.posForward();
1323                 }
1324                 else
1325                         lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1326                 break;
1327         }
1328
1329         case LFUN_DATE_INSERT: {
1330                 string const format = cmd.argument().empty()
1331                         ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1332                 string const time = formatted_time(current_time(), format);
1333                 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1334                 break;
1335         }
1336
1337         case LFUN_MOUSE_TRIPLE:
1338                 if (cmd.button() == mouse_button::button1) {
1339                         tm->cursorHome(cur);
1340                         cur.resetAnchor();
1341                         tm->cursorEnd(cur);
1342                         cur.setSelection();
1343                         bv->cursor() = cur;
1344                 }
1345                 break;
1346
1347         case LFUN_MOUSE_DOUBLE:
1348                 if (cmd.button() == mouse_button::button1) {
1349                         selectWord(cur, WHOLE_WORD_STRICT);
1350                         bv->cursor() = cur;
1351                 }
1352                 break;
1353
1354         // Single-click on work area
1355         case LFUN_MOUSE_PRESS: {
1356                 // We are not marking a selection with the keyboard in any case.
1357                 Cursor & bvcur = cur.bv().cursor();
1358                 bvcur.setMark(false);
1359                 switch (cmd.button()) {
1360                 case mouse_button::button1:
1361                         // Set the cursor
1362                         if (!bv->mouseSetCursor(cur, cmd.argument() == "region-select"))
1363                                 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1364                         if (bvcur.wordSelection())
1365                                 selectWord(bvcur, WHOLE_WORD);
1366                         break;
1367
1368                 case mouse_button::button2:
1369                         // Middle mouse pasting.
1370                         bv->mouseSetCursor(cur);
1371                         lyx::dispatch(
1372                                 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1373                                             "selection-paste ; primary-selection-paste paragraph"));
1374                         cur.noUpdate();
1375                         break;
1376
1377                 case mouse_button::button3: {
1378                         // Don't do anything if we right-click a
1379                         // selection, a context menu will popup.
1380                         if (bvcur.selection() && cur >= bvcur.selectionBegin()
1381                             && cur < bvcur.selectionEnd()) {
1382                                 cur.noUpdate();
1383                                 return;
1384                         }
1385                         if (!bv->mouseSetCursor(cur, false))
1386                                 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1387                         break;
1388                 }
1389
1390                 default:
1391                         break;
1392                 } // switch (cmd.button())
1393                 break;
1394         }
1395         case LFUN_MOUSE_MOTION: {
1396                 // Mouse motion with right or middle mouse do nothing for now.
1397                 if (cmd.button() != mouse_button::button1) {
1398                         cur.noUpdate();
1399                         return;
1400                 }
1401                 // ignore motions deeper nested than the real anchor
1402                 Cursor & bvcur = cur.bv().cursor();
1403                 if (!bvcur.anchor_.hasPart(cur)) {
1404                         cur.undispatched();
1405                         break;
1406                 }
1407                 CursorSlice old = bvcur.top();
1408
1409                 int const wh = bv->workHeight();
1410                 int const y = max(0, min(wh - 1, cmd.y));
1411
1412                 tm->setCursorFromCoordinates(cur, cmd.x, y);
1413                 cur.setTargetX(cmd.x);
1414                 if (cmd.y >= wh)
1415                         lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1416                 else if (cmd.y < 0)
1417                         lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1418                 // This is to allow jumping over large insets
1419                 if (cur.top() == old) {
1420                         if (cmd.y >= wh)
1421                                 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1422                         else if (cmd.y < 0)
1423                                 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1424                 }
1425                 // We continue with our existing selection or start a new one, so don't
1426                 // reset the anchor.
1427                 bvcur.setCursor(cur);
1428                 bvcur.setSelection(true);
1429                 if (cur.top() == old) {
1430                         // We didn't move one iota, so no need to update the screen.
1431                         cur.updateFlags(Update::SinglePar | Update::FitCursor);
1432                         //cur.noUpdate();
1433                         return;
1434                 }
1435                 break;
1436         }
1437
1438         case LFUN_MOUSE_RELEASE:
1439                 switch (cmd.button()) {
1440                 case mouse_button::button1:
1441                         // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1442                         // If there is a new selection, update persistent selection;
1443                         // otherwise, single click does not clear persistent selection
1444                         // buffer.
1445                         if (cur.selection()) {
1446                                 // Finish selection. If double click,
1447                                 // cur is moved to the end of word by
1448                                 // selectWord but bvcur is current
1449                                 // mouse position.
1450                                 cur.bv().cursor().setSelection();
1451                                 // We might have removed an empty but drawn selection
1452                                 // (probably a margin)
1453                                 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1454                         } else
1455                                 cur.noUpdate();
1456                         // FIXME: We could try to handle drag and drop of selection here.
1457                         return;
1458
1459                 case mouse_button::button2:
1460                         // Middle mouse pasting is handled at mouse press time,
1461                         // see LFUN_MOUSE_PRESS.
1462                         cur.noUpdate();
1463                         return;
1464
1465                 case mouse_button::button3:
1466                         // Cursor was set at LFUN_MOUSE_PRESS time.
1467                         // FIXME: If there is a selection we could try to handle a special
1468                         // drag & drop context menu.
1469                         cur.noUpdate();
1470                         return;
1471
1472                 case mouse_button::none:
1473                 case mouse_button::button4:
1474                 case mouse_button::button5:
1475                         break;
1476                 } // switch (cmd.button())
1477
1478                 break;
1479
1480         case LFUN_SELF_INSERT: {
1481                 if (cmd.argument().empty())
1482                         break;
1483
1484                 // Automatically delete the currently selected
1485                 // text and replace it with what is being
1486                 // typed in now. Depends on lyxrc settings
1487                 // "auto_region_delete", which defaults to
1488                 // true (on).
1489
1490                 if (lyxrc.auto_region_delete && cur.selection())
1491                         cutSelection(cur, false, false);
1492
1493                 cur.clearSelection();
1494
1495                 docstring::const_iterator cit = cmd.argument().begin();
1496                 docstring::const_iterator const end = cmd.argument().end();
1497                 for (; cit != end; ++cit)
1498                         bv->translateAndInsert(*cit, this, cur);
1499
1500                 cur.resetAnchor();
1501                 moveCursor(cur, false);
1502                 bv->bookmarkEditPosition();
1503                 break;
1504         }
1505
1506         case LFUN_HYPERLINK_INSERT: {
1507                 InsetCommandParams p(HYPERLINK_CODE);
1508                 docstring content;
1509                 if (cur.selection()) {
1510                         content = cur.selectionAsString(false);
1511                         cutSelection(cur, true, false);
1512                 }
1513                 p["target"] = (cmd.argument().empty()) ?
1514                         content : cmd.argument();
1515                 string const data = InsetCommand::params2string("href", p);
1516                 if (p["target"].empty()) {
1517                         bv->showDialog("href", data);
1518                 } else {
1519                         FuncRequest fr(LFUN_INSET_INSERT, data);
1520                         dispatch(cur, fr);
1521                 }
1522                 break;
1523         }
1524
1525         case LFUN_LABEL_INSERT: {
1526                 InsetCommandParams p(LABEL_CODE);
1527                 // Try to generate a valid label
1528                 p["name"] = (cmd.argument().empty()) ?
1529                         cur.getPossibleLabel() :
1530                         cmd.argument();
1531                 string const data = InsetCommand::params2string("label", p);
1532
1533                 if (cmd.argument().empty()) {
1534                         bv->showDialog("label", data);
1535                 } else {
1536                         FuncRequest fr(LFUN_INSET_INSERT, data);
1537                         dispatch(cur, fr);
1538                 }
1539                 break;
1540         }
1541
1542         case LFUN_INFO_INSERT: {
1543                 Inset * inset;
1544                 if (cmd.argument().empty() && cur.selection()) {
1545                         // if command argument is empty use current selection as parameter.
1546                         docstring ds = cur.selectionAsString(false);
1547                         cutSelection(cur, true, false);
1548                         FuncRequest cmd0(cmd, ds);
1549                         inset = createInset(cur.buffer(), cmd0);
1550                 } else {
1551                         inset = createInset(cur.buffer(), cmd);
1552                 }
1553                 if (!inset)
1554                         break;
1555                 cur.recordUndo();
1556                 insertInset(cur, inset);
1557                 cur.posForward();
1558                 break;
1559         }
1560         case LFUN_CAPTION_INSERT:
1561         case LFUN_FOOTNOTE_INSERT:
1562         case LFUN_NOTE_INSERT:
1563         case LFUN_FLEX_INSERT:
1564         case LFUN_BOX_INSERT:
1565         case LFUN_BRANCH_INSERT:
1566         case LFUN_PHANTOM_INSERT:
1567         case LFUN_ERT_INSERT:
1568         case LFUN_LISTING_INSERT:
1569         case LFUN_MARGINALNOTE_INSERT:
1570         case LFUN_OPTIONAL_INSERT:
1571         case LFUN_INDEX_INSERT:
1572                 // Open the inset, and move the current selection
1573                 // inside it.
1574                 doInsertInset(cur, this, cmd, true, true);
1575                 cur.posForward();
1576                 // Some insets are numbered, others are shown in the outline pane so
1577                 // let's update the labels and the toc backend.
1578                 bv->buffer().updateLabels();
1579                 break;
1580
1581         case LFUN_TABULAR_INSERT:
1582                 // if there were no arguments, just open the dialog
1583                 if (doInsertInset(cur, this, cmd, false, true))
1584                         cur.posForward();
1585                 else
1586                         bv->showDialog("tabularcreate");
1587
1588                 break;
1589
1590         case LFUN_FLOAT_INSERT:
1591         case LFUN_FLOAT_WIDE_INSERT:
1592         case LFUN_WRAP_INSERT: {
1593                 // will some text be moved into the inset?
1594                 bool content = cur.selection();
1595
1596                 doInsertInset(cur, this, cmd, true, true);
1597                 cur.posForward();
1598
1599                 // If some text is moved into the inset, doInsertInset 
1600                 // puts the cursor outside the inset. To insert the
1601                 // caption we put it back into the inset.
1602                 if (content)
1603                         cur.backwardPos();
1604
1605                 ParagraphList & pars = cur.text()->paragraphs();
1606
1607                 DocumentClass const & tclass = bv->buffer().params().documentClass();
1608
1609                 // add a separate paragraph for the caption inset
1610                 pars.push_back(Paragraph());
1611                 pars.back().setInsetOwner(&cur.text()->inset());
1612                 pars.back().setPlainOrDefaultLayout(tclass);
1613                 int cap_pit = pars.size() - 1;
1614
1615                 // if an empty inset was created, we create an additional empty
1616                 // paragraph at the bottom so that the user can choose where to put
1617                 // the graphics (or table).
1618                 if (!content) {
1619                         pars.push_back(Paragraph());
1620                         pars.back().setInsetOwner(&cur.text()->inset());
1621                         pars.back().setPlainOrDefaultLayout(tclass);
1622                 }
1623
1624                 // reposition the cursor to the caption
1625                 cur.pit() = cap_pit;
1626                 cur.pos() = 0;
1627                 // FIXME: This Text/Cursor dispatch handling is a mess!
1628                 // We cannot use Cursor::dispatch here it needs access to up to
1629                 // date metrics.
1630                 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1631                 doInsertInset(cur, cur.text(), cmd_caption, true, false);
1632                 bv->buffer().updateLabels();
1633                 cur.updateFlags(Update::Force);
1634                 // FIXME: When leaving the Float (or Wrap) inset we should
1635                 // delete any empty paragraph left above or below the
1636                 // caption.
1637                 break;
1638         }
1639
1640         case LFUN_NOMENCL_INSERT: {
1641                 InsetCommandParams p(NOMENCL_CODE);
1642                 if (cmd.argument().empty())
1643                         p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
1644                 else
1645                         p["symbol"] = cmd.argument();
1646                 string const data = InsetCommand::params2string("nomenclature", p);
1647                 bv->showDialog("nomenclature", data);
1648                 break;
1649         }
1650
1651         case LFUN_INDEX_PRINT: {
1652                 InsetCommandParams p(INDEX_PRINT_CODE);
1653                 if (cmd.argument().empty())
1654                         p["type"] = from_ascii("idx");
1655                 else
1656                         p["type"] = cmd.argument();
1657                 string const data = InsetCommand::params2string("index_print", p);
1658                 FuncRequest fr(LFUN_INSET_INSERT, data);
1659                 dispatch(cur, fr);
1660                 break;
1661         }
1662         
1663         case LFUN_NOMENCL_PRINT:
1664         case LFUN_TOC_INSERT:
1665         case LFUN_LINE_INSERT:
1666         case LFUN_NEWPAGE_INSERT:
1667                 // do nothing fancy
1668                 doInsertInset(cur, this, cmd, false, false);
1669                 cur.posForward();
1670                 break;
1671
1672         case LFUN_DEPTH_DECREMENT:
1673                 changeDepth(cur, DEC_DEPTH);
1674                 break;
1675
1676         case LFUN_DEPTH_INCREMENT:
1677                 changeDepth(cur, INC_DEPTH);
1678                 break;
1679
1680         case LFUN_MATH_DISPLAY:
1681                 mathDispatch(cur, cmd, true);
1682                 break;
1683
1684         case LFUN_REGEXP_MODE:
1685                 regexpDispatch(cur, cmd);
1686                 break;
1687
1688         case LFUN_MATH_MODE:
1689                 if (cmd.argument() == "on")
1690                         // don't pass "on" as argument
1691                         mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1692                 else
1693                         mathDispatch(cur, cmd, false);
1694                 break;
1695
1696         case LFUN_MATH_MACRO:
1697                 if (cmd.argument().empty())
1698                         cur.errorMessage(from_utf8(N_("Missing argument")));
1699                 else {
1700                         string s = to_utf8(cmd.argument());
1701                         string const s1 = token(s, ' ', 1);
1702                         int const nargs = s1.empty() ? 0 : convert<int>(s1);
1703                         string const s2 = token(s, ' ', 2);
1704                         MacroType type = MacroTypeNewcommand;
1705                         if (s2 == "def")
1706                                 type = MacroTypeDef;
1707                         MathMacroTemplate * inset = new MathMacroTemplate(cur.buffer(),
1708                                 from_utf8(token(s, ' ', 0)), nargs, false, type);
1709                         inset->setBuffer(bv->buffer());
1710                         insertInset(cur, inset);
1711
1712                         // enter macro inset and select the name
1713                         cur.push(*inset);
1714                         cur.top().pos() = cur.top().lastpos();
1715                         cur.resetAnchor();
1716                         cur.setSelection(true);
1717                         cur.top().pos() = 0;
1718                 }
1719                 break;
1720
1721         // passthrough hat and underscore outside mathed:
1722         case LFUN_MATH_SUBSCRIPT:
1723                 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1724                 break;
1725         case LFUN_MATH_SUPERSCRIPT:
1726                 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1727                 break;
1728
1729         case LFUN_MATH_INSERT:
1730         case LFUN_MATH_AMS_MATRIX:
1731         case LFUN_MATH_MATRIX:
1732         case LFUN_MATH_DELIM:
1733         case LFUN_MATH_BIGDELIM: {
1734                 cur.recordUndo();
1735                 cap::replaceSelection(cur);
1736                 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
1737                 checkAndActivateInset(cur, true);
1738                 LASSERT(cur.inMathed(), /**/);
1739                 cur.dispatch(cmd);
1740                 break;
1741         }
1742
1743         case LFUN_FONT_EMPH: {
1744                 Font font(ignore_font, ignore_language);
1745                 font.fontInfo().setEmph(FONT_TOGGLE);
1746                 toggleAndShow(cur, this, font);
1747                 break;
1748         }
1749
1750         case LFUN_FONT_ITAL: {
1751                 Font font(ignore_font, ignore_language);
1752                 font.fontInfo().setShape(ITALIC_SHAPE);
1753                 toggleAndShow(cur, this, font);
1754                 break;
1755         }
1756
1757         case LFUN_FONT_BOLD:
1758         case LFUN_FONT_BOLDSYMBOL: {
1759                 Font font(ignore_font, ignore_language);
1760                 font.fontInfo().setSeries(BOLD_SERIES);
1761                 toggleAndShow(cur, this, font);
1762                 break;
1763         }
1764
1765         case LFUN_FONT_NOUN: {
1766                 Font font(ignore_font, ignore_language);
1767                 font.fontInfo().setNoun(FONT_TOGGLE);
1768                 toggleAndShow(cur, this, font);
1769                 break;
1770         }
1771
1772         case LFUN_FONT_TYPEWRITER: {
1773                 Font font(ignore_font, ignore_language);
1774                 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1775                 toggleAndShow(cur, this, font);
1776                 break;
1777         }
1778
1779         case LFUN_FONT_SANS: {
1780                 Font font(ignore_font, ignore_language);
1781                 font.fontInfo().setFamily(SANS_FAMILY);
1782                 toggleAndShow(cur, this, font);
1783                 break;
1784         }
1785
1786         case LFUN_FONT_ROMAN: {
1787                 Font font(ignore_font, ignore_language);
1788                 font.fontInfo().setFamily(ROMAN_FAMILY);
1789                 toggleAndShow(cur, this, font);
1790                 break;
1791         }
1792
1793         case LFUN_FONT_DEFAULT: {
1794                 Font font(inherit_font, ignore_language);
1795                 toggleAndShow(cur, this, font);
1796                 break;
1797         }
1798
1799         case LFUN_FONT_STRIKEOUT: {
1800                 Font font(ignore_font, ignore_language);
1801                 font.fontInfo().setStrikeout(FONT_TOGGLE);
1802                 toggleAndShow(cur, this, font);
1803                 break;
1804         }
1805
1806         case LFUN_FONT_UULINE: {
1807                 Font font(ignore_font, ignore_language);
1808                 font.fontInfo().setUuline(FONT_TOGGLE);
1809                 toggleAndShow(cur, this, font);
1810                 break;
1811         }
1812
1813         case LFUN_FONT_UWAVE: {
1814                 Font font(ignore_font, ignore_language);
1815                 font.fontInfo().setUwave(FONT_TOGGLE);
1816                 toggleAndShow(cur, this, font);
1817                 break;
1818         }
1819
1820         case LFUN_FONT_UNDERLINE: {
1821                 Font font(ignore_font, ignore_language);
1822                 font.fontInfo().setUnderbar(FONT_TOGGLE);
1823                 toggleAndShow(cur, this, font);
1824                 break;
1825         }
1826
1827         case LFUN_FONT_SIZE: {
1828                 Font font(ignore_font, ignore_language);
1829                 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1830                 toggleAndShow(cur, this, font);
1831                 break;
1832         }
1833
1834         case LFUN_LANGUAGE: {
1835                 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1836                 if (!lang)
1837                         break;
1838                 Font font(ignore_font, lang);
1839                 toggleAndShow(cur, this, font);
1840                 break;
1841         }
1842
1843         case LFUN_TEXTSTYLE_APPLY:
1844                 toggleAndShow(cur, this, freefont, toggleall);
1845                 cur.message(_("Character set"));
1846                 break;
1847
1848         // Set the freefont using the contents of \param data dispatched from
1849         // the frontends and apply it at the current cursor location.
1850         case LFUN_TEXTSTYLE_UPDATE: {
1851                 Font font;
1852                 bool toggle;
1853                 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1854                         freefont = font;
1855                         toggleall = toggle;
1856                         toggleAndShow(cur, this, freefont, toggleall);
1857                         cur.message(_("Character set"));
1858                 } else {
1859                         lyxerr << "Argument not ok";
1860                 }
1861                 break;
1862         }
1863
1864         case LFUN_FINISHED_LEFT:
1865                 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
1866                 // We're leaving an inset, going left. If the inset is LTR, we're
1867                 // leaving from the front, so we should not move (remain at --- but
1868                 // not in --- the inset). If the inset is RTL, move left, without
1869                 // entering the inset itself; i.e., move to after the inset.
1870                 if (cur.paragraph().getFontSettings(
1871                                 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1872                         cursorVisLeft(cur, true);
1873                 break;
1874
1875         case LFUN_FINISHED_RIGHT:
1876                 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
1877                 // We're leaving an inset, going right. If the inset is RTL, we're
1878                 // leaving from the front, so we should not move (remain at --- but
1879                 // not in --- the inset). If the inset is LTR, move right, without
1880                 // entering the inset itself; i.e., move to after the inset.
1881                 if (!cur.paragraph().getFontSettings(
1882                                 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1883                         cursorVisRight(cur, true);
1884                 break;
1885
1886         case LFUN_FINISHED_BACKWARD:
1887                 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
1888                 break;
1889
1890         case LFUN_FINISHED_FORWARD:
1891                 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
1892                 ++cur.pos();
1893                 cur.setCurrentFont();
1894                 break;
1895
1896         case LFUN_LAYOUT_PARAGRAPH: {
1897                 string data;
1898                 params2string(cur.paragraph(), data);
1899                 data = "show\n" + data;
1900                 bv->showDialog("paragraph", data);
1901                 break;
1902         }
1903
1904         case LFUN_PARAGRAPH_UPDATE: {
1905                 string data;
1906                 params2string(cur.paragraph(), data);
1907
1908                 // Will the paragraph accept changes from the dialog?
1909                 bool const accept =
1910                         cur.inset().allowParagraphCustomization(cur.idx());
1911
1912                 data = "update " + convert<string>(accept) + '\n' + data;
1913                 bv->updateDialog("paragraph", data);
1914                 break;
1915         }
1916
1917         case LFUN_ACCENT_UMLAUT:
1918         case LFUN_ACCENT_CIRCUMFLEX:
1919         case LFUN_ACCENT_GRAVE:
1920         case LFUN_ACCENT_ACUTE:
1921         case LFUN_ACCENT_TILDE:
1922         case LFUN_ACCENT_CEDILLA:
1923         case LFUN_ACCENT_MACRON:
1924         case LFUN_ACCENT_DOT:
1925         case LFUN_ACCENT_UNDERDOT:
1926         case LFUN_ACCENT_UNDERBAR:
1927         case LFUN_ACCENT_CARON:
1928         case LFUN_ACCENT_BREVE:
1929         case LFUN_ACCENT_TIE:
1930         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1931         case LFUN_ACCENT_CIRCLE:
1932         case LFUN_ACCENT_OGONEK:
1933                 theApp()->handleKeyFunc(cmd.action);
1934                 if (!cmd.argument().empty())
1935                         // FIXME: Are all these characters encoded in one byte in utf8?
1936                         bv->translateAndInsert(cmd.argument()[0], this, cur);
1937                 break;
1938
1939         case LFUN_FLOAT_LIST_INSERT: {
1940                 DocumentClass const & tclass = bv->buffer().params().documentClass();
1941                 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1942                         cur.recordUndo();
1943                         if (cur.selection())
1944                                 cutSelection(cur, true, false);
1945                         breakParagraph(cur);
1946
1947                         if (cur.lastpos() != 0) {
1948                                 cursorBackward(cur);
1949                                 breakParagraph(cur);
1950                         }
1951
1952                         docstring const laystr = cur.inset().usePlainLayout() ?
1953                                 tclass.plainLayoutName() :
1954                                 tclass.defaultLayoutName();
1955                         setLayout(cur, laystr);
1956                         ParagraphParameters p;
1957                         // FIXME If this call were replaced with one to clearParagraphParams(),
1958                         // then we could get rid of this method altogether.
1959                         setParagraphs(cur, p);
1960                         // FIXME This should be simplified when InsetFloatList takes a
1961                         // Buffer in its constructor.
1962                         InsetFloatList * ifl = new InsetFloatList(cur.buffer(), to_utf8(cmd.argument()));
1963                         ifl->setBuffer(bv->buffer());
1964                         insertInset(cur, ifl);
1965                         cur.posForward();
1966                 } else {
1967                         lyxerr << "Non-existent float type: "
1968                                << to_utf8(cmd.argument()) << endl;
1969                 }
1970                 break;
1971         }
1972
1973         case LFUN_CHANGE_ACCEPT: {
1974                 acceptOrRejectChanges(cur, ACCEPT);
1975                 break;
1976         }
1977
1978         case LFUN_CHANGE_REJECT: {
1979                 acceptOrRejectChanges(cur, REJECT);
1980                 break;
1981         }
1982
1983         case LFUN_THESAURUS_ENTRY: {
1984                 docstring arg = cmd.argument();
1985                 if (arg.empty()) {
1986                         arg = cur.selectionAsString(false);
1987                         // FIXME
1988                         if (arg.size() > 100 || arg.empty()) {
1989                                 // Get word or selection
1990                                 selectWordWhenUnderCursor(cur, WHOLE_WORD);
1991                                 arg = cur.selectionAsString(false);
1992                                 arg += " lang=" + from_ascii(cur.getFont().language()->lang());
1993                         }
1994                 }
1995                 bv->showDialog("thesaurus", to_utf8(arg));
1996                 break;
1997         }
1998
1999         case LFUN_PARAGRAPH_PARAMS_APPLY: {
2000                 // Given data, an encoding of the ParagraphParameters
2001                 // generated in the Paragraph dialog, this function sets
2002                 // the current paragraph, or currently selected paragraphs,
2003                 // appropriately.
2004                 // NOTE: This function overrides all existing settings.
2005                 setParagraphs(cur, cmd.argument());
2006                 cur.message(_("Paragraph layout set"));
2007                 break;
2008         }
2009
2010         case LFUN_PARAGRAPH_PARAMS: {
2011                 // Given data, an encoding of the ParagraphParameters as we'd
2012                 // find them in a LyX file, this function modifies the current paragraph,
2013                 // or currently selected paragraphs.
2014                 // NOTE: This function only modifies, and does not override, existing
2015                 // settings.
2016                 setParagraphs(cur, cmd.argument(), true);
2017                 cur.message(_("Paragraph layout set"));
2018                 break;
2019         }
2020
2021         case LFUN_ESCAPE:
2022                 if (cur.selection()) {
2023                         cur.setSelection(false);
2024                 } else {
2025                         cur.undispatched();
2026                         // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2027                         // correct, but I'm not 100% sure -- dov, 071019
2028                         cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2029                 }
2030                 break;
2031
2032         case LFUN_OUTLINE_UP:
2033                 outline(OutlineUp, cur);
2034                 setCursor(cur, cur.pit(), 0);
2035                 cur.buffer()->updateLabels();
2036                 needsUpdate = true;
2037                 break;
2038
2039         case LFUN_OUTLINE_DOWN:
2040                 outline(OutlineDown, cur);
2041                 setCursor(cur, cur.pit(), 0);
2042                 cur.buffer()->updateLabels();
2043                 needsUpdate = true;
2044                 break;
2045
2046         case LFUN_OUTLINE_IN:
2047                 outline(OutlineIn, cur);
2048                 cur.buffer()->updateLabels();
2049                 needsUpdate = true;
2050                 break;
2051
2052         case LFUN_OUTLINE_OUT:
2053                 outline(OutlineOut, cur);
2054                 cur.buffer()->updateLabels();
2055                 needsUpdate = true;
2056                 break;
2057
2058         default:
2059                 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2060                 cur.undispatched();
2061                 break;
2062         }
2063
2064         needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2065
2066         // FIXME: The cursor flag is reset two lines below
2067         // so we need to check here if some of the LFUN did touch that.
2068         // for now only Text::erase() and Text::backspace() do that.
2069         // The plan is to verify all the LFUNs and then to remove this
2070         // singleParUpdate boolean altogether.
2071         if (cur.result().update() & Update::Force) {
2072                 singleParUpdate = false;
2073                 needsUpdate = true;
2074         }
2075
2076         // FIXME: the following code should go in favor of fine grained
2077         // update flag treatment.
2078         if (singleParUpdate) {
2079                 // Inserting characters does not change par height in general. So, try
2080                 // to update _only_ this paragraph. BufferView will detect if a full
2081                 // metrics update is needed anyway.
2082                 cur.updateFlags(Update::SinglePar | Update::FitCursor);
2083                 return;
2084         }
2085
2086         if (!needsUpdate
2087             && &oldTopSlice.inset() == &cur.inset()
2088             && oldTopSlice.idx() == cur.idx()
2089             && !sel // sel is a backup of cur.selection() at the beginning of the function.
2090             && !cur.selection())
2091                 // FIXME: it would be better if we could just do this
2092                 //
2093                 //if (cur.result().update() != Update::FitCursor)
2094                 //      cur.noUpdate();
2095                 //
2096                 // But some LFUNs do not set Update::FitCursor when needed, so we
2097                 // do it for all. This is not very harmfull as FitCursor will provoke
2098                 // a full redraw only if needed but still, a proper review of all LFUN
2099                 // should be done and this needsUpdate boolean can then be removed.
2100                 cur.updateFlags(Update::FitCursor);
2101         else
2102                 cur.updateFlags(Update::Force | Update::FitCursor);
2103 }
2104
2105
2106 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2107                         FuncStatus & flag) const
2108 {
2109         LASSERT(cur.text() == this, /**/);
2110
2111         FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2112         bool enable = true;
2113         InsetCode code = NO_CODE;
2114
2115         switch (cmd.action) {
2116
2117         case LFUN_DEPTH_DECREMENT:
2118                 enable = changeDepthAllowed(cur, DEC_DEPTH);
2119                 break;
2120
2121         case LFUN_DEPTH_INCREMENT:
2122                 enable = changeDepthAllowed(cur, INC_DEPTH);
2123                 break;
2124
2125         case LFUN_APPENDIX:
2126                 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2127                 break;
2128
2129         case LFUN_DIALOG_SHOW_NEW_INSET:
2130                 if (cmd.argument() == "bibitem")
2131                         code = BIBITEM_CODE;
2132                 else if (cmd.argument() == "bibtex") {
2133                         code = BIBTEX_CODE;
2134                         // not allowed in description items
2135                         enable = !inDescriptionItem(cur);
2136                 }
2137                 else if (cmd.argument() == "box")
2138                         code = BOX_CODE;
2139                 else if (cmd.argument() == "branch")
2140                         code = BRANCH_CODE;
2141                 else if (cmd.argument() == "citation")
2142                         code = CITE_CODE;
2143                 else if (cmd.argument() == "ert")
2144                         code = ERT_CODE;
2145                 else if (cmd.argument() == "external")
2146                         code = EXTERNAL_CODE;
2147                 else if (cmd.argument() == "float")
2148                         code = FLOAT_CODE;
2149                 else if (cmd.argument() == "graphics")
2150                         code = GRAPHICS_CODE;
2151                 else if (cmd.argument() == "href")
2152                         code = HYPERLINK_CODE;
2153                 else if (cmd.argument() == "include")
2154                         code = INCLUDE_CODE;
2155                 else if (cmd.argument() == "index")
2156                         code = INDEX_CODE;
2157                 else if (cmd.argument() == "index_print")
2158                         code = INDEX_PRINT_CODE;
2159                 else if (cmd.argument() == "nomenclature")
2160                         code = NOMENCL_CODE;
2161                 else if (cmd.argument() == "nomencl_print")
2162                         code = NOMENCL_PRINT_CODE;
2163                 else if (cmd.argument() == "label")
2164                         code = LABEL_CODE;
2165                 else if (cmd.argument() == "note")
2166                         code = NOTE_CODE;
2167                 else if (cmd.argument() == "phantom")
2168                         code = PHANTOM_CODE;
2169                 else if (cmd.argument() == "ref")
2170                         code = REF_CODE;
2171                 else if (cmd.argument() == "space")
2172                         code = SPACE_CODE;
2173                 else if (cmd.argument() == "toc")
2174                         code = TOC_CODE;
2175                 else if (cmd.argument() == "vspace")
2176                         code = VSPACE_CODE;
2177                 else if (cmd.argument() == "wrap")
2178                         code = WRAP_CODE;
2179                 else if (cmd.argument() == "listings")
2180                         code = LISTINGS_CODE;
2181                 break;
2182
2183         case LFUN_ERT_INSERT:
2184                 code = ERT_CODE;
2185                 break;
2186         case LFUN_LISTING_INSERT:
2187                 code = LISTINGS_CODE;
2188                 // not allowed in description items
2189                 enable = !inDescriptionItem(cur);
2190                 break;
2191         case LFUN_FOOTNOTE_INSERT:
2192                 code = FOOT_CODE;
2193                 break;
2194         case LFUN_TABULAR_INSERT:
2195                 code = TABULAR_CODE;
2196                 break;
2197         case LFUN_MARGINALNOTE_INSERT:
2198                 code = MARGIN_CODE;
2199                 break;
2200         case LFUN_FLOAT_INSERT:
2201         case LFUN_FLOAT_WIDE_INSERT:
2202                 // FIXME: If there is a selection, we should check whether there
2203                 // are floats in the selection, but this has performance issues, see
2204                 // LFUN_CHANGE_ACCEPT/REJECT.
2205                 code = FLOAT_CODE;
2206                 if (inDescriptionItem(cur))
2207                         // not allowed in description items
2208                         enable = false;
2209                 else {
2210                         InsetCode const inset_code = cur.inset().lyxCode();
2211
2212                         // algorithm floats cannot be put in another float
2213                         if (to_utf8(cmd.argument()) == "algorithm") {
2214                                 enable = inset_code != WRAP_CODE && inset_code != FLOAT_CODE;
2215                                 break;
2216                         }
2217
2218                         // for figures and tables: only allow in another
2219                         // float or wrap if it is of the same type and
2220                         // not a subfloat already
2221                         if(cur.inset().lyxCode() == code) {
2222                                 InsetFloat const & ins =
2223                                         static_cast<InsetFloat const &>(cur.inset());
2224                                 enable = ins.params().type == to_utf8(cmd.argument())
2225                                         && !ins.params().subfloat;
2226                         } else if(cur.inset().lyxCode() == WRAP_CODE) {
2227                                 InsetWrap const & ins =
2228                                         static_cast<InsetWrap const &>(cur.inset());
2229                                 enable = ins.params().type == to_utf8(cmd.argument());
2230                         }
2231                 }
2232                 break;
2233         case LFUN_WRAP_INSERT:
2234                 code = WRAP_CODE;
2235                 // not allowed in description items
2236                 enable = !inDescriptionItem(cur);
2237                 break;
2238         case LFUN_FLOAT_LIST_INSERT:
2239                 code = FLOAT_LIST_CODE;
2240                 // not allowed in description items
2241                 enable = !inDescriptionItem(cur);
2242                 break;
2243         case LFUN_CAPTION_INSERT:
2244                 code = CAPTION_CODE;
2245                 // not allowed in description items
2246                 enable = !inDescriptionItem(cur);
2247                 break;
2248         case LFUN_NOTE_INSERT:
2249                 code = NOTE_CODE;
2250                 // in commands (sections etc.) and description items,
2251                 // only Notes are allowed
2252                 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
2253                           (!cur.paragraph().layout().isCommand()
2254                            && !inDescriptionItem(cur)));
2255                 break;
2256         case LFUN_FLEX_INSERT: {
2257                 code = FLEX_CODE;
2258                 string s = cmd.getArg(0);
2259                 InsetLayout il =
2260                         cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2261                 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2262                     il.lyxtype() != InsetLayout::CUSTOM &&
2263                     il.lyxtype() != InsetLayout::ELEMENT &&
2264                     il.lyxtype ()!= InsetLayout::STANDARD)
2265                         enable = false;
2266                 break;
2267                 }
2268         case LFUN_BOX_INSERT:
2269                 code = BOX_CODE;
2270                 break;
2271         case LFUN_BRANCH_INSERT:
2272                 code = BRANCH_CODE;
2273                 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2274                     && cur.buffer()->params().branchlist().empty())
2275                         enable = false;
2276                 break;
2277         case LFUN_PHANTOM_INSERT:
2278                 code = PHANTOM_CODE;
2279                 break;
2280         case LFUN_LABEL_INSERT:
2281                 code = LABEL_CODE;
2282                 break;
2283         case LFUN_LINE_INSERT:
2284                 code = LINE_CODE;
2285                 break;
2286         case LFUN_INFO_INSERT:
2287                 code = INFO_CODE;
2288                 break;
2289         case LFUN_OPTIONAL_INSERT:
2290                 code = OPTARG_CODE;
2291                 enable = cur.paragraph().insetList().count(OPTARG_CODE)
2292                         < cur.paragraph().layout().optionalargs;
2293                 break;
2294         case LFUN_INDEX_INSERT:
2295                 code = INDEX_CODE;
2296                 break;
2297         case LFUN_INDEX_PRINT:
2298                 code = INDEX_PRINT_CODE;
2299                 // not allowed in description items
2300                 enable = !inDescriptionItem(cur);
2301                 break;
2302         case LFUN_NOMENCL_INSERT:
2303                 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2304                         enable = false;
2305                         break;
2306                 }
2307                 code = NOMENCL_CODE;
2308                 break;
2309         case LFUN_NOMENCL_PRINT:
2310                 code = NOMENCL_PRINT_CODE;
2311                 // not allowed in description items
2312                 enable = !inDescriptionItem(cur);
2313                 break;
2314         case LFUN_TOC_INSERT:
2315                 code = TOC_CODE;
2316                 // not allowed in description items
2317                 enable = !inDescriptionItem(cur);
2318                 break;
2319         case LFUN_HYPERLINK_INSERT:
2320                 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2321                         enable = false;
2322                         break;
2323                 }
2324                 code = HYPERLINK_CODE;
2325                 break;
2326         case LFUN_QUOTE_INSERT:
2327                 // always allow this, since we will inset a raw quote
2328                 // if an inset is not allowed.
2329                 break;
2330         case LFUN_SPECIALCHAR_INSERT:
2331                 code = SPECIALCHAR_CODE;
2332                 break;
2333         case LFUN_SPACE_INSERT:
2334                 // slight hack: we know this is allowed in math mode
2335                 if (cur.inTexted())
2336                         code = SPACE_CODE;
2337                 break;
2338
2339         case LFUN_MATH_INSERT:
2340         case LFUN_MATH_AMS_MATRIX:
2341         case LFUN_MATH_MATRIX:
2342         case LFUN_MATH_DELIM:
2343         case LFUN_MATH_BIGDELIM:
2344         case LFUN_MATH_DISPLAY:
2345         case LFUN_MATH_MODE:
2346         case LFUN_MATH_MACRO:
2347         case LFUN_MATH_SUBSCRIPT:
2348         case LFUN_MATH_SUPERSCRIPT:
2349                 code = MATH_HULL_CODE;
2350                 break;
2351
2352         case LFUN_INSET_MODIFY:
2353                 // We need to disable this, because we may get called for a
2354                 // tabular cell via
2355                 // InsetTabular::getStatus() -> InsetText::getStatus()
2356                 // and we don't handle LFUN_INSET_MODIFY.
2357                 enable = false;
2358                 break;
2359
2360         case LFUN_FONT_EMPH:
2361                 flag.setOnOff(fontinfo.emph() == FONT_ON);
2362                 break;
2363
2364         case LFUN_FONT_ITAL:
2365                 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
2366                 break;
2367
2368         case LFUN_FONT_NOUN:
2369                 flag.setOnOff(fontinfo.noun() == FONT_ON);
2370                 break;
2371
2372         case LFUN_FONT_BOLD:
2373         case LFUN_FONT_BOLDSYMBOL:
2374                 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2375                 break;
2376
2377         case LFUN_FONT_SANS:
2378                 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2379                 break;
2380
2381         case LFUN_FONT_ROMAN:
2382                 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2383                 break;
2384
2385         case LFUN_FONT_TYPEWRITER:
2386                 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2387                 break;
2388
2389         case LFUN_CUT:
2390         case LFUN_COPY:
2391                 enable = cur.selection();
2392                 break;
2393
2394         case LFUN_PASTE: {
2395                 if (cmd.argument().empty()) {
2396                         if (theClipboard().isInternal())
2397                                 enable = cap::numberOfSelections() > 0;
2398                         else
2399                                 enable = !theClipboard().empty();
2400                         break;
2401                 }
2402
2403                 // we have an argument
2404                 string const arg = to_utf8(cmd.argument());
2405                 if (isStrUnsignedInt(arg)) {
2406                         // it's a number and therefore means the internal stack
2407                         unsigned int n = convert<unsigned int>(arg);
2408                         enable = cap::numberOfSelections() > n;
2409                         break;
2410                 }
2411
2412                 // explicit graphics type?
2413                 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
2414                 if ((arg == "pdf" && (type = Clipboard::PdfGraphicsType))
2415                           || (arg == "png" && (type = Clipboard::PngGraphicsType))
2416                           || (arg == "jpeg" && (type = Clipboard::JpegGraphicsType))
2417                           || (arg == "linkback" &&  (type = Clipboard::LinkBackGraphicsType))
2418                           || (arg == "emf" &&  (type = Clipboard::EmfGraphicsType))
2419                           || (arg == "wmf" &&  (type = Clipboard::WmfGraphicsType))) {
2420                         enable = theClipboard().hasGraphicsContents(type);
2421                         break;
2422                 }
2423
2424                 // unknown argument
2425                 enable = false;
2426                 break;
2427          }
2428
2429         case LFUN_CLIPBOARD_PASTE:
2430                 enable = !theClipboard().empty();
2431                 break;
2432
2433         case LFUN_PRIMARY_SELECTION_PASTE:
2434                 enable = cur.selection() || !theSelection().empty();
2435                 break;
2436
2437         case LFUN_SELECTION_PASTE:
2438                 enable = cap::selection();
2439                 break;
2440
2441         case LFUN_PARAGRAPH_MOVE_UP:
2442                 enable = cur.pit() > 0 && !cur.selection();
2443                 break;
2444
2445         case LFUN_PARAGRAPH_MOVE_DOWN:
2446                 enable = cur.pit() < cur.lastpit() && !cur.selection();
2447                 break;
2448
2449         case LFUN_CHANGE_ACCEPT:
2450         case LFUN_CHANGE_REJECT:
2451                 // In principle, these LFUNs should only be enabled if there
2452                 // is a change at the current position/in the current selection.
2453                 // However, without proper optimizations, this will inevitably
2454                 // result in unacceptable performance - just imagine a user who
2455                 // wants to select the complete content of a long document.
2456                 if (!cur.selection())
2457                         enable = cur.paragraph().isChanged(cur.pos());
2458                 else
2459                         // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
2460                         // for selections.
2461                         enable = true;
2462                 break;
2463
2464         case LFUN_OUTLINE_UP:
2465         case LFUN_OUTLINE_DOWN:
2466         case LFUN_OUTLINE_IN:
2467         case LFUN_OUTLINE_OUT:
2468
2469         case LFUN_NEWLINE_INSERT:
2470                 // LaTeX restrictions (labels or empty par)
2471                 enable = (cur.pos() > cur.paragraph().beginOfBody());
2472                 break;
2473
2474         case LFUN_TAB_INSERT:
2475         case LFUN_TAB_DELETE:
2476                 enable = cur.inset().getLayout().isPassThru();
2477                 break;
2478
2479         case LFUN_SET_GRAPHICS_GROUP: {
2480                 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
2481                 if (!ins)
2482                         enable = false;
2483                 else
2484                         flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
2485                 break;
2486         }
2487
2488         case LFUN_NEWPAGE_INSERT:
2489                 // not allowed in description items
2490                 code = NEWPAGE_CODE;
2491                 enable = !inDescriptionItem(cur);
2492                 break;
2493
2494         case LFUN_DATE_INSERT: {
2495                 string const format = cmd.argument().empty()
2496                         ? lyxrc.date_insert_format : to_utf8(cmd.argument());
2497                 enable = support::os::is_valid_strftime(format);
2498                 break;
2499         }
2500
2501         case LFUN_LANGUAGE:
2502                 enable = !cur.inset().getLayout().isPassThru();
2503                 break;
2504
2505         case LFUN_BREAK_PARAGRAPH:
2506                 enable = cur.inset().getLayout().isMultiPar();
2507                 break;
2508
2509         case LFUN_WORD_DELETE_FORWARD:
2510         case LFUN_WORD_DELETE_BACKWARD:
2511         case LFUN_LINE_DELETE:
2512         case LFUN_WORD_FORWARD:
2513         case LFUN_WORD_BACKWARD:
2514         case LFUN_WORD_RIGHT:
2515         case LFUN_WORD_LEFT:
2516         case LFUN_CHAR_FORWARD:
2517         case LFUN_CHAR_FORWARD_SELECT:
2518         case LFUN_CHAR_BACKWARD:
2519         case LFUN_CHAR_BACKWARD_SELECT:
2520         case LFUN_CHAR_LEFT:
2521         case LFUN_CHAR_LEFT_SELECT:
2522         case LFUN_CHAR_RIGHT:
2523         case LFUN_CHAR_RIGHT_SELECT:
2524         case LFUN_UP:
2525         case LFUN_UP_SELECT:
2526         case LFUN_DOWN:
2527         case LFUN_DOWN_SELECT:
2528         case LFUN_PARAGRAPH_UP_SELECT:
2529         case LFUN_PARAGRAPH_DOWN_SELECT:
2530         case LFUN_LINE_BEGIN_SELECT:
2531         case LFUN_LINE_END_SELECT:
2532         case LFUN_WORD_FORWARD_SELECT:
2533         case LFUN_WORD_BACKWARD_SELECT:
2534         case LFUN_WORD_RIGHT_SELECT:
2535         case LFUN_WORD_LEFT_SELECT:
2536         case LFUN_WORD_SELECT:
2537         case LFUN_SECTION_SELECT:
2538         case LFUN_PARAGRAPH_UP:
2539         case LFUN_PARAGRAPH_DOWN:
2540         case LFUN_LINE_BEGIN:
2541         case LFUN_LINE_END:
2542         case LFUN_CHAR_DELETE_FORWARD:
2543         case LFUN_CHAR_DELETE_BACKWARD:
2544         case LFUN_INSET_INSERT:
2545         case LFUN_WORD_UPCASE:
2546         case LFUN_WORD_LOWCASE:
2547         case LFUN_WORD_CAPITALIZE:
2548         case LFUN_CHARS_TRANSPOSE:
2549         case LFUN_SERVER_GET_XY:
2550         case LFUN_SERVER_SET_XY:
2551         case LFUN_SERVER_GET_LAYOUT:
2552         case LFUN_LAYOUT:
2553         case LFUN_SELF_INSERT:
2554         case LFUN_FONT_DEFAULT:
2555         case LFUN_FONT_UNDERLINE:
2556         case LFUN_FONT_STRIKEOUT:
2557         case LFUN_FONT_UULINE:
2558         case LFUN_FONT_UWAVE:
2559         case LFUN_FONT_SIZE:
2560         case LFUN_TEXTSTYLE_APPLY:
2561         case LFUN_TEXTSTYLE_UPDATE:
2562         case LFUN_LAYOUT_PARAGRAPH:
2563         case LFUN_PARAGRAPH_UPDATE:
2564         case LFUN_ACCENT_UMLAUT:
2565         case LFUN_ACCENT_CIRCUMFLEX:
2566         case LFUN_ACCENT_GRAVE:
2567         case LFUN_ACCENT_ACUTE:
2568         case LFUN_ACCENT_TILDE:
2569         case LFUN_ACCENT_CEDILLA:
2570         case LFUN_ACCENT_MACRON:
2571         case LFUN_ACCENT_DOT:
2572         case LFUN_ACCENT_UNDERDOT:
2573         case LFUN_ACCENT_UNDERBAR:
2574         case LFUN_ACCENT_CARON:
2575         case LFUN_ACCENT_BREVE:
2576         case LFUN_ACCENT_TIE:
2577         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2578         case LFUN_ACCENT_CIRCLE:
2579         case LFUN_ACCENT_OGONEK:
2580         case LFUN_THESAURUS_ENTRY:
2581         case LFUN_PARAGRAPH_PARAMS_APPLY:
2582         case LFUN_PARAGRAPH_PARAMS:
2583         case LFUN_ESCAPE:
2584         case LFUN_BUFFER_BEGIN:
2585         case LFUN_BUFFER_END:
2586         case LFUN_BUFFER_BEGIN_SELECT:
2587         case LFUN_BUFFER_END_SELECT:
2588         case LFUN_INSET_BEGIN:
2589         case LFUN_INSET_END:
2590         case LFUN_INSET_BEGIN_SELECT:
2591         case LFUN_INSET_END_SELECT:
2592         case LFUN_INSET_SELECT_ALL:
2593         case LFUN_UNICODE_INSERT:
2594                 // these are handled in our dispatch()
2595                 enable = true;
2596                 break;
2597
2598         default:
2599                 return false;
2600         }
2601
2602         if (code != NO_CODE
2603             && (cur.empty() 
2604                 || !cur.inset().insetAllowed(code)
2605                 || cur.paragraph().layout().pass_thru))
2606                 enable = false;
2607
2608         switch (cmd.action) {
2609         case LFUN_ACCENT_ACUTE:
2610         case LFUN_ACCENT_BREVE:
2611         case LFUN_ACCENT_CARON:
2612         case LFUN_ACCENT_CEDILLA:
2613         case LFUN_ACCENT_CIRCLE:
2614         case LFUN_ACCENT_CIRCUMFLEX:
2615         case LFUN_ACCENT_DOT:
2616         case LFUN_ACCENT_GRAVE:
2617         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2618         case LFUN_ACCENT_MACRON:
2619         case LFUN_ACCENT_OGONEK:
2620         case LFUN_ACCENT_TIE:
2621         case LFUN_ACCENT_TILDE:
2622         case LFUN_ACCENT_UMLAUT:
2623         case LFUN_ACCENT_UNDERBAR:
2624         case LFUN_ACCENT_UNDERDOT:
2625         case LFUN_FONT_BOLD:
2626         case LFUN_FONT_BOLDSYMBOL:
2627         case LFUN_FONT_TYPEWRITER:
2628         case LFUN_FONT_DEFAULT:
2629         case LFUN_FONT_EMPH:
2630         case LFUN_FONT_NOUN:
2631         case LFUN_FONT_ROMAN:
2632         case LFUN_FONT_SANS:
2633         case LFUN_FONT_FRAK:
2634         case LFUN_FONT_ITAL:
2635         case LFUN_FONT_SIZE:
2636         case LFUN_FONT_STATE:
2637         case LFUN_FONT_UNDERLINE:
2638         case LFUN_FONT_STRIKEOUT:
2639         case LFUN_FONT_UULINE:
2640         case LFUN_FONT_UWAVE:
2641         case LFUN_TEXTSTYLE_APPLY:
2642         case LFUN_TEXTSTYLE_UPDATE:
2643                 if (cur.inset().getLayout().isPassThru())
2644                         enable = false;
2645                 break;
2646         default:
2647                 break;
2648         }
2649
2650         flag.setEnabled(enable);
2651         return true;
2652 }
2653
2654
2655 void Text::pasteString(Cursor & cur, docstring const & clip,
2656                 bool asParagraphs)
2657 {
2658         cur.clearSelection();
2659         if (!clip.empty()) {
2660                 cur.recordUndo();
2661                 if (asParagraphs)
2662                         insertStringAsParagraphs(cur, clip, cur.current_font);
2663                 else
2664                         insertStringAsLines(cur, clip, cur.current_font);
2665         }
2666 }
2667
2668
2669 // FIXME: an item inset would make things much easier.
2670 bool Text::inDescriptionItem(Cursor & cur) const
2671 {
2672         Paragraph & par = cur.paragraph();
2673         pos_type const pos = cur.pos();
2674         pos_type const body_pos = par.beginOfBody();
2675
2676         if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
2677             && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
2678                 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
2679                 return false;
2680
2681         return (pos < body_pos
2682                 || (pos == body_pos
2683                     && (pos == 0 || par.getChar(pos - 1) != ' ')));
2684 }
2685
2686 } // namespace lyx