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