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