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