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