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