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