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