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