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