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