]> git.lyx.org Git - lyx.git/blob - src/text3.C
4a494ecd466867590e8a0f0eb46833f58adf02bf
[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 "factory.h"
33 #include "funcrequest.h"
34 #include "gettext.h"
35 #include "intl.h"
36 #include "language.h"
37 #include "LyXAction.h"
38 #include "lyxfunc.h"
39 #include "lyxlex.h"
40 #include "lyxrc.h"
41 #include "lyxrow.h"
42 #include "paragraph.h"
43 #include "paragraph_funcs.h"
44 #include "ParagraphParameters.h"
45 #include "undo.h"
46 #include "vspace.h"
47 #include "pariterator.h"
48
49 #include "frontends/Dialogs.h"
50 #include "frontends/Gui.h"
51 #include "frontends/LyXView.h"
52 #include "frontends/Clipboard.h"
53 #include "frontends/Selection.h"
54
55 #include "insets/insetcommand.h"
56 #include "insets/insetfloatlist.h"
57 #include "insets/insetnewline.h"
58 #include "insets/insetquotes.h"
59 #include "insets/insetspecialchar.h"
60 #include "insets/insettext.h"
61
62 #include "support/lstrings.h"
63 #include "support/lyxlib.h"
64 #include "support/convert.h"
65 #include "support/lyxtime.h"
66
67 #include "mathed/math_hullinset.h"
68 #include "mathed/math_macrotemplate.h"
69
70 #include <boost/current_function.hpp>
71
72 #include <clocale>
73 #include <sstream>
74
75 using lyx::pos_type;
76
77 using lyx::cap::copySelection;
78 using lyx::cap::cutSelection;
79 using lyx::cap::pasteSelection;
80 using lyx::cap::replaceSelection;
81
82 using lyx::support::isStrUnsignedInt;
83 using lyx::support::token;
84
85 using lyx::frontend::Gui;
86 using lyx::frontend::Clipboard;
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 = 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("simple"));
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() == "none")
178                                         // Don't create pseudo formulas if
179                                         // delimiters are left out
180                                         formula->mutate("simple");
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_WORD_FORWARD:
403                 if (!cur.mark())
404                         cur.clearSelection();
405                 if (isRTL(cur.paragraph()))
406                         needsUpdate = cursorLeftOneWord(cur);
407                 else
408                         needsUpdate = cursorRightOneWord(cur);
409                 finishChange(cur, false);
410                 break;
411
412         case LFUN_WORD_BACKWARD:
413                 if (!cur.mark())
414                         cur.clearSelection();
415                 if (isRTL(cur.paragraph()))
416                         needsUpdate = cursorRightOneWord(cur);
417                 else
418                         needsUpdate = cursorLeftOneWord(cur);
419                 finishChange(cur, false);
420                 break;
421
422         case LFUN_BUFFER_BEGIN:
423                 if (cur.depth() == 1) {
424                         if (!cur.mark())
425                                 cur.clearSelection();
426                         needsUpdate = cursorTop(cur);
427                         finishChange(cur, false);
428                 } else {
429                         cur.undispatched();
430                 }
431                 break;
432
433         case LFUN_BUFFER_BEGIN_SELECT:
434                 if (cur.depth() == 1) {
435                         if (!cur.selection())
436                                 cur.resetAnchor();
437                         needsUpdate = cursorTop(cur);
438                         finishChange(cur, true);
439                 } else {
440                         cur.undispatched();
441                 }
442                 break;
443
444         case LFUN_BUFFER_END:
445                 if (cur.depth() == 1) {
446                         if (!cur.mark())
447                                 cur.clearSelection();
448                         needsUpdate = cursorBottom(cur);
449                         finishChange(cur, false);
450                 } else {
451                         cur.undispatched();
452                 }
453                 break;
454
455         case LFUN_BUFFER_END_SELECT:
456                 if (cur.depth() == 1) {
457                         if (!cur.selection())
458                                 cur.resetAnchor();
459                         needsUpdate = cursorBottom(cur);
460                         finishChange(cur, true);
461                 } else {
462                         cur.undispatched();
463                 }
464                 break;
465
466         case LFUN_CHAR_FORWARD:
467         case LFUN_CHAR_FORWARD_SELECT:
468                 //lyxerr << BOOST_CURRENT_FUNCTION
469                 //       << " LFUN_CHAR_FORWARD[SEL]:\n" << cur << endl;
470                 cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
471                 if (isRTL(cur.paragraph()))
472                         needsUpdate = cursorLeft(cur);
473                 else
474                         needsUpdate = cursorRight(cur);
475
476                 if (!needsUpdate && oldTopSlice == cur.top()
477                                 && cur.boundary() == oldBoundary) {
478                         cur.undispatched();
479                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
480                 }
481                 break;
482
483         case LFUN_CHAR_BACKWARD:
484         case LFUN_CHAR_BACKWARD_SELECT:
485                 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
486                 cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
487                 if (isRTL(cur.paragraph()))
488                         needsUpdate = cursorRight(cur);
489                 else
490                         needsUpdate = cursorLeft(cur);
491
492                 if (!needsUpdate && oldTopSlice == cur.top()
493                         && cur.boundary() == oldBoundary) {
494                         cur.undispatched();
495                         cmd = FuncRequest(LFUN_FINISHED_LEFT);
496                 }
497                 break;
498
499         case LFUN_UP:
500         case LFUN_UP_SELECT:
501                 update(cur);
502                 //lyxerr << "handle LFUN_UP[SEL]:\n" << cur << endl;
503                 cur.selHandle(cmd.action == LFUN_UP_SELECT);
504
505                 needsUpdate = cursorUp(cur);
506                 if (!needsUpdate && oldTopSlice == cur.top()
507                           && cur.boundary() == oldBoundary) {
508                         cur.undispatched();
509                         cmd = FuncRequest(LFUN_FINISHED_UP);
510                 }
511                 break;
512
513         case LFUN_DOWN:
514         case LFUN_DOWN_SELECT:
515                 update(cur);
516                 //lyxerr << "handle LFUN_DOWN[SEL]:\n" << cur << endl;
517                 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
518                 needsUpdate = cursorDown(cur);
519                 if (!needsUpdate && oldTopSlice == cur.top() &&
520                     cur.boundary() == oldBoundary)
521                 {
522                         cur.undispatched();
523                         cmd = FuncRequest(LFUN_FINISHED_DOWN);
524                 }
525                 break;
526
527         case LFUN_PARAGRAPH_UP:
528                 if (!cur.mark())
529                         cur.clearSelection();
530                 needsUpdate = cursorUpParagraph(cur);
531                 finishChange(cur, false);
532                 break;
533
534         case LFUN_PARAGRAPH_UP_SELECT:
535                 if (!cur.selection())
536                         cur.resetAnchor();
537                 cursorUpParagraph(cur);
538                 finishChange(cur, true);
539                 break;
540
541         case LFUN_PARAGRAPH_DOWN:
542                 if (!cur.mark())
543                         cur.clearSelection();
544                 needsUpdate = cursorDownParagraph(cur);
545                 finishChange(cur, false);
546                 break;
547
548         case LFUN_PARAGRAPH_DOWN_SELECT:
549                 if (!cur.selection())
550                         cur.resetAnchor();
551                 cursorDownParagraph(cur);
552                 finishChange(cur, true);
553                 break;
554
555         case LFUN_SCREEN_UP_SELECT:
556                 update(cur);
557                 if (!cur.selection())
558                         cur.resetAnchor();
559                 needsUpdate = cursorPrevious(cur);
560                 finishChange(cur, true);
561                 break;
562
563         case LFUN_SCREEN_DOWN_SELECT:
564                 update(cur);
565                 if (!cur.selection())
566                         cur.resetAnchor();
567                 needsUpdate = cursorNext(cur);
568                 finishChange(cur, true);
569                 break;
570
571         case LFUN_LINE_BEGIN_SELECT:
572                 update(cur);
573                 if (!cur.selection())
574                         cur.resetAnchor();
575                 needsUpdate = cursorHome(cur);
576                 finishChange(cur, true);
577                 break;
578
579         case LFUN_LINE_END_SELECT:
580                 update(cur);
581                 if (!cur.selection())
582                         cur.resetAnchor();
583                 needsUpdate = cursorEnd(cur);
584                 finishChange(cur, true);
585                 break;
586
587         case LFUN_WORD_FORWARD_SELECT:
588                 if (!cur.selection())
589                         cur.resetAnchor();
590                 if (isRTL(cur.paragraph()))
591                         cursorLeftOneWord(cur);
592                 else
593                         cursorRightOneWord(cur);
594                 finishChange(cur, true);
595                 break;
596
597         case LFUN_WORD_BACKWARD_SELECT:
598                 if (!cur.selection())
599                         cur.resetAnchor();
600                 if (isRTL(cur.paragraph()))
601                         cursorRightOneWord(cur);
602                 else
603                         cursorLeftOneWord(cur);
604                 finishChange(cur, true);
605                 break;
606
607         case LFUN_WORD_SELECT: {
608                 selectWord(cur, lyx::WHOLE_WORD);
609                 finishChange(cur, true);
610                 break;
611         }
612
613         case LFUN_SCREEN_UP:
614                 update(cur);
615                 if (!cur.mark())
616                         cur.clearSelection();
617                 finishChange(cur, false);
618                 if (cur.pit() == 0 && cur.textRow().pos() == 0) {
619                         cur.undispatched();
620                         cmd = FuncRequest(LFUN_FINISHED_UP);
621                 } else {
622                         needsUpdate = cursorPrevious(cur);
623                 }
624                 break;
625
626         case LFUN_SCREEN_DOWN:
627                 update(cur);
628                 if (!cur.mark())
629                         cur.clearSelection();
630                 finishChange(cur, false);
631                 if (cur.pit() == cur.lastpit()
632                           && cur.textRow().endpos() == cur.lastpos()) {
633                         cur.undispatched();
634                         cmd = FuncRequest(LFUN_FINISHED_DOWN);
635                 } else {
636                         needsUpdate = cursorNext(cur);
637                 }
638                 break;
639
640         case LFUN_LINE_BEGIN:
641                 if (!cur.mark())
642                         cur.clearSelection();
643                 needsUpdate = cursorHome(cur);
644                 finishChange(cur, false);
645                 break;
646
647         case LFUN_LINE_END:
648                 if (!cur.mark())
649                         cur.clearSelection();
650                 needsUpdate = cursorEnd(cur);
651                 finishChange(cur, false);
652                 break;
653
654         case LFUN_BREAK_LINE: {
655                 // Not allowed by LaTeX (labels or empty par)
656                 if (cur.pos() > cur.paragraph().beginOfBody()) {
657                         lyx::cap::replaceSelection(cur);
658                         cur.insert(new InsetNewline);
659                         cur.posRight();
660                         moveCursor(cur, false);
661                 }
662                 break;
663         }
664
665         case LFUN_CHAR_DELETE_FORWARD:
666                 if (!cur.selection()) {
667                         if (cur.pos() == cur.paragraph().size())
668                                 // Par boundary, force full-screen update
669                                 singleParUpdate = false;
670                         needsUpdate = erase(cur);
671                         cur.resetAnchor();
672                         // It is possible to make it a lot faster still
673                         // just comment out the line below...
674                 } else {
675                         cutSelection(cur, true, false);
676                         singleParUpdate = false;
677                 }
678                 moveCursor(cur, false);
679                 break;
680
681         case LFUN_DELETE_FORWARD_SKIP:
682                 // Reverse the effect of LFUN_BREAK_PARAGRAPH_SKIP.
683                 if (!cur.selection()) {
684                         if (cur.pos() == cur.lastpos()) {
685                                 cursorRight(cur);
686                                 cursorLeft(cur);
687                         }
688                         erase(cur);
689                         cur.resetAnchor();
690                 } else {
691                         cutSelection(cur, true, false);
692                 }
693                 break;
694
695
696         case LFUN_CHAR_DELETE_BACKWARD:
697                 if (!cur.selection()) {
698                         if (bv->owner()->getIntl().getTransManager().backspace()) {
699                                 // Par boundary, full-screen update
700                                 if (cur.pos() == 0)
701                                         singleParUpdate = false;
702                                 needsUpdate = backspace(cur);
703                                 cur.resetAnchor();
704                                 // It is possible to make it a lot faster still
705                                 // just comment out the line below...
706                         }
707                 } else {
708                         cutSelection(cur, true, false);
709                         singleParUpdate = false;
710                 }
711                 bv->switchKeyMap();
712                 break;
713
714         case LFUN_DELETE_BACKWARD_SKIP:
715                 // Reverse the effect of LFUN_BREAK_PARAGRAPH_SKIP.
716                 if (!cur.selection()) {
717 #ifdef WITH_WARNINGS
718 #warning look here
719 #endif
720                         //CursorSlice cur = cursor();
721                         backspace(cur);
722                         //anchor() = cur;
723                 } else {
724                         cutSelection(cur, true, false);
725                 }
726                 break;
727
728         case LFUN_BREAK_PARAGRAPH:
729                 lyx::cap::replaceSelection(cur);
730                 breakParagraph(cur, 0);
731                 cur.resetAnchor();
732                 bv->switchKeyMap();
733                 break;
734
735         case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
736                 lyx::cap::replaceSelection(cur);
737                 breakParagraph(cur, 1);
738                 cur.resetAnchor();
739                 bv->switchKeyMap();
740                 break;
741
742         case LFUN_BREAK_PARAGRAPH_SKIP: {
743                 // When at the beginning of a paragraph, remove
744                 // indentation.  Otherwise, do the same as LFUN_BREAK_PARAGRAPH.
745                 lyx::cap::replaceSelection(cur);
746                 if (cur.pos() == 0)
747                         cur.paragraph().params().labelWidthString(string());
748                 else
749                         breakParagraph(cur, 0);
750                 cur.resetAnchor();
751                 bv->switchKeyMap();
752                 break;
753         }
754
755         case LFUN_PARAGRAPH_SPACING: {
756                 Paragraph & par = cur.paragraph();
757                 Spacing::Space cur_spacing = par.params().spacing().getSpace();
758                 string cur_value = "1.0";
759                 if (cur_spacing == Spacing::Other)
760                         cur_value = par.params().spacing().getValueAsString();
761
762                 istringstream is(cmd.argument);
763                 string tmp;
764                 is >> tmp;
765                 Spacing::Space new_spacing = cur_spacing;
766                 string new_value = cur_value;
767                 if (tmp.empty()) {
768                         lyxerr << "Missing argument to `paragraph-spacing'"
769                                << endl;
770                 } else if (tmp == "single") {
771                         new_spacing = Spacing::Single;
772                 } else if (tmp == "onehalf") {
773                         new_spacing = Spacing::Onehalf;
774                 } else if (tmp == "double") {
775                         new_spacing = Spacing::Double;
776                 } else if (tmp == "other") {
777                         new_spacing = Spacing::Other;
778                         string tmpval = "0.0";
779                         is >> tmpval;
780                         lyxerr << "new_value = " << tmpval << endl;
781                         if (tmpval != "0.0")
782                                 new_value = tmpval;
783                 } else if (tmp == "default") {
784                         new_spacing = Spacing::Default;
785                 } else {
786                         lyxerr << _("Unknown spacing argument: ")
787                                << cmd.argument << endl;
788                 }
789                 if (cur_spacing != new_spacing || cur_value != new_value)
790                         par.params().spacing(Spacing(new_spacing, new_value));
791                 break;
792         }
793
794         case LFUN_INSET_INSERT: {
795                 recordUndo(cur);
796                 InsetBase * inset = createInset(bv, cmd);
797                 if (inset) {
798                         insertInset(cur, inset);
799                         cur.posRight();
800                 }
801                 break;
802         }
803
804         case LFUN_INSET_SETTINGS:
805                 cur.inset().showInsetDialog(bv);
806                 break;
807
808         case LFUN_NEXT_INSET_TOGGLE: {
809                 InsetBase * inset = cur.nextInset();
810                 // this is the real function we want to invoke
811                 cmd = FuncRequest(LFUN_INSET_TOGGLE);
812                 cur.undispatched();
813                 // if there is an inset at cursor, see whether it
814                 // wants to toggle.
815                 if (inset) {
816                         LCursor tmpcur = cur;
817                         tmpcur.pushLeft(*inset);
818                         inset->dispatch(tmpcur, cmd);
819                         if (tmpcur.result().dispatched()) {
820                                 cur.clearSelection();
821                                 cur.dispatched();
822                         }
823                 }
824                 // if it did not work, try the underlying inset.
825                 if (!cur.result().dispatched())
826                         cur.inset().dispatch(cur, cmd);
827                 break;
828         }
829
830         case LFUN_SPACE_INSERT:
831                 if (cur.paragraph().layout()->free_spacing)
832                         insertChar(cur, ' ');
833                 else {
834                         doInsertInset(cur, this, cmd, false, false);
835                         cur.posRight();
836                 }
837                 moveCursor(cur, false);
838                 break;
839
840         case LFUN_HYPHENATION_POINT_INSERT:
841                 specialChar(cur, InsetSpecialChar::HYPHENATION);
842                 break;
843
844         case LFUN_LIGATURE_BREAK_INSERT:
845                 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
846                 break;
847
848         case LFUN_DOTS_INSERT:
849                 specialChar(cur, InsetSpecialChar::LDOTS);
850                 break;
851
852         case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
853                 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
854                 break;
855
856         case LFUN_MENU_SEPARATOR_INSERT:
857                 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
858                 break;
859
860         case LFUN_WORD_UPCASE:
861                 changeCase(cur, LyXText::text_uppercase);
862                 break;
863
864         case LFUN_WORD_LOWCASE:
865                 changeCase(cur, LyXText::text_lowercase);
866                 break;
867
868         case LFUN_WORD_CAPITALIZE:
869                 changeCase(cur, LyXText::text_capitalization);
870                 break;
871
872         case LFUN_CHARS_TRANSPOSE:
873                 recordUndo(cur);
874                 break;
875
876         case LFUN_PASTE:
877                 cur.message(_("Paste"));
878                 lyx::cap::replaceSelection(cur);
879                 if (isStrUnsignedInt(cmd.argument))
880                         pasteSelection(cur, convert<unsigned int>(cmd.argument));
881                 else
882                         pasteSelection(cur, 0);
883                 cur.clearSelection(); // bug 393
884                 bv->switchKeyMap();
885                 finishUndo();
886                 break;
887
888         case LFUN_CUT:
889                 cutSelection(cur, true, true);
890                 cur.message(_("Cut"));
891                 break;
892
893         case LFUN_COPY:
894                 copySelection(cur);
895                 cur.message(_("Copy"));
896                 break;
897
898         case LFUN_SERVER_GET_XY:
899                 cur.message(convert<string>(cursorX(cur.top(), cur.boundary())) + ' '
900                           + convert<string>(cursorY(cur.top(), cur.boundary())));
901                 break;
902
903         case LFUN_SERVER_SET_XY: {
904                 int x = 0;
905                 int y = 0;
906                 istringstream is(cmd.argument);
907                 is >> x >> y;
908                 if (!is)
909                         lyxerr << "SETXY: Could not parse coordinates in '"
910                                << cmd.argument << std::endl;
911                 else
912                         setCursorFromCoordinates(cur, x, y);
913                 break;
914         }
915
916         case LFUN_SERVER_GET_FONT:
917                 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
918                         cur.message("E");
919                 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
920                         cur.message("N");
921                 else
922                         cur.message("0");
923                 break;
924
925         case LFUN_SERVER_GET_LAYOUT:
926                 cur.message(cur.paragraph().layout()->name());
927                 break;
928
929         case LFUN_LAYOUT: {
930                 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
931                   << cmd.argument << endl;
932
933                 // This is not the good solution to the empty argument
934                 // problem, but it will hopefully suffice for 1.2.0.
935                 // The correct solution would be to augument the
936                 // function list/array with information about what
937                 // functions needs arguments and their type.
938                 if (cmd.argument.empty()) {
939                         cur.errorMessage(_("LyX function 'layout' needs an argument."));
940                         break;
941                 }
942
943                 // Derive layout number from given argument (string)
944                 // and current buffer's textclass (number)
945                 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
946                 bool hasLayout = tclass.hasLayout(cmd.argument);
947                 string layout = cmd.argument;
948
949                 // If the entry is obsolete, use the new one instead.
950                 if (hasLayout) {
951                         string const & obs = tclass[layout]->obsoleted_by();
952                         if (!obs.empty())
953                                 layout = obs;
954                 }
955
956                 if (!hasLayout) {
957                         cur.errorMessage(string(N_("Layout ")) + cmd.argument +
958                                 N_(" not known"));
959                         break;
960                 }
961
962                 bool change_layout = (current_layout != layout);
963
964                 if (!change_layout && cur.selection() &&
965                         cur.selBegin().pit() != cur.selEnd().pit())
966                 {
967                         pit_type spit = cur.selBegin().pit();
968                         pit_type epit = cur.selEnd().pit() + 1;
969                         while (spit != epit) {
970                                 if (pars_[spit].layout()->name() != current_layout) {
971                                         change_layout = true;
972                                         break;
973                                 }
974                                 ++spit;
975                         }
976                 }
977
978                 if (change_layout) {
979                         current_layout = layout;
980                         setLayout(cur, layout);
981                         bv->owner()->setLayout(layout);
982                         bv->switchKeyMap();
983                 }
984                 break;
985         }
986
987         case LFUN_CLIPBOARD_PASTE: {
988                 cur.clearSelection();
989                 string const clip = bv->owner()->gui().clipboard().get();
990                 if (!clip.empty()) {
991                         recordUndo(cur);
992                         if (cmd.argument == "paragraph")
993                                 insertStringAsParagraphs(cur, clip);
994                         else
995                                 insertStringAsLines(cur, clip);
996                 }
997                 break;
998         }
999
1000         case LFUN_PRIMARY_SELECTION_PASTE: {
1001                 cur.clearSelection();
1002                 string const clip = bv->owner()->gui().selection().get();
1003                 if (!clip.empty()) {
1004                         recordUndo(cur);
1005                         if (cmd.argument == "paragraph")
1006                                 insertStringAsParagraphs(cur, clip);
1007                         else
1008                                 insertStringAsLines(cur, clip);
1009                 }
1010                 break;
1011         }
1012
1013         case LFUN_QUOTE_INSERT: {
1014                 lyx::cap::replaceSelection(cur);
1015                 Paragraph & par = cur.paragraph();
1016                 lyx::pos_type pos = cur.pos();
1017                 lyx::char_type c;
1018                 if (pos == 0)
1019                         c = ' ';
1020                 else if (cur.prevInset() && cur.prevInset()->isSpace())
1021                         c = ' ';
1022                 else
1023                         c = par.getChar(pos - 1);
1024
1025                 LyXLayout_ptr const & style = par.layout();
1026
1027                 BufferParams const & bufparams = bv->buffer()->params();
1028                 if (!style->pass_thru
1029                     && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1030                         string arg = cmd.argument;
1031                         if (arg == "single")
1032                                 cur.insert(new InsetQuotes(c,
1033                                     bufparams.quotes_language,
1034                                     InsetQuotes::SingleQ));
1035                         else
1036                                 cur.insert(new InsetQuotes(c,
1037                                     bufparams.quotes_language,
1038                                     InsetQuotes::DoubleQ));
1039                         cur.posRight();
1040                 }
1041                 else
1042                         bv->owner()->dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1043                 break;
1044         }
1045
1046         case LFUN_DATE_INSERT:
1047                 if (cmd.argument.empty())
1048                         bv->owner()->dispatch(FuncRequest(LFUN_SELF_INSERT,
1049                                 lyx::formatted_time(lyx::current_time())));
1050                 else
1051                         bv->owner()->dispatch(FuncRequest(LFUN_SELF_INSERT,
1052                                 lyx::formatted_time(lyx::current_time(), cmd.argument)));
1053                 break;
1054
1055         case LFUN_MOUSE_TRIPLE:
1056                 if (cmd.button() == mouse_button::button1) {
1057                         cursorHome(cur);
1058                         cur.resetAnchor();
1059                         cursorEnd(cur);
1060                         cur.setSelection();
1061                         bv->cursor() = cur;
1062                         bv->owner()->gui().selection().haveSelection(cur.selection());
1063                 }
1064                 break;
1065
1066         case LFUN_MOUSE_DOUBLE:
1067                 if (cmd.button() == mouse_button::button1) {
1068                         selectWord(cur, lyx::WHOLE_WORD_STRICT);
1069                         bv->cursor() = cur;
1070                         bv->owner()->gui().selection().haveSelection(cur.selection());
1071                 }
1072                 break;
1073
1074         // Single-click on work area
1075         case LFUN_MOUSE_PRESS: {
1076                 // Right click on a footnote flag opens float menu
1077                 if (cmd.button() == mouse_button::button3)
1078                         cur.clearSelection();
1079
1080                 // Middle button press pastes if we have a selection
1081                 // We do this here as if the selection was inside an inset
1082                 // it could get cleared on the unlocking of the inset so
1083                 // we have to check this first
1084                 bool paste_internally = false;
1085                 if (cmd.button() == mouse_button::button2 && cur.selection()) {
1086                         bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1087                         paste_internally = true;
1088                 }
1089
1090                 bv->mouseSetCursor(cur);
1091
1092                 // Insert primary selection with middle mouse
1093                 // if there is a local selection in the current buffer,
1094                 // insert this
1095                 if (cmd.button() == mouse_button::button2) {
1096                         if (paste_internally)
1097                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1098                         else
1099                                 bv->owner()->dispatch(FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph"));
1100                 }
1101
1102                 break;
1103         }
1104
1105         case LFUN_MOUSE_MOTION: {
1106                 // Only use motion with button 1
1107                 //if (cmd.button() != mouse_button::button1)
1108                 //      return false;
1109
1110                 // ignore motions deeper nested than the real anchor
1111                 LCursor & bvcur = cur.bv().cursor();
1112                 if (bvcur.anchor_.hasPart(cur)) {
1113                         CursorSlice old = bvcur.top();
1114
1115                         int const wh = bv->workHeight();
1116                         int const y = std::max(0, std::min(wh - 1, cmd.y));
1117
1118                         setCursorFromCoordinates(cur, cmd.x, y);
1119                         cur.x_target() = cmd.x;
1120                         if (cmd.y >= wh)
1121                                 cursorDown(cur);
1122                         else if (cmd.y < 0)
1123                                 cursorUp(cur);
1124                         // This is to allow jumping over large insets
1125                         if (cur.top() == old) {
1126                                 if (cmd.y >= wh)
1127                                         cursorDown(cur);
1128                                 else if (cmd.y < 0)
1129                                         cursorUp(cur);
1130                         }
1131
1132                         if (cur.top() == old)
1133                                 cur.noUpdate();
1134                         else {
1135                                 // don't set anchor_
1136                                 bvcur.setCursor(cur);
1137                                 bvcur.selection() = true;
1138                                 //lyxerr << "MOTION: " << bv->cursor() << endl;
1139                         }
1140
1141                 } else
1142                         cur.undispatched();
1143                 break;
1144         }
1145
1146         case LFUN_MOUSE_RELEASE: {
1147                 if (cmd.button() == mouse_button::button2)
1148                         break;
1149
1150                 // finish selection
1151                 if (cmd.button() == mouse_button::button1)
1152                         bv->owner()->gui().selection().haveSelection(cur.selection());
1153
1154                 bv->switchKeyMap();
1155                 bv->owner()->updateMenubar();
1156                 bv->owner()->updateToolbars();
1157                 break;
1158         }
1159
1160         case LFUN_SELF_INSERT: {
1161                 if (cmd.argument.empty())
1162                         break;
1163
1164                 // Automatically delete the currently selected
1165                 // text and replace it with what is being
1166                 // typed in now. Depends on lyxrc settings
1167                 // "auto_region_delete", which defaults to
1168                 // true (on).
1169
1170                 if (lyxrc.auto_region_delete) {
1171                         if (cur.selection())
1172                                 cutSelection(cur, false, false);
1173                         bv->owner()->gui().selection().haveSelection(false);
1174                 }
1175
1176                 cur.clearSelection();
1177                 LyXFont const old_font = real_current_font;
1178
1179                 string::const_iterator cit = cmd.argument.begin();
1180                 string::const_iterator end = cmd.argument.end();
1181                 for (; cit != end; ++cit)
1182                         bv->owner()->getIntl().getTransManager().
1183                                 translateAndInsert(*cit, this);
1184
1185                 cur.resetAnchor();
1186                 moveCursor(cur, false);
1187                 break;
1188         }
1189
1190         case LFUN_URL_INSERT: {
1191                 InsetCommandParams p("url");
1192                 string const data = InsetCommandMailer::params2string("url", p);
1193                 bv->owner()->getDialogs().show("url", data, 0);
1194                 break;
1195         }
1196
1197         case LFUN_HTML_INSERT: {
1198                 InsetCommandParams p("htmlurl");
1199                 string const data = InsetCommandMailer::params2string("url", p);
1200                 bv->owner()->getDialogs().show("url", data, 0);
1201                 break;
1202         }
1203
1204         case LFUN_LABEL_INSERT: {
1205                 // Try to generate a valid label
1206                 string const contents = cmd.argument.empty() ?
1207                         cur.getPossibleLabel() : cmd.argument;
1208
1209                 InsetCommandParams p("label", contents);
1210                 string const data = InsetCommandMailer::params2string("label", p);
1211
1212                 if (cmd.argument.empty()) {
1213                         bv->owner()->getDialogs().show("label", data, 0);
1214                 } else {
1215                         FuncRequest fr(LFUN_INSET_INSERT, data);
1216                         dispatch(cur, fr);
1217                 }
1218                 break;
1219         }
1220
1221
1222 #if 0
1223         case LFUN_LIST_INSERT:
1224         case LFUN_THEOREM_INSERT:
1225         case LFUN_CAPTION_INSERT:
1226 #endif
1227         case LFUN_NOTE_INSERT:
1228         case LFUN_CHARSTYLE_INSERT:
1229         case LFUN_BOX_INSERT:
1230         case LFUN_BRANCH_INSERT:
1231         case LFUN_BIBITEM_INSERT:
1232         case LFUN_ERT_INSERT:
1233         case LFUN_FOOTNOTE_INSERT:
1234         case LFUN_MARGINALNOTE_INSERT:
1235         case LFUN_OPTIONAL_INSERT:
1236         case LFUN_ENVIRONMENT_INSERT:
1237                 // Open the inset, and move the current selection
1238                 // inside it.
1239                 doInsertInset(cur, this, cmd, true, true);
1240                 cur.posRight();
1241                 break;
1242
1243         case LFUN_TABULAR_INSERT:
1244                 // if there were no arguments, just open the dialog
1245                 if (doInsertInset(cur, this, cmd, false, true))
1246                         cur.posRight();
1247                 else
1248                         bv->owner()->getDialogs().show("tabularcreate");
1249
1250                 break;
1251
1252         case LFUN_FLOAT_INSERT:
1253         case LFUN_FLOAT_WIDE_INSERT:
1254         case LFUN_WRAP_INSERT:
1255                 doInsertInset(cur, this, cmd, true, true);
1256                 cur.posRight();
1257                 // FIXME: the "Caption" name should not be hardcoded,
1258                 // but given by the float definition.
1259                 cur.dispatch(FuncRequest(LFUN_LAYOUT, "Caption"));
1260                 break;
1261
1262         case LFUN_INDEX_INSERT: {
1263                 InsetBase * inset = createInset(&cur.bv(), cmd);
1264                 if (!inset)
1265                         break;
1266
1267                 recordUndo(cur);
1268                 cur.clearSelection();
1269                 insertInset(cur, inset);
1270                 inset->edit(cur, true);
1271                 cur.posRight();
1272                 break;
1273         }
1274
1275         case LFUN_INDEX_PRINT:
1276         case LFUN_TOC_INSERT:
1277         case LFUN_HFILL_INSERT:
1278         case LFUN_LINE_INSERT:
1279         case LFUN_PAGEBREAK_INSERT:
1280                 // do nothing fancy
1281                 doInsertInset(cur, this, cmd, false, false);
1282                 cur.posRight();
1283                 break;
1284
1285         case LFUN_DEPTH_DECREMENT:
1286                 changeDepth(cur, DEC_DEPTH);
1287                 break;
1288
1289         case LFUN_DEPTH_INCREMENT:
1290                 changeDepth(cur, INC_DEPTH);
1291                 break;
1292
1293         case LFUN_MATH_DISPLAY:
1294                 mathDispatch(cur, cmd, true);
1295                 break;
1296
1297         case LFUN_MATH_IMPORT_SELECTION:
1298         case LFUN_MATH_MODE:
1299                 if (cmd.argument == "on")
1300                         // don't pass "on" as argument
1301                         mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1302                 else
1303                         mathDispatch(cur, cmd, false);
1304                 break;
1305
1306         case LFUN_MATH_MACRO:
1307                 if (cmd.argument.empty())
1308                         cur.errorMessage(N_("Missing argument"));
1309                 else {
1310                         string s = cmd.argument;
1311                         string const s1 = token(s, ' ', 1);
1312                         int const nargs = s1.empty() ? 0 : convert<int>(s1);
1313                         string const s2 = token(s, ' ', 2);
1314                         string const type = s2.empty() ? "newcommand" : s2;
1315                         cur.insert(new MathMacroTemplate(token(s, ' ', 0), nargs, type));
1316                         //cur.nextInset()->edit(cur, true);
1317                 }
1318                 break;
1319
1320         // passthrough hat and underscore outside mathed:
1321         case LFUN_MATH_SUBSCRIPT:
1322                 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1323                 break;
1324         case LFUN_MATH_SUPERSCRIPT:
1325                 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1326                 break;
1327
1328         case LFUN_MATH_INSERT:
1329         case LFUN_MATH_MATRIX:
1330         case LFUN_MATH_DELIM:
1331         case LFUN_MATH_BIGDELIM: {
1332                 cur.insert(new MathHullInset("simple"));
1333                 cur.dispatch(FuncRequest(LFUN_CHAR_FORWARD));
1334                 cur.dispatch(cmd);
1335                 break;
1336         }
1337
1338         case LFUN_FONT_EMPH: {
1339                 LyXFont font(LyXFont::ALL_IGNORE);
1340                 font.setEmph(LyXFont::TOGGLE);
1341                 toggleAndShow(cur, this, font);
1342                 break;
1343         }
1344
1345         case LFUN_FONT_BOLD: {
1346                 LyXFont font(LyXFont::ALL_IGNORE);
1347                 font.setSeries(LyXFont::BOLD_SERIES);
1348                 toggleAndShow(cur, this, font);
1349                 break;
1350         }
1351
1352         case LFUN_FONT_NOUN: {
1353                 LyXFont font(LyXFont::ALL_IGNORE);
1354                 font.setNoun(LyXFont::TOGGLE);
1355                 toggleAndShow(cur, this, font);
1356                 break;
1357         }
1358
1359         case LFUN_FONT_CODE: {
1360                 LyXFont font(LyXFont::ALL_IGNORE);
1361                 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
1362                 toggleAndShow(cur, this, font);
1363                 break;
1364         }
1365
1366         case LFUN_FONT_SANS: {
1367                 LyXFont font(LyXFont::ALL_IGNORE);
1368                 font.setFamily(LyXFont::SANS_FAMILY);
1369                 toggleAndShow(cur, this, font);
1370                 break;
1371         }
1372
1373         case LFUN_FONT_ROMAN: {
1374                 LyXFont font(LyXFont::ALL_IGNORE);
1375                 font.setFamily(LyXFont::ROMAN_FAMILY);
1376                 toggleAndShow(cur, this, font);
1377                 break;
1378         }
1379
1380         case LFUN_FONT_DEFAULT: {
1381                 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
1382                 toggleAndShow(cur, this, font);
1383                 break;
1384         }
1385
1386         case LFUN_FONT_UNDERLINE: {
1387                 LyXFont font(LyXFont::ALL_IGNORE);
1388                 font.setUnderbar(LyXFont::TOGGLE);
1389                 toggleAndShow(cur, this, font);
1390                 break;
1391         }
1392
1393         case LFUN_FONT_SIZE: {
1394                 LyXFont font(LyXFont::ALL_IGNORE);
1395                 font.setLyXSize(cmd.argument);
1396                 toggleAndShow(cur, this, font);
1397                 break;
1398         }
1399
1400         case LFUN_LANGUAGE: {
1401                 Language const * lang = languages.getLanguage(cmd.argument);
1402                 if (!lang)
1403                         break;
1404                 LyXFont font(LyXFont::ALL_IGNORE);
1405                 font.setLanguage(lang);
1406                 toggleAndShow(cur, this, font);
1407                 bv->switchKeyMap();
1408                 break;
1409         }
1410
1411         case LFUN_FONT_FREE_APPLY:
1412                 toggleAndShow(cur, this, freefont, toggleall);
1413                 cur.message(_("Character set"));
1414                 break;
1415
1416         // Set the freefont using the contents of \param data dispatched from
1417         // the frontends and apply it at the current cursor location.
1418         case LFUN_FONT_FREE_UPDATE: {
1419                 LyXFont font;
1420                 bool toggle;
1421                 if (bv_funcs::string2font(cmd.argument, font, toggle)) {
1422                         freefont = font;
1423                         toggleall = toggle;
1424                         toggleAndShow(cur, this, freefont, toggleall);
1425                         cur.message(_("Character set"));
1426                 }
1427                 break;
1428         }
1429
1430         case LFUN_FINISHED_LEFT:
1431                 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1432                 break;
1433
1434         case LFUN_FINISHED_RIGHT:
1435                 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1436                 ++cur.pos();
1437                 break;
1438
1439         case LFUN_FINISHED_UP:
1440                 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_UP:\n" << cur << endl;
1441                 cursorUp(cur);
1442                 break;
1443
1444         case LFUN_FINISHED_DOWN:
1445                 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_DOWN:\n" << cur << endl;
1446                 cursorDown(cur);
1447                 break;
1448
1449         case LFUN_LAYOUT_PARAGRAPH: {
1450                 string data;
1451                 params2string(cur.paragraph(), data);
1452                 data = "show\n" + data;
1453                 bv->owner()->getDialogs().show("paragraph", data);
1454                 break;
1455         }
1456
1457         case LFUN_PARAGRAPH_UPDATE: {
1458                 if (!bv->owner()->getDialogs().visible("paragraph"))
1459                         break;
1460                 string data;
1461                 params2string(cur.paragraph(), data);
1462
1463                 // Will the paragraph accept changes from the dialog?
1464                 bool const accept = !cur.inset().forceDefaultParagraphs(cur.idx());
1465
1466                 data = "update " + convert<string>(accept) + '\n' + data;
1467                 bv->owner()->getDialogs().update("paragraph", data);
1468                 break;
1469         }
1470
1471         case LFUN_ACCENT_UMLAUT:
1472         case LFUN_ACCENT_CIRCUMFLEX:
1473         case LFUN_ACCENT_GRAVE:
1474         case LFUN_ACCENT_ACUTE:
1475         case LFUN_ACCENT_TILDE:
1476         case LFUN_ACCENT_CEDILLA:
1477         case LFUN_ACCENT_MACRON:
1478         case LFUN_ACCENT_DOT:
1479         case LFUN_ACCENT_UNDERDOT:
1480         case LFUN_ACCENT_UNDERBAR:
1481         case LFUN_ACCENT_CARON:
1482         case LFUN_ACCENT_SPECIAL_CARON:
1483         case LFUN_ACCENT_BREVE:
1484         case LFUN_ACCENT_TIE:
1485         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1486         case LFUN_ACCENT_CIRCLE:
1487         case LFUN_ACCENT_OGONEK:
1488                 bv->owner()->getLyXFunc().handleKeyFunc(cmd.action);
1489                 if (!cmd.argument.empty())
1490                         bv->owner()->getIntl().getTransManager()
1491                                 .translateAndInsert(cmd.argument[0], this);
1492                 break;
1493
1494         case LFUN_FLOAT_LIST: {
1495                 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1496                 if (tclass.floats().typeExist(cmd.argument)) {
1497                         // not quite sure if we want this...
1498                         recordUndo(cur);
1499                         cur.clearSelection();
1500                         breakParagraph(cur);
1501
1502                         if (cur.lastpos() != 0) {
1503                                 cursorLeft(cur);
1504                                 breakParagraph(cur);
1505                         }
1506
1507                         setLayout(cur, tclass.defaultLayoutName());
1508                         setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1509                         insertInset(cur, new InsetFloatList(cmd.argument));
1510                         cur.posRight();
1511                 } else {
1512                         lyxerr << "Non-existent float type: "
1513                                << cmd.argument << endl;
1514                 }
1515                 break;
1516         }
1517
1518         case LFUN_CHANGE_ACCEPT: {
1519                 acceptChange(cur);
1520                 break;
1521         }
1522
1523         case LFUN_CHANGE_REJECT: {
1524                 rejectChange(cur);
1525                 break;
1526         }
1527
1528         case LFUN_THESAURUS_ENTRY: {
1529                 string arg = cmd.argument;
1530                 if (arg.empty()) {
1531                         arg = cur.selectionAsString(false);
1532                         // FIXME
1533                         if (arg.size() > 100 || arg.empty()) {
1534                                 // Get word or selection
1535                                 selectWordWhenUnderCursor(cur, lyx::WHOLE_WORD);
1536                                 arg = cur.selectionAsString(false);
1537                         }
1538                 }
1539                 bv->owner()->getDialogs().show("thesaurus", arg);
1540                 break;
1541         }
1542
1543         case LFUN_PARAGRAPH_PARAMS_APPLY: {
1544                 // Given data, an encoding of the ParagraphParameters
1545                 // generated in the Paragraph dialog, this function sets
1546                 // the current paragraph appropriately.
1547                 istringstream is(cmd.argument);
1548                 LyXLex lex(0, 0);
1549                 lex.setStream(is);
1550                 ParagraphParameters params;
1551                 params.read(lex);
1552                 setParagraph(cur,
1553                                          params.spacing(),
1554                                          params.align(),
1555                                          params.labelWidthString(),
1556                                          params.noindent());
1557                 cur.message(_("Paragraph layout set"));
1558                 break;
1559         }
1560
1561         case LFUN_INSET_DIALOG_SHOW: {
1562                 InsetBase * inset = cur.nextInset();
1563                 if (inset) {
1564                         FuncRequest fr(LFUN_INSET_DIALOG_SHOW);
1565                         inset->dispatch(cur, fr);
1566                 }
1567                 break;
1568         }
1569
1570         case LFUN_ESCAPE:
1571                 if (cur.selection()) {
1572                         cur.selection() = false;
1573                 } else {
1574                         cur.undispatched();
1575                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1576                 }
1577                 break;
1578
1579         default:
1580                 lyxerr[Debug::ACTION]
1581                         << BOOST_CURRENT_FUNCTION
1582                         << ": Command " << cmd
1583                         << " not DISPATCHED by LyXText" << endl;
1584                 cur.undispatched();
1585                 break;
1586         }
1587
1588         if (singleParUpdate)
1589                 // Inserting characters does not change par height
1590                 if (cur.bottom().paragraph().dim().height()
1591                     == olddim.height()) {
1592                         // if so, update _only_ this paragraph
1593                         cur.bv().update(Update::SinglePar |
1594                                         Update::FitCursor |
1595                                         Update::MultiParSel);
1596                         cur.noUpdate();
1597                         return;
1598                 } else
1599                         needsUpdate = true;
1600         if (!needsUpdate
1601             && &oldTopSlice.inset() == &cur.inset()
1602             && oldTopSlice.idx() == cur.idx()
1603             && !sel
1604             && !cur.selection())
1605                 cur.noUpdate();
1606         else
1607                 cur.needsUpdate();
1608 }
1609
1610
1611 bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
1612                         FuncStatus & flag) const
1613 {
1614         BOOST_ASSERT(cur.text() == this);
1615
1616         LyXFont const & font = real_current_font;
1617         bool enable = true;
1618         InsetBase::Code code = InsetBase::NO_CODE;
1619
1620         switch (cmd.action) {
1621
1622         case LFUN_DEPTH_DECREMENT:
1623                 enable = changeDepthAllowed(cur, DEC_DEPTH);
1624                 break;
1625
1626         case LFUN_DEPTH_INCREMENT:
1627                 enable = changeDepthAllowed(cur, INC_DEPTH);
1628                 break;
1629
1630         case LFUN_APPENDIX:
1631                 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1632                 return true;
1633
1634         case LFUN_BIBITEM_INSERT:
1635                 enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO);
1636                 break;
1637
1638         case LFUN_DIALOG_SHOW_NEW_INSET:
1639                 if (cmd.argument == "bibitem")
1640                         code = InsetBase::BIBITEM_CODE;
1641                 else if (cmd.argument == "bibtex")
1642                         code = InsetBase::BIBTEX_CODE;
1643                 else if (cmd.argument == "box")
1644                         code = InsetBase::BOX_CODE;
1645                 else if (cmd.argument == "branch")
1646                         code = InsetBase::BRANCH_CODE;
1647                 else if (cmd.argument == "citation")
1648                         code = InsetBase::CITE_CODE;
1649                 else if (cmd.argument == "ert")
1650                         code = InsetBase::ERT_CODE;
1651                 else if (cmd.argument == "external")
1652                         code = InsetBase::EXTERNAL_CODE;
1653                 else if (cmd.argument == "float")
1654                         code = InsetBase::FLOAT_CODE;
1655                 else if (cmd.argument == "graphics")
1656                         code = InsetBase::GRAPHICS_CODE;
1657                 else if (cmd.argument == "include")
1658                         code = InsetBase::INCLUDE_CODE;
1659                 else if (cmd.argument == "index")
1660                         code = InsetBase::INDEX_CODE;
1661                 else if (cmd.argument == "label")
1662                         code = InsetBase::LABEL_CODE;
1663                 else if (cmd.argument == "note")
1664                         code = InsetBase::NOTE_CODE;
1665                 else if (cmd.argument == "ref")
1666                         code = InsetBase::REF_CODE;
1667                 else if (cmd.argument == "toc")
1668                         code = InsetBase::TOC_CODE;
1669                 else if (cmd.argument == "url")
1670                         code = InsetBase::URL_CODE;
1671                 else if (cmd.argument == "vspace")
1672                         code = InsetBase::VSPACE_CODE;
1673                 else if (cmd.argument == "wrap")
1674                         code = InsetBase::WRAP_CODE;
1675                 break;
1676
1677         case LFUN_ERT_INSERT:
1678                 code = InsetBase::ERT_CODE;
1679                 break;
1680         case LFUN_FOOTNOTE_INSERT:
1681                 code = InsetBase::FOOT_CODE;
1682                 break;
1683         case LFUN_TABULAR_INSERT:
1684                 code = InsetBase::TABULAR_CODE;
1685                 break;
1686         case LFUN_MARGINALNOTE_INSERT:
1687                 code = InsetBase::MARGIN_CODE;
1688                 break;
1689         case LFUN_FLOAT_INSERT:
1690         case LFUN_FLOAT_WIDE_INSERT:
1691                 code = InsetBase::FLOAT_CODE;
1692                 break;
1693         case LFUN_WRAP_INSERT:
1694                 code = InsetBase::WRAP_CODE;
1695                 break;
1696         case LFUN_FLOAT_LIST:
1697                 code = InsetBase::FLOAT_LIST_CODE;
1698                 break;
1699 #if 0
1700         case LFUN_LIST_INSERT:
1701                 code = InsetBase::LIST_CODE;
1702                 break;
1703         case LFUN_THEOREM_INSERT:
1704                 code = InsetBase::THEOREM_CODE;
1705                 break;
1706 #endif
1707         case LFUN_CAPTION_INSERT:
1708                 code = InsetBase::CAPTION_CODE;
1709                 break;
1710         case LFUN_NOTE_INSERT:
1711                 code = InsetBase::NOTE_CODE;
1712                 break;
1713         case LFUN_CHARSTYLE_INSERT:
1714                 code = InsetBase::CHARSTYLE_CODE;
1715                 if (cur.buffer().params().getLyXTextClass().charstyles().empty())
1716                         enable = false;
1717                 break;
1718         case LFUN_BOX_INSERT:
1719                 code = InsetBase::BOX_CODE;
1720                 break;
1721         case LFUN_BRANCH_INSERT:
1722                 code = InsetBase::BRANCH_CODE;
1723                 if (cur.buffer().getMasterBuffer()->params().branchlist().empty())
1724                         enable = false;
1725                 break;
1726         case LFUN_LABEL_INSERT:
1727                 code = InsetBase::LABEL_CODE;
1728                 break;
1729         case LFUN_OPTIONAL_INSERT:
1730                 code = InsetBase::OPTARG_CODE;
1731                 enable = numberOfOptArgs(cur.paragraph())
1732                         < cur.paragraph().layout()->optionalargs;
1733                 break;
1734         case LFUN_ENVIRONMENT_INSERT:
1735                 code = InsetBase::BOX_CODE;
1736                 break;
1737         case LFUN_INDEX_INSERT:
1738                 code = InsetBase::INDEX_CODE;
1739                 break;
1740         case LFUN_INDEX_PRINT:
1741                 code = InsetBase::INDEX_PRINT_CODE;
1742                 break;
1743         case LFUN_TOC_INSERT:
1744                 code = InsetBase::TOC_CODE;
1745                 break;
1746         case LFUN_HTML_INSERT:
1747         case LFUN_URL_INSERT:
1748                 code = InsetBase::URL_CODE;
1749                 break;
1750         case LFUN_QUOTE_INSERT:
1751                 // always allow this, since we will inset a raw quote
1752                 // if an inset is not allowed.
1753                 break;
1754         case LFUN_HYPHENATION_POINT_INSERT:
1755         case LFUN_LIGATURE_BREAK_INSERT:
1756         case LFUN_HFILL_INSERT:
1757         case LFUN_MENU_SEPARATOR_INSERT:
1758         case LFUN_DOTS_INSERT:
1759         case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
1760                 code = InsetBase::SPECIALCHAR_CODE;
1761                 break;
1762         case LFUN_SPACE_INSERT:
1763                 // slight hack: we know this is allowed in math mode
1764                 if (cur.inTexted())
1765                         code = InsetBase::SPACE_CODE;
1766                 break;
1767
1768 #ifdef WITH_WARNINGS
1769 #warning This LFUN is not used anymore and should be nuked (JMarc 29/10/2005)
1770 #endif
1771 #if 0
1772         case LFUN_INSET_DIALOG_SHOW: {
1773                 InsetBase * inset = cur.nextInset();
1774                 enable = inset;
1775                 if (inset) {
1776                         code = inset->lyxCode();
1777                         if (!(code == InsetBase::INCLUDE_CODE
1778                                 || code == InsetBase::BIBTEX_CODE
1779                                 || code == InsetBase::FLOAT_LIST_CODE
1780                                 || code == InsetBase::TOC_CODE))
1781                                 enable = false;
1782                 }
1783                 break;
1784         }
1785 #endif
1786
1787         case LFUN_INSET_MODIFY:
1788                 // We need to disable this, because we may get called for a
1789                 // tabular cell via
1790                 // InsetTabular::getStatus() -> InsetText::getStatus()
1791                 // and we don't handle LFUN_INSET_MODIFY.
1792                 enable = false;
1793                 break;
1794
1795         case LFUN_FONT_EMPH:
1796                 flag.setOnOff(font.emph() == LyXFont::ON);
1797                 return true;
1798
1799         case LFUN_FONT_NOUN:
1800                 flag.setOnOff(font.noun() == LyXFont::ON);
1801                 return true;
1802
1803         case LFUN_FONT_BOLD:
1804                 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1805                 return true;
1806
1807         case LFUN_FONT_SANS:
1808                 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1809                 return true;
1810
1811         case LFUN_FONT_ROMAN:
1812                 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1813                 return true;
1814
1815         case LFUN_FONT_CODE:
1816                 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1817                 return true;
1818
1819         case LFUN_CUT:
1820         case LFUN_COPY:
1821                 enable = cur.selection();
1822                 break;
1823
1824         case LFUN_PASTE:
1825                 enable = lyx::cap::numberOfSelections() > 0;
1826                 break;
1827
1828         case LFUN_PARAGRAPH_MOVE_UP: {
1829                 enable = cur.pit() > 0 && !cur.selection();
1830                 break;
1831         }
1832
1833         case LFUN_PARAGRAPH_MOVE_DOWN: {
1834                 enable = cur.pit() < cur.lastpit() && !cur.selection();
1835                 break;
1836         }
1837
1838         case LFUN_WORD_DELETE_FORWARD:
1839         case LFUN_WORD_DELETE_BACKWARD:
1840         case LFUN_LINE_DELETE:
1841         case LFUN_WORD_FORWARD:
1842         case LFUN_WORD_BACKWARD:
1843         case LFUN_CHAR_FORWARD:
1844         case LFUN_CHAR_FORWARD_SELECT:
1845         case LFUN_CHAR_BACKWARD:
1846         case LFUN_CHAR_BACKWARD_SELECT:
1847         case LFUN_UP:
1848         case LFUN_UP_SELECT:
1849         case LFUN_DOWN:
1850         case LFUN_DOWN_SELECT:
1851         case LFUN_PARAGRAPH_UP_SELECT:
1852         case LFUN_PARAGRAPH_DOWN_SELECT:
1853         case LFUN_SCREEN_UP_SELECT:
1854         case LFUN_SCREEN_DOWN_SELECT:
1855         case LFUN_LINE_BEGIN_SELECT:
1856         case LFUN_LINE_END_SELECT:
1857         case LFUN_WORD_FORWARD_SELECT:
1858         case LFUN_WORD_BACKWARD_SELECT:
1859         case LFUN_WORD_SELECT:
1860         case LFUN_PARAGRAPH_UP:
1861         case LFUN_PARAGRAPH_DOWN:
1862         case LFUN_SCREEN_UP:
1863         case LFUN_SCREEN_DOWN:
1864         case LFUN_LINE_BEGIN:
1865         case LFUN_LINE_END:
1866         case LFUN_BREAK_LINE:
1867         case LFUN_CHAR_DELETE_FORWARD:
1868         case LFUN_DELETE_FORWARD_SKIP:
1869         case LFUN_CHAR_DELETE_BACKWARD:
1870         case LFUN_DELETE_BACKWARD_SKIP:
1871         case LFUN_BREAK_PARAGRAPH:
1872         case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
1873         case LFUN_BREAK_PARAGRAPH_SKIP:
1874         case LFUN_PARAGRAPH_SPACING:
1875         case LFUN_INSET_INSERT:
1876         case LFUN_NEXT_INSET_TOGGLE:
1877         case LFUN_WORD_UPCASE:
1878         case LFUN_WORD_LOWCASE:
1879         case LFUN_WORD_CAPITALIZE:
1880         case LFUN_CHARS_TRANSPOSE:
1881         case LFUN_SERVER_GET_XY:
1882         case LFUN_SERVER_SET_XY:
1883         case LFUN_SERVER_GET_FONT:
1884         case LFUN_SERVER_GET_LAYOUT:
1885         case LFUN_LAYOUT:
1886         case LFUN_CLIPBOARD_PASTE:
1887         case LFUN_PRIMARY_SELECTION_PASTE:
1888         case LFUN_DATE_INSERT:
1889         case LFUN_SELF_INSERT:
1890         case LFUN_LINE_INSERT:
1891         case LFUN_PAGEBREAK_INSERT:
1892         case LFUN_MATH_DISPLAY:
1893         case LFUN_MATH_IMPORT_SELECTION:
1894         case LFUN_MATH_MODE:
1895         case LFUN_MATH_MACRO:
1896         case LFUN_MATH_INSERT:
1897         case LFUN_MATH_MATRIX:
1898         case LFUN_MATH_DELIM:
1899         case LFUN_MATH_BIGDELIM:
1900         case LFUN_MATH_SUBSCRIPT:
1901         case LFUN_MATH_SUPERSCRIPT:
1902         case LFUN_FONT_DEFAULT:
1903         case LFUN_FONT_UNDERLINE:
1904         case LFUN_FONT_SIZE:
1905         case LFUN_LANGUAGE:
1906         case LFUN_FONT_FREE_APPLY:
1907         case LFUN_FONT_FREE_UPDATE:
1908         case LFUN_LAYOUT_PARAGRAPH:
1909         case LFUN_PARAGRAPH_UPDATE:
1910         case LFUN_ACCENT_UMLAUT:
1911         case LFUN_ACCENT_CIRCUMFLEX:
1912         case LFUN_ACCENT_GRAVE:
1913         case LFUN_ACCENT_ACUTE:
1914         case LFUN_ACCENT_TILDE:
1915         case LFUN_ACCENT_CEDILLA:
1916         case LFUN_ACCENT_MACRON:
1917         case LFUN_ACCENT_DOT:
1918         case LFUN_ACCENT_UNDERDOT:
1919         case LFUN_ACCENT_UNDERBAR:
1920         case LFUN_ACCENT_CARON:
1921         case LFUN_ACCENT_SPECIAL_CARON:
1922         case LFUN_ACCENT_BREVE:
1923         case LFUN_ACCENT_TIE:
1924         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1925         case LFUN_ACCENT_CIRCLE:
1926         case LFUN_ACCENT_OGONEK:
1927         case LFUN_CHANGE_ACCEPT:
1928         case LFUN_CHANGE_REJECT:
1929         case LFUN_THESAURUS_ENTRY:
1930         case LFUN_PARAGRAPH_PARAMS_APPLY:
1931         case LFUN_ESCAPE:
1932         case LFUN_BUFFER_END:
1933         case LFUN_BUFFER_BEGIN:
1934         case LFUN_BUFFER_BEGIN_SELECT:
1935         case LFUN_BUFFER_END_SELECT:
1936                 // these are handled in our dispatch()
1937                 enable = true;
1938                 break;
1939
1940         default:
1941                 return false;
1942         }
1943
1944         if (code != InsetBase::NO_CODE
1945             && (cur.empty() || !cur.inset().insetAllowed(code)))
1946                 enable = false;
1947
1948         flag.enabled(enable);
1949         return true;
1950 }