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