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