]> git.lyx.org Git - lyx.git/blob - src/text3.C
zlib stuff
[lyx.git] / src / text3.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2002 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #include "lyxtext.h"
14 #include "lyxrow.h"
15 #include "paragraph.h"
16 #include "BufferView.h"
17 #include "funcrequest.h"
18 #include "lyxrc.h"
19 #include "Lsstream.h"
20 #include "debug.h"
21 #include "bufferparams.h"
22 #include "buffer.h"
23 #include "bufferview_funcs.h"
24 #include "ParagraphParameters.h"
25 #include "gettext.h"
26 #include "factory.h"
27 #include "intl.h"
28 #include "box.h"
29 #include "language.h"
30 #include "support/tostr.h"
31 #include "support/lstrings.h"
32 #include "support/LAssert.h"
33 #include "frontends/LyXView.h"
34 #include "frontends/screen.h"
35 #include "frontends/Dialogs.h"
36 #include "insets/insetspecialchar.h"
37 #include "insets/insettext.h"
38 #include "insets/insetquotes.h"
39 #include "insets/insetcommand.h"
40 #include "insets/insetnewline.h"
41 #include "undo_funcs.h"
42 #include "text_funcs.h"
43 #include "Lsstream.h"
44
45 #include <ctime>
46 #include <clocale>
47
48 using namespace lyx::support;
49 using namespace bv_funcs;
50
51 using std::endl;
52 using std::find;
53 using std::vector;
54 using lyx::pos_type;
55
56 extern string current_layout;
57 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
58
59 // the selection possible is needed, that only motion events are
60 // used, where the bottom press event was on the drawing area too
61 bool selection_possible = false;
62
63
64 namespace {
65
66         void moveCursorUpdate(BufferView * bv, bool selecting)
67         {
68                 LyXText * lt = bv->getLyXText();
69
70                 if (selecting || lt->selection.mark()) {
71                         lt->setSelection();
72                         if (lt->isInInset())
73                                 bv->updateInset(lt->inset_owner);
74                         else
75                                 bv->toggleToggle();
76                 }
77                 if (!lt->isInInset()) {
78                         bv->update(lt, BufferView::SELECT);
79                 } else if (bv->text->needRefresh()) {
80                         bv->update(BufferView::SELECT);
81                 }
82
83                 if (!lt->selection.set())
84                         bv->haveSelection(false);
85
86                 bv->switchKeyMap();
87         }
88
89
90         void finishChange(BufferView * bv, bool selecting = false)
91         {
92                 finishUndo();
93                 moveCursorUpdate(bv, selecting);
94                 bv->owner()->view_state_changed();
95         }
96
97         // check if the given co-ordinates are inside an inset at the
98         // given cursor, if one exists. If so, the inset is returned,
99         // and the co-ordinates are made relative. Otherwise, 0 is returned.
100         InsetOld * checkInset(BufferView * bv, LyXText & text,
101                 LyXCursor const & cur, int & x, int & y)
102         {
103                 lyx::pos_type const pos = cur.pos();
104                 ParagraphList::iterator par = cur.par();
105
106                 if (pos >= par->size() || !par->isInset(pos))
107                         return 0;
108
109                 InsetOld /*const*/ * inset = par->getInset(pos);
110
111                 if (!isEditableInset(inset))
112                         return 0;
113
114                 // get inset dimensions
115                 Assert(par->getInset(pos));
116
117                 LyXFont const & font = text.getFont(par, pos);
118
119                 int const width = inset->width();
120                 int const inset_x = font.isVisibleRightToLeft()
121                         ? (cur.ix() - width) : cur.ix();
122
123                 Box b(
124                         inset_x + inset->scroll(),
125                         inset_x + width,
126                         cur.iy() - inset->ascent(),
127                         cur.iy() + inset->descent()
128                 );
129
130                 if (!b.contained(x, y)) {
131                         lyxerr[Debug::GUI] << "Missed inset at x,y "
132                                            << x << ',' << y
133                                            << " box " << b << endl;
134                         return 0;
135                 }
136
137                 text.setCursor(par, pos, true);
138
139                 x -= b.x1;
140                 // The origin of an inset is on the baseline
141                 y -= text.cursor.iy();
142
143                 return inset;
144         }
145
146 } // anon namespace
147
148
149 InsetOld * LyXText::checkInsetHit(int & x, int & y)
150 {
151         int y_tmp = y + top_y();
152
153         LyXCursor cur;
154         setCursorFromCoordinates(cur, x, y_tmp);
155
156         InsetOld * inset = checkInset(bv(), *this, cur, x, y_tmp);
157         if (inset) {
158                 y = y_tmp;
159                 return inset;
160         }
161
162         // look at previous position
163         if (cur.pos() == 0)
164                 return 0;
165
166         // move back one
167         setCursor(cur, cur.par(), cur.pos() - 1, true);
168
169         inset = checkInset(bv(), *this, cur, x, y_tmp);
170         if (inset)
171                 y = y_tmp;
172         return inset;
173 }
174
175
176 bool LyXText::gotoNextInset(vector<InsetOld::Code> const & codes,
177                             string const & contents)
178 {
179         ParagraphList::iterator end = ownerParagraphs().end();
180         ParagraphList::iterator pit = cursor.par();
181         pos_type pos = cursor.pos();
182
183         InsetOld * inset;
184         do {
185                 if (pos + 1 < pit->size()) {
186                         ++pos;
187                 } else  {
188                         ++pit;
189                         pos = 0;
190                 }
191
192         } while (pit != end &&
193                  !(pit->isInset(pos) &&
194                    (inset = pit->getInset(pos)) != 0 &&
195                    find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
196                    (contents.empty() ||
197                     static_cast<InsetCommand *>(pit->getInset(pos))->getContents()
198                     == contents)));
199
200         if (pit != end) {
201                 setCursor(pit, pos, false);
202                 return true;
203         }
204         return false;
205 }
206
207
208 void LyXText::gotoInset(vector<InsetOld::Code> const & codes,
209                         bool same_content)
210 {
211         bv()->beforeChange(this);
212         update();
213
214         string contents;
215         if (same_content && cursor.pos() < cursor.par()->size()
216             && cursor.par()->isInset(cursor.pos())) {
217                 InsetOld const * inset = cursor.par()->getInset(cursor.pos());
218                 if (find(codes.begin(), codes.end(), inset->lyxCode())
219                     != codes.end())
220                         contents = static_cast<InsetCommand const *>(inset)->getContents();
221         }
222
223         if (!gotoNextInset(codes, contents)) {
224                 if (cursor.pos() || cursor.par() != ownerParagraphs().begin()) {
225                         LyXCursor tmp = cursor;
226                         cursor.par(ownerParagraphs().begin());
227                         cursor.pos(0);
228                         if (!gotoNextInset(codes, contents)) {
229                                 cursor = tmp;
230                                 bv()->owner()->message(_("No more insets"));
231                         }
232                 } else {
233                         bv()->owner()->message(_("No more insets"));
234                 }
235         }
236         update();
237         selection.cursor = cursor;
238 }
239
240
241 void LyXText::gotoInset(InsetOld::Code code, bool same_content)
242 {
243         gotoInset(vector<InsetOld::Code>(1, code), same_content);
244 }
245
246
247 void LyXText::cursorPrevious()
248 {
249         int y = top_y();
250
251         if (cursorRow() == rows().begin()) {
252                 if (y > 0)
253                         bv()->updateScrollbar();
254                 return;
255         }
256
257         RowList::iterator cursorrow = cursorRow();
258
259         setCursorFromCoordinates(cursor.x_fix(), y);
260         finishUndo();
261
262         int new_y;
263         if (cursorrow == bv()->text->cursorRow()) {
264                 // we have a row which is taller than the workarea. The
265                 // simplest solution is to move to the previous row instead.
266                 cursorUp(true);
267                 return;
268                 // This is what we used to do, so we wouldn't skip right past
269                 // tall rows, but it's not working right now.
270         } else {
271                 if (inset_owner) {
272                         new_y = bv()->text->cursor.iy()
273                                 + bv()->theLockingInset()->insetInInsetY() + y
274                                 + cursorRow()->height()
275                                 - bv()->workHeight() + 1;
276                 } else {
277                         new_y = cursor.y()
278                                 - cursorRow()->baseline()
279                                 + cursorRow()->height()
280                                 - bv()->workHeight() + 1;
281                 }
282         }
283         //bv()->screen().draw(bv()->text, bv(), new_y < 0 ? 0 : new_y);
284         if (cursorRow() != rows().begin()) {
285                 LyXCursor cur;
286                 setCursor(cur, boost::prior(cursorRow())->par(),
287                           boost::prior(cursorRow())->pos(), false);
288                 if (cur.y() > top_y()) {
289                         cursorUp(true);
290                 }
291         }
292         bv()->updateScrollbar();
293 }
294
295
296 void LyXText::cursorNext()
297 {
298         int topy = top_y();
299
300         if (boost::next(cursorRow()) == rows().end()) {
301                 int y = cursor.y() - cursorRow()->baseline() +
302                         cursorRow()->height();
303                 if (y > topy + bv()->workHeight()) {
304                         //bv()->screen().draw(bv()->text, bv(), bv()->text->top_y() + bv()->workHeight());
305                         bv()->updateScrollbar();
306                 }
307                 return;
308         }
309
310         int y = topy + bv()->workHeight();
311         if (inset_owner && !topy) {
312                 y -= (bv()->text->cursor.iy()
313                           - bv()->text->top_y()
314                           + bv()->theLockingInset()->insetInInsetY());
315         }
316
317         getRowNearY(y);
318
319         RowList::iterator cursorrow = cursorRow();
320         setCursorFromCoordinates(cursor.x_fix(), y);
321         // + bv->workHeight());
322         finishUndo();
323
324         int new_y;
325         if (cursorrow == bv()->text->cursorRow()) {
326                 // we have a row which is taller than the workarea. The
327                 // simplest solution is to move to the next row instead.
328                 cursorDown(true);
329                 return;
330                 // This is what we used to do, so we wouldn't skip right past
331                 // tall rows, but it's not working right now.
332 #if 0
333                 new_y = bv->text->top_y() + bv->workHeight();
334 #endif
335         } else {
336                 if (inset_owner) {
337                         new_y = bv()->text->cursor.iy()
338                                 + bv()->theLockingInset()->insetInInsetY()
339                                 + y - cursorRow()->baseline();
340                 } else {
341                         new_y =  cursor.y() - cursorRow()->baseline();
342                 }
343         }
344         //bv()->screen().draw(bv()->text, bv(), new_y);
345
346         RowList::iterator next_row = boost::next(cursorRow());
347         if (next_row != rows().end()) {
348                 LyXCursor cur;
349                 setCursor(cur, next_row->par(), next_row->pos(), false);
350                 if (cur.y() < top_y() + bv()->workHeight()) {
351                         cursorDown(true);
352                 }
353         }
354         bv()->updateScrollbar();
355 }
356
357
358 void LyXText::update()
359 {
360         bv()->update(this, BufferView::SELECT);
361 }
362
363 namespace {
364
365 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
366 {
367         lt->update();
368         InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
369         replaceSelection(lt);
370         if (!bv->insertInset(new_inset))
371                 delete new_inset;
372         else
373                 bv->updateInset(new_inset);
374 }
375
376
377 void doInsertInset(LyXText * lt, FuncRequest const & cmd,
378                    bool edit, bool pastesel)
379 {
380         InsetOld * inset = createInset(cmd);
381         BufferView * bv = cmd.view();
382
383         if (inset) {
384                 bool gotsel = false;
385                 if (lt->selection.set()) {
386                         bv->owner()->dispatch(FuncRequest(LFUN_CUT));
387                         gotsel = true;
388                 }
389                 if (bv->insertInset(inset)) {
390                         if (edit) {
391                                 FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
392                                 inset->localDispatch(cmd);
393                         }
394                         if (gotsel && pastesel)
395                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
396                 }
397                 else
398                         delete inset;
399         }
400 }
401
402 }
403
404 InsetOld::RESULT LyXText::dispatch(FuncRequest const & cmd)
405 {
406         lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
407                               <<"] arg[" << cmd.argument << ']' << endl;
408
409         BufferView * bv = cmd.view();
410
411         switch (cmd.action) {
412
413         case LFUN_APPENDIX: {
414                 ParagraphList::iterator pit = cursor.par();
415                 bool start = !pit->params().startOfAppendix();
416
417                 // ensure that we have only one start_of_appendix in this document
418                 ParagraphList::iterator tmp = ownerParagraphs().begin();
419                 ParagraphList::iterator end = ownerParagraphs().end();
420
421                 for (; tmp != end; ++tmp) {
422                         if (tmp->params().startOfAppendix()) {
423                                 recordUndo(bv, Undo::ATOMIC, tmp);
424                                 tmp->params().startOfAppendix(false);
425                                 int tmpy;
426                                 setHeightOfRow(getRow(tmp, 0, tmpy));
427                                 break;
428                         }
429                 }
430
431                 recordUndo(bv, Undo::ATOMIC, pit);
432                 pit->params().startOfAppendix(start);
433
434                 // we can set the refreshing parameters now
435                 updateCounters();
436                 redoHeightOfParagraph();
437                 postPaint();
438                 setCursor(cursor.par(), cursor.pos());
439                 update();
440                 break;
441         }
442
443         case LFUN_DELETE_WORD_FORWARD:
444                 bv->beforeChange(this);
445                 update();
446                 deleteWordForward();
447                 update();
448                 finishChange(bv);
449                 break;
450
451         case LFUN_DELETE_WORD_BACKWARD:
452                 bv->beforeChange(this);
453                 update();
454                 deleteWordBackward();
455                 update();
456                 finishChange(bv);
457                 break;
458
459         case LFUN_DELETE_LINE_FORWARD:
460                 bv->beforeChange(this);
461                 update();
462                 deleteLineForward();
463                 update();
464                 finishChange(bv);
465                 break;
466
467         case LFUN_WORDRIGHT:
468                 if (!selection.mark())
469                         bv->beforeChange(this);
470                 update();
471                 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
472                         cursorLeftOneWord();
473                 else
474                         cursorRightOneWord();
475                 finishChange(bv);
476                 break;
477
478         case LFUN_WORDLEFT:
479                 if (!selection.mark())
480                         bv->beforeChange(this);
481                 update();
482                 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
483                         cursorRightOneWord();
484                 else
485                         cursorLeftOneWord();
486                 finishChange(bv);
487                 break;
488
489         case LFUN_BEGINNINGBUF:
490                 if (!selection.mark())
491                         bv->beforeChange(this);
492                 update();
493                 cursorTop();
494                 finishChange(bv);
495                 break;
496
497         case LFUN_ENDBUF:
498                 if (selection.mark())
499                         bv->beforeChange(this);
500                 update();
501                 cursorBottom();
502                 finishChange(bv);
503                 break;
504
505         case LFUN_RIGHTSEL:
506                 update();
507                 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
508                         cursorLeft(bv);
509                 else
510                         cursorRight(bv);
511                 finishChange(bv, true);
512                 break;
513
514         case LFUN_LEFTSEL:
515                 update();
516                 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
517                         cursorRight(bv);
518                 else
519                         cursorLeft(bv);
520                 finishChange(bv, true);
521                 break;
522
523         case LFUN_UPSEL:
524                 update();
525                 cursorUp(true);
526                 finishChange(bv, true);
527                 break;
528
529         case LFUN_DOWNSEL:
530                 update();
531                 cursorDown(true);
532                 finishChange(bv, true);
533                 break;
534
535         case LFUN_UP_PARAGRAPHSEL:
536                 update();
537                 cursorUpParagraph();
538                 finishChange(bv, true);
539                 break;
540
541         case LFUN_DOWN_PARAGRAPHSEL:
542                 update();
543                 cursorDownParagraph();
544                 finishChange(bv, true);
545                 break;
546
547         case LFUN_PRIORSEL:
548                 update();
549                 cursorPrevious();
550                 finishChange(bv, true);
551                 break;
552
553         case LFUN_NEXTSEL:
554                 update();
555                 cursorNext();
556                 finishChange(bv, true);
557                 break;
558
559         case LFUN_HOMESEL:
560                 update();
561                 cursorHome();
562                 finishChange(bv, true);
563                 break;
564
565         case LFUN_ENDSEL:
566                 update();
567                 cursorEnd();
568                 finishChange(bv, true);
569                 break;
570
571         case LFUN_WORDRIGHTSEL:
572                 update();
573                 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
574                         cursorLeftOneWord();
575                 else
576                         cursorRightOneWord();
577                 finishChange(bv, true);
578                 break;
579
580         case LFUN_WORDLEFTSEL:
581                 update();
582                 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
583                         cursorRightOneWord();
584                 else
585                         cursorLeftOneWord();
586                 finishChange(bv, true);
587                 break;
588
589         case LFUN_WORDSEL: {
590                 update();
591                 LyXCursor cur1 = cursor;
592                 LyXCursor cur2;
593                 ::getWord(cur1, cur2, lyx::WHOLE_WORD, ownerParagraphs());
594                 setCursor(cur1.par(), cur1.pos());
595                 bv->beforeChange(this);
596                 setCursor(cur2.par(), cur2.pos());
597                 finishChange(bv, true);
598                 break;
599         }
600
601         case LFUN_RIGHT: {
602                 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
603                 if (!selection.mark())
604                         bv->beforeChange(this);
605                 update();
606                 if (is_rtl)
607                         cursorLeft(false);
608                 if (cursor.pos() < cursor.par()->size()
609                     && cursor.par()->isInset(cursor.pos())
610                     && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
611                         InsetOld * tmpinset = cursor.par()->getInset(cursor.pos());
612                         cmd.message(tmpinset->editMessage());
613                         FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "right" : "left");
614                         tmpinset->localDispatch(cmd1);
615                         break;
616                 }
617                 if (!is_rtl)
618                         cursorRight(false);
619                 finishChange(bv);
620                 break;
621         }
622
623         case LFUN_LEFT: {
624                 // This is soooo ugly. Isn`t it possible to make
625                 // it simpler? (Lgb)
626                 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
627                 if (!selection.mark())
628                         bv->beforeChange(this);
629                 update();
630                 LyXCursor const cur = cursor;
631                 if (!is_rtl)
632                         cursorLeft(false);
633                 if ((is_rtl || cur != cursor) && // only if really moved!
634                     cursor.pos() < cursor.par()->size() &&
635                     cursor.par()->isInset(cursor.pos()) &&
636                     isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
637                         InsetOld * tmpinset = cursor.par()->getInset(cursor.pos());
638                         cmd.message(tmpinset->editMessage());
639                         FuncRequest cmd1(bv, LFUN_INSET_EDIT, is_rtl ? "left" : "right");
640                         tmpinset->localDispatch(cmd1);
641                         break;
642                 }
643                 if (is_rtl)
644                         cursorRight(false);
645                 finishChange(bv);
646                 break;
647         }
648
649         case LFUN_UP:
650                 if (!selection.mark())
651                         bv->beforeChange(this);
652                 bv->update(this, BufferView::UPDATE);
653                 cursorUp(false);
654                 finishChange(bv);
655                 break;
656
657         case LFUN_DOWN:
658                 if (!selection.mark())
659                         bv->beforeChange(this);
660                 bv->update(this, BufferView::UPDATE);
661                 cursorDown(false);
662                 finishChange(bv);
663                 break;
664
665         case LFUN_UP_PARAGRAPH:
666                 if (!selection.mark())
667                         bv->beforeChange(this);
668                 bv->update(this, BufferView::UPDATE);
669                 cursorUpParagraph();
670                 finishChange(bv);
671                 break;
672
673         case LFUN_DOWN_PARAGRAPH:
674                 if (!selection.mark())
675                         bv->beforeChange(this);
676                 bv->update(this, BufferView::UPDATE);
677                 cursorDownParagraph();
678                 finishChange(bv, false);
679                 break;
680
681         case LFUN_PRIOR:
682                 if (!selection.mark())
683                         bv->beforeChange(this);
684                 bv->update(this, BufferView::UPDATE);
685                 cursorPrevious();
686                 finishChange(bv, false);
687                 // 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                 replaceSelection(bv->getLyXText());
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                 replaceSelection(bv->getLyXText());
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                 replaceSelection(bv->getLyXText());
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                 replaceSelection(bv->getLyXText());
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                 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                 recordUndo(bv, Undo::ATOMIC, cursor.par());
1020                 if (transposeChars(cursor))
1021                         checkParagraph(cursor.par(), cursor.pos());
1022                 if (inset_owner)
1023                         bv->updateInset(inset_owner);
1024                 update();
1025                 break;
1026
1027         case LFUN_PASTE: {
1028                 cmd.message(_("Paste"));
1029                 replaceSelection(bv->getLyXText());
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(InsetOld::ERROR_CODE, false);
1182                 break;
1183
1184         case LFUN_GOTONOTE:
1185                 gotoInset(InsetOld::NOTE_CODE, false);
1186                 break;
1187
1188         case LFUN_REFERENCE_GOTO:
1189         {
1190                 vector<InsetOld::Code> tmp;
1191                 tmp.push_back(InsetOld::LABEL_CODE);
1192                 tmp.push_back(InsetOld::REF_CODE);
1193                 gotoInset(tmp, true);
1194                 break;
1195         }
1196
1197         case LFUN_QUOTE: {
1198                 replaceSelection(bv->getLyXText());
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                 replaceSelection(bv->getLyXText());
1221                 time_t now_time_t = time(NULL);
1222                 struct tm * now_tm = localtime(&now_time_t);
1223                 setlocale(LC_TIME, "");
1224                 string arg;
1225                 if (!cmd.argument.empty())
1226                         arg = cmd.argument;
1227                 else
1228                         arg = lyxrc.date_insert_format;
1229                 char datetmp[32];
1230                 int const datetmp_len =
1231                         ::strftime(datetmp, 32, arg.c_str(), now_tm);
1232
1233                 for (int i = 0; i < datetmp_len; i++) {
1234                         insertChar(datetmp[i]);
1235                         update();
1236                 }
1237                 selection.cursor = cursor;
1238                 moveCursorUpdate(bv, false);
1239                 break;
1240         }
1241
1242         case LFUN_MOUSE_TRIPLE:
1243                 if (!bv->buffer())
1244                         break;
1245                 if (!isInInset() && bv->theLockingInset())
1246                         break;
1247                 if (cmd.button() == mouse_button::button1) {
1248                         if (!isInInset()) {
1249                                 bv->screen().toggleSelection(this, bv);
1250                         }
1251                         cursorHome();
1252                         selection.cursor = cursor;
1253                         cursorEnd();
1254                         setSelection();
1255                         if (!isInInset())
1256                                 bv->screen().toggleSelection(this, bv, false);
1257                         update();
1258                         bv->haveSelection(selection.set());
1259                 }
1260                 break;
1261
1262         case LFUN_MOUSE_DOUBLE:
1263                 if (!bv->buffer())
1264                         break;
1265                 if (!isInInset() && bv->theLockingInset())
1266                         break;
1267                 if (cmd.button() == mouse_button::button1) {
1268                         if (!isInInset()) {
1269                                 bv->screen().toggleSelection(this, bv);
1270                                 selectWord(lyx::WHOLE_WORD_STRICT);
1271                                 bv->screen().toggleSelection(this, bv, false);
1272                         } else {
1273                                 selectWord(lyx::WHOLE_WORD_STRICT);
1274                         }
1275                         update();
1276                         bv->haveSelection(selection.set());
1277                 }
1278                 break;
1279
1280         case LFUN_MOUSE_MOTION:
1281         {
1282                 // Only use motion with button 1
1283                 //if (ev.button() != mouse_button::button1)
1284                 //      return false;
1285
1286                 if (!bv->buffer())
1287                         break;
1288
1289                 // Check for inset locking
1290                 if (bv->theLockingInset()) {
1291                         InsetOld * tli = bv->theLockingInset();
1292                         LyXCursor cursor = bv->text->cursor;
1293                         LyXFont font = bv->text->getFont(cursor.par(), cursor.pos());
1294                         int width = tli->width();
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                 InsetOld * 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                 InsetOld * 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() == InsetOld::REF_CODE)
1519                                 recordUndo(bv, Undo::ATOMIC);
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 }