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