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