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