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