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