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