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