]> git.lyx.org Git - lyx.git/blob - src/text3.C
move some selection related stuff over to textcursor.C
[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 "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 (cursorRow() == 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 = cursorRow();
256
257         setCursorFromCoordinates(cursor.x_fix(), y);
258         finishUndo();
259
260         int new_y;
261         if (cursorrow == bv()->text->cursorRow()) {
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                                 + cursorRow()->height()
276                                 - bv()->workHeight() + 1;
277                 } else {
278                         new_y = cursor.y()
279                                 - cursorRow()->baseline()
280                                 + cursorRow()->height()
281                                 - bv()->workHeight() + 1;
282                 }
283         }
284         bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
285         if (cursorRow() != rows().begin()) {
286                 LyXCursor cur;
287                 setCursor(cur, boost::prior(cursorRow())->par(),
288                           boost::prior(cursorRow())->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(cursorRow()) == rows().end()) {
302                 int y = cursor.y() - cursorRow()->baseline() +
303                         cursorRow()->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 = cursorRow();
321         setCursorFromCoordinates(cursor.x_fix(), y);
322         // + bv->workHeight());
323         finishUndo();
324
325         int new_y;
326         if (cursorrow == bv()->text->cursorRow()) {
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 - cursorRow()->baseline();
341                 } else {
342                         new_y =  cursor.y() - cursorRow()->baseline();
343                 }
344         }
345         bv()->screen().draw(bv()->text, bv(), new_y);
346
347         RowList::iterator next_row = boost::next(cursorRow());
348         if (next_row != rows().end()) {
349                 LyXCursor cur;
350                 setCursor(cur, next_row->par(), next_row->pos(), false);
351                 if (cur.y() < top_y() + bv()->workHeight()) {
352                         cursorDown(true);
353                 }
354         }
355         bv()->updateScrollbar();
356 }
357
358
359 void LyXText::update()
360 {
361         bv()->update(this, BufferView::SELECT);
362 }
363
364 namespace {
365
366 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
367 {
368         lt->update();
369         InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
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         Inset * 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 Inset::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                                 setUndo(bv, Undo::EDIT, tmp);
424                                 tmp->params().startOfAppendix(false);
425                                 int tmpy;
426                                 setHeightOfRow(getRow(tmp, 0, tmpy));
427                                 break;
428                         }
429                 }
430
431                 setUndo(bv, Undo::EDIT, pit);
432                 pit->params().startOfAppendix(start);
433
434                 // we can set the refreshing parameters now
435                 updateCounters();
436                 redoHeightOfParagraph();
437                 postPaint(0);
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;
592                 LyXCursor cur2;
593                 getWord(cur1, cur2, WHOLE_WORD);
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                         Inset * 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                         Inset * 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(bv);
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(bv);
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                 // was:
688                 // finishUndo();
689                 // moveCursorUpdate(bv, false, false);
690                 // owner_->view_state_changed();
691                 break;
692
693         case LFUN_NEXT:
694                 if (!selection.mark())
695                         bv->beforeChange(this);
696                 bv->update(this, BufferView::UPDATE);
697                 cursorNext();
698                 finishChange(bv, false);
699                 break;
700
701         case LFUN_HOME:
702                 if (!selection.mark())
703                         bv->beforeChange(this);
704                 update();
705                 cursorHome();
706                 finishChange(bv, false);
707                 break;
708
709         case LFUN_END:
710                 if (!selection.mark())
711                         bv->beforeChange(this);
712                 update();
713                 cursorEnd();
714                 finishChange(bv, false);
715                 break;
716
717         case LFUN_BREAKLINE: {
718                 lyx::pos_type body = cursor.par()->beginningOfBody();
719
720                 // Not allowed by LaTeX (labels or empty par)
721                 if (cursor.pos() <= body)
722                         break;
723
724                 bv->beforeChange(this);
725                 insertInset(new InsetNewline);
726                 update();
727                 setCursor(cursor.par(), cursor.pos());
728                 moveCursorUpdate(bv, false);
729                 break;
730         }
731
732         case LFUN_DELETE:
733                 if (!selection.set()) {
734                         Delete();
735                         selection.cursor = cursor;
736                         update();
737                         // It is possible to make it a lot faster still
738                         // just comment out the line below...
739                 } else {
740                         update();
741                         cutSelection(true, false);
742                         update();
743                 }
744                 moveCursorUpdate(bv, false);
745                 bv->owner()->view_state_changed();
746                 bv->switchKeyMap();
747                 break;
748
749         case LFUN_DELETE_SKIP:
750                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
751                 if (!selection.set()) {
752                         LyXCursor cur = cursor;
753                         if (cur.pos() == cur.par()->size()) {
754                                 cursorRight(bv);
755                                 cur = cursor;
756                                 if (cur.pos() == 0
757                                     && !(cur.par()->params().spaceTop()
758                                          == VSpace (VSpace::NONE))) {
759                                         setParagraph(
760                                                  cur.par()->params().lineTop(),
761                                                  cur.par()->params().lineBottom(),
762                                                  cur.par()->params().pagebreakTop(),
763                                                  cur.par()->params().pagebreakBottom(),
764                                                  VSpace(VSpace::NONE),
765                                                  cur.par()->params().spaceBottom(),
766                                                  cur.par()->params().spacing(),
767                                                  cur.par()->params().align(),
768                                                  cur.par()->params().labelWidthString(), 0);
769                                         cursorLeft(bv);
770                                         update();
771                                 } else {
772                                         cursorLeft(bv);
773                                         Delete();
774                                         selection.cursor = cursor;
775                                 }
776                         } else {
777                                 Delete();
778                                 selection.cursor = cursor;
779                         }
780                 } else {
781                         update();
782                         cutSelection(true, false);
783                 }
784                 update();
785                 break;
786
787
788         case LFUN_BACKSPACE:
789                 if (!selection.set()) {
790                         if (bv->owner()->getIntl().getTransManager().backspace()) {
791                                 backspace();
792                                 selection.cursor = cursor;
793                                 update();
794                                 // It is possible to make it a lot faster still
795                                 // just comment out the line below...
796                         }
797                 } else {
798                         update();
799                         cutSelection(true, false);
800                         update();
801                 }
802                 bv->owner()->view_state_changed();
803                 bv->switchKeyMap();
804                 break;
805
806         case LFUN_BACKSPACE_SKIP:
807                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
808                 if (!selection.set()) {
809                         LyXCursor cur = cursor;
810                         if (cur.pos() == 0
811                             && !(cur.par()->params().spaceTop()
812                                  == VSpace (VSpace::NONE))) {
813                                 setParagraph(
814                                          cur.par()->params().lineTop(),
815                                          cur.par()->params().lineBottom(),
816                                          cur.par()->params().pagebreakTop(),
817                                          cur.par()->params().pagebreakBottom(),
818                                          VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
819                                          cur.par()->params().spacing(),
820                                          cur.par()->params().align(),
821                                          cur.par()->params().labelWidthString(), 0);
822                         } else {
823                                 backspace();
824                                 selection.cursor = cur;
825                         }
826                 } else {
827                         update();
828                         cutSelection(true, false);
829                 }
830                 update();
831                 break;
832
833         case LFUN_BREAKPARAGRAPH:
834                 bv->beforeChange(this);
835                 breakParagraph(bv->buffer()->paragraphs, 0);
836                 update();
837                 selection.cursor = cursor;
838                 bv->switchKeyMap();
839                 bv->owner()->view_state_changed();
840                 break;
841
842         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
843                 bv->beforeChange(this);
844                 breakParagraph(bv->buffer()->paragraphs, 1);
845                 update();
846                 selection.cursor = cursor;
847                 bv->switchKeyMap();
848                 bv->owner()->view_state_changed();
849                 break;
850
851         case LFUN_BREAKPARAGRAPH_SKIP: {
852                 // When at the beginning of a paragraph, remove
853                 // indentation and add a "defskip" at the top.
854                 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
855                 LyXCursor cur = cursor;
856                 bv->beforeChange(this);
857                 if (cur.pos() == 0) {
858                         if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
859                                 setParagraph(
860                                          cur.par()->params().lineTop(),
861                                          cur.par()->params().lineBottom(),
862                                          cur.par()->params().pagebreakTop(),
863                                          cur.par()->params().pagebreakBottom(),
864                                          VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
865                                          cur.par()->params().spacing(),
866                                          cur.par()->params().align(),
867                                          cur.par()->params().labelWidthString(), 1);
868                         }
869                 }
870                 else {
871                         breakParagraph(bv->buffer()->paragraphs, 0);
872                 }
873                 update();
874                 selection.cursor = cur;
875                 bv->switchKeyMap();
876                 bv->owner()->view_state_changed();
877                 break;
878         }
879
880         case LFUN_PARAGRAPH_SPACING: {
881                 ParagraphList::iterator pit = cursor.par();
882                 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
883                 float cur_value = 1.0;
884                 if (cur_spacing == Spacing::Other)
885                         cur_value = pit->params().spacing().getValue();
886
887                 istringstream is(STRCONV(cmd.argument));
888                 string tmp;
889                 is >> tmp;
890                 Spacing::Space new_spacing = cur_spacing;
891                 float new_value = cur_value;
892                 if (tmp.empty()) {
893                         lyxerr << "Missing argument to `paragraph-spacing'"
894                                << endl;
895                 } else if (tmp == "single") {
896                         new_spacing = Spacing::Single;
897                 } else if (tmp == "onehalf") {
898                         new_spacing = Spacing::Onehalf;
899                 } else if (tmp == "double") {
900                         new_spacing = Spacing::Double;
901                 } else if (tmp == "other") {
902                         new_spacing = Spacing::Other;
903                         float tmpval = 0.0;
904                         is >> tmpval;
905                         lyxerr << "new_value = " << tmpval << endl;
906                         if (tmpval != 0.0)
907                                 new_value = tmpval;
908                 } else if (tmp == "default") {
909                         new_spacing = Spacing::Default;
910                 } else {
911                         lyxerr << _("Unknown spacing argument: ")
912                                << cmd.argument << endl;
913                 }
914                 if (cur_spacing != new_spacing || cur_value != new_value) {
915                         pit->params().spacing(Spacing(new_spacing, new_value));
916                         redoParagraph();
917                         update();
918                 }
919                 break;
920         }
921
922         case LFUN_INSET_SETTINGS:
923                 lyx::Assert(bv->theLockingInset());
924                 bv->theLockingInset()->getLockingInset()->showInsetDialog(bv);
925                 break;
926
927         case LFUN_INSET_TOGGLE:
928                 bv->beforeChange(this);
929                 update();
930                 toggleInset();
931                 update();
932                 bv->switchKeyMap();
933                 break;
934
935         case LFUN_SPACE_INSERT:
936                 if (cursor.par()->layout()->free_spacing) {
937                         insertChar(' ');
938                         update();
939                 } else {
940                         doInsertInset(this, cmd, false, false);
941                 }
942                 moveCursorUpdate(bv, false);
943                 break;
944
945         case LFUN_HYPHENATION:
946                 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
947                 break;
948
949         case LFUN_LIGATURE_BREAK:
950                 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
951                 break;
952
953         case LFUN_LDOTS:
954                 specialChar(this, bv, InsetSpecialChar::LDOTS);
955                 break;
956
957         case LFUN_END_OF_SENTENCE:
958                 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
959                 break;
960
961         case LFUN_MENU_SEPARATOR:
962                 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
963                 break;
964
965         case LFUN_MARK_OFF:
966                 bv->beforeChange(this);
967                 update();
968                 selection.cursor = cursor;
969                 cmd.message(N_("Mark off"));
970                 break;
971
972         case LFUN_MARK_ON:
973                 bv->beforeChange(this);
974                 selection.mark(true);
975                 update();
976                 selection.cursor = cursor;
977                 cmd.message(N_("Mark on"));
978                 break;
979
980         case LFUN_SETMARK:
981                 bv->beforeChange(this);
982                 if (selection.mark()) {
983                         update();
984                         cmd.message(N_("Mark removed"));
985                 } else {
986                         selection.mark(true);
987                         update();
988                         cmd.message(N_("Mark set"));
989                 }
990                 selection.cursor = cursor;
991                 break;
992
993         case LFUN_UPCASE_WORD:
994                 update();
995                 changeCase(LyXText::text_uppercase);
996                 if (inset_owner)
997                         bv->updateInset(inset_owner);
998                 update();
999                 break;
1000
1001         case LFUN_LOWCASE_WORD:
1002                 update();
1003                 changeCase(LyXText::text_lowercase);
1004                 if (inset_owner)
1005                         bv->updateInset(inset_owner);
1006                 update();
1007                 break;
1008
1009         case LFUN_CAPITALIZE_WORD:
1010                 update();
1011                 changeCase(LyXText::text_capitalization);
1012                 if (inset_owner)
1013                         bv->updateInset(inset_owner);
1014                 update();
1015                 break;
1016
1017         case LFUN_TRANSPOSE_CHARS:
1018                 update();
1019                 transposeChars(*this, cursor);
1020                 if (inset_owner)
1021                         bv->updateInset(inset_owner);
1022                 update();
1023                 break;
1024
1025         case LFUN_PASTE: {
1026                 cmd.message(_("Paste"));
1027                 // clear the selection
1028                 bv->toggleSelection();
1029                 clearSelection();
1030                 update();
1031                 size_t sel_index = 0;
1032                 string const & arg = cmd.argument;
1033                 if (isStrUnsignedInt(arg)) {
1034                         size_t const paste_arg = strToUnsignedInt(arg);
1035 #warning FIXME Check if the arg is in the domain of available selections.
1036                         sel_index = paste_arg;
1037                 }
1038                 pasteSelection(sel_index);
1039                 clearSelection(); // bug 393
1040                 update();
1041                 bv->switchKeyMap();
1042                 break;
1043         }
1044
1045         case LFUN_CUT:
1046                 update();
1047                 cutSelection(true, true);
1048                 update();
1049                 cmd.message(_("Cut"));
1050                 break;
1051
1052         case LFUN_COPY:
1053                 copySelection();
1054                 cmd.message(_("Copy"));
1055                 break;
1056
1057         case LFUN_BEGINNINGBUFSEL:
1058                 if (inset_owner)
1059                         return UNDISPATCHED;
1060                 update();
1061                 cursorTop();
1062                 finishChange(bv, true);
1063                 break;
1064
1065         case LFUN_ENDBUFSEL:
1066                 if (inset_owner)
1067                         return UNDISPATCHED;
1068                 update();
1069                 cursorBottom();
1070                 finishChange(bv, true);
1071                 break;
1072
1073         case LFUN_GETXY:
1074                 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
1075                 break;
1076
1077         case LFUN_SETXY: {
1078                 int x = 0;
1079                 int y = 0;
1080                 istringstream is(STRCONV(cmd.argument));
1081                 is >> x >> y;
1082                 if (!is)
1083                         lyxerr << "SETXY: Could not parse coordinates in '"
1084                                << cmd.argument << std::endl;
1085                 else
1086                         setCursorFromCoordinates(x, y);
1087                 break;
1088         }
1089
1090         case LFUN_GETFONT:
1091                 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1092                         cmd.message("E");
1093                 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1094                         cmd.message("N");
1095                 else
1096                         cmd.message("0");
1097                 break;
1098
1099         case LFUN_GETLAYOUT:
1100                 cmd.message(tostr(cursor.par()->layout()));
1101                 break;
1102
1103         case LFUN_LAYOUT: {
1104                 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1105                   << cmd.argument << endl;
1106
1107                 // This is not the good solution to the empty argument
1108                 // problem, but it will hopefully suffice for 1.2.0.
1109                 // The correct solution would be to augument the
1110                 // function list/array with information about what
1111                 // functions needs arguments and their type.
1112                 if (cmd.argument.empty()) {
1113                         cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1114                         break;
1115                 }
1116
1117                 // Derive layout number from given argument (string)
1118                 // and current buffer's textclass (number)
1119                 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
1120                 bool hasLayout = tclass.hasLayout(cmd.argument);
1121                 string layout = cmd.argument;
1122
1123                 // If the entry is obsolete, use the new one instead.
1124                 if (hasLayout) {
1125                         string const & obs = tclass[layout]->obsoleted_by();
1126                         if (!obs.empty())
1127                                 layout = obs;
1128                 }
1129
1130                 if (!hasLayout) {
1131                         cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1132                                 N_(" not known"));
1133                         break;
1134                 }
1135
1136                 bool change_layout = (current_layout != layout);
1137
1138                 if (!change_layout && selection.set() &&
1139                         selection.start.par() != selection.end.par())
1140                 {
1141                         ParagraphList::iterator spit = selection.start.par();
1142                         ParagraphList::iterator epit = boost::next(selection.end.par());
1143                         while (spit != epit) {
1144                                 if (spit->layout()->name() != current_layout) {
1145                                         change_layout = true;
1146                                         break;
1147                                 }
1148                                 ++spit;
1149                         }
1150                 }
1151
1152                 if (change_layout) {
1153                         current_layout = layout;
1154                         update();
1155                         setLayout(layout);
1156                         bv->owner()->setLayout(layout);
1157                         update();
1158                         bv->switchKeyMap();
1159                 }
1160                 break;
1161         }
1162
1163         case LFUN_PASTESELECTION: {
1164                 if (!bv->buffer())
1165                         break;
1166                 // this was originally a beforeChange(bv->text), i.e
1167                 // the outermost LyXText!
1168                 bv->beforeChange(this);
1169                 string const clip = bv->getClipboard();
1170                 if (!clip.empty()) {
1171                         if (cmd.argument == "paragraph")
1172                                 insertStringAsParagraphs(clip);
1173                         else
1174                                 insertStringAsLines(clip);
1175                         clearSelection();
1176                         update();
1177                 }
1178                 break;
1179         }
1180
1181         case LFUN_GOTOERROR:
1182                 gotoInset(Inset::ERROR_CODE, false);
1183                 break;
1184
1185         case LFUN_GOTONOTE:
1186                 gotoInset(Inset::NOTE_CODE, false);
1187                 break;
1188
1189         case LFUN_REFERENCE_GOTO:
1190         {
1191                 vector<Inset::Code> tmp;
1192                 tmp.push_back(Inset::LABEL_CODE);
1193                 tmp.push_back(Inset::REF_CODE);
1194                 gotoInset(tmp, true);
1195                 break;
1196         }
1197
1198         case LFUN_QUOTE: {
1199                 ParagraphList::iterator pit = cursor.par();
1200                 lyx::pos_type pos = cursor.pos();
1201                 char c;
1202                 if (!pos)
1203                         c = ' ';
1204                 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1205                         c = ' ';
1206                 else
1207                         c = pit->getChar(pos - 1);
1208
1209                 LyXLayout_ptr const & style = pit->layout();
1210
1211                 if (style->pass_thru ||
1212                                 pit->getFontSettings(bv->buffer()->params,
1213                                          pos).language()->lang() == "hebrew" ||
1214                         (!bv->insertInset(new InsetQuotes(c, bv->buffer()->params))))
1215                         bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1216                 break;
1217         }
1218
1219         case LFUN_DATE_INSERT: {
1220                 time_t now_time_t = time(NULL);
1221                 struct tm * now_tm = localtime(&now_time_t);
1222                 setlocale(LC_TIME, "");
1223                 string arg;
1224                 if (!cmd.argument.empty())
1225                         arg = cmd.argument;
1226                 else
1227                         arg = lyxrc.date_insert_format;
1228                 char datetmp[32];
1229                 int const datetmp_len =
1230                         ::strftime(datetmp, 32, arg.c_str(), now_tm);
1231
1232                 for (int i = 0; i < datetmp_len; i++) {
1233                         insertChar(datetmp[i]);
1234                         update();
1235                 }
1236                 selection.cursor = cursor;
1237                 moveCursorUpdate(bv, false);
1238                 break;
1239         }
1240
1241         case LFUN_MOUSE_TRIPLE:
1242                 if (!bv->buffer())
1243                         break;
1244                 if (!isInInset() && bv->theLockingInset())
1245                         break;
1246                 if (cmd.button() == mouse_button::button1) {
1247                         if (!isInInset()) {
1248                                 bv->screen().toggleSelection(this, bv);
1249                         }
1250                         cursorHome();
1251                         selection.cursor = cursor;
1252                         cursorEnd();
1253                         setSelection();
1254                         if (!isInInset())
1255                                 bv->screen().toggleSelection(this, bv, false);
1256                         update();
1257                         bv->haveSelection(selection.set());
1258                 }
1259                 break;
1260
1261         case LFUN_MOUSE_DOUBLE:
1262                 if (!bv->buffer())
1263                         break;
1264                 if (!isInInset() && bv->theLockingInset())
1265                         break;
1266                 if (cmd.button() == mouse_button::button1) {
1267                         if (!isInInset()) {
1268                                 bv->screen().toggleSelection(this, bv);
1269                                 selectWord(LyXText::WHOLE_WORD_STRICT);
1270                                 bv->screen().toggleSelection(this, bv, false);
1271                         } else {
1272                                 selectWord(LyXText::WHOLE_WORD_STRICT);
1273                         }
1274                         update();
1275                         bv->haveSelection(selection.set());
1276                 }
1277                 break;
1278
1279         case LFUN_MOUSE_MOTION:
1280         {
1281                 // Only use motion with button 1
1282                 //if (ev.button() != mouse_button::button1)
1283                 //      return false;
1284
1285                 if (!bv->buffer())
1286                         break;
1287
1288                 // Check for inset locking
1289                 if (bv->theLockingInset()) {
1290                         Inset * tli = bv->theLockingInset();
1291                         LyXCursor cursor = bv->text->cursor;
1292                         LyXFont font = bv->text->getFont(bv->buffer(),
1293                                                          cursor.par(), cursor.pos());
1294                         int width = tli->width(bv, font);
1295                         int inset_x = font.isVisibleRightToLeft()
1296                                 ? cursor.ix() - width : cursor.ix();
1297                         int start_x = inset_x + tli->scroll();
1298                         FuncRequest cmd1 = cmd;
1299                         cmd1.x = cmd.x - start_x;
1300                         cmd1.y = cmd.y - cursor.iy() + bv->text->top_y();
1301                         tli->localDispatch(cmd1);
1302                         break;
1303                 }
1304
1305                 // The test for not selection possible is needed, that only motion
1306                 // events are used, where the bottom press event was on
1307                 //  the drawing area too
1308                 if (!selection_possible) {
1309                         lyxerr[Debug::ACTION]
1310                                 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1311                         break;
1312                 }
1313
1314                 RowList::iterator cursorrow = bv->text->cursorRow();
1315                 bv->text->setCursorFromCoordinates(cmd.x, cmd.y + bv->text->top_y());
1316         #if 0
1317                 // sorry for this but I have a strange error that the y value jumps at
1318                 // a certain point. This seems like an error in my xforms library or
1319                 // in some other local environment, but I would like to leave this here
1320                 // for the moment until I can remove this (Jug 20020418)
1321                 if (y_before < bv->text->cursor.y())
1322                         lyxerr << y_before << ':'
1323                                << bv->text->cursor.y() << endl;
1324         #endif
1325                 // This is to allow jumping over large insets
1326                 if (cursorrow == bv->text->cursorRow()) {
1327                         if (cmd.y >= bv->workHeight())
1328                                 bv->text->cursorDown(false);
1329                         else if (cmd.y < 0)
1330                                 bv->text->cursorUp(false);
1331                 }
1332
1333                 // Maybe an empty line was deleted
1334                 if (!bv->text->selection.set())
1335                         bv->update(BufferView::UPDATE);
1336                 bv->text->setSelection();
1337                 bv->screen().toggleToggle(bv->text, bv);
1338                 bv->fitCursor();
1339                 break;
1340         }
1341
1342         // Single-click on work area
1343         case LFUN_MOUSE_PRESS:
1344         {
1345                 if (!bv->buffer())
1346                         break;
1347
1348                 // ok ok, this is a hack (for xforms)
1349                 // We shouldn't go further down as we really should only do the
1350                 // scrolling and be done with this. Otherwise we may open some
1351                 // dialogs (Jug 20020424).
1352                 if (cmd.button() == mouse_button::button4) {
1353                         bv->scroll(-lyxrc.wheel_jump);
1354                         break;
1355                 }
1356                 if (cmd.button() == mouse_button::button5) {
1357                         bv->scroll(lyxrc.wheel_jump);
1358                         break;
1359                 }
1360
1361                 int x = cmd.x;
1362                 int y = cmd.y;
1363                 Inset * inset_hit = bv->text->checkInsetHit(x, y);
1364
1365                 // Middle button press pastes if we have a selection
1366                 // We do this here as if the selection was inside an inset
1367                 // it could get cleared on the unlocking of the inset so
1368                 // we have to check this first
1369                 bool paste_internally = false;
1370                 if (cmd.button() == mouse_button::button2 && selection.set()) {
1371                         bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1372                         paste_internally = true;
1373                 }
1374
1375                 int const screen_first = bv->text->top_y();
1376
1377                 if (bv->theLockingInset()) {
1378                         // We are in inset locking mode
1379
1380                         // Check whether the inset was hit. If not reset mode,
1381                         // otherwise give the event to the inset
1382                         if (inset_hit == bv->theLockingInset()) {
1383                                 FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1384                                 bv->theLockingInset()->localDispatch(cmd1);
1385                                 break;
1386                         }
1387                         bv->unlockInset(bv->theLockingInset());
1388                 }
1389
1390                 if (!inset_hit)
1391                         selection_possible = true;
1392
1393                 // Clear the selection
1394                 bv->screen().toggleSelection(bv->text, bv);
1395                 bv->text->clearSelection();
1396                 bv->text->partialRebreak();
1397                 bv->update();
1398                 bv->updateScrollbar();
1399
1400                 // Single left click in math inset?
1401                 if (isHighlyEditableInset(inset_hit)) {
1402                         // Highly editable inset, like math
1403                         UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1404                         selection_possible = false;
1405                         bv->owner()->message(inset->editMessage());
1406                         // We just have to lock the inset before calling a PressEvent on it!
1407                         if (!bv->lockInset(inset))
1408                                 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1409                         FuncRequest cmd1(bv, LFUN_MOUSE_PRESS, x, y, cmd.button());
1410                         inset->localDispatch(cmd1);
1411                         break;
1412                 }
1413                 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1414
1415                 // Right click on a footnote flag opens float menu
1416                 if (cmd.button() == mouse_button::button3) {
1417                         selection_possible = false;
1418                         break;
1419                 }
1420
1421                 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1422                         bv->text->setCursorFromCoordinates(x, y + screen_first);
1423                 finishUndo();
1424                 bv->text->selection.cursor = bv->text->cursor;
1425                 bv->text->cursor.x_fix(bv->text->cursor.x());
1426
1427                 if (bv->fitCursor())
1428                         selection_possible = false;
1429
1430                 // Insert primary selection with middle mouse
1431                 // if there is a local selection in the current buffer,
1432                 // insert this
1433                 if (cmd.button() == mouse_button::button2) {
1434                         if (paste_internally)
1435                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1436                         else
1437                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1438                         selection_possible = false;
1439                 }
1440                 break;
1441         }
1442
1443         case LFUN_MOUSE_RELEASE:
1444         {
1445                 // do nothing if we used the mouse wheel
1446                 if (!bv->buffer())
1447                         break;
1448
1449                 if (cmd.button() == mouse_button::button4
1450                  || cmd.button() == mouse_button::button5)
1451                         break;
1452
1453                 // If we hit an inset, we have the inset coordinates in these
1454                 // and inset_hit points to the inset.  If we do not hit an
1455                 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1456                 int x = cmd.x;
1457                 int y = cmd.y;
1458                 Inset * inset_hit = bv->text->checkInsetHit(x, y);
1459
1460                 if (bv->theLockingInset()) {
1461                         // We are in inset locking mode.
1462
1463                         // LyX does a kind of work-area grabbing for insets.
1464                         // Only a ButtonPress FuncRequest outside the inset will
1465                         // force a insetUnlock.
1466                         FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1467                         bv->theLockingInset()->localDispatch(cmd1);
1468                         break;
1469                 }
1470
1471                 selection_possible = false;
1472
1473                 if (cmd.button() == mouse_button::button2)
1474                         break;
1475
1476                 // finish selection
1477                 if (cmd.button() == mouse_button::button1)
1478                         bv->haveSelection(selection.set());
1479
1480                 bv->switchKeyMap();
1481                 bv->owner()->view_state_changed();
1482                 bv->owner()->updateMenubar();
1483                 bv->owner()->updateToolbar();
1484
1485                 // Did we hit an editable inset?
1486                 if (inset_hit) {
1487                         selection_possible = false;
1488
1489                         // if we reach this point with a selection, it
1490                         // must mean we are currently selecting.
1491                         // But we don't want to open the inset
1492                         // because that is annoying for the user.
1493                         // So just pretend we didn't hit it.
1494                         // this is OK because a "kosher" ButtonRelease
1495                         // will follow a ButtonPress that clears
1496                         // the selection.
1497                         // Note this also fixes selection drawing
1498                         // problems if we end up opening an inset
1499                         if (selection.set())
1500                                 break;
1501
1502                         // CHECK fix this proper in 0.13
1503                         // well, maybe 13.0 !!!!!!!!!
1504
1505                         // Following a ref shouldn't issue
1506                         // a push on the undo-stack
1507                         // anylonger, now that we have
1508                         // keybindings for following
1509                         // references and returning from
1510                         // references.  IMHO though, it
1511                         // should be the inset's own business
1512                         // to push or not push on the undo
1513                         // stack. They don't *have* to
1514                         // alter the document...
1515                         // (Joacim)
1516                         // ...or maybe the SetCursorParUndo()
1517                         // below isn't necessary at all anylonger?
1518                         if (inset_hit->lyxCode() == Inset::REF_CODE)
1519                                 setCursorParUndo(bv);
1520
1521                         bv->owner()->message(inset_hit->editMessage());
1522
1523                         FuncRequest cmd1(bv, LFUN_MOUSE_RELEASE, x, y, cmd.button());
1524                         inset_hit->localDispatch(cmd1);
1525                 }
1526
1527                 break;
1528         }
1529
1530         case LFUN_SELFINSERT: {
1531                 if (cmd.argument.empty())
1532                         break;
1533
1534                 // Automatically delete the currently selected
1535                 // text and replace it with what is being
1536                 // typed in now. Depends on lyxrc settings
1537                 // "auto_region_delete", which defaults to
1538                 // true (on).
1539
1540                 if (lyxrc.auto_region_delete) {
1541                         if (selection.set()) {
1542                                 cutSelection(false, false);
1543                                 update();
1544                         }
1545                         bv->haveSelection(false);
1546                 }
1547
1548                 bv->beforeChange(this);
1549                 LyXFont const old_font(real_current_font);
1550
1551                 string::const_iterator cit = cmd.argument.begin();
1552                 string::const_iterator end = cmd.argument.end();
1553                 for (; cit != end; ++cit)
1554                         bv->owner()->getIntl().getTransManager().
1555                                 TranslateAndInsert(*cit, this);
1556
1557                 update();
1558                 selection.cursor = cursor;
1559                 moveCursorUpdate(bv, false);
1560
1561                 // real_current_font.number can change so we need to
1562                 // update the minibuffer
1563                 if (old_font != real_current_font)
1564                         bv->owner()->view_state_changed();
1565                 break;
1566         }
1567
1568         case LFUN_HTMLURL: {
1569                 InsetCommandParams p("htmlurl");
1570                 string const data = InsetCommandMailer::params2string("url", p);
1571                 bv->owner()->getDialogs().show("url", data, 0);
1572                 break;
1573         }
1574
1575         case LFUN_URL: {
1576                 InsetCommandParams p("url");
1577                 string const data = InsetCommandMailer::params2string("url", p);
1578                 bv->owner()->getDialogs().show("url", data, 0);
1579                 break;
1580         }
1581
1582
1583 #if 0
1584         case LFUN_INSET_LIST:
1585         case LFUN_INSET_THEOREM:
1586         case LFUN_INSET_CAPTION:
1587 #endif
1588         case LFUN_INSERT_NOTE:
1589         case LFUN_INSERT_BIBITEM:
1590         case LFUN_INSET_ERT:
1591         case LFUN_INSET_FLOAT:
1592         case LFUN_INSET_FOOTNOTE:
1593         case LFUN_INSET_MARGINAL:
1594         case LFUN_INSET_MINIPAGE:
1595         case LFUN_INSET_OPTARG:
1596         case LFUN_INSET_WIDE_FLOAT:
1597         case LFUN_INSET_WRAP:
1598         case LFUN_TABULAR_INSERT:
1599         case LFUN_ENVIRONMENT_INSERT:
1600                 // Open the inset, and move the current selection
1601                 // inside it.
1602                 doInsertInset(this, cmd, true, true);
1603                 break;
1604
1605         case LFUN_INDEX_INSERT:
1606                 // Just open the inset
1607                 doInsertInset(this, cmd, true, false);
1608                 break;
1609
1610         case LFUN_INDEX_PRINT:
1611         case LFUN_TOC_INSERT:
1612         case LFUN_HFILL:
1613                 // do nothing fancy
1614                 doInsertInset(this, cmd, false, false);
1615                 break;
1616
1617         default:
1618                 return UNDISPATCHED;
1619         }
1620
1621         return DISPATCHED;
1622 }