]> git.lyx.org Git - lyx.git/blob - src/Text3.cpp
Fix bug #5888. Yes, I do shame.
[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 set_buffers = 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                         set_buffers = 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                         set_buffers = false;
430                         break;
431                 }
432         }
433         if (set_buffers)
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                 cur.recordUndo();
1527                 insertInset(cur, inset);
1528                 cur.posForward();
1529                 break;
1530         }
1531         case LFUN_CAPTION_INSERT:
1532         case LFUN_FOOTNOTE_INSERT:
1533         case LFUN_NOTE_INSERT:
1534         case LFUN_FLEX_INSERT:
1535         case LFUN_BOX_INSERT:
1536         case LFUN_BRANCH_INSERT:
1537         case LFUN_PHANTOM_INSERT:
1538         case LFUN_ERT_INSERT:
1539         case LFUN_LISTING_INSERT:
1540         case LFUN_MARGINALNOTE_INSERT:
1541         case LFUN_OPTIONAL_INSERT:
1542         case LFUN_INDEX_INSERT:
1543                 // Open the inset, and move the current selection
1544                 // inside it.
1545                 doInsertInset(cur, this, cmd, true, true);
1546                 cur.posForward();
1547                 // Some insets are numbered, others are shown in the outline pane so
1548                 // let's update the labels and the toc backend.
1549                 bv->buffer().updateLabels();
1550                 break;
1551
1552         case LFUN_TABULAR_INSERT:
1553                 // if there were no arguments, just open the dialog
1554                 if (doInsertInset(cur, this, cmd, false, true))
1555                         cur.posForward();
1556                 else
1557                         bv->showDialog("tabularcreate");
1558
1559                 break;
1560
1561         case LFUN_FLOAT_INSERT:
1562         case LFUN_FLOAT_WIDE_INSERT:
1563         case LFUN_WRAP_INSERT: {
1564                 // will some text be moved into the inset?
1565                 bool content = cur.selection();
1566
1567                 doInsertInset(cur, this, cmd, true, true);
1568                 cur.posForward();
1569
1570                 // If some text is moved into the inset, doInsertInset 
1571                 // puts the cursor outside the inset. To insert the
1572                 // caption we put it back into the inset.
1573                 if (content)
1574                         cur.backwardPos();
1575
1576                 ParagraphList & pars = cur.text()->paragraphs();
1577
1578                 DocumentClass const & tclass = bv->buffer().params().documentClass();
1579
1580                 // add a separate paragraph for the caption inset
1581                 pars.push_back(Paragraph());
1582                 pars.back().setInsetOwner(&pars[0].inInset());
1583                 pars.back().setPlainOrDefaultLayout(tclass);
1584                 int cap_pit = pars.size() - 1;
1585
1586                 // if an empty inset was created, we create an additional empty
1587                 // paragraph at the bottom so that the user can choose where to put
1588                 // the graphics (or table).
1589                 if (!content) {
1590                         pars.push_back(Paragraph());
1591                         pars.back().setInsetOwner(&pars[0].inInset());
1592                         pars.back().setPlainOrDefaultLayout(tclass);
1593                 }
1594
1595                 // reposition the cursor to the caption
1596                 cur.pit() = cap_pit;
1597                 cur.pos() = 0;
1598                 // FIXME: This Text/Cursor dispatch handling is a mess!
1599                 // We cannot use Cursor::dispatch here it needs access to up to
1600                 // date metrics.
1601                 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1602                 doInsertInset(cur, cur.text(), cmd_caption, true, false);
1603                 bv->buffer().updateLabels();
1604                 cur.updateFlags(Update::Force);
1605                 // FIXME: When leaving the Float (or Wrap) inset we should
1606                 // delete any empty paragraph left above or below the
1607                 // caption.
1608                 break;
1609         }
1610
1611         case LFUN_NOMENCL_INSERT: {
1612                 InsetCommandParams p(NOMENCL_CODE);
1613                 if (cmd.argument().empty())
1614                         p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
1615                 else
1616                         p["symbol"] = cmd.argument();
1617                 string const data = InsetCommand::params2string("nomenclature", p);
1618                 bv->showDialog("nomenclature", data);
1619                 break;
1620         }
1621
1622         case LFUN_INDEX_PRINT: {
1623                 InsetCommandParams p(INDEX_PRINT_CODE);
1624                 if (cmd.argument().empty())
1625                         p["type"] = from_ascii("idx");
1626                 else
1627                         p["type"] = cmd.argument();
1628                 string const data = InsetCommand::params2string("index_print", p);
1629                 FuncRequest fr(LFUN_INSET_INSERT, data);
1630                 dispatch(cur, fr);
1631                 break;
1632         }
1633
1634         case LFUN_NOMENCL_PRINT:
1635         case LFUN_TOC_INSERT:
1636         case LFUN_LINE_INSERT:
1637         case LFUN_NEWPAGE_INSERT:
1638                 // do nothing fancy
1639                 doInsertInset(cur, this, cmd, false, false);
1640                 cur.posForward();
1641                 break;
1642
1643         case LFUN_DEPTH_DECREMENT:
1644                 changeDepth(cur, DEC_DEPTH);
1645                 break;
1646
1647         case LFUN_DEPTH_INCREMENT:
1648                 changeDepth(cur, INC_DEPTH);
1649                 break;
1650
1651         case LFUN_MATH_DISPLAY:
1652                 mathDispatch(cur, cmd, true);
1653                 break;
1654
1655         case LFUN_REGEXP_MODE:
1656                 regexpDispatch(cur, cmd);
1657                 break;
1658
1659         case LFUN_MATH_MODE:
1660                 if (cmd.argument() == "on")
1661                         // don't pass "on" as argument
1662                         mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1663                 else
1664                         mathDispatch(cur, cmd, false);
1665                 break;
1666
1667         case LFUN_MATH_MACRO:
1668                 if (cmd.argument().empty())
1669                         cur.errorMessage(from_utf8(N_("Missing argument")));
1670                 else {
1671                         string s = to_utf8(cmd.argument());
1672                         string const s1 = token(s, ' ', 1);
1673                         int const nargs = s1.empty() ? 0 : convert<int>(s1);
1674                         string const s2 = token(s, ' ', 2);
1675                         MacroType type = MacroTypeNewcommand;
1676                         if (s2 == "def")
1677                                 type = MacroTypeDef;
1678                         MathMacroTemplate * inset = new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, false, type);
1679                         inset->setBuffer(bv->buffer());
1680                         insertInset(cur, inset);
1681
1682                         // enter macro inset and select the name
1683                         cur.push(*inset);
1684                         cur.top().pos() = cur.top().lastpos();
1685                         cur.resetAnchor();
1686                         cur.setSelection(true);
1687                         cur.top().pos() = 0;
1688                 }
1689                 break;
1690
1691         // passthrough hat and underscore outside mathed:
1692         case LFUN_MATH_SUBSCRIPT:
1693                 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1694                 break;
1695         case LFUN_MATH_SUPERSCRIPT:
1696                 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1697                 break;
1698
1699         case LFUN_MATH_INSERT:
1700         case LFUN_MATH_MATRIX:
1701         case LFUN_MATH_DELIM:
1702         case LFUN_MATH_BIGDELIM: {
1703                 cur.recordUndo();
1704                 cap::replaceSelection(cur);
1705                 cur.insert(new InsetMathHull(hullSimple));
1706                 checkAndActivateInset(cur, true);
1707                 LASSERT(cur.inMathed(), /**/);
1708                 cur.dispatch(cmd);
1709                 break;
1710         }
1711
1712         case LFUN_FONT_EMPH: {
1713                 Font font(ignore_font, ignore_language);
1714                 font.fontInfo().setEmph(FONT_TOGGLE);
1715                 toggleAndShow(cur, this, font);
1716                 break;
1717         }
1718
1719         case LFUN_FONT_ITAL: {
1720                 Font font(ignore_font, ignore_language);
1721                 font.fontInfo().setShape(ITALIC_SHAPE);
1722                 toggleAndShow(cur, this, font);
1723                 break;
1724         }
1725
1726         case LFUN_FONT_BOLD:
1727         case LFUN_FONT_BOLDSYMBOL: {
1728                 Font font(ignore_font, ignore_language);
1729                 font.fontInfo().setSeries(BOLD_SERIES);
1730                 toggleAndShow(cur, this, font);
1731                 break;
1732         }
1733
1734         case LFUN_FONT_NOUN: {
1735                 Font font(ignore_font, ignore_language);
1736                 font.fontInfo().setNoun(FONT_TOGGLE);
1737                 toggleAndShow(cur, this, font);
1738                 break;
1739         }
1740
1741         case LFUN_FONT_TYPEWRITER: {
1742                 Font font(ignore_font, ignore_language);
1743                 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1744                 toggleAndShow(cur, this, font);
1745                 break;
1746         }
1747
1748         case LFUN_FONT_SANS: {
1749                 Font font(ignore_font, ignore_language);
1750                 font.fontInfo().setFamily(SANS_FAMILY);
1751                 toggleAndShow(cur, this, font);
1752                 break;
1753         }
1754
1755         case LFUN_FONT_ROMAN: {
1756                 Font font(ignore_font, ignore_language);
1757                 font.fontInfo().setFamily(ROMAN_FAMILY);
1758                 toggleAndShow(cur, this, font);
1759                 break;
1760         }
1761
1762         case LFUN_FONT_DEFAULT: {
1763                 Font font(inherit_font, ignore_language);
1764                 toggleAndShow(cur, this, font);
1765                 break;
1766         }
1767
1768         case LFUN_FONT_STRIKEOUT: {
1769                 Font font(ignore_font, ignore_language);
1770                 font.fontInfo().setStrikeout(FONT_TOGGLE);
1771                 toggleAndShow(cur, this, font);
1772                 break;
1773         }
1774
1775         case LFUN_FONT_UULINE: {
1776                 Font font(ignore_font, ignore_language);
1777                 font.fontInfo().setUuline(FONT_TOGGLE);
1778                 toggleAndShow(cur, this, font);
1779                 break;
1780         }
1781
1782         case LFUN_FONT_UWAVE: {
1783                 Font font(ignore_font, ignore_language);
1784                 font.fontInfo().setUwave(FONT_TOGGLE);
1785                 toggleAndShow(cur, this, font);
1786                 break;
1787         }
1788
1789         case LFUN_FONT_UNDERLINE: {
1790                 Font font(ignore_font, ignore_language);
1791                 font.fontInfo().setUnderbar(FONT_TOGGLE);
1792                 toggleAndShow(cur, this, font);
1793                 break;
1794         }
1795
1796         case LFUN_FONT_SIZE: {
1797                 Font font(ignore_font, ignore_language);
1798                 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1799                 toggleAndShow(cur, this, font);
1800                 break;
1801         }
1802
1803         case LFUN_LANGUAGE: {
1804                 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1805                 if (!lang)
1806                         break;
1807                 Font font(ignore_font, lang);
1808                 toggleAndShow(cur, this, font);
1809                 break;
1810         }
1811
1812         case LFUN_TEXTSTYLE_APPLY:
1813                 toggleAndShow(cur, this, freefont, toggleall);
1814                 cur.message(_("Character set"));
1815                 break;
1816
1817         // Set the freefont using the contents of \param data dispatched from
1818         // the frontends and apply it at the current cursor location.
1819         case LFUN_TEXTSTYLE_UPDATE: {
1820                 Font font;
1821                 bool toggle;
1822                 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1823                         freefont = font;
1824                         toggleall = toggle;
1825                         toggleAndShow(cur, this, freefont, toggleall);
1826                         cur.message(_("Character set"));
1827                 } else {
1828                         lyxerr << "Argument not ok";
1829                 }
1830                 break;
1831         }
1832
1833         case LFUN_FINISHED_LEFT:
1834                 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
1835                 // We're leaving an inset, going left. If the inset is LTR, we're
1836                 // leaving from the front, so we should not move (remain at --- but
1837                 // not in --- the inset). If the inset is RTL, move left, without
1838                 // entering the inset itself; i.e., move to after the inset.
1839                 if (cur.paragraph().getFontSettings(
1840                                 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1841                         cursorVisLeft(cur, true);
1842                 break;
1843
1844         case LFUN_FINISHED_RIGHT:
1845                 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
1846                 // We're leaving an inset, going right. If the inset is RTL, we're
1847                 // leaving from the front, so we should not move (remain at --- but
1848                 // not in --- the inset). If the inset is LTR, move right, without
1849                 // entering the inset itself; i.e., move to after the inset.
1850                 if (!cur.paragraph().getFontSettings(
1851                                 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1852                         cursorVisRight(cur, true);
1853                 break;
1854
1855         case LFUN_FINISHED_BACKWARD:
1856                 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
1857                 break;
1858
1859         case LFUN_FINISHED_FORWARD:
1860                 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
1861                 ++cur.pos();
1862                 cur.setCurrentFont();
1863                 break;
1864
1865         case LFUN_LAYOUT_PARAGRAPH: {
1866                 string data;
1867                 params2string(cur.paragraph(), data);
1868                 data = "show\n" + data;
1869                 bv->showDialog("paragraph", data);
1870                 break;
1871         }
1872
1873         case LFUN_PARAGRAPH_UPDATE: {
1874                 string data;
1875                 params2string(cur.paragraph(), data);
1876
1877                 // Will the paragraph accept changes from the dialog?
1878                 bool const accept =
1879                         cur.inset().allowParagraphCustomization(cur.idx());
1880
1881                 data = "update " + convert<string>(accept) + '\n' + data;
1882                 bv->updateDialog("paragraph", data);
1883                 break;
1884         }
1885
1886         case LFUN_ACCENT_UMLAUT:
1887         case LFUN_ACCENT_CIRCUMFLEX:
1888         case LFUN_ACCENT_GRAVE:
1889         case LFUN_ACCENT_ACUTE:
1890         case LFUN_ACCENT_TILDE:
1891         case LFUN_ACCENT_CEDILLA:
1892         case LFUN_ACCENT_MACRON:
1893         case LFUN_ACCENT_DOT:
1894         case LFUN_ACCENT_UNDERDOT:
1895         case LFUN_ACCENT_UNDERBAR:
1896         case LFUN_ACCENT_CARON:
1897         case LFUN_ACCENT_BREVE:
1898         case LFUN_ACCENT_TIE:
1899         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1900         case LFUN_ACCENT_CIRCLE:
1901         case LFUN_ACCENT_OGONEK:
1902                 theLyXFunc().handleKeyFunc(cmd.action);
1903                 if (!cmd.argument().empty())
1904                         // FIXME: Are all these characters encoded in one byte in utf8?
1905                         bv->translateAndInsert(cmd.argument()[0], this, cur);
1906                 break;
1907
1908         case LFUN_FLOAT_LIST_INSERT: {
1909                 DocumentClass const & tclass = bv->buffer().params().documentClass();
1910                 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1911                         cur.recordUndo();
1912                         if (cur.selection())
1913                                 cutSelection(cur, true, false);
1914                         breakParagraph(cur);
1915
1916                         if (cur.lastpos() != 0) {
1917                                 cursorBackward(cur);
1918                                 breakParagraph(cur);
1919                         }
1920
1921                         docstring const laystr = cur.inset().usePlainLayout() ?
1922                                 tclass.plainLayoutName() :
1923                                 tclass.defaultLayoutName();
1924                         setLayout(cur, laystr);
1925                         ParagraphParameters p;
1926                         // FIXME If this call were replaced with one to clearParagraphParams(),
1927                         // then we could get rid of this method altogether.
1928                         setParagraphs(cur, p);
1929                         // FIXME This should be simplified when InsetFloatList takes a
1930                         // Buffer in its constructor.
1931                         InsetFloatList * ifl = new InsetFloatList(to_utf8(cmd.argument()));
1932                         ifl->setBuffer(bv->buffer());
1933                         insertInset(cur, ifl);
1934                         cur.posForward();
1935                 } else {
1936                         lyxerr << "Non-existent float type: "
1937                                << to_utf8(cmd.argument()) << endl;
1938                 }
1939                 break;
1940         }
1941
1942         case LFUN_CHANGE_ACCEPT: {
1943                 acceptOrRejectChanges(cur, ACCEPT);
1944                 break;
1945         }
1946
1947         case LFUN_CHANGE_REJECT: {
1948                 acceptOrRejectChanges(cur, REJECT);
1949                 break;
1950         }
1951
1952         case LFUN_THESAURUS_ENTRY: {
1953                 docstring arg = cmd.argument();
1954                 if (arg.empty()) {
1955                         arg = cur.selectionAsString(false);
1956                         // FIXME
1957                         if (arg.size() > 100 || arg.empty()) {
1958                                 // Get word or selection
1959                                 selectWordWhenUnderCursor(cur, WHOLE_WORD);
1960                                 arg = cur.selectionAsString(false);
1961                                 arg += " lang=" + from_ascii(cur.getFont().language()->lang());
1962                         }
1963                 }
1964                 bv->showDialog("thesaurus", to_utf8(arg));
1965                 break;
1966         }
1967
1968         case LFUN_PARAGRAPH_PARAMS_APPLY: {
1969                 // Given data, an encoding of the ParagraphParameters
1970                 // generated in the Paragraph dialog, this function sets
1971                 // the current paragraph, or currently selected paragraphs,
1972                 // appropriately.
1973                 // NOTE: This function overrides all existing settings.
1974                 setParagraphs(cur, cmd.argument());
1975                 cur.message(_("Paragraph layout set"));
1976                 break;
1977         }
1978
1979         case LFUN_PARAGRAPH_PARAMS: {
1980                 // Given data, an encoding of the ParagraphParameters as we'd
1981                 // find them in a LyX file, this function modifies the current paragraph,
1982                 // or currently selected paragraphs.
1983                 // NOTE: This function only modifies, and does not override, existing
1984                 // settings.
1985                 setParagraphs(cur, cmd.argument(), true);
1986                 cur.message(_("Paragraph layout set"));
1987                 break;
1988         }
1989
1990         case LFUN_ESCAPE:
1991                 if (cur.selection()) {
1992                         cur.setSelection(false);
1993                 } else {
1994                         cur.undispatched();
1995                         // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
1996                         // correct, but I'm not 100% sure -- dov, 071019
1997                         cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1998                 }
1999                 break;
2000
2001         case LFUN_OUTLINE_UP:
2002                 outline(OutlineUp, cur);
2003                 setCursor(cur, cur.pit(), 0);
2004                 cur.buffer()->updateLabels();
2005                 needsUpdate = true;
2006                 break;
2007
2008         case LFUN_OUTLINE_DOWN:
2009                 outline(OutlineDown, cur);
2010                 setCursor(cur, cur.pit(), 0);
2011                 cur.buffer()->updateLabels();
2012                 needsUpdate = true;
2013                 break;
2014
2015         case LFUN_OUTLINE_IN:
2016                 outline(OutlineIn, cur);
2017                 cur.buffer()->updateLabels();
2018                 needsUpdate = true;
2019                 break;
2020
2021         case LFUN_OUTLINE_OUT:
2022                 outline(OutlineOut, cur);
2023                 cur.buffer()->updateLabels();
2024                 needsUpdate = true;
2025                 break;
2026
2027         default:
2028                 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2029                 cur.undispatched();
2030                 break;
2031         }
2032
2033         needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2034
2035         // FIXME: The cursor flag is reset two lines below
2036         // so we need to check here if some of the LFUN did touch that.
2037         // for now only Text::erase() and Text::backspace() do that.
2038         // The plan is to verify all the LFUNs and then to remove this
2039         // singleParUpdate boolean altogether.
2040         if (cur.result().update() & Update::Force) {
2041                 singleParUpdate = false;
2042                 needsUpdate = true;
2043         }
2044
2045         // FIXME: the following code should go in favor of fine grained
2046         // update flag treatment.
2047         if (singleParUpdate) {
2048                 // Inserting characters does not change par height in general. So, try
2049                 // to update _only_ this paragraph. BufferView will detect if a full
2050                 // metrics update is needed anyway.
2051                 cur.updateFlags(Update::SinglePar | Update::FitCursor);
2052                 return;
2053         }
2054
2055         if (!needsUpdate
2056             && &oldTopSlice.inset() == &cur.inset()
2057             && oldTopSlice.idx() == cur.idx()
2058             && !sel // sel is a backup of cur.selection() at the biginning of the function.
2059             && !cur.selection())
2060                 // FIXME: it would be better if we could just do this
2061                 //
2062                 //if (cur.result().update() != Update::FitCursor)
2063                 //      cur.noUpdate();
2064                 //
2065                 // But some LFUNs do not set Update::FitCursor when needed, so we
2066                 // do it for all. This is not very harmfull as FitCursor will provoke
2067                 // a full redraw only if needed but still, a proper review of all LFUN
2068                 // should be done and this needsUpdate boolean can then be removed.
2069                 cur.updateFlags(Update::FitCursor);
2070         else
2071                 cur.updateFlags(Update::Force | Update::FitCursor);
2072 }
2073
2074
2075 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2076                         FuncStatus & flag) const
2077 {
2078         LASSERT(cur.text() == this, /**/);
2079
2080         FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2081         bool enable = true;
2082         InsetCode code = NO_CODE;
2083
2084         switch (cmd.action) {
2085
2086         case LFUN_DEPTH_DECREMENT:
2087                 enable = changeDepthAllowed(cur, DEC_DEPTH);
2088                 break;
2089
2090         case LFUN_DEPTH_INCREMENT:
2091                 enable = changeDepthAllowed(cur, INC_DEPTH);
2092                 break;
2093
2094         case LFUN_APPENDIX:
2095                 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2096                 break;
2097
2098         case LFUN_DIALOG_SHOW_NEW_INSET:
2099                 if (cmd.argument() == "bibitem")
2100                         code = BIBITEM_CODE;
2101                 else if (cmd.argument() == "bibtex") {
2102                         code = BIBTEX_CODE;
2103                         // not allowed in description items
2104                         enable = !inDescriptionItem(cur);
2105                 }
2106                 else if (cmd.argument() == "box")
2107                         code = BOX_CODE;
2108                 else if (cmd.argument() == "branch")
2109                         code = BRANCH_CODE;
2110                 else if (cmd.argument() == "citation")
2111                         code = CITE_CODE;
2112                 else if (cmd.argument() == "ert")
2113                         code = ERT_CODE;
2114                 else if (cmd.argument() == "external")
2115                         code = EXTERNAL_CODE;
2116                 else if (cmd.argument() == "float")
2117                         code = FLOAT_CODE;
2118                 else if (cmd.argument() == "graphics")
2119                         code = GRAPHICS_CODE;
2120                 else if (cmd.argument() == "href")
2121                         code = HYPERLINK_CODE;
2122                 else if (cmd.argument() == "include")
2123                         code = INCLUDE_CODE;
2124                 else if (cmd.argument() == "index")
2125                         code = INDEX_CODE;
2126                 else if (cmd.argument() == "index_print")
2127                         code = INDEX_PRINT_CODE;
2128                 else if (cmd.argument() == "nomenclature")
2129                         code = NOMENCL_CODE;
2130                 else if (cmd.argument() == "label")
2131                         code = LABEL_CODE;
2132                 else if (cmd.argument() == "note")
2133                         code = NOTE_CODE;
2134                 else if (cmd.argument() == "phantom")
2135                         code = PHANTOM_CODE;
2136                 else if (cmd.argument() == "ref")
2137                         code = REF_CODE;
2138                 else if (cmd.argument() == "space")
2139                         code = SPACE_CODE;
2140                 else if (cmd.argument() == "toc")
2141                         code = TOC_CODE;
2142                 else if (cmd.argument() == "vspace")
2143                         code = VSPACE_CODE;
2144                 else if (cmd.argument() == "wrap")
2145                         code = WRAP_CODE;
2146                 else if (cmd.argument() == "listings")
2147                         code = LISTINGS_CODE;
2148                 break;
2149
2150         case LFUN_ERT_INSERT:
2151                 code = ERT_CODE;
2152                 break;
2153         case LFUN_LISTING_INSERT:
2154                 code = LISTINGS_CODE;
2155                 // not allowed in description items
2156                 enable = !inDescriptionItem(cur);
2157                 break;
2158         case LFUN_FOOTNOTE_INSERT:
2159                 code = FOOT_CODE;
2160                 break;
2161         case LFUN_TABULAR_INSERT:
2162                 code = TABULAR_CODE;
2163                 break;
2164         case LFUN_MARGINALNOTE_INSERT:
2165                 code = MARGIN_CODE;
2166                 break;
2167         case LFUN_FLOAT_INSERT:
2168         case LFUN_FLOAT_WIDE_INSERT:
2169                 code = FLOAT_CODE;
2170                 // not allowed in description items
2171                 enable = !inDescriptionItem(cur);
2172                 break;
2173         case LFUN_WRAP_INSERT:
2174                 code = WRAP_CODE;
2175                 // not allowed in description items
2176                 enable = !inDescriptionItem(cur);
2177                 break;
2178         case LFUN_FLOAT_LIST_INSERT:
2179                 code = FLOAT_LIST_CODE;
2180                 // not allowed in description items
2181                 enable = !inDescriptionItem(cur);
2182                 break;
2183         case LFUN_CAPTION_INSERT:
2184                 code = CAPTION_CODE;
2185                 // not allowed in description items
2186                 enable = !inDescriptionItem(cur);
2187                 break;
2188         case LFUN_NOTE_INSERT:
2189                 code = NOTE_CODE;
2190                 // in commands (sections etc.) and description items,
2191                 // only Notes are allowed
2192                 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
2193                           (!cur.paragraph().layout().isCommand()
2194                            && !inDescriptionItem(cur)));
2195                 break;
2196         case LFUN_FLEX_INSERT: {
2197                 code = FLEX_CODE;
2198                 string s = cmd.getArg(0);
2199                 InsetLayout il =
2200                         cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2201                 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2202                     il.lyxtype() != InsetLayout::CUSTOM &&
2203                     il.lyxtype() != InsetLayout::ELEMENT &&
2204                     il.lyxtype ()!= InsetLayout::STANDARD)
2205                         enable = false;
2206                 break;
2207                 }
2208         case LFUN_BOX_INSERT:
2209                 code = BOX_CODE;
2210                 break;
2211         case LFUN_BRANCH_INSERT:
2212                 code = BRANCH_CODE;
2213                 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2214                     && cur.buffer()->params().branchlist().empty())
2215                         enable = false;
2216                 break;
2217         case LFUN_PHANTOM_INSERT:
2218                 code = PHANTOM_CODE;
2219                 break;
2220         case LFUN_LABEL_INSERT:
2221                 code = LABEL_CODE;
2222                 break;
2223         case LFUN_INFO_INSERT:
2224                 code = INFO_CODE;
2225                 break;
2226         case LFUN_OPTIONAL_INSERT:
2227                 code = OPTARG_CODE;
2228                 enable = cur.paragraph().insetList().count(OPTARG_CODE)
2229                         < cur.paragraph().layout().optionalargs;
2230                 break;
2231         case LFUN_INDEX_INSERT:
2232                 code = INDEX_CODE;
2233                 break;
2234         case LFUN_INDEX_PRINT:
2235                 code = INDEX_PRINT_CODE;
2236                 // not allowed in description items
2237                 enable = !inDescriptionItem(cur);
2238                 break;
2239         case LFUN_NOMENCL_INSERT:
2240                 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2241                         enable = false;
2242                         break;
2243                 }
2244                 code = NOMENCL_CODE;
2245                 break;
2246         case LFUN_NOMENCL_PRINT:
2247                 code = NOMENCL_PRINT_CODE;
2248                 // not allowed in description items
2249                 enable = !inDescriptionItem(cur);
2250                 break;
2251         case LFUN_TOC_INSERT:
2252                 code = TOC_CODE;
2253                 // not allowed in description items
2254                 enable = !inDescriptionItem(cur);
2255                 break;
2256         case LFUN_HYPERLINK_INSERT:
2257                 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2258                         enable = false;
2259                         break;
2260                 }
2261                 code = HYPERLINK_CODE;
2262                 break;
2263         case LFUN_QUOTE_INSERT:
2264                 // always allow this, since we will inset a raw quote
2265                 // if an inset is not allowed.
2266                 break;
2267         case LFUN_SPECIALCHAR_INSERT:
2268                 code = SPECIALCHAR_CODE;
2269                 break;
2270         case LFUN_SPACE_INSERT:
2271                 // slight hack: we know this is allowed in math mode
2272                 if (cur.inTexted())
2273                         code = SPACE_CODE;
2274                 break;
2275
2276         case LFUN_INSET_MODIFY:
2277                 // We need to disable this, because we may get called for a
2278                 // tabular cell via
2279                 // InsetTabular::getStatus() -> InsetText::getStatus()
2280                 // and we don't handle LFUN_INSET_MODIFY.
2281                 enable = false;
2282                 break;
2283
2284         case LFUN_FONT_EMPH:
2285                 flag.setOnOff(fontinfo.emph() == FONT_ON);
2286                 break;
2287
2288         case LFUN_FONT_ITAL:
2289                 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
2290                 break;
2291
2292         case LFUN_FONT_NOUN:
2293                 flag.setOnOff(fontinfo.noun() == FONT_ON);
2294                 break;
2295
2296         case LFUN_FONT_BOLD:
2297         case LFUN_FONT_BOLDSYMBOL:
2298                 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2299                 break;
2300
2301         case LFUN_FONT_SANS:
2302                 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2303                 break;
2304
2305         case LFUN_FONT_ROMAN:
2306                 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2307                 break;
2308
2309         case LFUN_FONT_TYPEWRITER:
2310                 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2311                 break;
2312
2313         case LFUN_CUT:
2314         case LFUN_COPY:
2315                 enable = cur.selection();
2316                 break;
2317
2318         case LFUN_PASTE: {
2319                 if (cmd.argument().empty()) {
2320                         if (theClipboard().isInternal())
2321                                 enable = cap::numberOfSelections() > 0;
2322                         else
2323                                 enable = !theClipboard().empty();
2324                         break;
2325                 }
2326
2327                 // we have an argument
2328                 string const arg = to_utf8(cmd.argument());
2329                 if (isStrUnsignedInt(arg)) {
2330                         // it's a number and therefore means the internal stack
2331                         unsigned int n = convert<unsigned int>(arg);
2332                         enable = cap::numberOfSelections() > n;
2333                         break;
2334                 }
2335
2336                 // explicit graphics type?
2337                 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
2338                 if ((arg == "pdf" && (type = Clipboard::PdfGraphicsType))
2339                           || (arg == "png" && (type = Clipboard::PngGraphicsType))
2340                           || (arg == "jpeg" && (type = Clipboard::JpegGraphicsType))
2341                           || (arg == "linkback" &&  (type = Clipboard::LinkBackGraphicsType))
2342                           || (arg == "emf" &&  (type = Clipboard::EmfGraphicsType))
2343                           || (arg == "wmf" &&  (type = Clipboard::WmfGraphicsType))) {
2344                         enable = theClipboard().hasGraphicsContents(type);
2345                         break;
2346                 }
2347
2348                 // unknown argument
2349                 enable = false;
2350                 break;
2351          }
2352
2353         case LFUN_CLIPBOARD_PASTE:
2354                 enable = !theClipboard().empty();
2355                 break;
2356
2357         case LFUN_PRIMARY_SELECTION_PASTE:
2358                 enable = cur.selection() || !theSelection().empty();
2359                 break;
2360
2361         case LFUN_SELECTION_PASTE:
2362                 enable = cap::selection();
2363                 break;
2364
2365         case LFUN_PARAGRAPH_MOVE_UP:
2366                 enable = cur.pit() > 0 && !cur.selection();
2367                 break;
2368
2369         case LFUN_PARAGRAPH_MOVE_DOWN:
2370                 enable = cur.pit() < cur.lastpit() && !cur.selection();
2371                 break;
2372
2373         case LFUN_INSET_DISSOLVE:
2374                 if (!cmd.argument().empty()) {
2375                         InsetLayout const & il = cur.inset().getLayout(cur.buffer()->params());
2376                         InsetLayout::InsetLyXType const type = 
2377                                         translateLyXType(to_utf8(cmd.argument()));
2378                         enable = cur.inset().lyxCode() == FLEX_CODE
2379                                  && il.lyxtype() == type;
2380                 } else {
2381                         enable = ((!isMainText(cur.bv().buffer())
2382                                       && cur.inset().nargs() == 1)
2383                                   || (cur.nextInset()
2384                                       && cur.nextInset()->nargs() == 1));
2385                 }
2386                 break;
2387
2388         case LFUN_CHANGE_ACCEPT:
2389         case LFUN_CHANGE_REJECT:
2390                 // In principle, these LFUNs should only be enabled if there
2391                 // is a change at the current position/in the current selection.
2392                 // However, without proper optimizations, this will inevitably
2393                 // result in unacceptable performance - just imagine a user who
2394                 // wants to select the complete content of a long document.
2395                 if (!cur.selection()) {
2396                         Change const & change = cur.paragraph().lookupChange(cur.pos());
2397                         enable = change.changed();
2398                 } else
2399                         // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
2400                         // for selections.
2401                         enable = true;
2402                 break;
2403
2404         case LFUN_OUTLINE_UP:
2405         case LFUN_OUTLINE_DOWN:
2406         case LFUN_OUTLINE_IN:
2407         case LFUN_OUTLINE_OUT:
2408                 // FIXME: LyX is not ready for outlining within inset.
2409                 enable = isMainText(cur.bv().buffer())
2410                         && cur.paragraph().layout().toclevel != Layout::NOT_IN_TOC;
2411                 break;
2412
2413         case LFUN_NEWLINE_INSERT:
2414                 // LaTeX restrictions (labels or empty par)
2415                 enable = (cur.pos() > cur.paragraph().beginOfBody());
2416                 break;
2417
2418         case LFUN_SET_GRAPHICS_GROUP: {
2419                 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
2420                 if (!ins)
2421                         enable = false;
2422                 else
2423                         flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
2424                 break;
2425         }
2426
2427         case LFUN_NEWPAGE_INSERT:
2428                 // not allowed in description items
2429                 enable = !inDescriptionItem(cur);
2430                 break;
2431
2432         case LFUN_MATH_INSERT:
2433         case LFUN_MATH_MATRIX:
2434         case LFUN_MATH_DELIM:
2435         case LFUN_MATH_BIGDELIM:
2436                 // not allowed in ERT, for example.
2437                 enable = cur.inset().insetAllowed(MATH_CODE);
2438                 break;
2439
2440         case LFUN_DATE_INSERT: {
2441                 string const format = cmd.argument().empty()
2442                         ? lyxrc.date_insert_format : to_utf8(cmd.argument());
2443                 enable = support::os::is_valid_strftime(format);
2444                 break;
2445         }
2446
2447         case LFUN_WORD_DELETE_FORWARD:
2448         case LFUN_WORD_DELETE_BACKWARD:
2449         case LFUN_LINE_DELETE:
2450         case LFUN_WORD_FORWARD:
2451         case LFUN_WORD_BACKWARD:
2452         case LFUN_WORD_RIGHT:
2453         case LFUN_WORD_LEFT:
2454         case LFUN_CHAR_FORWARD:
2455         case LFUN_CHAR_FORWARD_SELECT:
2456         case LFUN_CHAR_BACKWARD:
2457         case LFUN_CHAR_BACKWARD_SELECT:
2458         case LFUN_CHAR_LEFT:
2459         case LFUN_CHAR_LEFT_SELECT:
2460         case LFUN_CHAR_RIGHT:
2461         case LFUN_CHAR_RIGHT_SELECT:
2462         case LFUN_UP:
2463         case LFUN_UP_SELECT:
2464         case LFUN_DOWN:
2465         case LFUN_DOWN_SELECT:
2466         case LFUN_PARAGRAPH_UP_SELECT:
2467         case LFUN_PARAGRAPH_DOWN_SELECT:
2468         case LFUN_LINE_BEGIN_SELECT:
2469         case LFUN_LINE_END_SELECT:
2470         case LFUN_WORD_FORWARD_SELECT:
2471         case LFUN_WORD_BACKWARD_SELECT:
2472         case LFUN_WORD_RIGHT_SELECT:
2473         case LFUN_WORD_LEFT_SELECT:
2474         case LFUN_WORD_SELECT:
2475         case LFUN_SECTION_SELECT:
2476         case LFUN_PARAGRAPH_UP:
2477         case LFUN_PARAGRAPH_DOWN:
2478         case LFUN_LINE_BEGIN:
2479         case LFUN_LINE_END:
2480         case LFUN_CHAR_DELETE_FORWARD:
2481         case LFUN_CHAR_DELETE_BACKWARD:
2482         case LFUN_BREAK_PARAGRAPH:
2483         case LFUN_PARAGRAPH_SPACING:
2484         case LFUN_INSET_INSERT:
2485         case LFUN_WORD_UPCASE:
2486         case LFUN_WORD_LOWCASE:
2487         case LFUN_WORD_CAPITALIZE:
2488         case LFUN_CHARS_TRANSPOSE:
2489         case LFUN_SERVER_GET_XY:
2490         case LFUN_SERVER_SET_XY:
2491         case LFUN_SERVER_GET_LAYOUT:
2492         case LFUN_LAYOUT:
2493         case LFUN_SELF_INSERT:
2494         case LFUN_LINE_INSERT:
2495         case LFUN_MATH_DISPLAY:
2496         case LFUN_MATH_MODE:
2497         case LFUN_MATH_MACRO:
2498         case LFUN_MATH_SUBSCRIPT:
2499         case LFUN_MATH_SUPERSCRIPT:
2500         case LFUN_FONT_DEFAULT:
2501         case LFUN_FONT_UNDERLINE:
2502         case LFUN_FONT_STRIKEOUT:
2503         case LFUN_FONT_UULINE:
2504         case LFUN_FONT_UWAVE:
2505         case LFUN_FONT_SIZE:
2506         case LFUN_LANGUAGE:
2507         case LFUN_TEXTSTYLE_APPLY:
2508         case LFUN_TEXTSTYLE_UPDATE:
2509         case LFUN_LAYOUT_PARAGRAPH:
2510         case LFUN_PARAGRAPH_UPDATE:
2511         case LFUN_ACCENT_UMLAUT:
2512         case LFUN_ACCENT_CIRCUMFLEX:
2513         case LFUN_ACCENT_GRAVE:
2514         case LFUN_ACCENT_ACUTE:
2515         case LFUN_ACCENT_TILDE:
2516         case LFUN_ACCENT_CEDILLA:
2517         case LFUN_ACCENT_MACRON:
2518         case LFUN_ACCENT_DOT:
2519         case LFUN_ACCENT_UNDERDOT:
2520         case LFUN_ACCENT_UNDERBAR:
2521         case LFUN_ACCENT_CARON:
2522         case LFUN_ACCENT_BREVE:
2523         case LFUN_ACCENT_TIE:
2524         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2525         case LFUN_ACCENT_CIRCLE:
2526         case LFUN_ACCENT_OGONEK:
2527         case LFUN_THESAURUS_ENTRY:
2528         case LFUN_PARAGRAPH_PARAMS_APPLY:
2529         case LFUN_PARAGRAPH_PARAMS:
2530         case LFUN_ESCAPE:
2531         case LFUN_BUFFER_BEGIN:
2532         case LFUN_BUFFER_END:
2533         case LFUN_BUFFER_BEGIN_SELECT:
2534         case LFUN_BUFFER_END_SELECT:
2535         case LFUN_INSET_BEGIN:
2536         case LFUN_INSET_END:
2537         case LFUN_INSET_BEGIN_SELECT:
2538         case LFUN_INSET_END_SELECT:
2539         case LFUN_UNICODE_INSERT:
2540                 // these are handled in our dispatch()
2541                 enable = true;
2542                 break;
2543
2544         default:
2545                 return false;
2546         }
2547
2548         if (code != NO_CODE
2549             && (cur.empty() || !cur.inset().insetAllowed(code)))
2550                 enable = false;
2551
2552         flag.setEnabled(enable);
2553         return true;
2554 }
2555
2556
2557 void Text::pasteString(Cursor & cur, docstring const & clip,
2558                 bool asParagraphs)
2559 {
2560         cur.clearSelection();
2561         if (!clip.empty()) {
2562                 cur.recordUndo();
2563                 if (asParagraphs)
2564                         insertStringAsParagraphs(cur, clip);
2565                 else
2566                         insertStringAsLines(cur, clip);
2567         }
2568 }
2569
2570
2571 // FIXME: an item inset would make things much easier.
2572 bool Text::inDescriptionItem(Cursor & cur) const
2573 {
2574         Paragraph & par = cur.paragraph();
2575         pos_type const pos = cur.pos();
2576         pos_type const body_pos = par.beginOfBody();
2577
2578         if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
2579             && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
2580                 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
2581                 return false;
2582
2583         return (pos < body_pos
2584                 || (pos == body_pos
2585                     && (pos == 0 || par.getChar(pos - 1) != ' ')));
2586 }
2587
2588 } // namespace lyx