]> git.lyx.org Git - lyx.git/blob - src/text3.C
Fix bug 2393 (from Dov Feldstern)
[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
74 namespace lyx {
75
76 using cap::copySelection;
77 using cap::cutSelection;
78 using cap::pasteSelection;
79 using cap::replaceSelection;
80
81 using support::isStrUnsignedInt;
82 using support::token;
83
84 using std::endl;
85 using std::string;
86 using std::istringstream;
87 using std::ostringstream;
88
89
90 extern string current_layout;
91
92
93 namespace {
94
95         // globals...
96         LyXFont freefont(LyXFont::ALL_IGNORE);
97         bool toggleall = false;
98
99
100         void toggleAndShow(LCursor & cur, LyXText * text,
101                 LyXFont const & font, bool toggleall = true)
102         {
103                 text->toggleFree(cur, font, toggleall);
104
105                 if (font.language() != ignore_language ||
106                                 font.number() != LyXFont::IGNORE) {
107                         Paragraph & par = cur.paragraph();
108                         text->bidi.computeTables(par, cur.buffer(), cur.textRow());
109                         if (cur.boundary() !=
110                                         text->bidi.isBoundary(cur.buffer(), par,
111                                                         cur.pos(),
112                                                         text->real_current_font))
113                                 text->setCursor(cur, cur.pit(), cur.pos(),
114                                                 false, !cur.boundary());
115                 }
116         }
117
118
119         void moveCursor(LCursor & cur, bool selecting)
120         {
121                 if (selecting || cur.mark())
122                         cur.setSelection();
123                 if (!cur.selection())
124                         theSelection().haveSelection(false);
125                 cur.bv().switchKeyMap();
126         }
127
128
129         void finishChange(LCursor & cur, bool selecting)
130         {
131                 finishUndo();
132                 moveCursor(cur, selecting);
133         }
134
135
136         void mathDispatch(LCursor & cur, FuncRequest const & cmd, bool display)
137         {
138                 recordUndo(cur);
139                 docstring sel = cur.selectionAsString(false);
140                 //lyxerr << "selection is: '" << sel << "'" << endl;
141
142                 // It may happen that sel is empty but there is a selection
143                 replaceSelection(cur);
144
145                 if (sel.empty()) {
146                         const int old_pos = cur.pos();
147                         cur.insert(new InsetMathHull(hullSimple));
148                         BOOST_ASSERT(old_pos == cur.pos());
149                         cur.nextInset()->edit(cur, true);
150                         // don't do that also for LFUN_MATH_MODE
151                         // unless you want end up with always changing
152                         // to mathrm when opening an inlined inset --
153                         // I really hate "LyXfunc overloading"...
154                         if (display)
155                                 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
156                         // Avoid an unnecessary undo step if cmd.argument
157                         // is empty
158                         if (!cmd.argument().empty())
159                                 cur.dispatch(FuncRequest(LFUN_MATH_INSERT,
160                                                          cmd.argument()));
161                 } else {
162                         // create a macro if we see "\\newcommand"
163                         // somewhere, and an ordinary formula
164                         // otherwise
165                         if (sel.find(from_ascii("\\newcommand")) == string::npos
166                             && sel.find(from_ascii("\\def")) == string::npos)
167                         {
168                                 InsetMathHull * formula = new InsetMathHull;
169                                 istringstream is(to_utf8(sel));
170                                 LyXLex lex(0, 0);
171                                 lex.setStream(is);
172                                 formula->read(cur.buffer(), lex);
173                                 if (formula->getType() == hullNone)
174                                         // Don't create pseudo formulas if
175                                         // delimiters are left out
176                                         formula->mutate(hullSimple);
177                                 cur.insert(formula);
178                         } else {
179                                 cur.insert(new MathMacroTemplate(sel));
180                         }
181                 }
182                 cur.message(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         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         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         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 } // anon namespace
288
289
290 void LyXText::number(LCursor & cur)
291 {
292         LyXFont font(LyXFont::ALL_IGNORE);
293         font.setNumber(LyXFont::TOGGLE);
294         toggleAndShow(cur, this, font);
295 }
296
297
298 bool LyXText::isRTL(Buffer const & buffer, Paragraph const & par) const
299 {
300         return par.isRightToLeftPar(buffer.params());
301 }
302
303
304 void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
305 {
306         lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
307
308         BOOST_ASSERT(cur.text() == this);
309         BufferView * bv = &cur.bv();
310         CursorSlice oldTopSlice = cur.top();
311         bool oldBoundary = cur.boundary();
312         bool sel = cur.selection();
313         // Signals that, even if needsUpdate == false, an update of the
314         // cursor paragraph is required
315         bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
316                 LyXAction::SingleParUpdate);
317         // Signals that a full-screen update is required
318         bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
319                 LyXAction::NoUpdate) || singleParUpdate);
320         // Remember the old paragraph metric (_outer_ paragraph!)
321         Dimension olddim = cur.bottom().paragraph().dim();
322
323         switch (cmd.action) {
324
325         case LFUN_PARAGRAPH_MOVE_DOWN: {
326                 pit_type const pit = cur.pit();
327                 recUndo(cur, pit, pit + 1);
328                 finishUndo();
329                 std::swap(pars_[pit], pars_[pit + 1]);
330
331                 ParIterator begin(cur);
332                 ++cur.pit();
333                 ParIterator end = boost::next(ParIterator(cur));
334                 updateLabels(cur.buffer(), begin, end);
335
336                 needsUpdate = true;
337                 break;
338         }
339
340         case LFUN_PARAGRAPH_MOVE_UP: {
341                 pit_type const pit = cur.pit();
342                 recUndo(cur, pit - 1, pit);
343                 finishUndo();
344                 std::swap(pars_[pit], pars_[pit - 1]);
345
346                 ParIterator end = boost::next(ParIterator(cur));
347                 --cur.pit();
348                 ParIterator begin(cur);
349                 updateLabels(cur.buffer(), begin, end);
350
351                 needsUpdate = true;
352                 break;
353         }
354
355         case LFUN_APPENDIX: {
356                 Paragraph & par = cur.paragraph();
357                 bool start = !par.params().startOfAppendix();
358
359 #ifdef WITH_WARNINGS
360 #warning The code below only makes sense at top level.
361 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
362 #endif
363                 // ensure that we have only one start_of_appendix in this document
364                 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
365                         if (pars_[tmp].params().startOfAppendix()) {
366                                 recUndo(cur, tmp);
367                                 pars_[tmp].params().startOfAppendix(false);
368                                 break;
369                         }
370                 }
371
372                 recordUndo(cur);
373                 par.params().startOfAppendix(start);
374
375                 // we can set the refreshing parameters now
376                 updateLabels(cur.buffer());
377                 break;
378         }
379
380         case LFUN_WORD_DELETE_FORWARD:
381                 cur.clearSelection();
382                 deleteWordForward(cur);
383                 finishChange(cur, false);
384                 break;
385
386         case LFUN_WORD_DELETE_BACKWARD:
387                 cur.clearSelection();
388                 deleteWordBackward(cur);
389                 finishChange(cur, false);
390                 break;
391
392         case LFUN_LINE_DELETE:
393                 cur.clearSelection();
394                 deleteLineForward(cur);
395                 finishChange(cur, false);
396                 break;
397
398         case LFUN_BUFFER_BEGIN:
399         case LFUN_BUFFER_BEGIN_SELECT:
400                 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_BEGIN_SELECT);
401                 if (cur.depth() == 1) {
402                         needsUpdate |= cursorTop(cur);
403                 } else {
404                         cur.undispatched();
405                 }
406                 break;
407
408         case LFUN_BUFFER_END:
409         case LFUN_BUFFER_END_SELECT:
410                 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_END_SELECT);
411                 if (cur.depth() == 1) {
412                         needsUpdate |= cursorBottom(cur);
413                 } else {
414                         cur.undispatched();
415                 }
416                 break;
417
418         case LFUN_CHAR_FORWARD:
419         case LFUN_CHAR_FORWARD_SELECT:
420                 //lyxerr << BOOST_CURRENT_FUNCTION
421                 //       << " LFUN_CHAR_FORWARD[SEL]:\n" << cur << endl;
422                 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
423                 if (isRTL(*cur.bv().buffer(), cur.paragraph()))
424                         needsUpdate |= cursorLeft(cur);
425                 else
426                         needsUpdate |= cursorRight(cur);
427
428                 if (!needsUpdate && oldTopSlice == cur.top()
429                                 && cur.boundary() == oldBoundary) {
430                         cur.undispatched();
431                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
432                 }
433                 break;
434
435         case LFUN_CHAR_BACKWARD:
436         case LFUN_CHAR_BACKWARD_SELECT:
437                 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
438                 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
439                 if (isRTL(*cur.bv().buffer(), cur.paragraph()))
440                         needsUpdate |= cursorRight(cur);
441                 else
442                         needsUpdate |= cursorLeft(cur);
443
444                 if (!needsUpdate && oldTopSlice == cur.top()
445                         && cur.boundary() == oldBoundary) {
446                         cur.undispatched();
447                         cmd = FuncRequest(LFUN_FINISHED_LEFT);
448                 }
449                 break;
450
451         case LFUN_UP:
452         case LFUN_UP_SELECT:
453                 //lyxerr << "handle LFUN_UP[SEL]:\n" << cur << endl;
454                 needsUpdate |= cur.selHandle(cmd.action == LFUN_UP_SELECT);
455
456                 needsUpdate |= cursorUp(cur);
457
458                 if (!needsUpdate && oldTopSlice == cur.top()
459                           && cur.boundary() == oldBoundary) {
460                         cur.undispatched();
461                         cmd = FuncRequest(LFUN_FINISHED_UP);
462                 }
463                 break;
464
465         case LFUN_DOWN:
466         case LFUN_DOWN_SELECT:
467                 //lyxerr << "handle LFUN_DOWN[SEL]:\n" << cur << endl;
468                 needsUpdate |= cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
469                 needsUpdate |= cursorDown(cur);
470
471                 if (!needsUpdate && oldTopSlice == cur.top() &&
472                     cur.boundary() == oldBoundary)
473                 {
474                         cur.undispatched();
475                         cmd = FuncRequest(LFUN_FINISHED_DOWN);
476                 }
477                 break;
478
479         case LFUN_PARAGRAPH_UP:
480         case LFUN_PARAGRAPH_UP_SELECT:
481                 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
482                 needsUpdate |= cursorUpParagraph(cur);
483                 break;
484
485         case LFUN_PARAGRAPH_DOWN:
486         case LFUN_PARAGRAPH_DOWN_SELECT:
487                 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
488                 needsUpdate |= cursorDownParagraph(cur);
489                 break;
490
491         case LFUN_SCREEN_UP:
492         case LFUN_SCREEN_UP_SELECT:
493                 needsUpdate |= cur.selHandle(cmd.action == LFUN_SCREEN_UP_SELECT);
494                 if (cur.pit() == 0 && cur.textRow().pos() == 0) {
495                         cur.undispatched();
496                         cmd = FuncRequest(LFUN_FINISHED_UP);
497                 } else {
498                         needsUpdate |= cursorPrevious(cur);
499                 }
500                 break;
501
502         case LFUN_SCREEN_DOWN:
503         case LFUN_SCREEN_DOWN_SELECT:
504                 needsUpdate |= cur.selHandle(cmd.action == LFUN_SCREEN_DOWN_SELECT);
505                 if (cur.pit() == cur.lastpit()
506                           && cur.textRow().endpos() == cur.lastpos()) {
507                         cur.undispatched();
508                         cmd = FuncRequest(LFUN_FINISHED_DOWN);
509                 } else {
510                         needsUpdate |= cursorNext(cur);
511                 }
512                 break;
513
514         case LFUN_LINE_BEGIN:
515         case LFUN_LINE_BEGIN_SELECT:
516                 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_BEGIN_SELECT);
517                 needsUpdate |= cursorHome(cur);
518                 break;
519
520         case LFUN_LINE_END:
521         case LFUN_LINE_END_SELECT:
522                 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
523                 needsUpdate |= cursorEnd(cur);
524                 break;
525
526         case LFUN_WORD_FORWARD:
527         case LFUN_WORD_FORWARD_SELECT:
528                 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT);
529                 if (isRTL(*cur.bv().buffer(), cur.paragraph()))
530                         needsUpdate |= cursorLeftOneWord(cur);
531                 else
532                         needsUpdate |= cursorRightOneWord(cur);
533                 break;
534
535         case LFUN_WORD_BACKWARD:
536         case LFUN_WORD_BACKWARD_SELECT:
537                 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT);
538                 if (isRTL(*cur.bv().buffer(), cur.paragraph()))
539                         needsUpdate |= cursorRightOneWord(cur);
540                 else
541                         needsUpdate |= cursorLeftOneWord(cur);
542                 break;
543
544         case LFUN_WORD_SELECT: {
545                 selectWord(cur, WHOLE_WORD);
546                 finishChange(cur, true);
547                 break;
548         }
549
550         case LFUN_BREAK_LINE: {
551                 // Not allowed by LaTeX (labels or empty par)
552                 if (cur.pos() > cur.paragraph().beginOfBody()) {
553                         cap::replaceSelection(cur);
554                         cur.insert(new InsetNewline);
555                         cur.posRight();
556                         moveCursor(cur, false);
557                 }
558                 break;
559         }
560
561         case LFUN_CHAR_DELETE_FORWARD:
562                 if (!cur.selection()) {
563                         if (cur.pos() == cur.paragraph().size())
564                                 // Par boundary, force full-screen update
565                                 singleParUpdate = false;
566                         needsUpdate |= erase(cur);
567                         cur.resetAnchor();
568                         // It is possible to make it a lot faster still
569                         // just comment out the line below...
570                 } else {
571                         cutSelection(cur, true, false);
572                         singleParUpdate = false;
573                 }
574                 moveCursor(cur, false);
575                 break;
576
577         case LFUN_DELETE_FORWARD_SKIP:
578                 // Reverse the effect of LFUN_BREAK_PARAGRAPH_SKIP.
579                 if (!cur.selection()) {
580                         if (cur.pos() == cur.lastpos()) {
581                                 cursorRight(cur);
582                                 cursorLeft(cur);
583                         }
584                         erase(cur);
585                         cur.resetAnchor();
586                 } else {
587                         cutSelection(cur, true, false);
588                 }
589                 break;
590
591
592         case LFUN_CHAR_DELETE_BACKWARD:
593                 if (!cur.selection()) {
594                         if (bv->getIntl().getTransManager().backspace()) {
595                                 // Par boundary, full-screen update
596                                 if (cur.pos() == 0)
597                                         singleParUpdate = false;
598                                 needsUpdate |= backspace(cur);
599                                 cur.resetAnchor();
600                                 // It is possible to make it a lot faster still
601                                 // just comment out the line below...
602                         }
603                 } else {
604                         cutSelection(cur, true, false);
605                         singleParUpdate = false;
606                 }
607                 bv->switchKeyMap();
608                 break;
609
610         case LFUN_DELETE_BACKWARD_SKIP:
611                 // Reverse the effect of LFUN_BREAK_PARAGRAPH_SKIP.
612                 if (!cur.selection()) {
613 #ifdef WITH_WARNINGS
614 #warning look here
615 #endif
616                         //CursorSlice cur = cursor();
617                         backspace(cur);
618                         //anchor() = cur;
619                 } else {
620                         cutSelection(cur, true, false);
621                 }
622                 break;
623
624         case LFUN_BREAK_PARAGRAPH:
625                 cap::replaceSelection(cur);
626                 breakParagraph(cur, 0);
627                 cur.resetAnchor();
628                 bv->switchKeyMap();
629                 break;
630
631         case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
632                 cap::replaceSelection(cur);
633                 breakParagraph(cur, 1);
634                 cur.resetAnchor();
635                 bv->switchKeyMap();
636                 break;
637
638         case LFUN_BREAK_PARAGRAPH_SKIP: {
639                 // When at the beginning of a paragraph, remove
640                 // indentation.  Otherwise, do the same as LFUN_BREAK_PARAGRAPH.
641                 cap::replaceSelection(cur);
642                 if (cur.pos() == 0)
643                         cur.paragraph().params().labelWidthString(docstring());
644                 else
645                         breakParagraph(cur, 0);
646                 cur.resetAnchor();
647                 bv->switchKeyMap();
648                 break;
649         }
650
651         case LFUN_PARAGRAPH_SPACING: {
652                 Paragraph & par = cur.paragraph();
653                 Spacing::Space cur_spacing = par.params().spacing().getSpace();
654                 string cur_value = "1.0";
655                 if (cur_spacing == Spacing::Other)
656                         cur_value = par.params().spacing().getValueAsString();
657
658                 istringstream is(to_utf8(cmd.argument()));
659                 string tmp;
660                 is >> tmp;
661                 Spacing::Space new_spacing = cur_spacing;
662                 string new_value = cur_value;
663                 if (tmp.empty()) {
664                         lyxerr << "Missing argument to `paragraph-spacing'"
665                                << endl;
666                 } else if (tmp == "single") {
667                         new_spacing = Spacing::Single;
668                 } else if (tmp == "onehalf") {
669                         new_spacing = Spacing::Onehalf;
670                 } else if (tmp == "double") {
671                         new_spacing = Spacing::Double;
672                 } else if (tmp == "other") {
673                         new_spacing = Spacing::Other;
674                         string tmpval = "0.0";
675                         is >> tmpval;
676                         lyxerr << "new_value = " << tmpval << endl;
677                         if (tmpval != "0.0")
678                                 new_value = tmpval;
679                 } else if (tmp == "default") {
680                         new_spacing = Spacing::Default;
681                 } else {
682                         lyxerr << to_utf8(_("Unknown spacing argument: "))
683                                << to_utf8(cmd.argument()) << endl;
684                 }
685                 if (cur_spacing != new_spacing || cur_value != new_value)
686                         par.params().spacing(Spacing(new_spacing, new_value));
687                 break;
688         }
689
690         case LFUN_INSET_INSERT: {
691                 recordUndo(cur);
692                 InsetBase * inset = createInset(bv, cmd);
693                 if (inset) {
694                         insertInset(cur, inset);
695                         cur.posRight();
696                 }
697                 break;
698         }
699
700         case LFUN_INSET_DISSOLVE:
701                 needsUpdate |= dissolveInset(cur);
702                 break;
703
704         case LFUN_INSET_SETTINGS:
705                 cur.inset().showInsetDialog(bv);
706                 break;
707
708         case LFUN_SPACE_INSERT:
709                 if (cur.paragraph().layout()->free_spacing)
710                         insertChar(cur, ' ');
711                 else {
712                         doInsertInset(cur, this, cmd, false, false);
713                         cur.posRight();
714                 }
715                 moveCursor(cur, false);
716                 break;
717
718         case LFUN_HYPHENATION_POINT_INSERT:
719                 specialChar(cur, InsetSpecialChar::HYPHENATION);
720                 break;
721
722         case LFUN_LIGATURE_BREAK_INSERT:
723                 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
724                 break;
725
726         case LFUN_DOTS_INSERT:
727                 specialChar(cur, InsetSpecialChar::LDOTS);
728                 break;
729
730         case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
731                 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
732                 break;
733
734         case LFUN_MENU_SEPARATOR_INSERT:
735                 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
736                 break;
737
738         case LFUN_WORD_UPCASE:
739                 changeCase(cur, LyXText::text_uppercase);
740                 break;
741
742         case LFUN_WORD_LOWCASE:
743                 changeCase(cur, LyXText::text_lowercase);
744                 break;
745
746         case LFUN_WORD_CAPITALIZE:
747                 changeCase(cur, LyXText::text_capitalization);
748                 break;
749
750         case LFUN_CHARS_TRANSPOSE:
751                 charsTranspose(cur);
752                 break;
753
754         case LFUN_PASTE:
755                 cur.message(_("Paste"));
756                 cap::replaceSelection(cur);
757                 if (isStrUnsignedInt(to_utf8(cmd.argument())))
758                         pasteSelection(cur, bv->buffer()->errorList("Paste"),
759                         convert<unsigned int>(to_utf8(cmd.argument())));
760                 else
761                         pasteSelection(cur, bv->buffer()->errorList("Paste"),
762                         0);
763                 bv->buffer()->errors("Paste");
764                 cur.clearSelection(); // bug 393
765                 bv->switchKeyMap();
766                 finishUndo();
767                 break;
768
769         case LFUN_CUT:
770                 cutSelection(cur, true, true);
771                 cur.message(_("Cut"));
772                 break;
773
774         case LFUN_COPY:
775                 copySelection(cur);
776                 cur.message(_("Copy"));
777                 break;
778
779         case LFUN_SERVER_GET_XY:
780                 cur.message(from_utf8(
781                         convert<string>(cursorX(cur.buffer(), cur.top(), cur.boundary()))
782                         + ' ' + convert<string>(cursorY(cur.top(), cur.boundary()))));
783                 break;
784
785         case LFUN_SERVER_SET_XY: {
786                 int x = 0;
787                 int y = 0;
788                 istringstream is(to_utf8(cmd.argument()));
789                 is >> x >> y;
790                 if (!is)
791                         lyxerr << "SETXY: Could not parse coordinates in '"
792                                << to_utf8(cmd.argument()) << std::endl;
793                 else
794                         setCursorFromCoordinates(cur, x, y);
795                 break;
796         }
797
798         case LFUN_SERVER_GET_FONT:
799                 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
800                         cur.message(from_ascii("E"));
801                 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
802                         cur.message(from_ascii("N"));
803                 else
804                         cur.message(from_ascii("0"));
805                 break;
806
807         case LFUN_SERVER_GET_LAYOUT:
808                 cur.message(from_utf8(cur.paragraph().layout()->name()));
809                 break;
810
811         case LFUN_LAYOUT: {
812                 string layout = to_ascii(cmd.argument());
813                 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) " << layout << endl;
814
815                 // Derive layout number from given argument (string)
816                 // and current buffer's textclass (number)
817                 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
818                 if (layout.empty())
819                         layout = tclass.defaultLayoutName();
820                 bool hasLayout = tclass.hasLayout(layout);
821
822                 // If the entry is obsolete, use the new one instead.
823                 if (hasLayout) {
824                         string const & obs = tclass[layout]->obsoleted_by();
825                         if (!obs.empty())
826                                 layout = obs;
827                 }
828
829                 if (!hasLayout) {
830                         cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
831                                 from_utf8(N_(" not known")));
832                         break;
833                 }
834
835                 bool change_layout = (current_layout != layout);
836
837                 if (!change_layout && cur.selection() &&
838                         cur.selBegin().pit() != cur.selEnd().pit())
839                 {
840                         pit_type spit = cur.selBegin().pit();
841                         pit_type epit = cur.selEnd().pit() + 1;
842                         while (spit != epit) {
843                                 if (pars_[spit].layout()->name() != current_layout) {
844                                         change_layout = true;
845                                         break;
846                                 }
847                                 ++spit;
848                         }
849                 }
850
851                 if (change_layout) {
852                         current_layout = layout;
853                         setLayout(cur, layout);
854                         // inform the GUI that the layout has changed.
855                         bv->layoutChanged(layout);
856                         bv->switchKeyMap();
857                 }
858                 break;
859         }
860
861         case LFUN_CLIPBOARD_PASTE: {
862                 cur.clearSelection();
863                 docstring const clip = theClipboard().get();
864                 if (!clip.empty()) {
865                         recordUndo(cur);
866                         if (cmd.argument() == "paragraph")
867                                 insertStringAsParagraphs(cur, clip);
868                         else
869                                 insertStringAsLines(cur, clip);
870                 }
871                 break;
872         }
873
874         case LFUN_PRIMARY_SELECTION_PASTE: {
875                 cur.clearSelection();
876                 docstring const clip = theSelection().get();
877                 if (!clip.empty()) {
878                         recordUndo(cur);
879                         if (cmd.argument() == "paragraph")
880                                 insertStringAsParagraphs(cur, clip);
881                         else
882                                 insertStringAsLines(cur, clip);
883                 }
884                 break;
885         }
886
887         case LFUN_UNICODE_INSERT: {
888                 if (cmd.argument().empty())
889                         break;
890                 docstring hexstring = cmd.argument();
891                 if (lyx::support::isHex(hexstring)) {
892                         char_type c = lyx::support::hexToInt(hexstring);
893                         if (c > 32 && c < 0x10ffff) {
894                                 lyxerr << "Inserting c: " << c << endl;
895                                 docstring s = docstring(1, c);
896                                 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
897                         }
898                 }
899                 break;
900         }
901                 
902         case LFUN_QUOTE_INSERT: {
903                 cap::replaceSelection(cur);
904                 Paragraph & par = cur.paragraph();
905                 pos_type pos = cur.pos();
906                 char_type c;
907                 if (pos == 0)
908                         c = ' ';
909                 else if (cur.prevInset() && cur.prevInset()->isSpace())
910                         c = ' ';
911                 else
912                         c = par.getChar(pos - 1);
913
914                 LyXLayout_ptr const & style = par.layout();
915
916                 BufferParams const & bufparams = bv->buffer()->params();
917                 if (!style->pass_thru
918                     && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
919                         string arg = to_utf8(cmd.argument());
920                         if (arg == "single")
921                                 cur.insert(new InsetQuotes(c,
922                                     bufparams.quotes_language,
923                                     InsetQuotes::SingleQ));
924                         else
925                                 cur.insert(new InsetQuotes(c,
926                                     bufparams.quotes_language,
927                                     InsetQuotes::DoubleQ));
928                         cur.posRight();
929                 }
930                 else
931                         lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
932                 break;
933         }
934
935         case LFUN_DATE_INSERT:
936                 if (cmd.argument().empty())
937                         lyx::dispatch(FuncRequest(LFUN_SELF_INSERT,
938                                 formatted_time(current_time())));
939                 else
940                         lyx::dispatch(FuncRequest(LFUN_SELF_INSERT,
941                                 formatted_time(current_time(), to_utf8(cmd.argument()))));
942                 break;
943
944         case LFUN_MOUSE_TRIPLE:
945                 if (cmd.button() == mouse_button::button1) {
946                         cursorHome(cur);
947                         cur.resetAnchor();
948                         cursorEnd(cur);
949                         cur.setSelection();
950                         bv->cursor() = cur;
951                         theSelection().haveSelection(cur.selection());
952                 }
953                 break;
954
955         case LFUN_MOUSE_DOUBLE:
956                 if (cmd.button() == mouse_button::button1) {
957                         selectWord(cur, WHOLE_WORD_STRICT);
958                         bv->cursor() = cur;
959                         theSelection().haveSelection(cur.selection());
960                 }
961                 break;
962
963         // Single-click on work area
964         case LFUN_MOUSE_PRESS: {
965                 // Right click on a footnote flag opens float menu
966                 if (cmd.button() == mouse_button::button3)
967                         cur.clearSelection();
968
969                 // Middle button press pastes if we have a selection
970                 // We do this here as if the selection was inside an inset
971                 // it could get cleared on the unlocking of the inset so
972                 // we have to check this first
973                 bool paste_internally = false;
974                 if (cmd.button() == mouse_button::button2 && cur.selection()) {
975                         lyx::dispatch(FuncRequest(LFUN_COPY));
976                         paste_internally = true;
977                 }
978
979                 bv->mouseSetCursor(cur);
980
981                 // Insert primary selection with middle mouse
982                 // if there is a local selection in the current buffer,
983                 // insert this
984                 if (cmd.button() == mouse_button::button2) {
985                         if (paste_internally)
986                                 lyx::dispatch(FuncRequest(LFUN_PASTE));
987                         else
988                                 lyx::dispatch(FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph"));
989                 }
990
991                 break;
992         }
993
994         case LFUN_MOUSE_MOTION: {
995                 // Only use motion with button 1
996                 //if (cmd.button() != mouse_button::button1)
997                 //      return false;
998
999                 // ignore motions deeper nested than the real anchor
1000                 LCursor & bvcur = cur.bv().cursor();
1001                 if (bvcur.anchor_.hasPart(cur)) {
1002                         CursorSlice old = bvcur.top();
1003
1004                         int const wh = bv->workHeight();
1005                         int const y = std::max(0, std::min(wh - 1, cmd.y));
1006
1007                         setCursorFromCoordinates(cur, cmd.x, y);
1008                         cur.x_target() = cmd.x;
1009                         if (cmd.y >= wh)
1010                                 cursorDown(cur);
1011                         else if (cmd.y < 0)
1012                                 cursorUp(cur);
1013                         // This is to allow jumping over large insets
1014                         if (cur.top() == old) {
1015                                 if (cmd.y >= wh)
1016                                         cursorDown(cur);
1017                                 else if (cmd.y < 0)
1018                                         cursorUp(cur);
1019                         }
1020
1021                         if (cur.top() == old)
1022                                 cur.noUpdate();
1023                         else {
1024                                 // don't set anchor_
1025                                 bvcur.setCursor(cur);
1026                                 bvcur.selection() = true;
1027                                 //lyxerr << "MOTION: " << bv->cursor() << endl;
1028                         }
1029
1030                 } else
1031                         cur.undispatched();
1032                 break;
1033         }
1034
1035         case LFUN_MOUSE_RELEASE: {
1036                 if (cmd.button() == mouse_button::button2)
1037                         break;
1038
1039                 // finish selection
1040                 if (cmd.button() == mouse_button::button1)
1041                         theSelection().haveSelection(cur.selection());
1042
1043                 bv->switchKeyMap();
1044                 break;
1045         }
1046
1047         case LFUN_SELF_INSERT: {
1048                 if (cmd.argument().empty())
1049                         break;
1050
1051                 // Automatically delete the currently selected
1052                 // text and replace it with what is being
1053                 // typed in now. Depends on lyxrc settings
1054                 // "auto_region_delete", which defaults to
1055                 // true (on).
1056
1057                 if (lyxrc.auto_region_delete) {
1058                         if (cur.selection())
1059                                 cutSelection(cur, false, false);
1060                         theSelection().haveSelection(false);
1061                 }
1062
1063                 cur.clearSelection();
1064                 LyXFont const old_font = real_current_font;
1065
1066                 docstring::const_iterator cit = cmd.argument().begin();
1067                 docstring::const_iterator end = cmd.argument().end();
1068                 for (; cit != end; ++cit)
1069 #if 0
1070                         bv->getIntl().getTransManager().
1071                                 translateAndInsert(*cit, this);
1072 #else
1073                         insertChar(bv->cursor(), *cit);
1074 #endif
1075
1076                 cur.resetAnchor();
1077                 moveCursor(cur, false);
1078                 break;
1079         }
1080
1081         case LFUN_URL_INSERT: {
1082                 InsetCommandParams p("url");
1083                 string const data = InsetCommandMailer::params2string("url", p);
1084                 bv->showInsetDialog("url", data, 0);
1085                 break;
1086         }
1087
1088         case LFUN_HTML_INSERT: {
1089                 InsetCommandParams p("htmlurl");
1090                 string const data = InsetCommandMailer::params2string("url", p);
1091                 bv->showInsetDialog("url", data, 0);
1092                 break;
1093         }
1094
1095         case LFUN_LABEL_INSERT: {
1096                 InsetCommandParams p("label");
1097                 // Try to generate a valid label
1098                 p["name"] = (cmd.argument().empty()) ?
1099                         cur.getPossibleLabel() :
1100                         cmd.argument();
1101                 string const data = InsetCommandMailer::params2string("label", p);
1102
1103                 if (cmd.argument().empty()) {
1104                         bv->showInsetDialog("label", data, 0);
1105                 } else {
1106                         FuncRequest fr(LFUN_INSET_INSERT, data);
1107                         dispatch(cur, fr);
1108                 }
1109                 break;
1110         }
1111
1112
1113 #if 0
1114         case LFUN_LIST_INSERT:
1115         case LFUN_THEOREM_INSERT:
1116         case LFUN_CAPTION_INSERT:
1117 #endif
1118         case LFUN_NOTE_INSERT:
1119         case LFUN_CHARSTYLE_INSERT:
1120         case LFUN_BOX_INSERT:
1121         case LFUN_BRANCH_INSERT:
1122         case LFUN_BIBITEM_INSERT:
1123         case LFUN_ERT_INSERT:
1124         case LFUN_FOOTNOTE_INSERT:
1125         case LFUN_MARGINALNOTE_INSERT:
1126         case LFUN_OPTIONAL_INSERT:
1127         case LFUN_ENVIRONMENT_INSERT:
1128                 // Open the inset, and move the current selection
1129                 // inside it.
1130                 doInsertInset(cur, this, cmd, true, true);
1131                 cur.posRight();
1132                 break;
1133
1134         case LFUN_TABULAR_INSERT:
1135                 // if there were no arguments, just open the dialog
1136                 if (doInsertInset(cur, this, cmd, false, true))
1137                         cur.posRight();
1138                 else
1139                         bv->showDialog("tabularcreate");
1140
1141                 break;
1142
1143         case LFUN_FLOAT_INSERT:
1144         case LFUN_FLOAT_WIDE_INSERT:
1145         case LFUN_WRAP_INSERT:
1146                 doInsertInset(cur, this, cmd, true, true);
1147                 cur.posRight();
1148                 // FIXME: the "Caption" name should not be hardcoded,
1149                 // but given by the float definition.
1150                 cur.dispatch(FuncRequest(LFUN_LAYOUT, "Caption"));
1151                 break;
1152
1153         case LFUN_INDEX_INSERT:
1154         case LFUN_NOMENCL_INSERT: {
1155                 InsetBase * inset = createInset(&cur.bv(), cmd);
1156                 if (!inset)
1157                         break;
1158
1159                 recordUndo(cur);
1160                 cur.clearSelection();
1161                 insertInset(cur, inset);
1162                 inset->edit(cur, true);
1163                 // Show the dialog for the nomenclature entry, since the
1164                 // description entry still needs to be filled in.
1165                 if (cmd.action == LFUN_NOMENCL_INSERT)
1166                         InsetCommandMailer("nomenclature",
1167                                 *reinterpret_cast<InsetCommand *>(inset)).showDialog(&cur.bv());
1168                 cur.posRight();
1169                 break;
1170         }
1171
1172         case LFUN_INDEX_PRINT:
1173         case LFUN_NOMENCL_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(from_utf8(N_("Missing argument")));
1207                 else {
1208                         string s = 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(from_utf8(token(s, ' ', 0)), nargs, from_utf8(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(to_utf8(cmd.argument()));
1294                 toggleAndShow(cur, this, font);
1295                 break;
1296         }
1297
1298         case LFUN_LANGUAGE: {
1299                 Language const * lang = languages.getLanguage(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(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, cur);
1389                 break;
1390
1391         case LFUN_FLOAT_LIST: {
1392                 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1393                 if (tclass.floats().typeExist(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, docstring(), 0);
1406                         insertInset(cur, new InsetFloatList(to_utf8(cmd.argument())));
1407                         cur.posRight();
1408                 } else {
1409                         lyxerr << "Non-existent float type: "
1410                                << 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, WHOLE_WORD);
1433                                 arg = cur.selectionAsString(false);
1434                         }
1435                 }
1436                 bv->showDialogWithData("thesaurus", 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(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         needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
1477         if (singleParUpdate)
1478                 // Inserting characters does not change par height
1479                 if (cur.bottom().paragraph().dim().height()
1480                     == olddim.height()) {
1481                         // if so, update _only_ this paragraph
1482                         cur.updateFlags(Update::SinglePar |
1483                                 Update::FitCursor |
1484                                 Update::MultiParSel);
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.updateFlags(Update::Force | Update::FitCursor);
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() == "nomenclature")
1550                         code = InsetBase::NOMENCL_CODE;
1551                 else if (cmd.argument() == "label")
1552                         code = InsetBase::LABEL_CODE;
1553                 else if (cmd.argument() == "note")
1554                         code = InsetBase::NOTE_CODE;
1555                 else if (cmd.argument() == "ref")
1556                         code = InsetBase::REF_CODE;
1557                 else if (cmd.argument() == "toc")
1558                         code = InsetBase::TOC_CODE;
1559                 else if (cmd.argument() == "url")
1560                         code = InsetBase::URL_CODE;
1561                 else if (cmd.argument() == "vspace")
1562                         code = InsetBase::VSPACE_CODE;
1563                 else if (cmd.argument() == "wrap")
1564                         code = InsetBase::WRAP_CODE;
1565                 break;
1566
1567         case LFUN_ERT_INSERT:
1568                 code = InsetBase::ERT_CODE;
1569                 break;
1570         case LFUN_FOOTNOTE_INSERT:
1571                 code = InsetBase::FOOT_CODE;
1572                 break;
1573         case LFUN_TABULAR_INSERT:
1574                 code = InsetBase::TABULAR_CODE;
1575                 break;
1576         case LFUN_MARGINALNOTE_INSERT:
1577                 code = InsetBase::MARGIN_CODE;
1578                 break;
1579         case LFUN_FLOAT_INSERT:
1580         case LFUN_FLOAT_WIDE_INSERT:
1581                 code = InsetBase::FLOAT_CODE;
1582                 break;
1583         case LFUN_WRAP_INSERT:
1584                 code = InsetBase::WRAP_CODE;
1585                 break;
1586         case LFUN_FLOAT_LIST:
1587                 code = InsetBase::FLOAT_LIST_CODE;
1588                 break;
1589 #if 0
1590         case LFUN_LIST_INSERT:
1591                 code = InsetBase::LIST_CODE;
1592                 break;
1593         case LFUN_THEOREM_INSERT:
1594                 code = InsetBase::THEOREM_CODE;
1595                 break;
1596 #endif
1597         case LFUN_CAPTION_INSERT:
1598                 code = InsetBase::CAPTION_CODE;
1599                 break;
1600         case LFUN_NOTE_INSERT:
1601                 code = InsetBase::NOTE_CODE;
1602                 break;
1603         case LFUN_CHARSTYLE_INSERT:
1604                 code = InsetBase::CHARSTYLE_CODE;
1605                 if (cur.buffer().params().getLyXTextClass().charstyles().empty())
1606                         enable = false;
1607                 break;
1608         case LFUN_BOX_INSERT:
1609                 code = InsetBase::BOX_CODE;
1610                 break;
1611         case LFUN_BRANCH_INSERT:
1612                 code = InsetBase::BRANCH_CODE;
1613                 if (cur.buffer().getMasterBuffer()->params().branchlist().empty())
1614                         enable = false;
1615                 break;
1616         case LFUN_LABEL_INSERT:
1617                 code = InsetBase::LABEL_CODE;
1618                 break;
1619         case LFUN_OPTIONAL_INSERT:
1620                 code = InsetBase::OPTARG_CODE;
1621                 enable = numberOfOptArgs(cur.paragraph())
1622                         < cur.paragraph().layout()->optionalargs;
1623                 break;
1624         case LFUN_ENVIRONMENT_INSERT:
1625                 code = InsetBase::BOX_CODE;
1626                 break;
1627         case LFUN_INDEX_INSERT:
1628                 code = InsetBase::INDEX_CODE;
1629                 break;
1630         case LFUN_INDEX_PRINT:
1631                 code = InsetBase::INDEX_PRINT_CODE;
1632                 break;
1633         case LFUN_NOMENCL_INSERT:
1634                 code = InsetBase::NOMENCL_CODE;
1635                 break;
1636         case LFUN_NOMENCL_PRINT:
1637                 code = InsetBase::NOMENCL_PRINT_CODE;
1638                 break;
1639         case LFUN_TOC_INSERT:
1640                 code = InsetBase::TOC_CODE;
1641                 break;
1642         case LFUN_HTML_INSERT:
1643         case LFUN_URL_INSERT:
1644                 code = InsetBase::URL_CODE;
1645                 break;
1646         case LFUN_QUOTE_INSERT:
1647                 // always allow this, since we will inset a raw quote
1648                 // if an inset is not allowed.
1649                 break;
1650         case LFUN_HYPHENATION_POINT_INSERT:
1651         case LFUN_LIGATURE_BREAK_INSERT:
1652         case LFUN_HFILL_INSERT:
1653         case LFUN_MENU_SEPARATOR_INSERT:
1654         case LFUN_DOTS_INSERT:
1655         case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
1656                 code = InsetBase::SPECIALCHAR_CODE;
1657                 break;
1658         case LFUN_SPACE_INSERT:
1659                 // slight hack: we know this is allowed in math mode
1660                 if (cur.inTexted())
1661                         code = InsetBase::SPACE_CODE;
1662                 break;
1663
1664         case LFUN_INSET_MODIFY:
1665                 // We need to disable this, because we may get called for a
1666                 // tabular cell via
1667                 // InsetTabular::getStatus() -> InsetText::getStatus()
1668                 // and we don't handle LFUN_INSET_MODIFY.
1669                 enable = false;
1670                 break;
1671
1672         case LFUN_FONT_EMPH:
1673                 flag.setOnOff(font.emph() == LyXFont::ON);
1674                 return true;
1675
1676         case LFUN_FONT_NOUN:
1677                 flag.setOnOff(font.noun() == LyXFont::ON);
1678                 return true;
1679
1680         case LFUN_FONT_BOLD:
1681                 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1682                 return true;
1683
1684         case LFUN_FONT_SANS:
1685                 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1686                 return true;
1687
1688         case LFUN_FONT_ROMAN:
1689                 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1690                 return true;
1691
1692         case LFUN_FONT_CODE:
1693                 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1694                 return true;
1695
1696         case LFUN_CUT:
1697         case LFUN_COPY:
1698                 enable = cur.selection();
1699                 break;
1700
1701         case LFUN_PASTE:
1702                 enable = cap::numberOfSelections() > 0;
1703                 break;
1704
1705         case LFUN_PARAGRAPH_MOVE_UP:
1706                 enable = cur.pit() > 0 && !cur.selection();
1707                 break;
1708
1709         case LFUN_PARAGRAPH_MOVE_DOWN:
1710                 enable = cur.pit() < cur.lastpit() && !cur.selection();
1711                 break;
1712
1713         case LFUN_INSET_DISSOLVE:
1714                 enable = !isMainText(*cur.bv().buffer()) && cur.inset().nargs() == 1;
1715                 break;
1716
1717         case LFUN_CHANGE_ACCEPT:
1718         case LFUN_CHANGE_REJECT:
1719                 enable = true; // FIXME: Change tracking (MG)
1720                 break;
1721
1722         case LFUN_WORD_DELETE_FORWARD:
1723         case LFUN_WORD_DELETE_BACKWARD:
1724         case LFUN_LINE_DELETE:
1725         case LFUN_WORD_FORWARD:
1726         case LFUN_WORD_BACKWARD:
1727         case LFUN_CHAR_FORWARD:
1728         case LFUN_CHAR_FORWARD_SELECT:
1729         case LFUN_CHAR_BACKWARD:
1730         case LFUN_CHAR_BACKWARD_SELECT:
1731         case LFUN_UP:
1732         case LFUN_UP_SELECT:
1733         case LFUN_DOWN:
1734         case LFUN_DOWN_SELECT:
1735         case LFUN_PARAGRAPH_UP_SELECT:
1736         case LFUN_PARAGRAPH_DOWN_SELECT:
1737         case LFUN_SCREEN_UP_SELECT:
1738         case LFUN_SCREEN_DOWN_SELECT:
1739         case LFUN_LINE_BEGIN_SELECT:
1740         case LFUN_LINE_END_SELECT:
1741         case LFUN_WORD_FORWARD_SELECT:
1742         case LFUN_WORD_BACKWARD_SELECT:
1743         case LFUN_WORD_SELECT:
1744         case LFUN_PARAGRAPH_UP:
1745         case LFUN_PARAGRAPH_DOWN:
1746         case LFUN_SCREEN_UP:
1747         case LFUN_SCREEN_DOWN:
1748         case LFUN_LINE_BEGIN:
1749         case LFUN_LINE_END:
1750         case LFUN_BREAK_LINE:
1751         case LFUN_CHAR_DELETE_FORWARD:
1752         case LFUN_DELETE_FORWARD_SKIP:
1753         case LFUN_CHAR_DELETE_BACKWARD:
1754         case LFUN_DELETE_BACKWARD_SKIP:
1755         case LFUN_BREAK_PARAGRAPH:
1756         case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
1757         case LFUN_BREAK_PARAGRAPH_SKIP:
1758         case LFUN_PARAGRAPH_SPACING:
1759         case LFUN_INSET_INSERT:
1760         case LFUN_WORD_UPCASE:
1761         case LFUN_WORD_LOWCASE:
1762         case LFUN_WORD_CAPITALIZE:
1763         case LFUN_CHARS_TRANSPOSE:
1764         case LFUN_SERVER_GET_XY:
1765         case LFUN_SERVER_SET_XY:
1766         case LFUN_SERVER_GET_FONT:
1767         case LFUN_SERVER_GET_LAYOUT:
1768         case LFUN_LAYOUT:
1769         case LFUN_CLIPBOARD_PASTE:
1770         case LFUN_PRIMARY_SELECTION_PASTE:
1771         case LFUN_DATE_INSERT:
1772         case LFUN_SELF_INSERT:
1773         case LFUN_LINE_INSERT:
1774         case LFUN_PAGEBREAK_INSERT:
1775         case LFUN_MATH_DISPLAY:
1776         case LFUN_MATH_IMPORT_SELECTION:
1777         case LFUN_MATH_MODE:
1778         case LFUN_MATH_MACRO:
1779         case LFUN_MATH_MATRIX:
1780         case LFUN_MATH_DELIM:
1781         case LFUN_MATH_BIGDELIM:
1782         case LFUN_MATH_SUBSCRIPT:
1783         case LFUN_MATH_SUPERSCRIPT:
1784         case LFUN_FONT_DEFAULT:
1785         case LFUN_FONT_UNDERLINE:
1786         case LFUN_FONT_SIZE:
1787         case LFUN_LANGUAGE:
1788         case LFUN_FONT_FREE_APPLY:
1789         case LFUN_FONT_FREE_UPDATE:
1790         case LFUN_LAYOUT_PARAGRAPH:
1791         case LFUN_PARAGRAPH_UPDATE:
1792         case LFUN_ACCENT_UMLAUT:
1793         case LFUN_ACCENT_CIRCUMFLEX:
1794         case LFUN_ACCENT_GRAVE:
1795         case LFUN_ACCENT_ACUTE:
1796         case LFUN_ACCENT_TILDE:
1797         case LFUN_ACCENT_CEDILLA:
1798         case LFUN_ACCENT_MACRON:
1799         case LFUN_ACCENT_DOT:
1800         case LFUN_ACCENT_UNDERDOT:
1801         case LFUN_ACCENT_UNDERBAR:
1802         case LFUN_ACCENT_CARON:
1803         case LFUN_ACCENT_SPECIAL_CARON:
1804         case LFUN_ACCENT_BREVE:
1805         case LFUN_ACCENT_TIE:
1806         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1807         case LFUN_ACCENT_CIRCLE:
1808         case LFUN_ACCENT_OGONEK:
1809         case LFUN_THESAURUS_ENTRY:
1810         case LFUN_PARAGRAPH_PARAMS_APPLY:
1811         case LFUN_ESCAPE:
1812         case LFUN_BUFFER_END:
1813         case LFUN_BUFFER_BEGIN:
1814         case LFUN_BUFFER_BEGIN_SELECT:
1815         case LFUN_BUFFER_END_SELECT:
1816         case LFUN_UNICODE_INSERT:
1817                 // these are handled in our dispatch()
1818                 enable = true;
1819                 break;
1820
1821         default:
1822                 return false;
1823         }
1824
1825         if (code != InsetBase::NO_CODE
1826             && (cur.empty() || !cur.inset().insetAllowed(code)))
1827                 enable = false;
1828
1829         flag.enabled(enable);
1830         return true;
1831 }
1832
1833
1834 } // namespace lyx