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