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