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