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