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