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