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