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