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