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