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