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