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