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