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