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