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