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