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