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