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