]> git.lyx.org Git - features.git/blob - src/Text3.cpp
* src/BufferView.cpp:
[features.git] / src / Text3.cpp
1 /**
2  * \file text3.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author Lars Gullik Bjønnes
8  * \author Alfredo Braunstein
9  * \author Angus Leeming
10  * \author John Levon
11  * \author André Pönitz
12  *
13  * Full author contact details are available in file CREDITS.
14  */
15
16 #include <config.h>
17
18 #include "Text.h"
19
20 #include "Bidi.h"
21 #include "BranchList.h"
22 #include "FloatList.h"
23 #include "FuncStatus.h"
24 #include "Buffer.h"
25 #include "buffer_funcs.h"
26 #include "BufferParams.h"
27 #include "BufferView.h"
28 #include "Cursor.h"
29 #include "CutAndPaste.h"
30 #include "debug.h"
31 #include "DispatchResult.h"
32 #include "ErrorList.h"
33 #include "factory.h"
34 #include "FuncRequest.h"
35 #include "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 "TextMetrics.h"
48 #include "VSpace.h"
49 #include "ParIterator.h"
50
51 #include "frontends/Clipboard.h"
52 #include "frontends/Selection.h"
53
54 #include "insets/InsetCollapsable.h"
55 #include "insets/InsetCommand.h"
56 #include "insets/InsetFloatList.h"
57 #include "insets/InsetNewline.h"
58 #include "insets/InsetQuotes.h"
59 #include "insets/InsetSpecialChar.h"
60 #include "insets/InsetText.h"
61 #include "insets/InsetInfo.h"
62
63 #include "support/lstrings.h"
64 #include "support/lyxlib.h"
65 #include "support/convert.h"
66 #include "support/lyxtime.h"
67
68 #include "mathed/InsetMathHull.h"
69 #include "mathed/MathMacroTemplate.h"
70
71 #include <boost/current_function.hpp>
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);
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 << endl;
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                 //lyxerr << BOOST_CURRENT_FUNCTION
510                 //       << " LFUN_CHAR_FORWARD[SEL]:\n" << cur << endl;
511                 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
512                 needsUpdate |= cursorForward(cur);
513
514                 if (!needsUpdate && oldTopSlice == cur.top()
515                                 && cur.boundary() == oldBoundary) {
516                         cur.undispatched();
517                         cmd = FuncRequest(LFUN_FINISHED_FORWARD);
518                 }
519                 break;
520
521         case LFUN_CHAR_BACKWARD:
522         case LFUN_CHAR_BACKWARD_SELECT:
523                 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
524                 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
525                 needsUpdate |= cursorBackward(cur);
526
527                 if (!needsUpdate && oldTopSlice == cur.top()
528                         && cur.boundary() == oldBoundary) {
529                         cur.undispatched();
530                         cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
531                 }
532                 break;
533
534         case LFUN_CHAR_LEFT:
535         case LFUN_CHAR_LEFT_SELECT:
536                 //FIXME: for visual cursor, really move left
537                 if (reverseDirectionNeeded(cur)) {
538                         lyx::dispatch(FuncRequest(
539                                 cmd.action == LFUN_CHAR_LEFT_SELECT ? 
540                                         LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
541                 } else {
542                         lyx::dispatch(FuncRequest(
543                                 cmd.action == LFUN_CHAR_LEFT_SELECT ? 
544                                         LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
545                 }
546                 break;
547
548         case LFUN_CHAR_RIGHT:
549         case LFUN_CHAR_RIGHT_SELECT:
550                 //FIXME: for visual cursor, really move right
551                 if (reverseDirectionNeeded(cur)) {
552                         lyx::dispatch(FuncRequest(
553                                 cmd.action == LFUN_CHAR_RIGHT_SELECT ? 
554                                         LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
555                 } else {
556                         lyx::dispatch(FuncRequest(
557                                 cmd.action == LFUN_CHAR_RIGHT_SELECT ? 
558                                         LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
559                 }
560                 break;
561
562         case LFUN_UP_SELECT:
563         case LFUN_DOWN_SELECT:
564         case LFUN_UP:
565         case LFUN_DOWN: {
566                 // stop/start the selection
567                 bool select = cmd.action == LFUN_DOWN_SELECT ||
568                         cmd.action == LFUN_UP_SELECT;
569                 cur.selHandle(select);
570                 
571                 // move cursor up/down
572                 bool up = cmd.action == LFUN_UP_SELECT || cmd.action == LFUN_UP;
573                 bool const successful = cur.upDownInText(up, needsUpdate);
574                 if (successful) {
575                         // notify insets which were left and get their update flags 
576                         notifyCursorLeaves(cur.beforeDispatchCursor(), cur);
577                         cur.fixIfBroken();
578                         
579                         // redraw if you leave mathed (for the decorations)
580                         needsUpdate |= cur.beforeDispatchCursor().inMathed();
581                 } else
582                         cur.undispatched();
583                 
584                 break;
585         }
586
587         case LFUN_PARAGRAPH_UP:
588         case LFUN_PARAGRAPH_UP_SELECT:
589                 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
590                 needsUpdate |= cursorUpParagraph(cur);
591                 break;
592
593         case LFUN_PARAGRAPH_DOWN:
594         case LFUN_PARAGRAPH_DOWN_SELECT:
595                 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
596                 needsUpdate |= cursorDownParagraph(cur);
597                 break;
598
599         case LFUN_SCREEN_UP_SELECT:
600                 needsUpdate |= cur.selHandle(true);
601                 if (cur.pit() == 0 && cur.textRow().pos() == 0)
602                         cur.undispatched();
603                 else {
604                         tm.cursorPrevious(cur);
605                 }
606                 break;
607
608         case LFUN_SCREEN_DOWN_SELECT:
609                 needsUpdate |= cur.selHandle(true);
610                 if (cur.pit() == cur.lastpit()
611                           && cur.textRow().endpos() == cur.lastpos())
612                         cur.undispatched();
613                 else {
614                         tm.cursorNext(cur);
615                 }
616                 break;
617
618         case LFUN_LINE_BEGIN:
619         case LFUN_LINE_BEGIN_SELECT:
620                 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_BEGIN_SELECT);
621                 needsUpdate |= tm.cursorHome(cur);
622                 break;
623
624         case LFUN_LINE_END:
625         case LFUN_LINE_END_SELECT:
626                 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
627                 needsUpdate |= tm.cursorEnd(cur);
628                 break;
629
630         case LFUN_WORD_RIGHT:
631         case LFUN_WORD_RIGHT_SELECT:
632                 //FIXME: for visual cursor mode, really move right
633                 if (reverseDirectionNeeded(cur)) {
634                         lyx::dispatch(FuncRequest(
635                                 cmd.action == LFUN_WORD_RIGHT_SELECT ?
636                                         LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD));
637                 } else {
638                         lyx::dispatch(FuncRequest(
639                                 cmd.action == LFUN_WORD_RIGHT_SELECT ?
640                                         LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD));
641                 }
642                 break;
643
644         case LFUN_WORD_FORWARD:
645         case LFUN_WORD_FORWARD_SELECT:
646                 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT);
647                 needsUpdate |= cursorForwardOneWord(cur);
648                 break;
649
650         case LFUN_WORD_LEFT:
651         case LFUN_WORD_LEFT_SELECT:
652                 //FIXME: for visual cursor mode, really move left
653                 if (reverseDirectionNeeded(cur)) {
654                         lyx::dispatch(FuncRequest(
655                                 cmd.action == LFUN_WORD_LEFT_SELECT ?
656                                         LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD));
657                 } else {
658                         lyx::dispatch(FuncRequest(
659                                 cmd.action == LFUN_WORD_LEFT_SELECT ?
660                                         LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD));
661                 }
662                 break;
663
664         case LFUN_WORD_BACKWARD:
665         case LFUN_WORD_BACKWARD_SELECT:
666                 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT);
667                 needsUpdate |= cursorBackwardOneWord(cur);
668                 break;
669
670         case LFUN_WORD_SELECT: {
671                 selectWord(cur, WHOLE_WORD);
672                 finishChange(cur, true);
673                 break;
674         }
675
676         case LFUN_BREAK_LINE: {
677                 // Not allowed by LaTeX (labels or empty par)
678                 if (cur.pos() > cur.paragraph().beginOfBody()) {
679                         // this avoids a double undo
680                         // FIXME: should not be needed, ideally
681                         if (!cur.selection())
682                                 cur.recordUndo();
683                         cap::replaceSelection(cur);
684                         cur.insert(new InsetNewline);
685                         cur.posForward();
686                         moveCursor(cur, false);
687                 }
688                 break;
689         }
690
691         case LFUN_CHAR_DELETE_FORWARD:
692                 if (!cur.selection()) {
693                         if (cur.pos() == cur.paragraph().size())
694                                 // Par boundary, force full-screen update
695                                 singleParUpdate = false;
696                         needsUpdate |= erase(cur);
697                         cur.resetAnchor();
698                         // It is possible to make it a lot faster still
699                         // just comment out the line below...
700                 } else {
701                         cutSelection(cur, true, false);
702                         singleParUpdate = false;
703                 }
704                 moveCursor(cur, false);
705                 break;
706
707         case LFUN_DELETE_FORWARD_SKIP:
708                 // Reverse the effect of LFUN_BREAK_PARAGRAPH_SKIP.
709                 if (!cur.selection()) {
710                         if (cur.pos() == cur.lastpos()) {
711                                 cursorForward(cur);
712                                 cursorBackward(cur);
713                         }
714                         erase(cur);
715                         cur.resetAnchor();
716                 } else {
717                         cutSelection(cur, true, false);
718                 }
719                 break;
720
721
722         case LFUN_CHAR_DELETE_BACKWARD:
723                 if (!cur.selection()) {
724                         if (bv->getIntl().getTransManager().backspace()) {
725                                 // Par boundary, full-screen update
726                                 if (cur.pos() == 0)
727                                         singleParUpdate = false;
728                                 needsUpdate |= backspace(cur);
729                                 cur.resetAnchor();
730                                 // It is possible to make it a lot faster still
731                                 // just comment out the line below...
732                         }
733                 } else {
734                         cutSelection(cur, true, false);
735                         singleParUpdate = false;
736                 }
737                 break;
738
739         case LFUN_DELETE_BACKWARD_SKIP:
740                 // Reverse the effect of LFUN_BREAK_PARAGRAPH_SKIP.
741                 if (!cur.selection()) {
742                         // FIXME: look here
743                         //CursorSlice cur = cursor();
744                         backspace(cur);
745                         //anchor() = cur;
746                 } else {
747                         cutSelection(cur, true, false);
748                 }
749                 break;
750
751         case LFUN_BREAK_PARAGRAPH:
752                 cap::replaceSelection(cur);
753                 breakParagraph(cur, cmd.argument() == "inverse");
754                 cur.resetAnchor();
755                 break;
756
757         case LFUN_BREAK_PARAGRAPH_SKIP: {
758                 // When at the beginning of a paragraph, remove
759                 // indentation.  Otherwise, do the same as LFUN_BREAK_PARAGRAPH.
760                 cap::replaceSelection(cur);
761                 if (cur.pos() == 0)
762                         cur.paragraph().params().labelWidthString(docstring());
763                 else
764                         breakParagraph(cur, false);
765                 cur.resetAnchor();
766                 break;
767         }
768
769         // TODO
770         // With the creation of LFUN_PARAGRAPH_PARAMS, this is now redundant,
771         // as its duties can be performed there. Should it be removed??
772         // FIXME For now, it can just dispatch LFUN_PARAGRAPH_PARAMS...
773         case LFUN_PARAGRAPH_SPACING: {
774                 Paragraph & par = cur.paragraph();
775                 Spacing::Space cur_spacing = par.params().spacing().getSpace();
776                 string cur_value = "1.0";
777                 if (cur_spacing == Spacing::Other)
778                         cur_value = par.params().spacing().getValueAsString();
779
780                 istringstream is(to_utf8(cmd.argument()));
781                 string tmp;
782                 is >> tmp;
783                 Spacing::Space new_spacing = cur_spacing;
784                 string new_value = cur_value;
785                 if (tmp.empty()) {
786                         lyxerr << "Missing argument to `paragraph-spacing'"
787                                << endl;
788                 } else if (tmp == "single") {
789                         new_spacing = Spacing::Single;
790                 } else if (tmp == "onehalf") {
791                         new_spacing = Spacing::Onehalf;
792                 } else if (tmp == "double") {
793                         new_spacing = Spacing::Double;
794                 } else if (tmp == "other") {
795                         new_spacing = Spacing::Other;
796                         string tmpval = "0.0";
797                         is >> tmpval;
798                         lyxerr << "new_value = " << tmpval << endl;
799                         if (tmpval != "0.0")
800                                 new_value = tmpval;
801                 } else if (tmp == "default") {
802                         new_spacing = Spacing::Default;
803                 } else {
804                         lyxerr << to_utf8(_("Unknown spacing argument: "))
805                                << to_utf8(cmd.argument()) << endl;
806                 }
807                 if (cur_spacing != new_spacing || cur_value != new_value)
808                         par.params().spacing(Spacing(new_spacing, new_value));
809                 break;
810         }
811
812         case LFUN_INSET_INSERT: {
813                 cur.recordUndo();
814                 Inset * inset = createInset(bv->buffer(), cmd);
815                 if (inset) {
816                         // FIXME (Abdel 01/02/2006):
817                         // What follows would be a partial fix for bug 2154:
818                         //   http://bugzilla.lyx.org/show_bug.cgi?id=2154
819                         // This automatically put the label inset _after_ a
820                         // numbered section. It should be possible to extend the mechanism
821                         // to any kind of LateX environement.
822                         // The correct way to fix that bug would be at LateX generation.
823                         // I'll let the code here for reference as it could be used for some
824                         // other feature like "automatic labelling".
825                         /*
826                         Paragraph & par = pars_[cur.pit()];
827                         if (inset->lyxCode() == LABEL_CODE
828                                 && par.layout()->labeltype == LABEL_COUNTER) {
829                                 // Go to the end of the paragraph
830                                 // Warning: Because of Change-Tracking, the last
831                                 // position is 'size()' and not 'size()-1':
832                                 cur.pos() = par.size();
833                                 // Insert a new paragraph
834                                 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
835                                 dispatch(cur, fr);
836                         }
837                         */
838                         if (cur.selection())
839                                 cutSelection(cur, true, false);
840                         insertInset(cur, inset);
841                         cur.posForward();
842                 }
843                 break;
844         }
845
846         case LFUN_INSET_DISSOLVE:
847                 needsUpdate |= dissolveInset(cur);
848                 break;
849
850         case LFUN_INSET_SETTINGS:
851                 cur.inset().showInsetDialog(bv);
852                 break;
853
854         case LFUN_SPACE_INSERT:
855                 if (cur.paragraph().layout()->free_spacing)
856                         insertChar(cur, ' ');
857                 else {
858                         doInsertInset(cur, this, cmd, false, false);
859                         cur.posForward();
860                 }
861                 moveCursor(cur, false);
862                 break;
863
864         case LFUN_HYPHENATION_POINT_INSERT:
865                 specialChar(cur, InsetSpecialChar::HYPHENATION);
866                 break;
867
868         case LFUN_LIGATURE_BREAK_INSERT:
869                 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
870                 break;
871
872         case LFUN_DOTS_INSERT:
873                 specialChar(cur, InsetSpecialChar::LDOTS);
874                 break;
875
876         case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
877                 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
878                 break;
879
880         case LFUN_MENU_SEPARATOR_INSERT:
881                 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
882                 break;
883
884         case LFUN_WORD_UPCASE:
885                 changeCase(cur, text_uppercase);
886                 break;
887
888         case LFUN_WORD_LOWCASE:
889                 changeCase(cur, text_lowercase);
890                 break;
891
892         case LFUN_WORD_CAPITALIZE:
893                 changeCase(cur, text_capitalization);
894                 break;
895
896         case LFUN_CHARS_TRANSPOSE:
897                 charsTranspose(cur);
898                 break;
899
900         case LFUN_PASTE:
901                 cur.message(_("Paste"));
902                 cap::replaceSelection(cur);
903                 if (cmd.argument().empty() && !theClipboard().isInternal())
904                         pasteClipboard(cur, bv->buffer().errorList("Paste"));
905                 else {
906                         string const arg(to_utf8(cmd.argument()));
907                         pasteFromStack(cur, bv->buffer().errorList("Paste"),
908                                         isStrUnsignedInt(arg) ?
909                                                 convert<unsigned int>(arg) :
910                                                 0);
911                 }
912                 bv->buffer().errors("Paste");
913                 cur.clearSelection(); // bug 393
914                 cur.finishUndo();
915                 break;
916
917         case LFUN_CUT:
918                 cutSelection(cur, true, true);
919                 cur.message(_("Cut"));
920                 break;
921
922         case LFUN_COPY:
923                 copySelection(cur);
924                 cur.message(_("Copy"));
925                 break;
926
927         case LFUN_SERVER_GET_XY:
928                 cur.message(from_utf8(
929                         convert<string>(tm.cursorX(cur.top(), cur.boundary()))
930                         + ' ' + convert<string>(tm.cursorY(cur.top(), cur.boundary()))));
931                 break;
932
933         case LFUN_SERVER_SET_XY: {
934                 int x = 0;
935                 int y = 0;
936                 istringstream is(to_utf8(cmd.argument()));
937                 is >> x >> y;
938                 if (!is)
939                         lyxerr << "SETXY: Could not parse coordinates in '"
940                                << to_utf8(cmd.argument()) << std::endl;
941                 else
942                         tm.setCursorFromCoordinates(cur, x, y);
943                 break;
944         }
945
946         case LFUN_SERVER_GET_FONT:
947                 if (cur.current_font.fontInfo().shape() == ITALIC_SHAPE)
948                         cur.message(from_ascii("E"));
949                 else if (cur.current_font.fontInfo().shape() == SMALLCAPS_SHAPE)
950                         cur.message(from_ascii("N"));
951                 else
952                         cur.message(from_ascii("0"));
953                 break;
954
955         case LFUN_SERVER_GET_LAYOUT:
956                 cur.message(cur.paragraph().layout()->name());
957                 break;
958
959         case LFUN_LAYOUT: {
960                 docstring layout = cmd.argument();
961                 LYXERR(Debug::INFO) << "LFUN_LAYOUT: (arg) " << to_utf8(layout) << endl;
962
963                 docstring const old_layout = cur.paragraph().layout()->name();
964
965                 // Derive layout number from given argument (string)
966                 // and current buffer's textclass (number)
967                 TextClass const & tclass = bv->buffer().params().getTextClass();
968                 if (layout.empty())
969                         layout = tclass.defaultLayoutName();
970                 bool hasLayout = tclass.hasLayout(layout);
971
972                 // If the entry is obsolete, use the new one instead.
973                 if (hasLayout) {
974                         docstring const & obs = tclass[layout]->obsoleted_by();
975                         if (!obs.empty())
976                                 layout = obs;
977                 }
978
979                 if (!hasLayout) {
980                         cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
981                                 from_utf8(N_(" not known")));
982                         break;
983                 }
984
985                 bool change_layout = (old_layout != layout);
986
987                 if (!change_layout && cur.selection() &&
988                         cur.selBegin().pit() != cur.selEnd().pit())
989                 {
990                         pit_type spit = cur.selBegin().pit();
991                         pit_type epit = cur.selEnd().pit() + 1;
992                         while (spit != epit) {
993                                 if (pars_[spit].layout()->name() != old_layout) {
994                                         change_layout = true;
995                                         break;
996                                 }
997                                 ++spit;
998                         }
999                 }
1000
1001                 if (change_layout)
1002                         setLayout(cur, layout);
1003
1004                 break;
1005         }
1006
1007         case LFUN_CLIPBOARD_PASTE:
1008                 cur.clearSelection();
1009                 pasteClipboard(cur, bv->buffer().errorList("Paste"),
1010                                cmd.argument() == "paragraph");
1011                 bv->buffer().errors("Paste");
1012                 break;
1013
1014         case LFUN_PRIMARY_SELECTION_PASTE:
1015                 pasteString(cur, theSelection().get(),
1016                             cmd.argument() == "paragraph");
1017                 break;
1018
1019         case LFUN_UNICODE_INSERT: {
1020                 if (cmd.argument().empty())
1021                         break;
1022                 docstring hexstring = cmd.argument();
1023                 if (lyx::support::isHex(hexstring)) {
1024                         char_type c = lyx::support::hexToInt(hexstring);
1025                         if (c >= 32 && c < 0x10ffff) {
1026                                 lyxerr << "Inserting c: " << c << endl;
1027                                 docstring s = docstring(1, c);
1028                                 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1029                         }
1030                 }
1031                 break;
1032         }
1033
1034         case LFUN_QUOTE_INSERT: {
1035                 Paragraph & par = cur.paragraph();
1036                 pos_type pos = cur.pos();
1037                 BufferParams const & bufparams = bv->buffer().params();
1038                 LayoutPtr const & style = par.layout();
1039                 if (!style->pass_thru
1040                     && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1041                         // this avoids a double undo
1042                         // FIXME: should not be needed, ideally
1043                         if (!cur.selection())
1044                                 cur.recordUndo();
1045                         cap::replaceSelection(cur);
1046                         pos = cur.pos();
1047                         char_type c;
1048                         if (pos == 0)
1049                                 c = ' ';
1050                         else if (cur.prevInset() && cur.prevInset()->isSpace())
1051                                 c = ' ';
1052                         else
1053                                 c = par.getChar(pos - 1);
1054                         string arg = to_utf8(cmd.argument());
1055                         if (arg == "single")
1056                                 cur.insert(new InsetQuotes(c,
1057                                     bufparams.quotes_language,
1058                                     InsetQuotes::SingleQ));
1059                         else
1060                                 cur.insert(new InsetQuotes(c,
1061                                     bufparams.quotes_language,
1062                                     InsetQuotes::DoubleQ));
1063                         cur.posForward();
1064                 }
1065                 else
1066                         lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1067                 break;
1068         }
1069
1070         case LFUN_DATE_INSERT: {
1071                 string const format = cmd.argument().empty()
1072                         ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1073                 string const time = formatted_time(current_time(), format);
1074                 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1075                 break;
1076         }
1077
1078         case LFUN_MOUSE_TRIPLE:
1079                 if (cmd.button() == mouse_button::button1) {
1080                         tm.cursorHome(cur);
1081                         cur.resetAnchor();
1082                         tm.cursorEnd(cur);
1083                         cur.setSelection();
1084                         bv->cursor() = cur;
1085                 }
1086                 break;
1087
1088         case LFUN_MOUSE_DOUBLE:
1089                 if (cmd.button() == mouse_button::button1) {
1090                         selectWord(cur, WHOLE_WORD_STRICT);
1091                         bv->cursor() = cur;
1092                 }
1093                 break;
1094
1095         // Single-click on work area
1096         case LFUN_MOUSE_PRESS: {
1097                 // Right click on a footnote flag opens float menu
1098                 // FIXME: Why should we clear the selection in this case?
1099                 if (cmd.button() == mouse_button::button3)
1100                         cur.clearSelection();
1101
1102                 bool do_selection = cmd.button() == mouse_button::button1
1103                         && cmd.argument() == "region-select";
1104                 // Set the cursor
1105                 bool update = bv->mouseSetCursor(cur, do_selection);
1106
1107                 // Insert primary selection with middle mouse
1108                 // if there is a local selection in the current buffer,
1109                 // insert this
1110                 if (cmd.button() == mouse_button::button2) {
1111                         if (cap::selection()) {
1112                                 // Copy the selection buffer to the clipboard
1113                                 // stack, because we want it to appear in the
1114                                 // "Edit->Paste recent" menu.
1115                                 cap::copySelectionToStack();
1116
1117                                 cap::pasteSelection(bv->cursor(), 
1118                                                     bv->buffer().errorList("Paste"));
1119                                 bv->buffer().errors("Paste");
1120                                 bv->buffer().markDirty();
1121                                 bv->cursor().finishUndo();
1122                         } else {
1123                                 lyx::dispatch(FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph"));
1124                         }
1125                 }
1126
1127                 // we have to update after dEPM triggered
1128                 if (!update && cmd.button() == mouse_button::button1) {
1129                         needsUpdate = false;
1130                         cur.noUpdate();
1131                 }
1132
1133                 break;
1134         }
1135
1136         case LFUN_MOUSE_MOTION: {
1137                 // Only use motion with button 1
1138                 //if (cmd.button() != mouse_button::button1)
1139                 //      return false;
1140
1141                 // ignore motions deeper nested than the real anchor
1142                 Cursor & bvcur = cur.bv().cursor();
1143                 if (bvcur.anchor_.hasPart(cur)) {
1144                         CursorSlice old = bvcur.top();
1145
1146                         int const wh = bv->workHeight();
1147                         int const y = std::max(0, std::min(wh - 1, cmd.y));
1148
1149                         tm.setCursorFromCoordinates(cur, cmd.x, y);
1150                         cur.setTargetX(cmd.x);
1151                         if (cmd.y >= wh)
1152                                 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1153                         else if (cmd.y < 0)
1154                                 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1155                         // This is to allow jumping over large insets
1156                         if (cur.top() == old) {
1157                                 if (cmd.y >= wh)
1158                                         lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1159                                 else if (cmd.y < 0)
1160                                         lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1161                         }
1162
1163                         if (cur.top() == old)
1164                                 cur.noUpdate();
1165                         else {
1166                                 // don't set anchor_
1167                                 bvcur.setCursor(cur);
1168                                 bvcur.selection() = true;
1169                                 //lyxerr << "MOTION: " << bv->cursor() << endl;
1170                         }
1171
1172                 } else
1173                         cur.undispatched();
1174                 break;
1175         }
1176
1177         case LFUN_MOUSE_RELEASE: {
1178                 if (cmd.button() == mouse_button::button2)
1179                         break;
1180
1181                 if (cmd.button() == mouse_button::button1) {
1182                         // if there is new selection, update persistent
1183                         // selection, otherwise, single click does not
1184                         // clear persistent selection buffer
1185                         if (cur.selection()) {
1186                                 // finish selection
1187                                 // if double click, cur is moved to the end of word by selectWord
1188                                 // but bvcur is current mouse position
1189                                 Cursor & bvcur = cur.bv().cursor();
1190                                 bvcur.selection() = true;
1191                         }
1192                         needsUpdate = false;
1193                         cur.noUpdate();
1194                 }
1195
1196                 break;
1197         }
1198
1199         case LFUN_SELF_INSERT: {
1200                 if (cmd.argument().empty())
1201                         break;
1202
1203                 // Automatically delete the currently selected
1204                 // text and replace it with what is being
1205                 // typed in now. Depends on lyxrc settings
1206                 // "auto_region_delete", which defaults to
1207                 // true (on).
1208
1209                 if (lyxrc.auto_region_delete && cur.selection()) {
1210                         pit_type const begpit = cur.selBegin().pit();
1211                         pit_type const endpit = cur.selEnd().pit();
1212                         cutSelection(cur, false, false);
1213                         // When a selection spans multiple paragraphs, the metrics update
1214                         // mechanism sometimes fail to detect that a full update is needed.
1215                         // In this case, we force the full update:
1216                         // (see http://bugzilla.lyx.org/show_bug.cgi?id=4317)
1217                         if (isMainText(cur.bv().buffer()) && begpit != endpit)
1218                                 cur.updateFlags(Update::Force);
1219                 }
1220
1221                 cur.clearSelection();
1222                 Font const old_font = cur.real_current_font;
1223
1224                 docstring::const_iterator cit = cmd.argument().begin();
1225                 docstring::const_iterator end = cmd.argument().end();
1226                 for (; cit != end; ++cit)
1227                         bv->translateAndInsert(*cit, this, cur);
1228
1229                 cur.resetAnchor();
1230                 moveCursor(cur, false);
1231                 break;
1232         }
1233
1234         case LFUN_HYPERLINK_INSERT: {
1235                 InsetCommandParams p(HYPERLINK_CODE);
1236                 docstring content;
1237                 if (cur.selection()) {
1238                         content = cur.selectionAsString(false);
1239                         cutSelection(cur, true, false);
1240                 }
1241                 p["target"] = (cmd.argument().empty()) ?
1242                         content : cmd.argument();
1243                 string const data = InsetCommandMailer::params2string("href", p);
1244                 if (p["target"].empty()) {
1245                         bv->showInsetDialog("href", data, 0);
1246                 } else {
1247                         FuncRequest fr(LFUN_INSET_INSERT, data);
1248                         dispatch(cur, fr);
1249                 }
1250                 break;
1251         }
1252
1253         case LFUN_LABEL_INSERT: {
1254                 InsetCommandParams p(LABEL_CODE);
1255                 // Try to generate a valid label
1256                 p["name"] = (cmd.argument().empty()) ?
1257                         cur.getPossibleLabel() :
1258                         cmd.argument();
1259                 string const data = InsetCommandMailer::params2string("label", p);
1260
1261                 if (cmd.argument().empty()) {
1262                         bv->showInsetDialog("label", data, 0);
1263                 } else {
1264                         FuncRequest fr(LFUN_INSET_INSERT, data);
1265                         dispatch(cur, fr);
1266                 }
1267                 break;
1268         }
1269
1270         case LFUN_INFO_INSERT: {
1271                 Inset * inset = createInset(cur.bv().buffer(), cmd);
1272                 if (!inset)
1273                         break;
1274                 // if an empty inset is created (cmd.argument() is empty)
1275                 // use current selection as parameter.
1276                 if (cmd.argument().empty() && cur.selection()) {
1277                         // use selected text as info to avoid a separate UI
1278                         docstring ds = cur.selectionAsString(false);
1279                         cutSelection(cur, true, false);
1280                         static_cast<InsetInfo *>(inset)->setInfo(to_utf8(ds));
1281                 }
1282                 insertInset(cur, inset);
1283                 cur.posForward();
1284                 break;
1285         }
1286 #if 0
1287         case LFUN_LIST_INSERT:
1288         case LFUN_THEOREM_INSERT:
1289 #endif
1290         case LFUN_CAPTION_INSERT:
1291         case LFUN_FOOTNOTE_INSERT:
1292                 // Open the inset, and move the current selection
1293                 // inside it.
1294                 doInsertInset(cur, this, cmd, true, true);
1295                 cur.posForward();
1296                 // These insets are numbered.
1297                 updateLabels(bv->buffer());
1298                 break;
1299         case LFUN_NOTE_INSERT:
1300         case LFUN_FLEX_INSERT:
1301         case LFUN_BOX_INSERT:
1302         case LFUN_BRANCH_INSERT:
1303         case LFUN_BIBITEM_INSERT:
1304         case LFUN_ERT_INSERT:
1305         case LFUN_LISTING_INSERT:
1306         case LFUN_MARGINALNOTE_INSERT:
1307         case LFUN_OPTIONAL_INSERT:
1308         case LFUN_ENVIRONMENT_INSERT:
1309                 // Open the inset, and move the current selection
1310                 // inside it.
1311                 doInsertInset(cur, this, cmd, true, true);
1312                 cur.posForward();
1313                 break;
1314
1315         case LFUN_TABULAR_INSERT:
1316                 // if there were no arguments, just open the dialog
1317                 if (doInsertInset(cur, this, cmd, false, true))
1318                         cur.posForward();
1319                 else
1320                         bv->showDialog("tabularcreate");
1321
1322                 break;
1323
1324         case LFUN_FLOAT_INSERT:
1325         case LFUN_FLOAT_WIDE_INSERT:
1326         case LFUN_WRAP_INSERT: {
1327                 bool content = cur.selection();  // will some text be moved into the inset?
1328
1329                 doInsertInset(cur, this, cmd, true, true);
1330                 cur.posForward();
1331                 ParagraphList & pars = cur.text()->paragraphs();
1332
1333                 TextClass const & tclass = bv->buffer().params().getTextClass();
1334
1335                 // add a separate paragraph for the caption inset
1336                 pars.push_back(Paragraph());
1337                 pars.back().setInsetOwner(pars[0].inInset());
1338                 pars.back().layout(tclass.defaultLayout());
1339
1340                 int cap_pit = pars.size() - 1;
1341
1342                 // if an empty inset was created, we create an additional empty
1343                 // paragraph at the bottom so that the user can choose where to put
1344                 // the graphics (or table).
1345                 if (!content) {
1346                         pars.push_back(Paragraph());
1347                         pars.back().setInsetOwner(pars[0].inInset());
1348                         pars.back().layout(tclass.defaultLayout());
1349
1350                 }
1351
1352                 // reposition the cursor to the caption
1353                 cur.pit() = cap_pit;
1354                 cur.pos() = 0;
1355                 // FIXME: This Text/Cursor dispatch handling is a mess!
1356                 // We cannot use Cursor::dispatch here it needs access to up to
1357                 // date metrics.
1358                 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1359                 cur.text()->dispatch(cur, cmd_caption);
1360                 cur.updateFlags(Update::Force);
1361                 // FIXME: When leaving the Float (or Wrap) inset we should
1362                 // delete any empty paragraph left above or below the
1363                 // caption.
1364                 break;
1365         }
1366
1367         case LFUN_INDEX_INSERT:
1368                 doInsertInset(cur, this, cmd, true, true);
1369                 cur.posForward();
1370                 break;
1371
1372         case LFUN_NOMENCL_INSERT: {
1373                 FuncRequest cmd1 = cmd;
1374                 if (cmd.argument().empty())
1375                         cmd1 = FuncRequest(cmd,
1376                                 bv->cursor().innerText()->getStringToIndex(bv->cursor()));
1377                 Inset * inset = createInset(cur.bv().buffer(), cmd1);
1378                 if (!inset)
1379                         break;
1380                 cur.recordUndo();
1381                 cur.clearSelection();
1382                 insertInset(cur, inset);
1383                 // Show the dialog for the nomenclature entry, since the
1384                 // description entry still needs to be filled in.
1385                 if (cmd.action == LFUN_NOMENCL_INSERT)
1386                         inset->edit(cur, true);
1387                 cur.posForward();
1388                 break;
1389         }
1390
1391         case LFUN_INDEX_PRINT:
1392         case LFUN_NOMENCL_PRINT:
1393         case LFUN_TOC_INSERT:
1394         case LFUN_HFILL_INSERT:
1395         case LFUN_LINE_INSERT:
1396         case LFUN_PAGEBREAK_INSERT:
1397         case LFUN_CLEARPAGE_INSERT:
1398         case LFUN_CLEARDOUBLEPAGE_INSERT:
1399                 // do nothing fancy
1400                 doInsertInset(cur, this, cmd, false, false);
1401                 cur.posForward();
1402                 break;
1403
1404         case LFUN_DEPTH_DECREMENT:
1405                 changeDepth(cur, DEC_DEPTH);
1406                 break;
1407
1408         case LFUN_DEPTH_INCREMENT:
1409                 changeDepth(cur, INC_DEPTH);
1410                 break;
1411
1412         case LFUN_MATH_DISPLAY:
1413                 mathDispatch(cur, cmd, true);
1414                 break;
1415
1416         case LFUN_MATH_IMPORT_SELECTION:
1417         case LFUN_MATH_MODE:
1418                 if (cmd.argument() == "on")
1419                         // don't pass "on" as argument
1420                         mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1421                 else
1422                         mathDispatch(cur, cmd, false);
1423                 break;
1424
1425         case LFUN_MATH_MACRO:
1426                 if (cmd.argument().empty())
1427                         cur.errorMessage(from_utf8(N_("Missing argument")));
1428                 else {
1429                         string s = to_utf8(cmd.argument());
1430                         string const s1 = token(s, ' ', 1);
1431                         int const nargs = s1.empty() ? 0 : convert<int>(s1);
1432                         string const s2 = token(s, ' ', 2);
1433                         string const type = s2.empty() ? "newcommand" : s2;
1434                         cur.insert(new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, false, from_utf8(type)));
1435                         //cur.nextInset()->edit(cur, true);
1436                 }
1437                 break;
1438
1439         // passthrough hat and underscore outside mathed:
1440         case LFUN_MATH_SUBSCRIPT:
1441                 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1442                 break;
1443         case LFUN_MATH_SUPERSCRIPT:
1444                 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1445                 break;
1446
1447         case LFUN_MATH_INSERT:
1448         case LFUN_MATH_MATRIX:
1449         case LFUN_MATH_DELIM:
1450         case LFUN_MATH_BIGDELIM: {
1451                 if (cur.selection())
1452                         cur.clearSelection();
1453                 // FIXME: instead of the above, this one
1454                 // should be used (but it asserts with Bidi enabled)
1455                 // cf. http://bugzilla.lyx.org/show_bug.cgi?id=4055
1456                 // cap::replaceSelection(cur);
1457                 cur.insert(new InsetMathHull(hullSimple));
1458                 checkAndActivateInset(cur, true);
1459                 BOOST_ASSERT(cur.inMathed());
1460                 cur.dispatch(cmd);
1461                 break;
1462         }
1463
1464         case LFUN_FONT_EMPH: {
1465                 Font font(ignore_font);
1466                 font.fontInfo().setEmph(FONT_TOGGLE);
1467                 toggleAndShow(cur, this, font);
1468                 break;
1469         }
1470
1471         case LFUN_FONT_BOLD: {
1472                 Font font(ignore_font);
1473                 font.fontInfo().setSeries(BOLD_SERIES);
1474                 toggleAndShow(cur, this, font);
1475                 break;
1476         }
1477
1478         case LFUN_FONT_NOUN: {
1479                 Font font(ignore_font);
1480                 font.fontInfo().setNoun(FONT_TOGGLE);
1481                 toggleAndShow(cur, this, font);
1482                 break;
1483         }
1484
1485         case LFUN_FONT_TYPEWRITER: {
1486                 Font font(ignore_font);
1487                 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1488                 toggleAndShow(cur, this, font);
1489                 break;
1490         }
1491
1492         case LFUN_FONT_SANS: {
1493                 Font font(ignore_font);
1494                 font.fontInfo().setFamily(SANS_FAMILY);
1495                 toggleAndShow(cur, this, font);
1496                 break;
1497         }
1498
1499         case LFUN_FONT_ROMAN: {
1500                 Font font(ignore_font);
1501                 font.fontInfo().setFamily(ROMAN_FAMILY);
1502                 toggleAndShow(cur, this, font);
1503                 break;
1504         }
1505
1506         case LFUN_FONT_DEFAULT: {
1507                 Font font(inherit_font, ignore_language);
1508                 toggleAndShow(cur, this, font);
1509                 break;
1510         }
1511
1512         case LFUN_FONT_UNDERLINE: {
1513                 Font font(ignore_font);
1514                 font.fontInfo().setUnderbar(FONT_TOGGLE);
1515                 toggleAndShow(cur, this, font);
1516                 break;
1517         }
1518
1519         case LFUN_FONT_SIZE: {
1520                 Font font(ignore_font);
1521                 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1522                 toggleAndShow(cur, this, font);
1523                 break;
1524         }
1525
1526         case LFUN_LANGUAGE: {
1527                 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1528                 if (!lang)
1529                         break;
1530                 Font font(ignore_font);
1531                 font.setLanguage(lang);
1532                 toggleAndShow(cur, this, font);
1533                 break;
1534         }
1535
1536         case LFUN_FONT_FREE_APPLY:
1537                 toggleAndShow(cur, this, freefont, toggleall);
1538                 cur.message(_("Character set"));
1539                 break;
1540
1541         // Set the freefont using the contents of \param data dispatched from
1542         // the frontends and apply it at the current cursor location.
1543         case LFUN_FONT_FREE_UPDATE: {
1544                 Font font;
1545                 bool toggle;
1546                 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1547                         freefont = font;
1548                         toggleall = toggle;
1549                         toggleAndShow(cur, this, freefont, toggleall);
1550                         cur.message(_("Character set"));
1551                 } else {
1552                         lyxerr << "Argument not ok";
1553                 }
1554                 break;
1555         }
1556
1557         case LFUN_FINISHED_LEFT:
1558                 LYXERR(Debug::DEBUG) << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1559                 if (reverseDirectionNeeded(cur)) {
1560                         ++cur.pos();
1561                         cur.setCurrentFont();
1562                 }
1563                 break;
1564
1565         case LFUN_FINISHED_RIGHT:
1566                 LYXERR(Debug::DEBUG) << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1567                 if (!reverseDirectionNeeded(cur)) {
1568                         ++cur.pos();
1569                         cur.setCurrentFont();
1570                 }
1571                 break;
1572
1573         case LFUN_FINISHED_BACKWARD:
1574                 LYXERR(Debug::DEBUG) << "handle LFUN_FINISHED_BACKWARD:\n" << cur << endl;
1575                 break;
1576
1577         case LFUN_FINISHED_FORWARD:
1578                 LYXERR(Debug::DEBUG) << "handle LFUN_FINISHED_FORWARD:\n" << cur << endl;
1579                 ++cur.pos();
1580                 cur.setCurrentFont();
1581                 break;
1582
1583         case LFUN_LAYOUT_PARAGRAPH: {
1584                 string data;
1585                 params2string(cur.paragraph(), data);
1586                 data = "show\n" + data;
1587                 bv->showDialogWithData("paragraph", data);
1588                 break;
1589         }
1590
1591         case LFUN_PARAGRAPH_UPDATE: {
1592                 string data;
1593                 params2string(cur.paragraph(), data);
1594
1595                 // Will the paragraph accept changes from the dialog?
1596                 bool const accept = !cur.inset().forceDefaultParagraphs(cur.idx());
1597
1598                 data = "update " + convert<string>(accept) + '\n' + data;
1599                 bv->updateDialog("paragraph", data);
1600                 break;
1601         }
1602
1603         case LFUN_ACCENT_UMLAUT:
1604         case LFUN_ACCENT_CIRCUMFLEX:
1605         case LFUN_ACCENT_GRAVE:
1606         case LFUN_ACCENT_ACUTE:
1607         case LFUN_ACCENT_TILDE:
1608         case LFUN_ACCENT_CEDILLA:
1609         case LFUN_ACCENT_MACRON:
1610         case LFUN_ACCENT_DOT:
1611         case LFUN_ACCENT_UNDERDOT:
1612         case LFUN_ACCENT_UNDERBAR:
1613         case LFUN_ACCENT_CARON:
1614         case LFUN_ACCENT_SPECIAL_CARON:
1615         case LFUN_ACCENT_BREVE:
1616         case LFUN_ACCENT_TIE:
1617         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1618         case LFUN_ACCENT_CIRCLE:
1619         case LFUN_ACCENT_OGONEK:
1620                 theLyXFunc().handleKeyFunc(cmd.action);
1621                 if (!cmd.argument().empty())
1622                         // FIXME: Are all these characters encoded in one byte in utf8?
1623                         bv->translateAndInsert(cmd.argument()[0], this, cur);
1624                 break;
1625
1626         case LFUN_FLOAT_LIST: {
1627                 TextClass const & tclass = bv->buffer().params().getTextClass();
1628                 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1629                         cur.recordUndo();
1630                         if (cur.selection())
1631                                 cutSelection(cur, true, false);
1632                         breakParagraph(cur);
1633
1634                         if (cur.lastpos() != 0) {
1635                                 cursorBackward(cur);
1636                                 breakParagraph(cur);
1637                         }
1638
1639                         setLayout(cur, tclass.defaultLayoutName());
1640                         ParagraphParameters p;
1641                         setParagraphs(cur, p);
1642                         insertInset(cur, new InsetFloatList(to_utf8(cmd.argument())));
1643                         cur.posForward();
1644                 } else {
1645                         lyxerr << "Non-existent float type: "
1646                                << to_utf8(cmd.argument()) << endl;
1647                 }
1648                 break;
1649         }
1650
1651         case LFUN_CHANGE_ACCEPT: {
1652                 acceptOrRejectChanges(cur, ACCEPT);
1653                 break;
1654         }
1655
1656         case LFUN_CHANGE_REJECT: {
1657                 acceptOrRejectChanges(cur, REJECT);
1658                 break;
1659         }
1660
1661         case LFUN_THESAURUS_ENTRY: {
1662                 docstring arg = cmd.argument();
1663                 if (arg.empty()) {
1664                         arg = cur.selectionAsString(false);
1665                         // FIXME
1666                         if (arg.size() > 100 || arg.empty()) {
1667                                 // Get word or selection
1668                                 selectWordWhenUnderCursor(cur, WHOLE_WORD);
1669                                 arg = cur.selectionAsString(false);
1670                         }
1671                 }
1672                 bv->showDialogWithData("thesaurus", to_utf8(arg));
1673                 break;
1674         }
1675
1676         case LFUN_PARAGRAPH_PARAMS_APPLY: {
1677                 // Given data, an encoding of the ParagraphParameters
1678                 // generated in the Paragraph dialog, this function sets
1679                 // the current paragraph, or currently selected paragraphs,
1680                 // appropriately. 
1681                 // NOTE: This function overrides all existing settings.
1682                 setParagraphs(cur, cmd.argument());
1683                 cur.message(_("Paragraph layout set"));
1684                 break;
1685         }
1686         
1687         case LFUN_PARAGRAPH_PARAMS: {
1688                 // Given data, an encoding of the ParagraphParameters as we'd
1689                 // find them in a LyX file, this function modifies the current paragraph, 
1690                 // or currently selected paragraphs. 
1691                 // NOTE: This function only modifies, and does not override, existing
1692                 // settings.
1693                 setParagraphs(cur, cmd.argument(), true);
1694                 cur.message(_("Paragraph layout set"));
1695                 break;
1696         }
1697
1698         case LFUN_ESCAPE:
1699                 if (cur.selection()) {
1700                         cur.selection() = false;
1701                 } else {
1702                         cur.undispatched();
1703                         // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
1704                         // correct, but I'm not 100% sure -- dov, 071019
1705                         cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1706                 }
1707                 break;
1708
1709         case LFUN_OUTLINE_UP:
1710                 outline(OutlineUp, cur);
1711                 setCursor(cur, cur.pit(), 0);
1712                 updateLabels(cur.buffer());
1713                 needsUpdate = true;
1714                 break;
1715
1716         case LFUN_OUTLINE_DOWN:
1717                 outline(OutlineDown, cur);
1718                 setCursor(cur, cur.pit(), 0);
1719                 updateLabels(cur.buffer());
1720                 needsUpdate = true;
1721                 break;
1722
1723         case LFUN_OUTLINE_IN:
1724                 outline(OutlineIn, cur);
1725                 updateLabels(cur.buffer());
1726                 needsUpdate = true;
1727                 break;
1728
1729         case LFUN_OUTLINE_OUT:
1730                 outline(OutlineOut, cur);
1731                 updateLabels(cur.buffer());
1732                 needsUpdate = true;
1733                 break;
1734
1735         default:
1736                 LYXERR(Debug::ACTION)
1737                         << BOOST_CURRENT_FUNCTION
1738                         << ": Command " << cmd
1739                         << " not DISPATCHED by Text" << endl;
1740                 cur.undispatched();
1741                 break;
1742         }
1743
1744         needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
1745
1746         // FIXME: The cursor flag is reset two lines below
1747         // so we need to check here if some of the LFUN did touch that.
1748         // for now only Text::erase() and Text::backspace() do that.
1749         // The plan is to verify all the LFUNs and then to remove this
1750         // singleParUpdate boolean altogether.
1751         if (cur.result().update() & Update::Force) {
1752                 singleParUpdate = false;
1753                 needsUpdate = true;
1754         }
1755
1756         // FIXME: the following code should go in favor of fine grained
1757         // update flag treatment.
1758         if (singleParUpdate) {
1759                 // Inserting characters does not change par height
1760                 ParagraphMetrics const & pms
1761                         = cur.bv().parMetrics(cur.bottom().text(), cur.bottom().pit());
1762                 if (pms.dim().height()
1763                     == olddim.height()) {
1764                         // if so, update _only_ this paragraph
1765                         cur.updateFlags(Update::SinglePar |
1766                                 Update::FitCursor);
1767                         return;
1768                 } else
1769                         needsUpdate = true;
1770         }
1771
1772         if (!needsUpdate
1773             && &oldTopSlice.inset() == &cur.inset()
1774             && oldTopSlice.idx() == cur.idx()
1775             && !sel // sel is a backup of cur.selection() at the biginning of the function.
1776             && !cur.selection())
1777                 // FIXME: it would be better if we could just do this
1778                 //
1779                 //if (cur.result().update() != Update::FitCursor)
1780                 //      cur.noUpdate();
1781                 //
1782                 // But some LFUNs do not set Update::FitCursor when needed, so we
1783                 // do it for all. This is not very harmfull as FitCursor will provoke
1784                 // a full redraw only if needed but still, a proper review of all LFUN
1785                 // should be done and this needsUpdate boolean can then be removed.
1786                 cur.updateFlags(Update::FitCursor);
1787         else
1788                 cur.updateFlags(Update::Force | Update::FitCursor);
1789 }
1790
1791
1792 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
1793                         FuncStatus & flag) const
1794 {
1795         BOOST_ASSERT(cur.text() == this);
1796
1797         Font const & font = cur.real_current_font;
1798         FontInfo const & fontinfo = font.fontInfo();
1799         bool enable = true;
1800         InsetCode code = NO_CODE;
1801
1802         switch (cmd.action) {
1803
1804         case LFUN_DEPTH_DECREMENT:
1805                 enable = changeDepthAllowed(cur, DEC_DEPTH);
1806                 break;
1807
1808         case LFUN_DEPTH_INCREMENT:
1809                 enable = changeDepthAllowed(cur, INC_DEPTH);
1810                 break;
1811
1812         case LFUN_APPENDIX:
1813                 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1814                 return true;
1815
1816         case LFUN_BIBITEM_INSERT:
1817                 enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO
1818                           && cur.pos() == 0);
1819                 break;
1820
1821         case LFUN_DIALOG_SHOW_NEW_INSET:
1822                 if (cmd.argument() == "bibitem")
1823                         code = BIBITEM_CODE;
1824                 else if (cmd.argument() == "bibtex")
1825                         code = BIBTEX_CODE;
1826                 else if (cmd.argument() == "box")
1827                         code = BOX_CODE;
1828                 else if (cmd.argument() == "branch")
1829                         code = BRANCH_CODE;
1830                 else if (cmd.argument() == "citation")
1831                         code = CITE_CODE;
1832                 else if (cmd.argument() == "ert")
1833                         code = ERT_CODE;
1834                 else if (cmd.argument() == "external")
1835                         code = EXTERNAL_CODE;
1836                 else if (cmd.argument() == "float")
1837                         code = FLOAT_CODE;
1838                 else if (cmd.argument() == "graphics")
1839                         code = GRAPHICS_CODE;
1840                 else if (cmd.argument() == "href")
1841                         code = HYPERLINK_CODE;
1842                 else if (cmd.argument() == "include")
1843                         code = INCLUDE_CODE;
1844                 else if (cmd.argument() == "index")
1845                         code = INDEX_CODE;
1846                 else if (cmd.argument() == "nomenclature")
1847                         code = NOMENCL_CODE;
1848                 else if (cmd.argument() == "label")
1849                         code = LABEL_CODE;
1850                 else if (cmd.argument() == "note")
1851                         code = NOTE_CODE;
1852                 else if (cmd.argument() == "ref")
1853                         code = REF_CODE;
1854                 else if (cmd.argument() == "toc")
1855                         code = TOC_CODE;
1856                 else if (cmd.argument() == "vspace")
1857                         code = VSPACE_CODE;
1858                 else if (cmd.argument() == "wrap")
1859                         code = WRAP_CODE;
1860                 else if (cmd.argument() == "listings")
1861                         code = LISTINGS_CODE;
1862                 break;
1863
1864         case LFUN_ERT_INSERT:
1865                 code = ERT_CODE;
1866                 break;
1867         case LFUN_LISTING_INSERT:
1868             code = LISTINGS_CODE;
1869                 break;
1870         case LFUN_FOOTNOTE_INSERT:
1871                 code = FOOT_CODE;
1872                 break;
1873         case LFUN_TABULAR_INSERT:
1874                 code = TABULAR_CODE;
1875                 break;
1876         case LFUN_MARGINALNOTE_INSERT:
1877                 code = MARGIN_CODE;
1878                 break;
1879         case LFUN_FLOAT_INSERT:
1880         case LFUN_FLOAT_WIDE_INSERT:
1881                 code = FLOAT_CODE;
1882                 break;
1883         case LFUN_WRAP_INSERT:
1884                 code = WRAP_CODE;
1885                 break;
1886         case LFUN_FLOAT_LIST:
1887                 code = FLOAT_LIST_CODE;
1888                 break;
1889 #if 0
1890         case LFUN_LIST_INSERT:
1891                 code = LIST_CODE;
1892                 break;
1893         case LFUN_THEOREM_INSERT:
1894                 code = THEOREM_CODE;
1895                 break;
1896 #endif
1897         case LFUN_CAPTION_INSERT:
1898                 code = CAPTION_CODE;
1899                 break;
1900         case LFUN_NOTE_INSERT:
1901                 code = NOTE_CODE;
1902                 break;
1903         case LFUN_FLEX_INSERT: {
1904                 code = FLEX_CODE;
1905                 string s = cmd.getArg(0);
1906                 InsetLayout il =  cur.buffer().params().getTextClass().insetlayout(from_utf8(s));
1907                 if (il.lyxtype != "charstyle" &&
1908                     il.lyxtype != "custom" &&
1909                     il.lyxtype != "element")
1910                         enable = false;
1911                 break;
1912                 }
1913         case LFUN_BOX_INSERT:
1914                 code = BOX_CODE;
1915                 break;
1916         case LFUN_BRANCH_INSERT:
1917                 code = BRANCH_CODE;
1918                 if (cur.buffer().masterBuffer()->params().branchlist().empty())
1919                         enable = false;
1920                 break;
1921         case LFUN_LABEL_INSERT:
1922                 code = LABEL_CODE;
1923                 break;
1924         case LFUN_INFO_INSERT:
1925                 code = INFO_CODE;
1926                 break;
1927         case LFUN_OPTIONAL_INSERT:
1928                 code = OPTARG_CODE;
1929                 enable = cur.paragraph().insetList().count(OPTARG_CODE)
1930                         < cur.paragraph().layout()->optionalargs;
1931                 break;
1932         case LFUN_ENVIRONMENT_INSERT:
1933                 code = BOX_CODE;
1934                 break;
1935         case LFUN_INDEX_INSERT:
1936                 code = INDEX_CODE;
1937                 break;
1938         case LFUN_INDEX_PRINT:
1939                 code = INDEX_PRINT_CODE;
1940                 break;
1941         case LFUN_NOMENCL_INSERT:
1942                 code = NOMENCL_CODE;
1943                 break;
1944         case LFUN_NOMENCL_PRINT:
1945                 code = NOMENCL_PRINT_CODE;
1946                 break;
1947         case LFUN_TOC_INSERT:
1948                 code = TOC_CODE;
1949                 break;
1950         case LFUN_HYPERLINK_INSERT:
1951                 code = HYPERLINK_CODE;
1952                 break;
1953         case LFUN_QUOTE_INSERT:
1954                 // always allow this, since we will inset a raw quote
1955                 // if an inset is not allowed.
1956                 break;
1957         case LFUN_HYPHENATION_POINT_INSERT:
1958         case LFUN_LIGATURE_BREAK_INSERT:
1959         case LFUN_HFILL_INSERT:
1960         case LFUN_MENU_SEPARATOR_INSERT:
1961         case LFUN_DOTS_INSERT:
1962         case LFUN_END_OF_SENTENCE_PERIOD_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_END:
2104         case LFUN_BREAK_LINE:
2105         case LFUN_CHAR_DELETE_FORWARD:
2106         case LFUN_DELETE_FORWARD_SKIP:
2107         case LFUN_CHAR_DELETE_BACKWARD:
2108         case LFUN_DELETE_BACKWARD_SKIP:
2109         case LFUN_BREAK_PARAGRAPH:
2110         case LFUN_BREAK_PARAGRAPH_SKIP:
2111         case LFUN_PARAGRAPH_SPACING:
2112         case LFUN_INSET_INSERT:
2113         case LFUN_WORD_UPCASE:
2114         case LFUN_WORD_LOWCASE:
2115         case LFUN_WORD_CAPITALIZE:
2116         case LFUN_CHARS_TRANSPOSE:
2117         case LFUN_SERVER_GET_XY:
2118         case LFUN_SERVER_SET_XY:
2119         case LFUN_SERVER_GET_FONT:
2120         case LFUN_SERVER_GET_LAYOUT:
2121         case LFUN_LAYOUT:
2122         case LFUN_DATE_INSERT:
2123         case LFUN_SELF_INSERT:
2124         case LFUN_LINE_INSERT:
2125         case LFUN_PAGEBREAK_INSERT:
2126         case LFUN_CLEARPAGE_INSERT:
2127         case LFUN_CLEARDOUBLEPAGE_INSERT:
2128         case LFUN_MATH_DISPLAY:
2129         case LFUN_MATH_IMPORT_SELECTION:
2130         case LFUN_MATH_MODE:
2131         case LFUN_MATH_MACRO:
2132         case LFUN_MATH_MATRIX:
2133         case LFUN_MATH_DELIM:
2134         case LFUN_MATH_BIGDELIM:
2135         case LFUN_MATH_INSERT:
2136         case LFUN_MATH_SUBSCRIPT:
2137         case LFUN_MATH_SUPERSCRIPT:
2138         case LFUN_FONT_DEFAULT:
2139         case LFUN_FONT_UNDERLINE:
2140         case LFUN_FONT_SIZE:
2141         case LFUN_LANGUAGE:
2142         case LFUN_FONT_FREE_APPLY:
2143         case LFUN_FONT_FREE_UPDATE:
2144         case LFUN_LAYOUT_PARAGRAPH:
2145         case LFUN_PARAGRAPH_UPDATE:
2146         case LFUN_ACCENT_UMLAUT:
2147         case LFUN_ACCENT_CIRCUMFLEX:
2148         case LFUN_ACCENT_GRAVE:
2149         case LFUN_ACCENT_ACUTE:
2150         case LFUN_ACCENT_TILDE:
2151         case LFUN_ACCENT_CEDILLA:
2152         case LFUN_ACCENT_MACRON:
2153         case LFUN_ACCENT_DOT:
2154         case LFUN_ACCENT_UNDERDOT:
2155         case LFUN_ACCENT_UNDERBAR:
2156         case LFUN_ACCENT_CARON:
2157         case LFUN_ACCENT_SPECIAL_CARON:
2158         case LFUN_ACCENT_BREVE:
2159         case LFUN_ACCENT_TIE:
2160         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2161         case LFUN_ACCENT_CIRCLE:
2162         case LFUN_ACCENT_OGONEK:
2163         case LFUN_THESAURUS_ENTRY:
2164         case LFUN_PARAGRAPH_PARAMS_APPLY:
2165         case LFUN_PARAGRAPH_PARAMS:
2166         case LFUN_ESCAPE:
2167         case LFUN_BUFFER_END:
2168         case LFUN_BUFFER_BEGIN:
2169         case LFUN_BUFFER_BEGIN_SELECT:
2170         case LFUN_BUFFER_END_SELECT:
2171         case LFUN_UNICODE_INSERT:
2172                 // these are handled in our dispatch()
2173                 enable = true;
2174                 break;
2175
2176         default:
2177                 return false;
2178         }
2179
2180         if (code != NO_CODE
2181             && (cur.empty() || !cur.inset().insetAllowed(code)))
2182                 enable = false;
2183
2184         flag.enabled(enable);
2185         return true;
2186 }
2187
2188
2189 void Text::pasteString(Cursor & cur, docstring const & clip,
2190                 bool asParagraphs)
2191 {
2192         cur.clearSelection();
2193         if (!clip.empty()) {
2194                 cur.recordUndo();
2195                 if (asParagraphs)
2196                         insertStringAsParagraphs(cur, clip);
2197                 else
2198                         insertStringAsLines(cur, clip);
2199         }
2200 }
2201
2202 } // namespace lyx