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