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