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