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