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