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