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