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