]> git.lyx.org Git - lyx.git/blob - src/text3.C
add lfun 'quote-insert' arguments single/double (ERT fixes not included)
[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 "buffer.h"
21 #include "bufferparams.h"
22 #include "BufferView.h"
23 #include "cursor.h"
24 #include "debug.h"
25 #include "dispatchresult.h"
26 #include "factory.h"
27 #include "FloatList.h"
28 #include "funcrequest.h"
29 #include "gettext.h"
30 #include "intl.h"
31 #include "language.h"
32 #include "lyxfunc.h"
33 #include "lyxlex.h"
34 #include "lyxrc.h"
35 #include "lyxrow.h"
36 #include "paragraph.h"
37 #include "paragraph_funcs.h"
38 #include "ParagraphParameters.h"
39 #include "undo.h"
40 #include "vspace.h"
41
42 #include "frontends/Dialogs.h"
43 #include "frontends/LyXView.h"
44
45 #include "insets/insetcommand.h"
46 #include "insets/insetfloatlist.h"
47 #include "insets/insetnewline.h"
48 #include "insets/insetquotes.h"
49 #include "insets/insetspecialchar.h"
50 #include "insets/insettext.h"
51
52 #include "support/lstrings.h"
53 #include "support/lyxlib.h"
54 #include "support/tostr.h"
55 #include "support/std_sstream.h"
56
57 #include "mathed/math_hullinset.h"
58 #include "mathed/formulamacro.h"
59
60 #include <clocale>
61
62 using lyx::pos_type;
63
64 using lyx::support::isStrUnsignedInt;
65 using lyx::support::strToUnsignedInt;
66 using lyx::support::atoi;
67 using lyx::support::token;
68
69 using std::endl;
70 using std::find;
71 using std::string;
72 using std::istringstream;
73 using std::vector;
74
75
76 extern string current_layout;
77
78 // the selection possible is needed, that only motion events are
79 // used, where the button press event was on the drawing area too
80 bool selection_possible = false;
81
82
83 namespace {
84
85         // globals...
86         LyXFont freefont(LyXFont::ALL_IGNORE);
87         bool toggleall = false;
88
89
90         void toggleAndShow(LCursor & cur, LyXText * text,
91                 LyXFont const & font, bool toggleall = true)
92         {
93                 if (!cur.bv().available())
94                         return;
95
96                 text->toggleFree(cur, font, toggleall);
97
98                 if (font.language() != ignore_language ||
99                                 font.number() != LyXFont::IGNORE) {
100                         Paragraph & par = cur.paragraph();
101                         text->bidi.computeTables(par, *cur.bv().buffer(), cur.textRow());
102                         if (cur.boundary() !=
103                                         text->bidi.isBoundary(*cur.bv().buffer(), par,
104                                                         cur.pos(),
105                                                         text->real_current_font))
106                                 text->setCursor(cur, cur.par(), cur.pos(),
107                                                 false, !cur.boundary());
108                 }
109                 cur.update();
110         }
111
112
113         void moveCursor(LCursor & cur, bool selecting)
114         {
115                 if (selecting || cur.mark())
116                         cur.setSelection();
117                 if (!cur.selection())
118                         cur.bv().haveSelection(false);
119                 cur.bv().switchKeyMap();
120         }
121
122
123         void finishChange(LCursor & cur, bool selecting = false)
124         {
125                 finishUndo();
126                 moveCursor(cur, selecting);
127                 cur.bv().owner()->view_state_changed();
128         }
129
130
131         void mathDispatch(LCursor & cur, LyXText * text,
132                 FuncRequest const & cmd, bool display)
133         {
134                 string sel = cur.selectionAsString(false);
135                 lyxerr << "selection is: '" << sel << "'" << endl;
136
137                 if (sel.empty()) {
138                         cur.insert(new MathHullInset);
139                         cur.dispatch(FuncRequest(LFUN_RIGHT));
140                         cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
141                         // don't do that also for LFUN_MATH_MODE unless you want end up with
142                         // always changing to mathrm when opening an inlined inset
143                         // -- I really hate "LyXfunc overloading"...
144                         if (display)
145                                 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
146                         cur.dispatch(FuncRequest(LFUN_INSERT_MATH, cmd.argument));
147                 } else {
148                         // create a macro if we see "\\newcommand" somewhere, and an ordinary
149                         // formula otherwise
150                         text->cutSelection(cur, true, true);
151                         if (sel.find("\\newcommand") == string::npos &&
152                                         sel.find("\\def") == string::npos)
153                         {
154                                 cur.insert(new MathHullInset);
155                                 cur.dispatch(FuncRequest(LFUN_RIGHT));
156                                 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
157                                 cur.dispatch(FuncRequest(LFUN_INSERT_MATH, sel));
158                         } else {
159                                 cur.insert(new InsetFormulaMacro(sel));
160                                 cur.dispatch(FuncRequest(LFUN_RIGHT));
161                         }
162                 }
163                 cur.message(N_("Math editor mode"));
164         }
165
166 } // namespace anon
167
168
169
170 namespace bv_funcs {
171
172 string const freefont2string()
173 {
174         string data;
175         if (font2string(freefont, toggleall, data))
176                 return data;
177         return string();
178 }
179
180 }
181
182
183 //takes absolute x,y coordinates
184 InsetBase * LyXText::checkInsetHit(int x, int y)
185 {
186         ParagraphList::iterator pit;
187         ParagraphList::iterator end;
188
189         getParsInRange(paragraphs(),
190                        bv()->top_y() - yo_,
191                        bv()->top_y() - yo_ + bv()->workHeight(),
192                        pit, end);
193
194         lyxerr << "checkInsetHit: x: " << x << " y: " << y << endl;
195         for ( ; pit != end; ++pit) {
196                 InsetList::iterator iit = pit->insetlist.begin();
197                 InsetList::iterator iend = pit->insetlist.end();
198                 for ( ; iit != iend; ++iit) {
199                         InsetBase * inset = iit->inset;
200 #if 1
201                         lyxerr << "examining inset " << inset
202                         //<< " xo/yo: " << inset->xo() << "/" << inset->yo()
203                                 << " xo: " << inset->xo() << "..." << inset->xo() + inset->width()
204                                 << " yo: " << inset->yo() - inset->ascent() << "..."
205                                 << inset->yo() + inset->descent() << endl;
206 #endif
207                         if (inset->covers(x, y)) {
208                                 lyxerr << "Hit inset: " << inset << endl;
209                                 return inset;
210                         }
211                 }
212         }
213         lyxerr << "No inset hit. " << endl;
214         return 0;
215 }
216
217
218 bool LyXText::gotoNextInset(LCursor & cur,
219         vector<InsetOld_code> const & codes, string const & contents)
220 {
221         BOOST_ASSERT(this == cur.text());
222         ParagraphList::iterator end = paragraphs().end();
223         ParagraphList::iterator pit = getPar(cur.par());
224         pos_type pos = cur.pos();
225
226         InsetBase * inset;
227         do {
228                 if (pos + 1 < pit->size()) {
229                         ++pos;
230                 } else  {
231                         ++pit;
232                         pos = 0;
233                 }
234
235         } while (pit != end &&
236                  !(pit->isInset(pos) &&
237                    (inset = pit->getInset(pos)) != 0 &&
238                    find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
239                    (contents.empty() ||
240                     static_cast<InsetCommand *>(pit->getInset(pos))->getContents()
241                     == contents)));
242
243         if (pit == end)
244                 return false;
245
246         setCursor(cur, parOffset(pit), pos, false);
247         return true;
248 }
249
250
251 void LyXText::gotoInset(LCursor & cur,
252         vector<InsetOld_code> const & codes, bool same_content)
253 {
254         cur.clearSelection();
255
256         string contents;
257         if (same_content
258             && cur.pos() < cur.lastpos()
259             && cur.paragraph().isInset(cur.pos())) {
260                 InsetBase const * inset = cur.paragraph().getInset(cur.pos());
261                 if (find(codes.begin(), codes.end(), inset->lyxCode())
262                     != codes.end())
263                         contents = static_cast<InsetCommand const *>(inset)->getContents();
264         }
265
266         if (!gotoNextInset(cur, codes, contents)) {
267                 if (cur.pos() || cur.par() != 0) {
268                         CursorSlice tmp = cur.current();
269                         cur.par() = 0;
270                         cur.pos() = 0;
271                         if (!gotoNextInset(cur, codes, contents)) {
272                                 cursor() = tmp;
273                                 cur.message(_("No more insets"));
274                         }
275                 } else {
276                         cur.message(_("No more insets"));
277                 }
278         }
279         cur.update();
280         cur.resetAnchor();
281 }
282
283
284 void LyXText::gotoInset(LCursor & cur, InsetOld_code code, bool same_content)
285 {
286         gotoInset(cur, vector<InsetOld_code>(1, code), same_content);
287 }
288
289
290 void LyXText::cursorPrevious(LCursor & cur)
291 {
292         pos_type cpos = cur.pos();
293         lyx::paroffset_type cpar = cur.par();
294
295         int x = cur.x_target();
296         int y = bv()->top_y();
297         setCursorFromCoordinates(cur, x, y);
298
299         if (cpar == cur.par() && cpos == cur.pos()) {
300                 // we have a row which is taller than the workarea. The
301                 // simplest solution is to move to the previous row instead.
302                 cursorUp(cur, true);
303         }
304
305         bv()->updateScrollbar();
306         finishUndo();
307 }
308
309
310 void LyXText::cursorNext(LCursor & cur)
311 {
312         pos_type cpos = cur.pos();
313         lyx::paroffset_type cpar = cur.par();
314
315         int x = cur.x_target();
316         int y = bv()->top_y() + bv()->workHeight();
317         setCursorFromCoordinates(cur, x, y);
318
319         if (cpar == cur.par() && cpos == cur.pos()) {
320                 // we have a row which is taller than the workarea. The
321                 // simplest solution is to move to the next row instead.
322                 cursorDown(cur, true);
323         }
324
325         bv()->updateScrollbar();
326         finishUndo();
327 }
328
329
330 namespace {
331
332 void specialChar(LCursor & cur, LyXText * text, InsetSpecialChar::Kind kind)
333 {
334         text->replaceSelection(cur);
335         cur.insert(new InsetSpecialChar(kind));
336         cur.update();
337 }
338
339
340 void doInsertInset(LCursor & cur, LyXText * text,
341         FuncRequest const & cmd, bool edit, bool pastesel)
342 {
343         InsetBase * inset = createInset(&cur.bv(), cmd);
344         if (!inset)
345                 return;
346
347         bool gotsel = false;
348         if (cur.selection()) {
349                 cur.bv().owner()->dispatch(FuncRequest(LFUN_CUT));
350                 gotsel = true;
351         }
352         text->insertInset(cur, inset);
353         if (edit)
354                 inset->edit(cur, true);
355         if (gotsel && pastesel)
356                 cur.bv().owner()->dispatch(FuncRequest(LFUN_PASTE));
357 }
358
359 } // anon namespace
360
361
362 void LyXText::number(LCursor & cur)
363 {
364         LyXFont font(LyXFont::ALL_IGNORE);
365         font.setNumber(LyXFont::TOGGLE);
366         toggleAndShow(cur, this, font);
367 }
368
369
370 bool LyXText::isRTL(Paragraph const & par) const
371 {
372         return par.isRightToLeftPar(bv()->buffer()->params());
373 }
374
375
376 DispatchResult LyXText::dispatch(LCursor & cur, FuncRequest const & cmd)
377 {
378         lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
379         //lyxerr << "*** LyXText::dispatch: cmd: " << cmd << endl;
380
381         BufferView * bv = &cur.bv();
382
383         switch (cmd.action) {
384
385         case LFUN_APPENDIX: {
386                 Paragraph & par = cur.paragraph();
387                 bool start = !par.params().startOfAppendix();
388
389                 // ensure that we have only one start_of_appendix in this document
390                 ParagraphList::iterator tmp = paragraphs().begin();
391                 ParagraphList::iterator end = paragraphs().end();
392
393                 for (; tmp != end; ++tmp) {
394                         if (tmp->params().startOfAppendix()) {
395                                 recUndo(parOffset(tmp));
396                                 tmp->params().startOfAppendix(false);
397                                 redoParagraph(tmp);
398                                 break;
399                         }
400                 }
401
402                 recordUndo(cur);
403                 par.params().startOfAppendix(start);
404
405                 // we can set the refreshing parameters now
406                 updateCounters();
407                 redoParagraph(cur);
408                 cur.update();
409                 break;
410         }
411
412         case LFUN_DELETE_WORD_FORWARD:
413                 cur.clearSelection();
414                 deleteWordForward(cur);
415                 finishChange(cur);
416                 break;
417
418         case LFUN_DELETE_WORD_BACKWARD:
419                 cur.clearSelection();
420                 deleteWordBackward(cur);
421                 finishChange(cur);
422                 break;
423
424         case LFUN_DELETE_LINE_FORWARD:
425                 cur.clearSelection();
426                 deleteLineForward(cur);
427                 finishChange(cur);
428                 break;
429
430         case LFUN_WORDRIGHT:
431                 if (!cur.mark())
432                         cur.clearSelection();
433                 if (isRTL(cur.paragraph()))
434                         cursorLeftOneWord(cur);
435                 else
436                         cursorRightOneWord(cur);
437                 finishChange(cur);
438                 break;
439
440         case LFUN_WORDLEFT:
441                 if (!cur.mark())
442                         cur.clearSelection();
443                 if (isRTL(cur.paragraph()))
444                         cursorRightOneWord(cur);
445                 else
446                         cursorLeftOneWord(cur);
447                 finishChange(cur);
448                 break;
449
450         case LFUN_BEGINNINGBUF:
451                 if (!cur.mark())
452                         cur.clearSelection();
453                 cursorTop(cur);
454                 finishChange(cur);
455                 break;
456
457         case LFUN_ENDBUF:
458                 if (!cur.mark())
459                         cur.clearSelection();
460                 cursorBottom(cur);
461                 finishChange(cur);
462                 break;
463
464         case LFUN_RIGHTSEL:
465                 if (!cur.selection())
466                         cur.resetAnchor();
467                 if (isRTL(cur.paragraph()))
468                         cursorLeft(cur, true);
469                 else
470                         cursorRight(cur, true);
471                 finishChange(cur, true);
472                 break;
473
474         case LFUN_LEFTSEL:
475                 if (!cur.selection())
476                         cur.resetAnchor();
477                 if (isRTL(cur.paragraph()))
478                         cursorRight(cur, true);
479                 else
480                         cursorLeft(cur, true);
481                 finishChange(cur, true);
482                 break;
483
484         case LFUN_UPSEL:
485                 if (!cur.selection())
486                         cur.resetAnchor();
487                 cursorUp(cur, true);
488                 finishChange(cur, true);
489                 break;
490
491         case LFUN_DOWNSEL:
492                 if (!cur.selection())
493                         cur.resetAnchor();
494                 cursorDown(cur, true);
495                 finishChange(cur, true);
496                 break;
497
498         case LFUN_UP_PARAGRAPHSEL:
499                 if (!cur.selection())
500                         cur.resetAnchor();
501                 cursorUpParagraph(cur);
502                 finishChange(cur, true);
503                 break;
504
505         case LFUN_DOWN_PARAGRAPHSEL:
506                 if (!cur.selection())
507                         cur.resetAnchor();
508                 cursorDownParagraph(cur);
509                 finishChange(cur, true);
510                 break;
511
512         case LFUN_PRIORSEL:
513                 if (!cur.selection())
514                         cur.resetAnchor();
515                 cursorPrevious(cur);
516                 finishChange(cur, true);
517                 break;
518
519         case LFUN_NEXTSEL:
520                 if (!cur.selection())
521                         cur.resetAnchor();
522                 cursorNext(cur);
523                 finishChange(cur, true);
524                 break;
525
526         case LFUN_HOMESEL:
527                 if (!cur.selection())
528                         cur.resetAnchor();
529                 cursorHome(cur);
530                 finishChange(cur, true);
531                 break;
532
533         case LFUN_ENDSEL:
534                 if (!cur.selection())
535                         cur.resetAnchor();
536                 cursorEnd(cur);
537                 finishChange(cur, true);
538                 break;
539
540         case LFUN_WORDRIGHTSEL:
541                 if (!cur.selection())
542                         cur.resetAnchor();
543                 if (isRTL(cur.paragraph()))
544                         cursorLeftOneWord(cur);
545                 else
546                         cursorRightOneWord(cur);
547                 finishChange(cur, true);
548                 break;
549
550         case LFUN_WORDLEFTSEL:
551                 if (!cur.selection())
552                         cur.resetAnchor();
553                 if (isRTL(cur.paragraph()))
554                         cursorRightOneWord(cur);
555                 else
556                         cursorLeftOneWord(cur);
557                 finishChange(cur, true);
558                 break;
559
560         case LFUN_WORDSEL: {
561                 selectWord(cur, lyx::WHOLE_WORD);
562                 finishChange(cur, true);
563                 break;
564         }
565
566         case LFUN_RIGHT:
567                 finishChange(cur);
568                 return moveRight(cur);
569
570         case LFUN_LEFT:
571                 finishChange(cur);
572                 return moveLeft(cur);
573
574         case LFUN_UP:
575                 finishChange(cur);
576                 return moveUp(cur);
577
578         case LFUN_DOWN:
579                 finishChange(cur);
580                 return moveDown(cur);
581
582         case LFUN_UP_PARAGRAPH:
583                 if (!cur.mark())
584                         cur.clearSelection();
585                 cursorUpParagraph(cur);
586                 finishChange(cur);
587                 break;
588
589         case LFUN_DOWN_PARAGRAPH:
590                 if (!cur.mark())
591                         cur.clearSelection();
592                 cursorDownParagraph(cur);
593                 finishChange(cur, false);
594                 break;
595
596         case LFUN_PRIOR:
597                 if (!cur.mark())
598                         cur.clearSelection();
599                 finishChange(cur, false);
600                 if (cur.par() == 0 && cur.textRow().pos() == 0)
601                         return DispatchResult(false, FINISHED_UP);
602                 cursorPrevious(cur);
603                 break;
604
605         case LFUN_NEXT:
606                 if (!cur.mark())
607                         cur.clearSelection();
608                 finishChange(cur, false);
609                 if (cur.par() == cur.lastpar()
610                           && cur.textRow().endpos() == cur.lastpos())
611                         return DispatchResult(false, FINISHED_DOWN);
612                 cursorNext(cur);
613                 break;
614
615         case LFUN_HOME:
616                 if (!cur.mark())
617                         cur.clearSelection();
618                 cursorHome(cur);
619                 finishChange(cur, false);
620                 break;
621
622         case LFUN_END:
623                 if (!cur.mark())
624                         cur.clearSelection();
625                 cursorEnd(cur);
626                 finishChange(cur, false);
627                 break;
628
629         case LFUN_BREAKLINE: {
630                 // Not allowed by LaTeX (labels or empty par)
631                 if (cur.pos() > cur.paragraph().beginOfBody()) {
632                         replaceSelection(cur);
633                         cur.insert(new InsetNewline);
634                         moveCursor(cur, false);
635                 }
636                 break;
637         }
638
639         case LFUN_DELETE:
640                 if (!cur.selection()) {
641                         Delete(cur);
642                         cur.resetAnchor();
643                         // It is possible to make it a lot faster still
644                         // just comment out the line below...
645                 } else {
646                         cutSelection(cur, true, false);
647                 }
648                 moveCursor(cur, false);
649                 bv->owner()->view_state_changed();
650                 break;
651
652         case LFUN_DELETE_SKIP:
653                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
654                 if (!cur.selection()) {
655                         if (cur.pos() == cur.lastpos()) {
656                                 cursorRight(cur, true);
657                                 cursorLeft(cur, true);
658                         }
659                         Delete(cur);
660                         cur.resetAnchor();
661                 } else {
662                         cutSelection(cur, true, false);
663                 }
664                 cur.update();
665                 break;
666
667
668         case LFUN_BACKSPACE:
669                 if (!cur.selection()) {
670                         if (bv->owner()->getIntl().getTransManager().backspace()) {
671                                 backspace(cur);
672                                 cur.resetAnchor();
673                                 // It is possible to make it a lot faster still
674                                 // just comment out the line below...
675                         }
676                 } else {
677                         cutSelection(cur, true, false);
678                 }
679                 bv->owner()->view_state_changed();
680                 bv->switchKeyMap();
681                 cur.update();
682                 break;
683
684         case LFUN_BACKSPACE_SKIP:
685                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
686                 if (!cur.selection()) {
687 #warning look here
688                         //CursorSlice cur = cursor();
689                         backspace(cur);
690                         //anchor() = cur;
691                 } else {
692                         cutSelection(cur, true, false);
693                 }
694                 cur.update();
695                 break;
696
697         case LFUN_BREAKPARAGRAPH:
698                 replaceSelection(cur);
699                 breakParagraph(cur, 0);
700                 cur.update();
701                 cur.resetAnchor();
702                 bv->switchKeyMap();
703                 bv->owner()->view_state_changed();
704                 break;
705
706         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
707                 replaceSelection(cur);
708                 breakParagraph(cur, 1);
709                 cur.update();
710                 cur.resetAnchor();
711                 bv->switchKeyMap();
712                 bv->owner()->view_state_changed();
713                 break;
714
715         case LFUN_BREAKPARAGRAPH_SKIP: {
716                 // When at the beginning of a paragraph, remove
717                 // indentation and add a "defskip" at the top.
718                 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
719                 replaceSelection(cur);
720                 if (cur.pos() == 0) {
721                         ParagraphParameters & params = cur.paragraph().params();
722                         setParagraph(cur,
723                                         params.spacing(),
724                                         params.align(),
725                                         params.labelWidthString(), 1);
726                 } else {
727                         breakParagraph(cur, 0);
728                 }
729                 cur.update();
730 //      anchor() = cur;
731                 bv->switchKeyMap();
732                 bv->owner()->view_state_changed();
733                 break;
734         }
735
736         case LFUN_PARAGRAPH_SPACING: {
737                 Paragraph & par = cur.paragraph();
738                 Spacing::Space cur_spacing = par.params().spacing().getSpace();
739                 float cur_value = 1.0;
740                 if (cur_spacing == Spacing::Other)
741                         cur_value = par.params().spacing().getValue();
742
743                 istringstream is(cmd.argument);
744                 string tmp;
745                 is >> tmp;
746                 Spacing::Space new_spacing = cur_spacing;
747                 float new_value = cur_value;
748                 if (tmp.empty()) {
749                         lyxerr << "Missing argument to `paragraph-spacing'"
750                                << endl;
751                 } else if (tmp == "single") {
752                         new_spacing = Spacing::Single;
753                 } else if (tmp == "onehalf") {
754                         new_spacing = Spacing::Onehalf;
755                 } else if (tmp == "double") {
756                         new_spacing = Spacing::Double;
757                 } else if (tmp == "other") {
758                         new_spacing = Spacing::Other;
759                         float tmpval = 0.0;
760                         is >> tmpval;
761                         lyxerr << "new_value = " << tmpval << endl;
762                         if (tmpval != 0.0)
763                                 new_value = tmpval;
764                 } else if (tmp == "default") {
765                         new_spacing = Spacing::Default;
766                 } else {
767                         lyxerr << _("Unknown spacing argument: ")
768                                << cmd.argument << endl;
769                 }
770                 if (cur_spacing != new_spacing || cur_value != new_value) {
771                         par.params().spacing(Spacing(new_spacing, new_value));
772                         redoParagraph(cur);
773                         cur.update();
774                 }
775                 break;
776         }
777
778         case LFUN_INSET_APPLY: {
779                 string const name = cmd.getArg(0);
780                 InsetBase * inset = bv->owner()->getDialogs().getOpenInset(name);
781                 if (inset)
782                         inset->dispatch(cur, FuncRequest(LFUN_INSET_MODIFY, cmd.argument));
783                 else
784                         dispatch(cur, FuncRequest(LFUN_INSET_INSERT, cmd.argument));
785                 break;
786         }
787
788         case LFUN_INSET_INSERT: {
789                 InsetBase * inset = createInset(bv, cmd);
790                 if (inset)
791                         insertInset(cur, inset);
792                 break;
793         }
794
795         case LFUN_INSET_SETTINGS:
796                 if (cur.inset() && cur.inset()->asUpdatableInset())
797                         cur.inset()->asUpdatableInset()->showInsetDialog(bv);
798                 break;
799
800         case LFUN_INSET_TOGGLE:
801                 cur.clearSelection();
802                 if (!toggleInset(cur))
803                         return DispatchResult(false);
804                 cur.update();
805                 bv->switchKeyMap();
806                 break;
807
808         case LFUN_SPACE_INSERT:
809                 if (cur.paragraph().layout()->free_spacing)
810                         insertChar(cur, ' ');
811                 else
812                         doInsertInset(cur, this, cmd, false, false);
813                 moveCursor(cur, false);
814                 break;
815
816         case LFUN_HYPHENATION:
817                 specialChar(cur, this, InsetSpecialChar::HYPHENATION);
818                 break;
819
820         case LFUN_LIGATURE_BREAK:
821                 specialChar(cur, this, InsetSpecialChar::LIGATURE_BREAK);
822                 break;
823
824         case LFUN_LDOTS:
825                 specialChar(cur, this, InsetSpecialChar::LDOTS);
826                 break;
827
828         case LFUN_END_OF_SENTENCE:
829                 specialChar(cur, this, InsetSpecialChar::END_OF_SENTENCE);
830                 break;
831
832         case LFUN_MENU_SEPARATOR:
833                 specialChar(cur, this, InsetSpecialChar::MENU_SEPARATOR);
834                 break;
835
836         case LFUN_UPCASE_WORD:
837                 changeCase(cur, LyXText::text_uppercase);
838                 cur.update();
839                 break;
840
841         case LFUN_LOWCASE_WORD:
842                 changeCase(cur, LyXText::text_lowercase);
843                 cur.update();
844                 break;
845
846         case LFUN_CAPITALIZE_WORD:
847                 changeCase(cur, LyXText::text_capitalization);
848                 cur.update();
849                 break;
850
851         case LFUN_TRANSPOSE_CHARS:
852                 recordUndo(cur);
853                 redoParagraph(cur);
854                 cur.update();
855                 break;
856
857         case LFUN_PASTE:
858                 cur.message(_("Paste"));
859                 replaceSelection(cur);
860 #warning FIXME Check if the arg is in the domain of available selections.
861                 if (isStrUnsignedInt(cmd.argument))
862                         pasteSelection(cur, strToUnsignedInt(cmd.argument));
863                 else
864                         pasteSelection(cur, 0);
865                 cur.clearSelection(); // bug 393
866                 cur.update();
867                 bv->switchKeyMap();
868                 finishUndo();
869                 break;
870
871         case LFUN_CUT:
872                 cutSelection(cur, true, true);
873                 cur.message(_("Cut"));
874                 cur.update();
875                 break;
876
877         case LFUN_COPY:
878                 copySelection(cur);
879                 cur.message(_("Copy"));
880                 break;
881
882         case LFUN_BEGINNINGBUFSEL:
883                 if (in_inset_)
884                         return DispatchResult(false);
885                 if (!cur.selection())
886                         cur.resetAnchor();
887                 cursorTop(cur);
888                 finishChange(cur, true);
889                 break;
890
891         case LFUN_ENDBUFSEL:
892                 if (in_inset_)
893                         return DispatchResult(false);
894                 if (!cur.selection())
895                         cur.resetAnchor();
896                 cursorBottom(cur);
897                 finishChange(cur, true);
898                 break;
899
900         case LFUN_GETXY:
901                 cur.message(tostr(cursorX(cur.current())) + ' '
902                           + tostr(cursorY(cur.current())));
903                 break;
904
905         case LFUN_SETXY: {
906                 int x = 0;
907                 int y = 0;
908                 istringstream is(cmd.argument);
909                 is >> x >> y;
910                 if (!is)
911                         lyxerr << "SETXY: Could not parse coordinates in '"
912                                << cmd.argument << std::endl;
913                 else
914                         setCursorFromCoordinates(cur, x, y);
915                 break;
916         }
917
918         case LFUN_GETFONT:
919                 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
920                         cur.message("E");
921                 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
922                         cur.message("N");
923                 else
924                         cur.message("0");
925                 break;
926
927         case LFUN_GETLAYOUT:
928                 cur.message(cur.paragraph().layout()->name());
929                 break;
930
931         case LFUN_LAYOUT: {
932                 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
933                   << cmd.argument << endl;
934
935                 // This is not the good solution to the empty argument
936                 // problem, but it will hopefully suffice for 1.2.0.
937                 // The correct solution would be to augument the
938                 // function list/array with information about what
939                 // functions needs arguments and their type.
940                 if (cmd.argument.empty()) {
941                         cur.errorMessage(_("LyX function 'layout' needs an argument."));
942                         break;
943                 }
944
945                 // Derive layout number from given argument (string)
946                 // and current buffer's textclass (number)
947                 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
948                 bool hasLayout = tclass.hasLayout(cmd.argument);
949                 string layout = cmd.argument;
950
951                 // If the entry is obsolete, use the new one instead.
952                 if (hasLayout) {
953                         string const & obs = tclass[layout]->obsoleted_by();
954                         if (!obs.empty())
955                                 layout = obs;
956                 }
957
958                 if (!hasLayout) {
959                         cur.errorMessage(string(N_("Layout ")) + cmd.argument +
960                                 N_(" not known"));
961                         break;
962                 }
963
964                 bool change_layout = (current_layout != layout);
965
966                 if (!change_layout && cur.selection() &&
967                         cur.selBegin().par() != cur.selEnd().par())
968                 {
969                         ParagraphList::iterator spit = getPar(cur.selBegin());
970                         ParagraphList::iterator epit = boost::next(getPar(cur.selEnd()));
971                         while (spit != epit) {
972                                 if (spit->layout()->name() != current_layout) {
973                                         change_layout = true;
974                                         break;
975                                 }
976                                 ++spit;
977                         }
978                 }
979
980                 if (change_layout) {
981                         current_layout = layout;
982                         setLayout(cur, layout);
983                         bv->owner()->setLayout(layout);
984                         cur.update();
985                         bv->switchKeyMap();
986                 }
987                 break;
988         }
989
990         case LFUN_PASTESELECTION: {
991                 cur.clearSelection();
992                 string const clip = bv->getClipboard();
993                 if (!clip.empty()) {
994                         if (cmd.argument == "paragraph")
995                                 insertStringAsParagraphs(cur, clip);
996                         else
997                                 insertStringAsLines(cur, clip);
998                         cur.update();
999                 }
1000                 break;
1001         }
1002
1003         case LFUN_GOTOERROR:
1004                 gotoInset(cur, InsetBase::ERROR_CODE, false);
1005                 break;
1006
1007         case LFUN_GOTONOTE:
1008                 gotoInset(cur, InsetBase::NOTE_CODE, false);
1009                 break;
1010
1011         case LFUN_REFERENCE_GOTO: {
1012                 vector<InsetOld_code> tmp;
1013                 tmp.push_back(InsetBase::LABEL_CODE);
1014                 tmp.push_back(InsetBase::REF_CODE);
1015                 gotoInset(cur, tmp, true);
1016                 break;
1017         }
1018
1019         case LFUN_QUOTE: {
1020                 replaceSelection(cur);
1021                 Paragraph & par = cur.paragraph();
1022                 lyx::pos_type pos = cur.pos();
1023                 char c;
1024                 if (pos == 0)
1025                         c = ' ';
1026                 else if (cur.prevInset() && cur.prevInset()->isSpace())
1027                         c = ' ';
1028                 else
1029                         c = par.getChar(pos - 1);
1030
1031                 LyXLayout_ptr const & style = par.layout();
1032                 
1033                 BufferParams const & bufparams = bv->buffer()->params();
1034                 if (!style->pass_thru
1035                     && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1036                         string arg = cmd.argument;
1037                         if (arg == "single")
1038                                 cur.insert(new InsetQuotes(c,
1039                                     bufparams.quotes_language, 
1040                                     InsetQuotes::SingleQ));
1041                         else if (arg == "double")
1042                                 cur.insert(new InsetQuotes(c,
1043                                     bufparams.quotes_language, 
1044                                     InsetQuotes::DoubleQ));
1045                         else
1046                                 cur.insert(new InsetQuotes(c, bufparams));
1047                         }
1048                 else
1049                         bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1050                 break;
1051         }
1052
1053         case LFUN_DATE_INSERT: {
1054                 replaceSelection(cur);
1055                 time_t now_time_t = time(NULL);
1056                 struct tm * now_tm = localtime(&now_time_t);
1057                 setlocale(LC_TIME, "");
1058                 string arg;
1059                 if (!cmd.argument.empty())
1060                         arg = cmd.argument;
1061                 else
1062                         arg = lyxrc.date_insert_format;
1063                 char datetmp[32];
1064                 int const datetmp_len =
1065                         ::strftime(datetmp, 32, arg.c_str(), now_tm);
1066
1067                 for (int i = 0; i < datetmp_len; i++)
1068                         insertChar(cur, datetmp[i]);
1069
1070                 cur.resetAnchor();
1071                 moveCursor(cur, false);
1072                 break;
1073         }
1074
1075         case LFUN_MOUSE_TRIPLE:
1076                 if (cmd.button() == mouse_button::button1) {
1077                         selection_possible = true;
1078                         cursorHome(cur);
1079                         cur.resetAnchor();
1080                         cursorEnd(cur);
1081                         cur.setSelection();
1082                         bv->haveSelection(cur.selection());
1083                 }
1084                 break;
1085
1086         case LFUN_MOUSE_DOUBLE:
1087                 if (cmd.button() == mouse_button::button1) {
1088                         selection_possible = true;
1089                         selectWord(cur, lyx::WHOLE_WORD_STRICT);
1090                         bv->haveSelection(cur.selection());
1091                 }
1092                 break;
1093
1094         case LFUN_MOUSE_MOTION: {
1095                 // Only use motion with button 1
1096                 //if (cmd.button() != mouse_button::button1)
1097                 //      return false;
1098                 // The test for not selection possible is needed, that
1099                 // only motion events are used, where the bottom press
1100                 // event was on the drawing area too
1101                 if (!selection_possible) {
1102                         lyxerr[Debug::ACTION] << "BufferView::Pimpl::"
1103                                 "Dispatch: no selection possible\n";
1104                         break;
1105                 }
1106                 CursorSlice old = cur.current();
1107                 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1108
1109                 // This is to allow jumping over large insets
1110                 // FIXME: shouldn't be top-text-specific
1111                 if (!in_inset_ && cur.current() == old) {
1112                         if (cmd.y - bv->top_y() >= bv->workHeight())
1113                                 cursorDown(cur, true);
1114                         else if (cmd.y - bv->top_y() < 0)
1115                                 cursorUp(cur, true);
1116                 }
1117
1118                 // don't set anchor_
1119                 bv->cursor().cursor_ = cur.cursor_;
1120                 bv->cursor().setSelection();
1121                 break;
1122         }
1123
1124         // Single-click on work area
1125         case LFUN_MOUSE_PRESS: {
1126                 // ok ok, this is a hack (for xforms)
1127                 // We shouldn't go further down as we really should only do the
1128                 // scrolling and be done with this. Otherwise we may open some
1129                 // dialogs (Jug 20020424).
1130                 if (cmd.button() == mouse_button::button4) {
1131                         bv->scroll(-lyxrc.wheel_jump);
1132                         break;
1133                 }
1134
1135                 if (cmd.button() == mouse_button::button5) {
1136                         bv->scroll(lyxrc.wheel_jump);
1137                         break;
1138                 }
1139
1140                 // Right click on a footnote flag opens float menu
1141                 if (cmd.button() == mouse_button::button3) {
1142                         cur.clearSelection();
1143                         selection_possible = false;
1144                         break;
1145                 }
1146
1147                 // Middle button press pastes if we have a selection
1148                 // We do this here as if the selection was inside an inset
1149                 // it could get cleared on the unlocking of the inset so
1150                 // we have to check this first
1151                 bool paste_internally = false;
1152                 if (cmd.button() == mouse_button::button2 && cur.selection()) {
1153                         bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1154                         paste_internally = true;
1155                 }
1156
1157                 selection_possible = true;
1158
1159                 // Clear the selection
1160                 cur.clearSelection();
1161
1162                 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1163                 cur.resetAnchor();
1164                 finishUndo();
1165                 cur.x_target() = cursorX(cur.current());
1166
1167                 // set cursor and anchor to this position
1168                 bv->cursor() = cur;
1169
1170                 if (bv->fitCursor())
1171                         selection_possible = false;
1172
1173                 // Insert primary selection with middle mouse
1174                 // if there is a local selection in the current buffer,
1175                 // insert this
1176                 if (cmd.button() == mouse_button::button2) {
1177                         if (paste_internally)
1178                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1179                         else
1180                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1181                         selection_possible = false;
1182                 }
1183                 break;
1184         }
1185
1186         case LFUN_MOUSE_RELEASE: {
1187                 // do nothing if we used the mouse wheel
1188                 if (cmd.button() == mouse_button::button4
1189                  || cmd.button() == mouse_button::button5)
1190                         return DispatchResult(true, false);
1191
1192                 selection_possible = false;
1193
1194                 if (cmd.button() == mouse_button::button2)
1195                         break;
1196
1197                 // finish selection
1198                 if (cmd.button() == mouse_button::button1)
1199                         bv->haveSelection(cur.selection());
1200
1201                 bv->switchKeyMap();
1202                 bv->owner()->view_state_changed();
1203                 bv->owner()->updateMenubar();
1204                 bv->owner()->updateToolbar();
1205                 break;
1206         }
1207
1208         case LFUN_SELFINSERT: {
1209                 if (cmd.argument.empty())
1210                         break;
1211
1212                 // Automatically delete the currently selected
1213                 // text and replace it with what is being
1214                 // typed in now. Depends on lyxrc settings
1215                 // "auto_region_delete", which defaults to
1216                 // true (on).
1217
1218                 if (lyxrc.auto_region_delete) {
1219                         if (cur.selection())
1220                                 cutSelection(cur, false, false);
1221                         bv->haveSelection(false);
1222                 }
1223
1224                 cur.clearSelection();
1225                 LyXFont const old_font = real_current_font;
1226
1227                 string::const_iterator cit = cmd.argument.begin();
1228                 string::const_iterator end = cmd.argument.end();
1229                 for (; cit != end; ++cit)
1230                         bv->owner()->getIntl().getTransManager().
1231                                 TranslateAndInsert(*cit, this);
1232
1233                 cur.resetAnchor();
1234                 moveCursor(cur, false);
1235
1236                 // real_current_font.number can change so we need to
1237                 // update the minibuffer
1238                 if (old_font != real_current_font)
1239                         bv->owner()->view_state_changed();
1240                 bv->updateScrollbar();
1241                 break;
1242         }
1243
1244         case LFUN_URL: {
1245                 InsetCommandParams p("url");
1246                 string const data = InsetCommandMailer::params2string("url", p);
1247                 bv->owner()->getDialogs().show("url", data, 0);
1248                 break;
1249         }
1250
1251         case LFUN_HTMLURL: {
1252                 InsetCommandParams p("htmlurl");
1253                 string const data = InsetCommandMailer::params2string("url", p);
1254                 bv->owner()->getDialogs().show("url", data, 0);
1255                 break;
1256         }
1257
1258         case LFUN_INSERT_LABEL: {
1259                 InsetCommandParams p("label");
1260                 string const data = InsetCommandMailer::params2string("label", p);
1261                 bv->owner()->getDialogs().show("label", data, 0);
1262                 break;
1263         }
1264
1265
1266 #if 0
1267         case LFUN_INSET_LIST:
1268         case LFUN_INSET_THEOREM:
1269         case LFUN_INSET_CAPTION:
1270 #endif
1271         case LFUN_INSERT_NOTE:
1272         case LFUN_INSERT_CHARSTYLE:
1273         case LFUN_INSERT_BOX:
1274         case LFUN_INSERT_BRANCH:
1275         case LFUN_INSERT_BIBITEM:
1276         case LFUN_INSET_ERT:
1277         case LFUN_INSET_FLOAT:
1278         case LFUN_INSET_FOOTNOTE:
1279         case LFUN_INSET_MARGINAL:
1280         case LFUN_INSET_OPTARG:
1281         case LFUN_INSET_WIDE_FLOAT:
1282         case LFUN_INSET_WRAP:
1283         case LFUN_TABULAR_INSERT:
1284         case LFUN_ENVIRONMENT_INSERT:
1285                 // Open the inset, and move the current selection
1286                 // inside it.
1287                 doInsertInset(cur, this, cmd, true, true);
1288                 break;
1289
1290         case LFUN_INDEX_INSERT:
1291                 // Just open the inset
1292                 doInsertInset(cur, this, cmd, true, false);
1293                 break;
1294
1295         case LFUN_INDEX_PRINT:
1296         case LFUN_TOC_INSERT:
1297         case LFUN_HFILL:
1298         case LFUN_INSERT_LINE:
1299         case LFUN_INSERT_PAGEBREAK:
1300                 // do nothing fancy
1301                 doInsertInset(cur, this, cmd, false, false);
1302                 break;
1303
1304         case LFUN_DEPTH_MIN:
1305                 changeDepth(cur, bv_funcs::DEC_DEPTH);
1306                 cur.update();
1307                 break;
1308
1309         case LFUN_DEPTH_PLUS:
1310                 changeDepth(cur, bv_funcs::INC_DEPTH);
1311                 cur.update();
1312                 break;
1313
1314         case LFUN_MATH_DISPLAY:
1315                 mathDispatch(cur, this, cmd, true);
1316                 break;
1317
1318         case LFUN_MATH_IMPORT_SELECTION:
1319         case LFUN_MATH_MODE:
1320                 mathDispatch(cur, this, cmd, false);
1321                 break;
1322
1323         case LFUN_MATH_MACRO:
1324                 if (cmd.argument.empty())
1325                         cur.errorMessage(N_("Missing argument"));
1326                 else {
1327                         string s = cmd.argument;
1328                         string const s1 = token(s, ' ', 1);
1329                         int const nargs = s1.empty() ? 0 : atoi(s1);
1330                         string const s2 = token(s, ' ', 2);
1331                         string const type = s2.empty() ? "newcommand" : s2;
1332                         cur.insert(new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
1333                         cur.nextInset()->edit(cur, true);
1334                 }
1335                 break;
1336
1337         case LFUN_INSERT_MATH:
1338         case LFUN_INSERT_MATRIX:
1339         case LFUN_MATH_DELIM: {
1340                 cur.insert(new MathHullInset);
1341                 cur.dispatch(FuncRequest(LFUN_RIGHT));
1342                 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
1343                 cur.dispatch(cmd);
1344                 break;
1345         }
1346
1347         case LFUN_EMPH: {
1348                 LyXFont font(LyXFont::ALL_IGNORE);
1349                 font.setEmph(LyXFont::TOGGLE);
1350                 toggleAndShow(cur, this, font);
1351                 bv->owner()->view_state_changed();
1352                 break;
1353         }
1354
1355         case LFUN_BOLD: {
1356                 LyXFont font(LyXFont::ALL_IGNORE);
1357                 font.setSeries(LyXFont::BOLD_SERIES);
1358                 toggleAndShow(cur, this, font);
1359                 bv->owner()->view_state_changed();
1360                 break;
1361         }
1362
1363         case LFUN_NOUN: {
1364                 LyXFont font(LyXFont::ALL_IGNORE);
1365                 font.setNoun(LyXFont::TOGGLE);
1366                 toggleAndShow(cur, this, font);
1367                 bv->owner()->view_state_changed();
1368                 break;
1369         }
1370
1371         case LFUN_CODE: {
1372                 LyXFont font(LyXFont::ALL_IGNORE);
1373                 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
1374                 toggleAndShow(cur, this, font);
1375                 bv->owner()->view_state_changed();
1376                 break;
1377         }
1378
1379         case LFUN_SANS: {
1380                 LyXFont font(LyXFont::ALL_IGNORE);
1381                 font.setFamily(LyXFont::SANS_FAMILY);
1382                 toggleAndShow(cur, this, font);
1383                 bv->owner()->view_state_changed();
1384                 break;
1385         }
1386
1387         case LFUN_ROMAN: {
1388                 LyXFont font(LyXFont::ALL_IGNORE);
1389                 font.setFamily(LyXFont::ROMAN_FAMILY);
1390                 toggleAndShow(cur, this, font);
1391                 bv->owner()->view_state_changed();
1392                 break;
1393         }
1394
1395         case LFUN_DEFAULT: {
1396                 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
1397                 toggleAndShow(cur, this, font);
1398                 bv->owner()->view_state_changed();
1399                 break;
1400         }
1401
1402         case LFUN_UNDERLINE: {
1403                 LyXFont font(LyXFont::ALL_IGNORE);
1404                 font.setUnderbar(LyXFont::TOGGLE);
1405                 toggleAndShow(cur, this, font);
1406                 bv->owner()->view_state_changed();
1407                 break;
1408         }
1409
1410         case LFUN_FONT_SIZE: {
1411                 LyXFont font(LyXFont::ALL_IGNORE);
1412                 font.setLyXSize(cmd.argument);
1413                 toggleAndShow(cur, this, font);
1414                 bv->owner()->view_state_changed();
1415                 break;
1416         }
1417
1418         case LFUN_LANGUAGE: {
1419                 Language const * lang = languages.getLanguage(cmd.argument);
1420                 if (!lang)
1421                         break;
1422                 LyXFont font(LyXFont::ALL_IGNORE);
1423                 font.setLanguage(lang);
1424                 toggleAndShow(cur, this, font);
1425                 bv->switchKeyMap();
1426                 bv->owner()->view_state_changed();
1427                 break;
1428         }
1429
1430         case LFUN_FREEFONT_APPLY:
1431                 toggleAndShow(cur, this, freefont, toggleall);
1432                 bv->owner()->view_state_changed();
1433                 cur.message(_("Character set"));
1434                 break;
1435
1436         // Set the freefont using the contents of \param data dispatched from
1437         // the frontends and apply it at the current cursor location.
1438         case LFUN_FREEFONT_UPDATE: {
1439                 LyXFont font;
1440                 bool toggle;
1441                 if (bv_funcs::string2font(cmd.argument, font, toggle)) {
1442                         freefont = font;
1443                         toggleall = toggle;
1444                         toggleAndShow(cur, this, freefont, toggleall);
1445                         bv->owner()->view_state_changed();
1446                         cur.message(_("Character set"));
1447                 }
1448                 break;
1449         }
1450
1451         case LFUN_FINISHED_LEFT:
1452                 lyxerr << "handle LFUN_FINISHED_LEFT" << endl;
1453                 cur.pop(cur.currentDepth());
1454                 if (isRTL(cur.paragraph()))
1455                         cursorLeft(cur, true);
1456                 cur.bv().cursor() = cur;
1457                 break;
1458
1459         case LFUN_FINISHED_RIGHT:
1460                 lyxerr << "handle LFUN_FINISHED_RIGHT" << endl;
1461                 cur.pop(cur.currentDepth());
1462                 if (!isRTL(cur.paragraph()))
1463                         cursorRight(cur, true);
1464                 cur.bv().cursor() = cur;
1465                 break;
1466
1467         case LFUN_FINISHED_UP:
1468                 lyxerr << "handle LFUN_FINISHED_UP" << endl;
1469                 cur.pop(cur.currentDepth());
1470                 cursorUp(cur, true);
1471                 cur.bv().cursor() = cur;
1472                 break;
1473
1474         case LFUN_FINISHED_DOWN:
1475                 lyxerr << "handle LFUN_FINISHED_DOWN" << endl;
1476                 cur.pop(cur.currentDepth());
1477                 cursorDown(cur, true);
1478                 cur.bv().cursor() = cur;
1479                 break;
1480
1481         case LFUN_LAYOUT_PARAGRAPH: {
1482                 string data;
1483                 params2string(cur.paragraph(), data);
1484                 data = "show\n" + data;
1485                 bv->owner()->getDialogs().show("paragraph", data);
1486                 break;
1487         }
1488
1489         case LFUN_PARAGRAPH_UPDATE: {
1490                 if (!bv->owner()->getDialogs().visible("paragraph"))
1491                         break;
1492                 string data;
1493                 params2string(cur.paragraph(), data);
1494
1495                 // Will the paragraph accept changes from the dialog?
1496                 InsetBase * const inset = cur.inset();
1497                 bool const accept =
1498                         !(inset && inset->forceDefaultParagraphs(inset));
1499
1500                 data = "update " + tostr(accept) + '\n' + data;
1501                 bv->owner()->getDialogs().update("paragraph", data);
1502                 break;
1503         }
1504
1505         case LFUN_UMLAUT:
1506         case LFUN_CIRCUMFLEX:
1507         case LFUN_GRAVE:
1508         case LFUN_ACUTE:
1509         case LFUN_TILDE:
1510         case LFUN_CEDILLA:
1511         case LFUN_MACRON:
1512         case LFUN_DOT:
1513         case LFUN_UNDERDOT:
1514         case LFUN_UNDERBAR:
1515         case LFUN_CARON:
1516         case LFUN_SPECIAL_CARON:
1517         case LFUN_BREVE:
1518         case LFUN_TIE:
1519         case LFUN_HUNG_UMLAUT:
1520         case LFUN_CIRCLE:
1521         case LFUN_OGONEK:
1522                 bv->owner()->getLyXFunc().handleKeyFunc(cmd.action);
1523                 if (!cmd.argument.empty())
1524                         bv->owner()->getIntl().getTransManager()
1525                                 .TranslateAndInsert(cmd.argument[0], this);
1526                 break;
1527
1528         case LFUN_FLOAT_LIST: {
1529                 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1530                 if (tclass.floats().typeExist(cmd.argument)) {
1531                         // not quite sure if we want this...
1532                         recordUndo(cur);
1533                         freezeUndo();
1534                         cur.clearSelection();
1535                         breakParagraph(cur);
1536
1537                         if (cur.lastpos() != 0) {
1538                                 cursorLeft(cur, true);
1539                                 breakParagraph(cur);
1540                         }
1541
1542                         setLayout(cur, tclass.defaultLayoutName());
1543                         setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1544                         cur.insert(new InsetFloatList(cmd.argument));
1545                         unFreezeUndo();
1546                 } else {
1547                         lyxerr << "Non-existent float type: "
1548                                << cmd.argument << endl;
1549                 }
1550                 break;
1551         }
1552
1553         case LFUN_ACCEPT_CHANGE: {
1554                 acceptChange(cur);
1555                 cur.update();
1556                 break;
1557         }
1558
1559         case LFUN_REJECT_CHANGE: {
1560                 rejectChange(cur);
1561                 cur.update();
1562                 break;
1563         }
1564
1565         case LFUN_THESAURUS_ENTRY: {
1566                 string arg = cmd.argument;
1567                 if (arg.empty()) {
1568                         arg = cur.selectionAsString(false);
1569                         // FIXME
1570                         if (arg.size() > 100 || arg.empty()) {
1571                                 // Get word or selection
1572                                 selectWordWhenUnderCursor(cur, lyx::WHOLE_WORD);
1573                                 arg = cur.selectionAsString(false);
1574                         }
1575                 }
1576                 bv->owner()->getDialogs().show("thesaurus", arg);
1577                 break;
1578         }
1579
1580         case LFUN_PARAGRAPH_APPLY: {
1581                 // Given data, an encoding of the ParagraphParameters
1582                 // generated in the Paragraph dialog, this function sets
1583                 // the current paragraph appropriately.
1584                 istringstream is(cmd.argument);
1585                 LyXLex lex(0, 0);
1586                 lex.setStream(is);
1587                 ParagraphParameters params;
1588                 params.read(lex);
1589                 setParagraph(cur,
1590                                          params.spacing(),
1591                                          params.align(),
1592                                          params.labelWidthString(),
1593                                          params.noindent());
1594                 cur.update();
1595                 cur.message(_("Paragraph layout set"));
1596                 break;
1597         }
1598
1599         default:
1600                 return DispatchResult(false);
1601         }
1602
1603         return DispatchResult(true, true);
1604 }