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