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