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