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