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