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