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