]> git.lyx.org Git - lyx.git/blob - src/BufferView_pimpl.C
layout as layout
[lyx.git] / src / BufferView_pimpl.C
1 /**
2  * \file BufferView_pimpl.C
3  * Copyright 2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author various
7  */
8
9 #include <config.h>
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include "BufferView_pimpl.h"
16 #include "frontends/WorkArea.h"
17 #include "frontends/screen.h"
18 #include "frontends/LyXScreenFactory.h"
19 #include "frontends/WorkAreaFactory.h"
20 #include "frontends/Dialogs.h"
21 #include "frontends/Alert.h"
22 #include "frontends/FileDialog.h"
23 #include "lyxtext.h"
24 #include "lyxrow.h"
25 #include "paragraph.h"
26 #include "frontends/LyXView.h"
27 #include "commandtags.h"
28 #include "lyxfunc.h"
29 #include "debug.h"
30 #include "bufferview_funcs.h"
31 #include "TextCache.h"
32 #include "bufferlist.h"
33 #include "lyxrc.h"
34 #include "intl.h"
35 // added for Dispatch functions
36 #include "lyx_cb.h"
37 #include "lyx_main.h"
38 #include "FloatList.h"
39 #include "gettext.h"
40 #include "ParagraphParameters.h"
41 #include "undo_funcs.h"
42 #include "lyxtextclasslist.h"
43
44 #include "insets/insetbib.h"
45 #include "insets/insettext.h"
46 #include "insets/inseturl.h"
47 #include "insets/insetlatexaccent.h"
48 #include "insets/insettoc.h"
49 #include "insets/insetref.h"
50 #include "insets/insetparent.h"
51 #include "insets/insetindex.h"
52 #include "insets/insetnote.h"
53 #include "insets/insetinclude.h"
54 #include "insets/insetcite.h"
55 #include "insets/insetert.h"
56 #include "insets/insetexternal.h"
57 #include "insets/insetgraphics.h"
58 #include "insets/insetfoot.h"
59 #include "insets/insetmarginal.h"
60 #include "insets/insetminipage.h"
61 #include "insets/insetfloat.h"
62 #include "insets/insettabular.h"
63 #if 0
64 #include "insets/insettheorem.h"
65 #include "insets/insetlist.h"
66 #endif
67 #include "insets/insetcaption.h"
68 #include "insets/insetfloatlist.h"
69 #include "insets/insetspecialchar.h"
70
71 #include "mathed/formulabase.h"
72
73 #include "support/LAssert.h"
74 #include "support/lstrings.h"
75 #include "support/filetools.h"
76 #include "support/lyxfunctional.h"
77
78 #include <boost/bind.hpp>
79
80 #include <cstdio>
81 #include <ctime>
82 #include <unistd.h>
83 #include <sys/wait.h>
84 #include <clocale>
85
86
87 extern string current_layout;
88
89 #ifndef CXX_GLOBAL_CSTD
90 using std::tm;
91 using std::localtime;
92 using std::time;
93 using std::setlocale;
94 using std::strftime;
95 #endif
96
97 using std::vector;
98 using std::find_if;
99 using std::find;
100 using std::pair;
101 using std::endl;
102 using std::make_pair;
103 using std::min;
104
105 using lyx::pos_type;
106 using lyx::textclass_type;
107
108 /* the selection possible is needed, that only motion events are
109  * used, where the bottom press event was on the drawing area too */
110 bool selection_possible = false;
111
112 extern BufferList bufferlist;
113 extern char ascii_type;
114
115 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
116
117
118 namespace {
119
120 const unsigned int saved_positions_num = 20;
121
122
123 } // anon namespace
124
125
126 BufferView::Pimpl::Pimpl(BufferView * b, LyXView * o,
127              int xpos, int ypos, int width, int height)
128         : bv_(b), owner_(o), buffer_(0), cursor_timeout(400),
129           using_xterm_cursor(false)
130 {
131         workarea_.reset(WorkAreaFactory::create(xpos, ypos, width, height));
132         screen_.reset(LyXScreenFactory::create(workarea()));
133
134         // Setup the signals
135         workarea().scrollDocView.connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
136         workarea().workAreaResize
137                 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
138         workarea().workAreaButtonPress
139                 .connect(boost::bind(&BufferView::Pimpl::workAreaButtonPress, this, _1, _2, _3));
140         workarea().workAreaButtonRelease
141                 .connect(boost::bind(&BufferView::Pimpl::workAreaButtonRelease, this, _1, _2, _3));
142         workarea().workAreaMotionNotify
143                 .connect(boost::bind(&BufferView::Pimpl::workAreaMotionNotify, this, _1, _2, _3));
144         workarea().workAreaDoubleClick
145                 .connect(boost::bind(&BufferView::Pimpl::doubleClick, this, _1, _2, _3));
146         workarea().workAreaTripleClick
147                 .connect(boost::bind(&BufferView::Pimpl::tripleClick, this, _1, _2, _3));
148         workarea().workAreaKeyPress
149                 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
150         workarea().selectionRequested
151                 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
152         workarea().selectionLost
153                 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
154
155         cursor_timeout.timeout.connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
156         cursor_timeout.start();
157         saved_positions.resize(saved_positions_num);
158 }
159
160
161 WorkArea & BufferView::Pimpl::workarea() const
162 {
163         return *workarea_.get();
164 }
165
166
167 LyXScreen & BufferView::Pimpl::screen() const
168 {
169         return *screen_.get();
170 }
171
172
173 Painter & BufferView::Pimpl::painter() const
174 {
175         return workarea().getPainter();
176 }
177
178
179 void BufferView::Pimpl::buffer(Buffer * b)
180 {
181         lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
182                             << b << ")" << endl;
183         if (buffer_) {
184                 buffer_->delUser(bv_);
185
186                 // Put the old text into the TextCache, but
187                 // only if the buffer is still loaded.
188                 // Also set the owner of the test to 0
189                 //              bv_->text->owner(0);
190                 textcache.add(buffer_, workarea().workWidth(), bv_->text);
191                 if (lyxerr.debugging())
192                         textcache.show(lyxerr, "BufferView::buffer");
193
194                 bv_->text = 0;
195         }
196
197         // Set current buffer
198         buffer_ = b;
199
200         if (bufferlist.getState() == BufferList::CLOSING) return;
201
202         // If we are closing the buffer, use the first buffer as current
203         if (!buffer_) {
204                 buffer_ = bufferlist.first();
205         }
206
207         if (buffer_) {
208                 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
209                 buffer_->addUser(bv_);
210
211                 // If we don't have a text object for this, we make one
212                 if (bv_->text == 0) {
213                         resizeCurrentBuffer();
214                 }
215
216                 // FIXME: needed when ?
217                 bv_->text->first_y = screen().topCursorVisible(bv_->text->cursor, bv_->text->first_y);
218
219                 // Similarly, buffer-dependent dialogs should be updated or
220                 // hidden. This should go here because some dialogs (eg ToC)
221                 // require bv_->text.
222                 owner_->getDialogs()->updateBufferDependent(true);
223         } else {
224                 lyxerr[Debug::INFO] << "  No Buffer!" << endl;
225                 owner_->getDialogs()->hideBufferDependent();
226
227                 // Also remove all remaining text's from the testcache.
228                 // (there should not be any!) (if there is any it is a
229                 // bug!)
230                 if (lyxerr.debugging())
231                         textcache.show(lyxerr, "buffer delete all");
232                 textcache.clear();
233         }
234
235         repaint();
236         updateScrollbar();
237         owner_->updateMenubar();
238         owner_->updateToolbar();
239         owner_->updateLayoutChoice();
240         owner_->updateWindowTitle();
241 }
242
243
244 bool BufferView::Pimpl::fitCursor()
245 {
246         bool ret;
247
248         if (bv_->theLockingInset()) {
249                 bv_->theLockingInset()->fitInsetCursor(bv_);
250                 ret = true;
251         } else {
252                 ret = screen().fitCursor(bv_->text, bv_);
253         }
254
255         bv_->owner()->getDialogs()->updateParagraph();
256         if (ret)
257             updateScrollbar();
258         return ret;
259 }
260
261
262 void BufferView::Pimpl::redoCurrentBuffer()
263 {
264         lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
265         if (buffer_ && bv_->text) {
266                 resizeCurrentBuffer();
267                 owner_->updateLayoutChoice();
268         }
269 }
270
271
272 int BufferView::Pimpl::resizeCurrentBuffer()
273 {
274         lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
275
276         Paragraph * par = 0;
277         Paragraph * selstartpar = 0;
278         Paragraph * selendpar = 0;
279         UpdatableInset * the_locking_inset = 0;
280
281         pos_type pos = 0;
282         pos_type selstartpos = 0;
283         pos_type selendpos = 0;
284         bool selection = false;
285         bool mark_set  = false;
286
287         owner_->prohibitInput();
288
289         owner_->message(_("Formatting document..."));
290
291         if (bv_->text) {
292                 par = bv_->text->cursor.par();
293                 pos = bv_->text->cursor.pos();
294                 selstartpar = bv_->text->selection.start.par();
295                 selstartpos = bv_->text->selection.start.pos();
296                 selendpar = bv_->text->selection.end.par();
297                 selendpos = bv_->text->selection.end.pos();
298                 selection = bv_->text->selection.set();
299                 mark_set = bv_->text->selection.mark();
300                 the_locking_inset = bv_->theLockingInset();
301                 buffer_->resizeInsets(bv_);
302                 // I don't think the delete and new are necessary here we just could
303                 // call only init! (Jug 20020419)
304                 delete bv_->text;
305                 bv_->text = new LyXText(bv_);
306                 bv_->text->init(bv_);
307         } else {
308                 // See if we have a text in TextCache that fits
309                 // the new buffer_ with the correct width.
310                 bv_->text = textcache.findFit(buffer_, workarea().workWidth());
311                 if (bv_->text) {
312                         if (lyxerr.debugging()) {
313                                 lyxerr << "Found a LyXText that fits:\n";
314                                 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea().workWidth(), bv_->text)));
315                         }
316                         // Set the owner of the newly found text
317                         //      bv_->text->owner(bv_);
318                         if (lyxerr.debugging())
319                                 textcache.show(lyxerr, "resizeCurrentBuffer");
320                 } else {
321                         bv_->text = new LyXText(bv_);
322                         bv_->text->init(bv_);
323                         //buffer_->resizeInsets(bv_);
324                 }
325         }
326
327         if (par) {
328                 bv_->text->selection.set(true);
329                 // At this point just to avoid the Delete-Empty-Paragraph-
330                 // Mechanism when setting the cursor.
331                 bv_->text->selection.mark(mark_set);
332                 if (selection) {
333                         bv_->text->setCursor(bv_, selstartpar, selstartpos);
334                         bv_->text->selection.cursor = bv_->text->cursor;
335                         bv_->text->setCursor(bv_, selendpar, selendpos);
336                         bv_->text->setSelection(bv_);
337                         bv_->text->setCursor(bv_, par, pos);
338                 } else {
339                         bv_->text->setCursor(bv_, par, pos);
340                         bv_->text->selection.cursor = bv_->text->cursor;
341                         bv_->text->selection.set(false);
342                 }
343                 // remake the inset locking
344                 bv_->theLockingInset(the_locking_inset);
345         }
346
347         bv_->text->first_y = screen().topCursorVisible(bv_->text->cursor, bv_->text->first_y);
348
349         setState();
350         owner_->allowInput();
351
352         /// clear the "Formatting Document" message
353         owner_->message("");
354
355         return 0;
356 }
357
358
359 void BufferView::Pimpl::repaint()
360 {
361         // Regenerate the screen.
362         screen().redraw(bv_->text, bv_);
363 }
364
365
366 void BufferView::Pimpl::updateScrollbar()
367 {
368         if (!bv_->text) {
369                 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
370                 workarea().setScrollbarParams(0, 0, 0);
371                 return;
372         }
373
374         LyXText const & t = *bv_->text;
375
376         workarea().setScrollbarParams(t.height, t.first_y, t.defaultHeight());
377 }
378
379
380 void BufferView::Pimpl::scrollDocView(int value)
381 {
382         lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
383
384         if (!buffer_) return;
385
386         screen().draw(bv_->text, bv_, value);
387
388         if (!lyxrc.cursor_follows_scrollbar) {
389                 return;
390         }
391
392         LyXText * vbt = bv_->text;
393
394         int const height = vbt->defaultHeight();
395         int const first = static_cast<int>((bv_->text->first_y + height));
396         int const last = static_cast<int>((bv_->text->first_y + workarea().workHeight() - height));
397
398         if (vbt->cursor.y() < first)
399                 vbt->setCursorFromCoordinates(bv_, 0, first);
400         else if (vbt->cursor.y() > last)
401                 vbt->setCursorFromCoordinates(bv_, 0, last);
402 }
403
404
405 int BufferView::Pimpl::scroll(long time)
406 {
407         if (!buffer_)
408                 return 0;
409
410         LyXText const * t = bv_->text;
411
412         double const diff = t->defaultHeight()
413                 + double(time) * double(time) * 0.125;
414
415         scrollDocView(int(diff));
416         workarea().setScrollbarParams(t->height, t->first_y, t->defaultHeight());
417         return 0;
418 }
419
420
421 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
422                                          key_modifier::state state)
423 {
424         bv_->owner()->getLyXFunc()->processKeySym(key, state);
425 }
426
427
428 void BufferView::Pimpl::workAreaMotionNotify(int x, int y, mouse_button::state state)
429 {
430         // Only use motion with button 1
431         if (!(state & mouse_button::button1))
432                 return;
433
434         if (!buffer_)
435                 return;
436
437         // Check for inset locking
438         if (bv_->theLockingInset()) {
439                 LyXCursor cursor = bv_->text->cursor;
440                 LyXFont font = bv_->text->getFont(buffer_,
441                                                   cursor.par(), cursor.pos());
442                 int width = bv_->theLockingInset()->width(bv_, font);
443                 int inset_x = font.isVisibleRightToLeft()
444                         ? cursor.ix() - width : cursor.ix();
445                 int start_x = inset_x + bv_->theLockingInset()->scroll();
446
447                 bv_->theLockingInset()->
448                         insetMotionNotify(bv_,
449                                           x - start_x,
450                                           y - cursor.iy() + bv_->text->first_y,
451                                           state);
452                 return;
453         }
454
455         /* The test for not selection possible is needed, that only motion
456            events are used, where the bottom press event was on
457            the drawing area too */
458         if (!selection_possible)
459                 return;
460
461         screen().hideCursor();
462
463         Row * cursorrow = bv_->text->cursor.row();
464         bv_->text->setCursorFromCoordinates(bv_, x, y + bv_->text->first_y);
465 #if 0
466         // sorry for this but I have a strange error that the y value jumps at
467         // a certain point. This seems like an error in my xforms library or
468         // in some other local environment, but I would like to leave this here
469         // for the moment until I can remove this (Jug 20020418)
470         if (y_before < bv_->text->cursor.y())
471                 lyxerr << y_before << ":" << bv_->text->cursor.y() << endl;
472 #endif
473         // This is to allow jumping over large insets
474         if (cursorrow == bv_->text->cursor.row()) {
475                 if (y >= int(workarea().workHeight())) {
476                         bv_->text->cursorDown(bv_, false);
477                 } else if (y < 0) {
478                         bv_->text->cursorUp(bv_, false);
479                 }
480         }
481
482         if (!bv_->text->selection.set())
483                 update(bv_->text, BufferView::UPDATE); // Maybe an empty line was deleted
484
485         bv_->text->setSelection(bv_);
486         screen().toggleToggle(bv_->text, bv_);
487         fitCursor();
488         showCursor();
489 }
490
491
492 // Single-click on work area
493 void BufferView::Pimpl::workAreaButtonPress(int xpos, int ypos,
494                                             mouse_button::state button)
495 {
496         if (!buffer_)
497                 return;
498
499         // ok ok, this is a hack (for xforms)
500         if (button == mouse_button::button4) {
501                 scroll(-lyxrc.wheel_jump);
502                 // We shouldn't go further down as we really should only do the
503                 // scrolling and be done with this. Otherwise we may open some
504                 // dialogs (Jug 20020424).
505                 return;
506         } else if (button == mouse_button::button5) {
507                 scroll(lyxrc.wheel_jump);
508                 // We shouldn't go further down as we really should only do the
509                 // scrolling and be done with this. Otherwise we may open some
510                 // dialogs (Jug 20020424).
511                 return;
512         }
513
514         Inset * inset_hit = checkInsetHit(bv_->text, xpos, ypos);
515
516         // Middle button press pastes if we have a selection
517         // We do this here as if the selection was inside an inset
518         // it could get cleared on the unlocking of the inset so
519         // we have to check this first
520         bool paste_internally = false;
521         if (button == mouse_button::button2 && bv_->getLyXText()->selection.set()) {
522                 owner_->getLyXFunc()->dispatch(LFUN_COPY);
523                 paste_internally = true;
524         }
525
526         int const screen_first = bv_->text->first_y;
527
528         if (bv_->theLockingInset()) {
529                 // We are in inset locking mode
530
531                 /* Check whether the inset was hit. If not reset mode,
532                    otherwise give the event to the inset */
533                 if (inset_hit == bv_->theLockingInset()) {
534                         bv_->theLockingInset()->
535                                 insetButtonPress(bv_, xpos, ypos, button);
536                         return;
537                 } else {
538                         bv_->unlockInset(bv_->theLockingInset());
539                 }
540         }
541
542         if (!inset_hit)
543                 selection_possible = true;
544         screen().hideCursor();
545
546         // Clear the selection
547         screen().toggleSelection(bv_->text, bv_);
548         bv_->text->clearSelection();
549         bv_->text->fullRebreak(bv_);
550         update();
551         updateScrollbar();
552
553         // Single left click in math inset?
554         if (isHighlyEditableInset(inset_hit)) {
555                 // Highly editable inset, like math
556                 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
557                 selection_possible = false;
558                 owner_->updateLayoutChoice();
559                 owner_->message(inset->editMessage());
560                 //inset->edit(bv_, xpos, ypos, button);
561                 // We just have to lock the inset before calling a PressEvent on it!
562                 // we don't need the edit() call here! (Jug20020329)
563                 if (!bv_->lockInset(inset)) {
564                         lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
565                 }
566                 inset->insetButtonPress(bv_, xpos, ypos, button);
567                 return;
568         }
569         // I'm not sure we should continue here if we hit an inset (Jug20020403)
570
571         // Right click on a footnote flag opens float menu
572         if (button == mouse_button::button3) {
573                 selection_possible = false;
574                 return;
575         }
576
577         if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
578                 bv_->text->setCursorFromCoordinates(bv_, xpos, ypos + screen_first);
579         finishUndo();
580         bv_->text->selection.cursor = bv_->text->cursor;
581         bv_->text->cursor.x_fix(bv_->text->cursor.x());
582
583         owner_->updateLayoutChoice();
584         if (fitCursor()) {
585                 selection_possible = false;
586         }
587
588         // Insert primary selection with middle mouse
589         // if there is a local selection in the current buffer,
590         // insert this
591         if (button == mouse_button::button2) {
592                 if (paste_internally)
593                         owner_->getLyXFunc()->dispatch(LFUN_PASTE);
594                 else
595                         owner_->getLyXFunc()->dispatch(LFUN_PASTESELECTION,
596                                                        "paragraph");
597                 selection_possible = false;
598                 return;
599         }
600 }
601
602
603 void BufferView::Pimpl::doubleClick(int /*x*/, int /*y*/, mouse_button::state button)
604 {
605         if (!buffer_)
606                 return;
607
608         LyXText * text = bv_->getLyXText();
609
610         if (text->bv_owner && bv_->theLockingInset())
611                 return;
612
613         if (button == mouse_button::button1) {
614                 if (text->bv_owner) {
615                         screen().hideCursor();
616                         screen().toggleSelection(text, bv_);
617                         text->selectWord(bv_, LyXText::WHOLE_WORD_STRICT);
618                         screen().toggleSelection(text, bv_, false);
619                 } else {
620                         text->selectWord(bv_, LyXText::WHOLE_WORD_STRICT);
621                 }
622                 /* This will fit the cursor on the screen
623                  * if necessary */
624                 update(text, BufferView::SELECT|BufferView::FITCUR);
625         }
626 }
627
628
629 void BufferView::Pimpl::tripleClick(int /*x*/, int /*y*/, mouse_button::state button)
630 {
631         if (!buffer_)
632                 return;
633
634         LyXText * text = bv_->getLyXText();
635
636         if (text->bv_owner && bv_->theLockingInset())
637             return;
638
639         if (button == mouse_button::button1) {
640                 if (text->bv_owner) {
641                         screen().hideCursor();
642                         screen().toggleSelection(text, bv_);
643                 }
644                 text->cursorHome(bv_);
645                 text->selection.cursor = text->cursor;
646                 text->cursorEnd(bv_);
647                 text->setSelection(bv_);
648                 if (text->bv_owner) {
649                         screen().toggleSelection(text, bv_, false);
650                 }
651                 /* This will fit the cursor on the screen
652                  * if necessary */
653                 update(text, BufferView::SELECT|BufferView::FITCUR);
654         }
655 }
656
657
658 void BufferView::Pimpl::selectionRequested()
659 {
660         static string sel;
661
662         if (!available())
663                 return;
664
665         LyXText * text = bv_->getLyXText();
666
667         if (text->selection.set() &&
668                 (!bv_->text->xsel_cache.set() ||
669                  text->selection.start != bv_->text->xsel_cache.start ||
670                  text->selection.end != bv_->text->xsel_cache.end))
671         {
672                 bv_->text->xsel_cache = text->selection;
673                 sel = text->selectionAsString(bv_->buffer(), false);
674         } else if (!text->selection.set()) {
675                 sel = string();
676                 bv_->text->xsel_cache.set(false);
677         }
678         if (!sel.empty()) {
679                 workarea().putClipboard(sel);
680         }
681 }
682
683
684 void BufferView::Pimpl::selectionLost()
685 {
686         if (available()) {
687                 hideCursor();
688                 toggleSelection();
689                 bv_->getLyXText()->clearSelection();
690                 showCursor();
691                 bv_->text->xsel_cache.set(false);
692         }
693 }
694
695
696 void BufferView::Pimpl::workAreaButtonRelease(int x, int y,
697                                               mouse_button::state button)
698 {
699         // do nothing if we used the mouse wheel
700         if (!buffer_ || button == mouse_button::button4 || button == mouse_button::button5)
701                 return;
702
703         // If we hit an inset, we have the inset coordinates in these
704         // and inset_hit points to the inset.  If we do not hit an
705         // inset, inset_hit is 0, and inset_x == x, inset_y == y.
706         Inset * inset_hit = checkInsetHit(bv_->text, x, y);
707
708         if (bv_->theLockingInset()) {
709                 // We are in inset locking mode.
710
711                 /* LyX does a kind of work-area grabbing for insets.
712                    Only a ButtonPress Event outside the inset will
713                    force a insetUnlock. */
714                 bv_->theLockingInset()->
715                         insetButtonRelease(bv_, x, y, button);
716                 return;
717         }
718
719         selection_possible = false;
720
721         if (button == mouse_button::button2)
722                 return;
723
724         // finish selection
725         if (button == mouse_button::button1) {
726                 workarea().haveSelection(bv_->getLyXText()->selection.set());
727         }
728
729         setState();
730         owner_->showState();
731         owner_->updateMenubar();
732         owner_->updateToolbar();
733
734         // Did we hit an editable inset?
735         if (inset_hit) {
736                 selection_possible = false;
737
738                 // if we reach this point with a selection, it
739                 // must mean we are currently selecting.
740                 // But we don't want to open the inset
741                 // because that is annoying for the user.
742                 // So just pretend we didn't hit it.
743                 // this is OK because a "kosher" ButtonRelease
744                 // will follow a ButtonPress that clears
745                 // the selection.
746                 // Note this also fixes selection drawing
747                 // problems if we end up opening an inset
748                 if (bv_->getLyXText()->selection.set())
749                         return;
750
751                 // CHECK fix this proper in 0.13
752                 // well, maybe 13.0 !!!!!!!!!
753
754                 // Following a ref shouldn't issue
755                 // a push on the undo-stack
756                 // anylonger, now that we have
757                 // keybindings for following
758                 // references and returning from
759                 // references.  IMHO though, it
760                 // should be the inset's own business
761                 // to push or not push on the undo
762                 // stack. They don't *have* to
763                 // alter the document...
764                 // (Joacim)
765                 // ...or maybe the SetCursorParUndo()
766                 // below isn't necessary at all anylonger?
767                 if (inset_hit->lyxCode() == Inset::REF_CODE) {
768                         setCursorParUndo(bv_);
769                 }
770
771                 owner_->message(inset_hit->editMessage());
772
773                 if (isHighlyEditableInset(inset_hit)) {
774                         // Highly editable inset, like math
775                         UpdatableInset *inset = (UpdatableInset *)inset_hit;
776                         inset->insetButtonRelease(bv_, x, y, button);
777                 } else {
778                         inset_hit->insetButtonRelease(bv_, x, y, button);
779                         // IMO this is a grosshack! Inset's should be changed so that
780                         // they call the actions they have to do with the insetButtonRel.
781                         // function and not in the edit(). This should be changed
782                         // (Jug 20020329)
783                         inset_hit->edit(bv_, x, y, button);
784                 }
785                 return;
786         }
787
788         // Maybe we want to edit a bibitem ale970302
789         if (bv_->text->cursor.par()->bibkey && x < 20 +
790             bibitemMaxWidth(bv_, textclasslist[buffer_->params.textclass].defaultfont())) {
791                 bv_->text->cursor.par()->bibkey->edit(bv_, 0, 0, mouse_button::none);
792         }
793
794         return;
795 }
796
797
798 Box BufferView::Pimpl::insetDimensions(LyXText const & text,
799                                        LyXCursor const & cursor) const
800 {
801         Paragraph /*const*/ & par = *cursor.par();
802         pos_type const pos = cursor.pos();
803
804         lyx::Assert(par.getInset(pos));
805
806         Inset const & inset(*par.getInset(pos));
807
808         LyXFont const & font = text.getFont(buffer_, &par, pos);
809
810         int const width = inset.width(bv_, font);
811         int const inset_x = font.isVisibleRightToLeft()
812                 ? (cursor.ix() - width) : cursor.ix();
813
814         return Box(
815                 inset_x + inset.scroll(),
816                 inset_x + width,
817                 cursor.iy() - inset.ascent(bv_, font),
818                 cursor.iy() + inset.descent(bv_, font));
819 }
820
821
822 Inset * BufferView::Pimpl::checkInset(LyXText const & text,
823                                       LyXCursor const & cursor,
824                                       int & x, int & y) const
825 {
826         pos_type const pos(cursor.pos());
827         Paragraph /*const*/ & par(*cursor.par());
828
829         if (pos >= par.size() || !par.isInset(pos)) {
830                 return 0;
831         }
832
833         Inset /*const*/ * inset = par.getInset(pos);
834
835         if (!isEditableInset(inset)) {
836                 return 0;
837         }
838
839         Box b(insetDimensions(text, cursor));
840
841         if (!b.contained(x, y)) {
842                 lyxerr[Debug::GUI] << "Missed inset at x,y " << x << "," << y
843                         << " box " << b << endl;
844                 return 0;
845         }
846
847         text.setCursor(bv_, &par, pos, true);
848
849         x -= b.x1;
850         // The origin of an inset is on the baseline
851         y -= text.cursor.iy();
852
853         return inset;
854 }
855
856
857 Inset * BufferView::Pimpl::checkInsetHit(LyXText * text, int & x, int & y)
858 {
859         int y_tmp = y + text->first_y;
860
861         LyXCursor cursor;
862         text->setCursorFromCoordinates(bv_, cursor, x, y_tmp);
863
864         Inset * inset(checkInset(*text, cursor, x, y_tmp));
865
866         if (inset) {
867                 y = y_tmp;
868                 return inset;
869         }
870
871         // look at previous position
872
873         if (cursor.pos() == 0) {
874                 return 0;
875         }
876
877         // move back one
878         text->setCursor(bv_, cursor, cursor.par(), cursor.pos() - 1, true);
879
880         inset = checkInset(*text, cursor, x, y_tmp);
881         if (inset) {
882                 y = y_tmp;
883         }
884         return inset;
885 }
886
887
888 void BufferView::Pimpl::workAreaResize()
889 {
890         static int work_area_width;
891         static unsigned int work_area_height;
892
893         bool const widthChange = workarea().workWidth() != work_area_width;
894         bool const heightChange = workarea().workHeight() != work_area_height;
895
896         // update from work area
897         work_area_width = workarea().workWidth();
898         work_area_height = workarea().workHeight();
899
900         if (buffer_ != 0) {
901                 if (widthChange) {
902                         // The visible LyXView need a resize
903                         resizeCurrentBuffer();
904
905                         // Remove all texts from the textcache
906                         // This is not _really_ what we want to do. What
907                         // we really want to do is to delete in textcache
908                         // that does not have a BufferView with matching
909                         // width, but as long as we have only one BufferView
910                         // deleting all gives the same result.
911                         if (lyxerr.debugging())
912                                 textcache.show(lyxerr, "Expose delete all");
913                         textcache.clear();
914                         // FIXME: this is aalready done in resizeCurrentBuffer() ??
915                         buffer_->resizeInsets(bv_);
916                 } else if (heightChange) {
917                         // fitCursor() ensures we don't jump back
918                         // to the start of the document on vertical
919                         // resize
920                         fitCursor();
921                 }
922         }
923
924         if (widthChange || heightChange) {
925                 repaint();
926         }
927
928         // always make sure that the scrollbar is sane.
929         updateScrollbar();
930         owner_->updateLayoutChoice();
931         return;
932 }
933
934
935 void BufferView::Pimpl::update()
936 {
937         if (!bv_->theLockingInset() || !bv_->theLockingInset()->nodraw()) {
938                 LyXText::text_status st = bv_->text->status();
939                 screen().update(bv_->text, bv_);
940                 bool fitc = false;
941                 while (bv_->text->status() == LyXText::CHANGED_IN_DRAW) {
942                         bv_->text->fullRebreak(bv_);
943                         st = LyXText::NEED_MORE_REFRESH;
944                         bv_->text->setCursor(bv_, bv_->text->cursor.par(),
945                                              bv_->text->cursor.pos());
946                         if (bv_->text->selection.set()) {
947                                 bv_->text->setCursor(bv_, bv_->text->selection.start,
948                                                      bv_->text->selection.start.par(),
949                                                      bv_->text->selection.start.pos());
950                                 bv_->text->setCursor(bv_, bv_->text->selection.end,
951                                                      bv_->text->selection.end.par(),
952                                                      bv_->text->selection.end.pos());
953                         }
954                         fitc = true;
955                         bv_->text->status(bv_, st);
956                         screen().update(bv_->text, bv_);
957                 }
958                 // do this here instead of in the screen::update because of
959                 // the above loop!
960                 bv_->text->status(bv_, LyXText::UNCHANGED);
961                 if (fitc)
962                         fitCursor();
963         }
964 }
965
966 // Values used when calling update:
967 // -3 - update
968 // -2 - update, move sel_cursor if selection, fitcursor
969 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
970 //  0 - update, move sel_cursor if selection, fitcursor
971 //  1 - update, move sel_cursor if selection, fitcursor, mark dirty
972 //  3 - update, move sel_cursor if selection
973 //
974 // update -
975 // a simple redraw of the parts that need refresh
976 //
977 // move sel_cursor if selection -
978 // the text's sel_cursor is moved if there is selection is progress
979 //
980 // fitcursor -
981 // fitCursor() is called and the scrollbar updated
982 //
983 // mark dirty -
984 // the buffer is marked dirty.
985 //
986 // enum {
987 //       UPDATE = 0,
988 //       SELECT = 1,
989 //       FITCUR = 2,
990 //       CHANGE = 4
991 // };
992 //
993 // UPDATE_ONLY = UPDATE;
994 // UPDATE_SELECT = UPDATE | SELECT;
995 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
996 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
997 //
998 // update(-3) -> update(0)         -> update(0) -> update(UPDATE)
999 // update(-2) -> update(1 + 2)     -> update(3) -> update(SELECT|FITCUR)
1000 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1001 // update(1)  -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1002 // update(3)  -> update(1)         -> update(1) -> update(SELECT)
1003
1004 void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
1005 {
1006         owner_->updateLayoutChoice();
1007
1008         if (!text->selection.set() && (f & SELECT)) {
1009                 text->selection.cursor = text->cursor;
1010         }
1011
1012         text->fullRebreak(bv_);
1013
1014         if (text->inset_owner) {
1015                 text->inset_owner->setUpdateStatus(bv_, InsetText::NONE);
1016             updateInset(text->inset_owner, false);
1017         } else {
1018             update();
1019         }
1020
1021         if ((f & FITCUR)) {
1022                 fitCursor();
1023         }
1024
1025         if ((f & CHANGE)) {
1026                 buffer_->markDirty();
1027         }
1028 }
1029
1030
1031 // Callback for cursor timer
1032 void BufferView::Pimpl::cursorToggle()
1033 {
1034         if (!buffer_) {
1035                 cursor_timeout.restart();
1036                 return;
1037         }
1038
1039         /* FIXME */
1040         extern void reapSpellchecker(void);
1041         reapSpellchecker();
1042
1043         if (!bv_->theLockingInset()) {
1044                 screen().cursorToggle(bv_);
1045         } else {
1046                 bv_->theLockingInset()->toggleInsetCursor(bv_);
1047         }
1048
1049         cursor_timeout.restart();
1050 }
1051
1052
1053 void BufferView::Pimpl::cursorPrevious(LyXText * text)
1054 {
1055         if (!text->cursor.row()->previous()) {
1056                 if (text->first_y > 0) {
1057                         int new_y = bv_->text->first_y - workarea().workHeight();
1058                         screen().draw(bv_->text, bv_, new_y < 0 ? 0 : new_y);
1059                         updateScrollbar();
1060                 }
1061                 return;
1062         }
1063
1064         int y = text->first_y;
1065         Row * cursorrow = text->cursor.row();
1066
1067         text->setCursorFromCoordinates(bv_, text->cursor.x_fix(), y);
1068         finishUndo();
1069
1070         int new_y;
1071         if (cursorrow == bv_->text->cursor.row()) {
1072                 // we have a row which is higher than the workarea so we leave the
1073                 // cursor on the start of the row and move only the draw up as soon
1074                 // as we move the cursor or do something while inside the row (it may
1075                 // span several workarea-heights) we'll move to the top again, but this
1076                 // is better than just jump down and only display part of the row.
1077                 new_y = bv_->text->first_y - workarea().workHeight();
1078         } else {
1079                 if (text->inset_owner) {
1080                         new_y = bv_->text->cursor.iy()
1081                                 + bv_->theLockingInset()->insetInInsetY() + y
1082                                 + text->cursor.row()->height()
1083                                 - workarea().workHeight() + 1;
1084                 } else {
1085                         new_y = text->cursor.y()
1086                                 - text->cursor.row()->baseline()
1087                                 + text->cursor.row()->height()
1088                                 - workarea().workHeight() + 1;
1089                 }
1090         }
1091         screen().draw(bv_->text, bv_,  new_y < 0 ? 0 : new_y);
1092         if (text->cursor.row()->previous()) {
1093                 LyXCursor cur;
1094                 text->setCursor(bv_, cur, text->cursor.row()->previous()->par(),
1095                                                 text->cursor.row()->previous()->pos(), false);
1096                 if (cur.y() > text->first_y) {
1097                         text->cursorUp(bv_, true);
1098                 }
1099         }
1100         updateScrollbar();
1101 }
1102
1103
1104 void BufferView::Pimpl::cursorNext(LyXText * text)
1105 {
1106         if (!text->cursor.row()->next()) {
1107                 int y = text->cursor.y() - text->cursor.row()->baseline() +
1108                         text->cursor.row()->height();
1109                 if (y > int(text->first_y + workarea().workHeight())) {
1110                         screen().draw(bv_->text, bv_,
1111                                                   bv_->text->first_y + workarea().workHeight());
1112                         updateScrollbar();
1113                 }
1114                 return;
1115         }
1116
1117         int y = text->first_y + workarea().workHeight();
1118         if (text->inset_owner && !text->first_y) {
1119                 y -= (bv_->text->cursor.iy()
1120                           - bv_->text->first_y
1121                           + bv_->theLockingInset()->insetInInsetY());
1122         }
1123
1124         text->getRowNearY(y);
1125
1126         Row * cursorrow = text->cursor.row();
1127         text->setCursorFromCoordinates(bv_, text->cursor.x_fix(), y); // + workarea().workHeight());
1128         finishUndo();
1129
1130         int new_y;
1131         if (cursorrow == bv_->text->cursor.row()) {
1132                 // we have a row which is higher than the workarea so we leave the
1133                 // cursor on the start of the row and move only the draw down as soon
1134                 // as we move the cursor or do something while inside the row (it may
1135                 // span several workarea-heights) we'll move to the top again, but this
1136                 // is better than just jump down and only display part of the row.
1137                 new_y = bv_->text->first_y + workarea().workHeight();
1138         } else {
1139                 if (text->inset_owner) {
1140                         new_y = bv_->text->cursor.iy()
1141                                 + bv_->theLockingInset()->insetInInsetY()
1142                                 + y - text->cursor.row()->baseline();
1143                 } else {
1144                         new_y =  text->cursor.y() - text->cursor.row()->baseline();
1145                 }
1146         }
1147         screen().draw(bv_->text, bv_, new_y);
1148         if (text->cursor.row()->next()) {
1149                 LyXCursor cur;
1150                 text->setCursor(bv_, cur, text->cursor.row()->next()->par(),
1151                                                 text->cursor.row()->next()->pos(), false);
1152                 if (cur.y() < int(text->first_y + workarea().workHeight())) {
1153                         text->cursorDown(bv_, true);
1154                 }
1155         }
1156         updateScrollbar();
1157 }
1158
1159
1160 bool BufferView::Pimpl::available() const
1161 {
1162         if (buffer_ && bv_->text)
1163                 return true;
1164         return false;
1165 }
1166
1167
1168 void BufferView::Pimpl::beforeChange(LyXText * text)
1169 {
1170         toggleSelection();
1171         text->clearSelection();
1172 }
1173
1174
1175 void BufferView::Pimpl::savePosition(unsigned int i)
1176 {
1177         if (i >= saved_positions_num)
1178                 return;
1179         saved_positions[i] = Position(buffer_->fileName(),
1180                                       bv_->text->cursor.par()->id(),
1181                                       bv_->text->cursor.pos());
1182         if (i > 0) {
1183                 ostringstream str;
1184                 str << _("Saved bookmark") << ' ' << i;
1185                 owner_->message(str.str().c_str());
1186         }
1187 }
1188
1189
1190 void BufferView::Pimpl::restorePosition(unsigned int i)
1191 {
1192         if (i >= saved_positions_num)
1193                 return;
1194
1195         string const fname = saved_positions[i].filename;
1196
1197         beforeChange(bv_->text);
1198
1199         if (fname != buffer_->fileName()) {
1200                 Buffer * b = bufferlist.exists(fname) ?
1201                         bufferlist.getBuffer(fname) :
1202                         bufferlist.loadLyXFile(fname); // don't ask, just load it
1203                 if (b != 0) buffer(b);
1204         }
1205
1206         Paragraph * par = buffer_->getParFromID(saved_positions[i].par_id);
1207         if (!par)
1208                 return;
1209
1210         bv_->text->setCursor(bv_, par,
1211                              min(par->size(), saved_positions[i].par_pos));
1212
1213         update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
1214         if (i > 0) {
1215                 ostringstream str;
1216                 str << _("Moved to bookmark") << ' ' << i;
1217                 owner_->message(str.str().c_str());
1218         }
1219 }
1220
1221
1222 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
1223 {
1224         if (i >= saved_positions_num)
1225                 return false;
1226
1227         return !saved_positions[i].filename.empty();
1228 }
1229
1230
1231 void BufferView::Pimpl::setState()
1232 {
1233         if (!lyxrc.rtl_support)
1234                 return;
1235
1236         LyXText * text = bv_->getLyXText();
1237         if (text->real_current_font.isRightToLeft()
1238             && !(bv_->theLockingInset()
1239                  && bv_->theLockingInset()->lyxCode()== Inset::ERT_CODE))
1240         {
1241                 if (owner_->getIntl()->keymap == Intl::PRIMARY)
1242                         owner_->getIntl()->KeyMapSec();
1243         } else {
1244                 if (owner_->getIntl()->keymap == Intl::SECONDARY)
1245                         owner_->getIntl()->KeyMapPrim();
1246         }
1247 }
1248
1249
1250 void BufferView::Pimpl::insetUnlock()
1251 {
1252         if (bv_->theLockingInset()) {
1253                 bv_->theLockingInset()->insetUnlock(bv_);
1254                 bv_->theLockingInset(0);
1255                 finishUndo();
1256         }
1257 }
1258
1259
1260 void BufferView::Pimpl::showCursor()
1261 {
1262         if (bv_->theLockingInset())
1263                 bv_->theLockingInset()->showInsetCursor(bv_);
1264         else
1265                 screen().showCursor(bv_->text, bv_);
1266 }
1267
1268
1269 void BufferView::Pimpl::hideCursor()
1270 {
1271         if (!bv_->theLockingInset())
1272                 screen().hideCursor();
1273 }
1274
1275
1276 void BufferView::Pimpl::toggleSelection(bool b)
1277 {
1278         if (bv_->theLockingInset())
1279                 bv_->theLockingInset()->toggleSelection(bv_, b);
1280         screen().toggleSelection(bv_->text, bv_, b);
1281 }
1282
1283
1284 void BufferView::Pimpl::toggleToggle()
1285 {
1286         screen().toggleToggle(bv_->text, bv_);
1287 }
1288
1289
1290 void BufferView::Pimpl::center()
1291 {
1292         beforeChange(bv_->text);
1293         if (bv_->text->cursor.y() > static_cast<int>((workarea().workHeight() / 2))) {
1294                 screen().draw(bv_->text, bv_, bv_->text->cursor.y() - workarea().workHeight() / 2);
1295         } else {
1296                 screen().draw(bv_->text, bv_, 0);
1297         }
1298         update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
1299 }
1300
1301
1302 void BufferView::Pimpl::pasteClipboard(bool asPara)
1303 {
1304         if (!buffer_)
1305                 return;
1306
1307         screen().hideCursor();
1308         beforeChange(bv_->text);
1309
1310         string const clip(workarea().getClipboard());
1311
1312         if (clip.empty())
1313                 return;
1314
1315         if (asPara) {
1316                 bv_->getLyXText()->insertStringAsParagraphs(bv_, clip);
1317         } else {
1318                 bv_->getLyXText()->insertStringAsLines(bv_, clip);
1319         }
1320         bv_->getLyXText()->clearSelection();
1321         update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1322 }
1323
1324
1325 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
1326 {
1327         workarea().putClipboard(stuff);
1328 }
1329
1330
1331 /*
1332  * Dispatch functions for actions which can be valid for BufferView->text
1333  * and/or InsetText->text!!!
1334  */
1335
1336
1337 inline
1338 void BufferView::Pimpl::moveCursorUpdate(bool selecting, bool fitcur)
1339 {
1340         LyXText * lt = bv_->getLyXText();
1341
1342         if (selecting || lt->selection.mark()) {
1343                 lt->setSelection(bv_);
1344                 if (lt->bv_owner)
1345                         toggleToggle();
1346                 else
1347                         updateInset(lt->inset_owner, false);
1348         }
1349         if (lt->bv_owner) {
1350                 if (fitcur)
1351                         update(lt, BufferView::SELECT|BufferView::FITCUR);
1352                 else
1353                         update(lt, BufferView::SELECT);
1354                 showCursor();
1355         } else if (bv_->text->status() != LyXText::UNCHANGED) {
1356                 bv_->theLockingInset()->hideInsetCursor(bv_);
1357                 update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
1358                 showCursor();
1359         }
1360
1361         if (!lt->selection.set())
1362                 workarea().haveSelection(false);
1363
1364         /* ---> Everytime the cursor is moved, show the current font state. */
1365         // should this too me moved out of this func?
1366         //owner->showState();
1367         setState();
1368 }
1369
1370
1371 Inset * BufferView::Pimpl::getInsetByCode(Inset::Code code)
1372 {
1373         LyXCursor cursor = bv_->getLyXText()->cursor;
1374         Buffer::inset_iterator it =
1375                 find_if(Buffer::inset_iterator(
1376                         cursor.par(), cursor.pos()),
1377                         buffer_->inset_iterator_end(),
1378                         lyx::compare_memfun(&Inset::lyxCode, code));
1379         return it != buffer_->inset_iterator_end() ? (*it) : 0;
1380 }
1381
1382
1383 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
1384 {
1385         string filename = filen;
1386
1387         if (filename.empty()) {
1388                 // Launch a file browser
1389                 string initpath = lyxrc.document_path;
1390
1391                 if (available()) {
1392                         string const trypath = owner_->buffer()->filePath();
1393                         // If directory is writeable, use this as default.
1394                         if (IsDirWriteable(trypath))
1395                                 initpath = trypath;
1396                 }
1397
1398                 FileDialog fileDlg(bv_->owner(),
1399                                    _("Select LyX document to insert"),
1400                         LFUN_FILE_INSERT,
1401                         make_pair(string(_("Documents|#o#O")),
1402                                   string(lyxrc.document_path)),
1403                         make_pair(string(_("Examples|#E#e")),
1404                                   string(AddPath(system_lyxdir, "examples"))));
1405
1406                 FileDialog::Result result =
1407                         fileDlg.Select(initpath,
1408                                        _("*.lyx| LyX Documents (*.lyx)"));
1409
1410                 if (result.first == FileDialog::Later)
1411                         return;
1412
1413                 filename = result.second;
1414
1415                 // check selected filename
1416                 if (filename.empty()) {
1417                         owner_->message(_("Canceled."));
1418                         return;
1419                 }
1420         }
1421
1422         // get absolute path of file and add ".lyx" to the filename if
1423         // necessary
1424         filename = FileSearch(string(), filename, "lyx");
1425
1426         string const disp_fn(MakeDisplayPath(filename));
1427
1428         ostringstream s1;
1429         s1 << _("Inserting document") << ' '
1430            << disp_fn << " ...";
1431         owner_->message(s1.str().c_str());
1432         bool const res = bv_->insertLyXFile(filename);
1433         if (res) {
1434                 ostringstream str;
1435                 str << _("Document") << ' ' << disp_fn
1436                     << ' ' << _("inserted.");
1437                 owner_->message(str.str().c_str());
1438         } else {
1439                 ostringstream str;
1440                 str << _("Could not insert document") << ' '
1441                     << disp_fn;
1442                 owner_->message(str.str().c_str());
1443         }
1444 }
1445
1446
1447 bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
1448 {
1449         lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch: action["
1450                               << action <<"] arg[" << argument << "]" << endl;
1451
1452         LyXTextClass const & tclass = textclasslist[buffer_->params.textclass];
1453
1454         switch (action) {
1455                 // --- Misc -------------------------------------------
1456         case LFUN_APPENDIX:
1457         {
1458                 if (available()) {
1459                         LyXText * lt = bv_->getLyXText();
1460                         lt->toggleAppendix(bv_);
1461                         update(lt,
1462                                BufferView::SELECT
1463                                | BufferView::FITCUR
1464                                | BufferView::CHANGE);
1465                 }
1466         }
1467         break;
1468
1469         case LFUN_TOC_INSERT:
1470         {
1471                 InsetCommandParams p;
1472                 p.setCmdName("tableofcontents");
1473                 Inset * inset = new InsetTOC(p);
1474                 if (!insertInset(inset, tclass.defaultLayoutName()))
1475                         delete inset;
1476                 break;
1477         }
1478
1479         case LFUN_SCROLL_INSET:
1480                 // this is not handled here as this funktion is only aktive
1481                 // if we have a locking_inset and that one is (or contains)
1482                 // a tabular-inset
1483                 break;
1484
1485         case LFUN_INSET_GRAPHICS:
1486         {
1487                 Inset * new_inset = new InsetGraphics;
1488                 if (!insertInset(new_inset)) {
1489                         delete new_inset;
1490                 } else {
1491                         // this is need because you don't use a inset->Edit()
1492                         updateInset(new_inset, true);
1493                         new_inset->edit(bv_);
1494                 }
1495                 break;
1496         }
1497
1498         case LFUN_PASTE:
1499                 bv_->paste();
1500                 setState();
1501                 break;
1502
1503         case LFUN_PASTESELECTION:
1504         {
1505                 bool asPara = false;
1506                 if (argument == "paragraph")
1507                         asPara = true;
1508                 pasteClipboard(asPara);
1509         }
1510         break;
1511
1512         case LFUN_CUT:
1513                 bv_->cut();
1514                 break;
1515
1516         case LFUN_COPY:
1517                 bv_->copy();
1518                 break;
1519
1520         case LFUN_LAYOUT_COPY:
1521                 bv_->copyEnvironment();
1522                 break;
1523
1524         case LFUN_LAYOUT_PASTE:
1525                 bv_->pasteEnvironment();
1526                 setState();
1527                 break;
1528
1529         case LFUN_GOTOERROR:
1530                 gotoInset(Inset::ERROR_CODE, false);
1531                 break;
1532
1533         case LFUN_GOTONOTE:
1534                 gotoInset(Inset::IGNORE_CODE, false);
1535                 break;
1536
1537         case LFUN_REFERENCE_GOTO:
1538         {
1539                 vector<Inset::Code> tmp;
1540                 tmp.push_back(Inset::LABEL_CODE);
1541                 tmp.push_back(Inset::REF_CODE);
1542                 gotoInset(tmp, true);
1543                 break;
1544         }
1545
1546         case LFUN_HYPHENATION:
1547                 specialChar(InsetSpecialChar::HYPHENATION);
1548                 break;
1549
1550         case LFUN_LIGATURE_BREAK:
1551                 specialChar(InsetSpecialChar::LIGATURE_BREAK);
1552                 break;
1553
1554         case LFUN_LDOTS:
1555                 specialChar(InsetSpecialChar::LDOTS);
1556                 break;
1557
1558         case LFUN_END_OF_SENTENCE:
1559                 specialChar(InsetSpecialChar::END_OF_SENTENCE);
1560                 break;
1561
1562         case LFUN_MENU_SEPARATOR:
1563                 specialChar(InsetSpecialChar::MENU_SEPARATOR);
1564                 break;
1565
1566         case LFUN_HFILL:
1567                 hfill();
1568                 break;
1569
1570         case LFUN_DEPTH_MIN:
1571                 changeDepth(bv_, bv_->getLyXText(), -1);
1572                 break;
1573
1574         case LFUN_DEPTH_PLUS:
1575                 changeDepth(bv_, bv_->getLyXText(), 1);
1576                 break;
1577
1578         case LFUN_FREE:
1579                 owner_->getDialogs()->setUserFreeFont();
1580                 break;
1581
1582         case LFUN_FILE_INSERT:
1583                 MenuInsertLyXFile(argument);
1584                 break;
1585
1586         case LFUN_FILE_INSERT_ASCII_PARA:
1587                 InsertAsciiFile(bv_, argument, true);
1588                 break;
1589
1590         case LFUN_FILE_INSERT_ASCII:
1591                 InsertAsciiFile(bv_, argument, false);
1592                 break;
1593
1594         case LFUN_LAYOUT:
1595         {
1596                 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1597                                     << argument << endl;
1598
1599                 // This is not the good solution to the empty argument
1600                 // problem, but it will hopefully suffice for 1.2.0.
1601                 // The correct solution would be to augument the
1602                 // function list/array with information about what
1603                 // functions needs arguments and their type.
1604                 if (argument.empty()) {
1605                         owner_->getLyXFunc()->setErrorMessage(
1606                                 _("LyX function 'layout' needs an argument."));
1607                         break;
1608                 }
1609
1610                 // Derive layout number from given argument (string)
1611                 // and current buffer's textclass (number). */
1612                 bool hasLayout = tclass.hasLayout(argument);
1613                 string layout = argument;
1614
1615                 // If the entry is obsolete, use the new one instead.
1616                 if (hasLayout) {
1617                         string const & obs = tclass[layout]->obsoleted_by();
1618                         if (!obs.empty())
1619                                 layout = obs;
1620                 }
1621
1622                 if (!hasLayout) {
1623                         owner_->getLyXFunc()->setErrorMessage(
1624                                 string(N_("Layout ")) + argument +
1625                                 N_(" not known"));
1626                         break;
1627                 }
1628
1629                 bool change_layout = (current_layout != layout);
1630                 LyXText * lt = bv_->getLyXText();
1631                 if (!change_layout && lt->selection.set() &&
1632                         lt->selection.start.par() != lt->selection.end.par())
1633                 {
1634                         Paragraph * spar = lt->selection.start.par();
1635                         Paragraph * epar = lt->selection.end.par()->next();
1636                         while(spar != epar) {
1637                                 if (spar->layout()->name() != current_layout) {
1638                                         change_layout = true;
1639                                         break;
1640                                 }
1641                         }
1642                 }
1643                 if (change_layout) {
1644                         hideCursor();
1645                         current_layout = layout;
1646                         update(lt,
1647                                BufferView::SELECT
1648                                | BufferView::FITCUR);
1649                         lt->setLayout(bv_, layout);
1650                         owner_->setLayout(layout);
1651                         update(lt,
1652                                BufferView::SELECT
1653                                | BufferView::FITCUR
1654                                | BufferView::CHANGE);
1655                         setState();
1656                 }
1657         }
1658         break;
1659
1660         case LFUN_LANGUAGE:
1661                 lang(bv_, argument);
1662                 setState();
1663                 owner_->showState();
1664                 break;
1665
1666         case LFUN_EMPH:
1667                 emph(bv_);
1668                 owner_->showState();
1669                 break;
1670
1671         case LFUN_BOLD:
1672                 bold(bv_);
1673                 owner_->showState();
1674                 break;
1675
1676         case LFUN_NOUN:
1677                 noun(bv_);
1678                 owner_->showState();
1679                 break;
1680
1681         case LFUN_CODE:
1682                 code(bv_);
1683                 owner_->showState();
1684                 break;
1685
1686         case LFUN_SANS:
1687                 sans(bv_);
1688                 owner_->showState();
1689                 break;
1690
1691         case LFUN_ROMAN:
1692                 roman(bv_);
1693                 owner_->showState();
1694                 break;
1695
1696         case LFUN_DEFAULT:
1697                 styleReset(bv_);
1698                 owner_->showState();
1699                 break;
1700
1701         case LFUN_UNDERLINE:
1702                 underline(bv_);
1703                 owner_->showState();
1704                 break;
1705
1706         case LFUN_FONT_SIZE:
1707                 fontSize(bv_, argument);
1708                 owner_->showState();
1709                 break;
1710
1711         case LFUN_FONT_STATE:
1712                 owner_->getLyXFunc()->setMessage(currentState(bv_));
1713                 break;
1714
1715         case LFUN_UPCASE_WORD:
1716         {
1717                 LyXText * lt = bv_->getLyXText();
1718
1719                 update(lt,
1720                        BufferView::SELECT
1721                        | BufferView::FITCUR);
1722                 lt->changeCase(bv_, LyXText::text_uppercase);
1723                 if (lt->inset_owner)
1724                         updateInset(lt->inset_owner, true);
1725                 update(lt,
1726                        BufferView::SELECT
1727                        | BufferView::FITCUR
1728                        | BufferView::CHANGE);
1729         }
1730         break;
1731
1732         case LFUN_LOWCASE_WORD:
1733         {
1734                 LyXText * lt = bv_->getLyXText();
1735
1736                 update(lt, BufferView::SELECT|BufferView::FITCUR);
1737                 lt->changeCase(bv_, LyXText::text_lowercase);
1738                 if (lt->inset_owner)
1739                         updateInset(lt->inset_owner, true);
1740                 update(lt,
1741                        BufferView::SELECT
1742                        | BufferView::FITCUR
1743                        | BufferView::CHANGE);
1744         }
1745         break;
1746
1747         case LFUN_CAPITALIZE_WORD:
1748         {
1749                 LyXText * lt = bv_->getLyXText();
1750
1751                 update(lt, BufferView::SELECT|BufferView::FITCUR);
1752                 lt->changeCase(bv_, LyXText::text_capitalization);
1753                 if (lt->inset_owner)
1754                         updateInset(lt->inset_owner, true);
1755                 update(lt,
1756                        BufferView::SELECT
1757                        | BufferView::FITCUR
1758                        | BufferView::CHANGE);
1759         }
1760         break;
1761
1762         case LFUN_TRANSPOSE_CHARS:
1763         {
1764                 LyXText * lt = bv_->getLyXText();
1765
1766                 update(lt, BufferView::SELECT|BufferView::FITCUR);
1767                 lt->transposeChars(*bv_);
1768                 if (lt->inset_owner)
1769                         updateInset(lt->inset_owner, true);
1770                 update(lt,
1771                        BufferView::SELECT
1772                        | BufferView::FITCUR
1773                        | BufferView::CHANGE);
1774         }
1775         break;
1776
1777
1778         case LFUN_INSERT_LABEL:
1779                 MenuInsertLabel(bv_, argument);
1780                 break;
1781
1782         case LFUN_REF_INSERT:
1783                 if (argument.empty()) {
1784                         InsetCommandParams p("ref");
1785                         owner_->getDialogs()->createRef(p.getAsString());
1786                 } else {
1787                         InsetCommandParams p;
1788                         p.setFromString(argument);
1789
1790                         InsetRef * inset = new InsetRef(p, *buffer_);
1791                         if (!insertInset(inset))
1792                                 delete inset;
1793                         else
1794                                 updateInset(inset, true);
1795                 }
1796                 break;
1797
1798         case LFUN_BOOKMARK_SAVE:
1799                 savePosition(strToUnsignedInt(argument));
1800                 break;
1801
1802         case LFUN_BOOKMARK_GOTO:
1803                 restorePosition(strToUnsignedInt(argument));
1804                 break;
1805
1806         case LFUN_REF_GOTO:
1807         {
1808                 string label(argument);
1809                 if (label.empty()) {
1810                         InsetRef * inset =
1811                                 static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1812                         if (inset) {
1813                                 label = inset->getContents();
1814                                 savePosition(0);
1815                         }
1816                 }
1817
1818                 if (!label.empty()) {
1819                         //bv_->savePosition(0);
1820                         if (!bv_->gotoLabel(label))
1821                                 Alert::alert(_("Error"),
1822                                            _("Couldn't find this label"),
1823                                            _("in current document."));
1824                 }
1825         }
1826         break;
1827
1828                 // --- Cursor Movements -----------------------------
1829         case LFUN_RIGHT:
1830         {
1831                 LyXText * lt = bv_->getLyXText();
1832
1833                 bool is_rtl = lt->cursor.par()->isRightToLeftPar(buffer_->params);
1834                 if (!lt->selection.mark())
1835                         beforeChange(lt);
1836                 update(lt, BufferView::SELECT|BufferView::FITCUR);
1837                 if (is_rtl)
1838                         lt->cursorLeft(bv_, false);
1839                 if (lt->cursor.pos() < lt->cursor.par()->size()
1840                     && lt->cursor.par()->isInset(lt->cursor.pos())
1841                     && isHighlyEditableInset(lt->cursor.par()->getInset(lt->cursor.pos()))) {
1842                         Inset * tmpinset = lt->cursor.par()->getInset(lt->cursor.pos());
1843                         owner_->getLyXFunc()->setMessage(tmpinset->editMessage());
1844                         if (is_rtl)
1845                                 tmpinset->edit(bv_, false);
1846                         else
1847                                 tmpinset->edit(bv_);
1848                         break;
1849                 }
1850                 if (!is_rtl)
1851                         lt->cursorRight(bv_, false);
1852                 finishUndo();
1853                 moveCursorUpdate(false);
1854                 owner_->showState();
1855         }
1856         break;
1857
1858         case LFUN_LEFT:
1859         {
1860                 // This is soooo ugly. Isn`t it possible to make
1861                 // it simpler? (Lgb)
1862                 LyXText * lt = bv_->getLyXText();
1863                 bool const is_rtl = lt->cursor.par()->isRightToLeftPar(buffer_->params);
1864                 if (!lt->selection.mark())
1865                         beforeChange(lt);
1866                 update(lt, BufferView::SELECT|BufferView::FITCUR);
1867                 LyXCursor const cur = lt->cursor;
1868                 if (!is_rtl)
1869                         lt->cursorLeft(bv_, false);
1870                 if ((is_rtl || cur != lt->cursor) && // only if really moved!
1871                     lt->cursor.pos() < lt->cursor.par()->size() &&
1872                     lt->cursor.par()->isInset(lt->cursor.pos()) &&
1873                     isHighlyEditableInset(lt->cursor.par()->getInset(lt->cursor.pos()))) {
1874                         Inset * tmpinset = lt->cursor.par()->getInset(lt->cursor.pos());
1875                         owner_->getLyXFunc()->setMessage(tmpinset->editMessage());
1876                         if (is_rtl)
1877                                 tmpinset->edit(bv_);
1878                         else
1879                                 tmpinset->edit(bv_, false);
1880                         break;
1881                 }
1882                 if  (is_rtl)
1883                         lt->cursorRight(bv_, false);
1884
1885                 finishUndo();
1886                 moveCursorUpdate(false);
1887                 owner_->showState();
1888         }
1889         break;
1890
1891         case LFUN_UP:
1892         {
1893                 LyXText * lt = bv_->getLyXText();
1894
1895                 if (!lt->selection.mark())
1896                         beforeChange(lt);
1897                 update(lt, BufferView::UPDATE);
1898                 lt->cursorUp(bv_);
1899                 finishUndo();
1900                 moveCursorUpdate(false);
1901                 owner_->showState();
1902         }
1903         break;
1904
1905         case LFUN_DOWN:
1906         {
1907                 LyXText * lt = bv_->getLyXText();
1908
1909                 if (!lt->selection.mark())
1910                         beforeChange(lt);
1911                 update(lt, BufferView::UPDATE);
1912                 lt->cursorDown(bv_);
1913                 finishUndo();
1914                 moveCursorUpdate(false);
1915                 owner_->showState();
1916         }
1917         break;
1918
1919         case LFUN_UP_PARAGRAPH:
1920         {
1921                 LyXText * lt = bv_->getLyXText();
1922
1923                 if (!lt->selection.mark())
1924                         beforeChange(lt);
1925                 update(lt, BufferView::UPDATE);
1926                 lt->cursorUpParagraph(bv_);
1927                 finishUndo();
1928                 moveCursorUpdate(false);
1929                 owner_->showState();
1930         }
1931         break;
1932
1933         case LFUN_DOWN_PARAGRAPH:
1934         {
1935                 LyXText * lt = bv_->getLyXText();
1936
1937                 if (!lt->selection.mark())
1938                         beforeChange(lt);
1939                 update(lt, BufferView::UPDATE);
1940                 lt->cursorDownParagraph(bv_);
1941                 finishUndo();
1942                 moveCursorUpdate(false);
1943                 owner_->showState();
1944         }
1945         break;
1946
1947         case LFUN_PRIOR:
1948         {
1949                 LyXText * lt = bv_->getLyXText();
1950
1951                 if (!lt->selection.mark())
1952                         beforeChange(lt);
1953                 update(lt, BufferView::UPDATE);
1954                 cursorPrevious(lt);
1955                 finishUndo();
1956                 moveCursorUpdate(false, false);
1957                 owner_->showState();
1958         }
1959         break;
1960
1961         case LFUN_NEXT:
1962         {
1963                 LyXText * lt = bv_->getLyXText();
1964
1965                 if (!lt->selection.mark())
1966                         beforeChange(lt);
1967                 update(lt, BufferView::UPDATE);
1968                 cursorNext(lt);
1969                 finishUndo();
1970                 moveCursorUpdate(false, false);
1971                 owner_->showState();
1972         }
1973         break;
1974
1975         case LFUN_HOME:
1976         {
1977                 LyXText * lt = bv_->getLyXText();
1978
1979                 if (!lt->selection.mark())
1980                         beforeChange(lt);
1981                 update(lt, BufferView::SELECT|BufferView::FITCUR);
1982                 lt->cursorHome(bv_);
1983                 finishUndo();
1984                 moveCursorUpdate(false);
1985                 owner_->showState();
1986         }
1987         break;
1988
1989         case LFUN_END:
1990         {
1991                 LyXText * lt = bv_->getLyXText();
1992
1993                 if (!lt->selection.mark())
1994                         beforeChange(lt);
1995                 update(lt,
1996                        BufferView::SELECT|BufferView::FITCUR);
1997                 lt->cursorEnd(bv_);
1998                 finishUndo();
1999                 moveCursorUpdate(false);
2000                 owner_->showState();
2001         }
2002         break;
2003
2004         case LFUN_SHIFT_TAB:
2005         case LFUN_TAB:
2006         {
2007                 LyXText * lt = bv_->getLyXText();
2008
2009                 if (!lt->selection.mark())
2010                         beforeChange(lt);
2011                 update(lt,
2012                        BufferView::SELECT|BufferView::FITCUR);
2013                 lt->cursorTab(bv_);
2014                 finishUndo();
2015                 moveCursorUpdate(false);
2016                 owner_->showState();
2017         }
2018         break;
2019
2020         case LFUN_WORDRIGHT:
2021         {
2022                 LyXText * lt = bv_->getLyXText();
2023
2024                 if (!lt->selection.mark())
2025                         beforeChange(lt);
2026                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2027                 if (lt->cursor.par()->isRightToLeftPar(buffer_->params))
2028                         lt->cursorLeftOneWord(bv_);
2029                 else
2030                         lt->cursorRightOneWord(bv_);
2031                 finishUndo();
2032                 moveCursorUpdate(false);
2033                 owner_->showState();
2034         }
2035         break;
2036
2037         case LFUN_WORDLEFT:
2038         {
2039                 LyXText * lt = bv_->getLyXText();
2040
2041                 if (!lt->selection.mark())
2042                         beforeChange(lt);
2043                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2044                 if (lt->cursor.par()->isRightToLeftPar(buffer_->params))
2045                         lt->cursorRightOneWord(bv_);
2046                 else
2047                         lt->cursorLeftOneWord(bv_);
2048                 finishUndo();
2049                 moveCursorUpdate(false);
2050                 owner_->showState();
2051         }
2052         break;
2053
2054         case LFUN_BEGINNINGBUF:
2055         {
2056                 LyXText * lt = bv_->getLyXText();
2057
2058                 if (!lt->selection.mark())
2059                         beforeChange(lt);
2060                 update(lt,
2061                        BufferView::SELECT|BufferView::FITCUR);
2062                 lt->cursorTop(bv_);
2063                 finishUndo();
2064                 moveCursorUpdate(false);
2065                 owner_->showState();
2066         }
2067         break;
2068
2069         case LFUN_ENDBUF:
2070         {
2071                 LyXText * lt = bv_->getLyXText();
2072
2073                 if (!lt->selection.mark())
2074                         beforeChange(lt);
2075                 update(lt,
2076                        BufferView::SELECT|BufferView::FITCUR);
2077                 lt->cursorBottom(bv_);
2078                 finishUndo();
2079                 moveCursorUpdate(false);
2080                 owner_->showState();
2081         }
2082         break;
2083
2084                 /* cursor selection ---------------------------- */
2085         case LFUN_RIGHTSEL:
2086         {
2087                 LyXText * lt = bv_->getLyXText();
2088
2089                 update(lt,
2090                        BufferView::SELECT|BufferView::FITCUR);
2091                 if (lt->cursor.par()->isRightToLeftPar(buffer_->params))
2092                         lt->cursorLeft(bv_);
2093                 else
2094                         lt->cursorRight(bv_);
2095                 finishUndo();
2096                 moveCursorUpdate(true);
2097                 owner_->showState();
2098         }
2099         break;
2100
2101         case LFUN_LEFTSEL:
2102         {
2103                 LyXText * lt = bv_->getLyXText();
2104
2105                 update(lt,
2106                        BufferView::SELECT|BufferView::FITCUR);
2107                 if (lt->cursor.par()->isRightToLeftPar(buffer_->params))
2108                         lt->cursorRight(bv_);
2109                 else
2110                         lt->cursorLeft(bv_);
2111                 finishUndo();
2112                 moveCursorUpdate(true);
2113                 owner_->showState();
2114         }
2115         break;
2116
2117         case LFUN_UPSEL:
2118         {
2119                 LyXText * lt = bv_->getLyXText();
2120
2121                 update(lt,
2122                        BufferView::SELECT|BufferView::FITCUR);
2123                 lt->cursorUp(bv_, true);
2124                 finishUndo();
2125                 moveCursorUpdate(true);
2126                 owner_->showState();
2127         }
2128         break;
2129
2130         case LFUN_DOWNSEL:
2131         {
2132                 LyXText * lt = bv_->getLyXText();
2133
2134                 update(lt,
2135                        BufferView::SELECT|BufferView::FITCUR);
2136                 lt->cursorDown(bv_, true);
2137                 finishUndo();
2138                 moveCursorUpdate(true);
2139                 owner_->showState();
2140         }
2141         break;
2142
2143         case LFUN_UP_PARAGRAPHSEL:
2144         {
2145                 LyXText * lt = bv_->getLyXText();
2146
2147                 update(lt,
2148                        BufferView::SELECT|BufferView::FITCUR);
2149                 lt->cursorUpParagraph(bv_);
2150                 finishUndo();
2151                 moveCursorUpdate(true);
2152                 owner_->showState();
2153         }
2154         break;
2155
2156         case LFUN_DOWN_PARAGRAPHSEL:
2157         {
2158                 LyXText * lt = bv_->getLyXText();
2159
2160                 update(lt,
2161                        BufferView::SELECT|BufferView::FITCUR);
2162                 lt->cursorDownParagraph(bv_);
2163                 finishUndo();
2164                 moveCursorUpdate(true);
2165                 owner_->showState();
2166         }
2167         break;
2168
2169         case LFUN_PRIORSEL:
2170         {
2171                 LyXText * lt = bv_->getLyXText();
2172
2173                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2174                 cursorPrevious(lt);
2175                 finishUndo();
2176                 moveCursorUpdate(true);
2177                 owner_->showState();
2178         }
2179         break;
2180
2181         case LFUN_NEXTSEL:
2182         {
2183                 LyXText * lt = bv_->getLyXText();
2184
2185                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2186                 cursorNext(lt);
2187                 finishUndo();
2188                 moveCursorUpdate(true);
2189                 owner_->showState();
2190         }
2191         break;
2192
2193         case LFUN_HOMESEL:
2194         {
2195                 LyXText * lt = bv_->getLyXText();
2196
2197                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2198                 lt->cursorHome(bv_);
2199                 finishUndo();
2200                 moveCursorUpdate(true);
2201                 owner_->showState();
2202         }
2203         break;
2204
2205         case LFUN_ENDSEL:
2206         {
2207                 LyXText * lt = bv_->getLyXText();
2208
2209                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2210                 lt->cursorEnd(bv_);
2211                 finishUndo();
2212                 moveCursorUpdate(true);
2213                 owner_->showState();
2214         }
2215         break;
2216
2217         case LFUN_WORDRIGHTSEL:
2218         {
2219                 LyXText * lt = bv_->getLyXText();
2220
2221                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2222                 if (lt->cursor.par()->isRightToLeftPar(buffer_->params))
2223                         lt->cursorLeftOneWord(bv_);
2224                 else
2225                         lt->cursorRightOneWord(bv_);
2226                 finishUndo();
2227                 moveCursorUpdate(true);
2228                 owner_->showState();
2229         }
2230         break;
2231
2232         case LFUN_WORDLEFTSEL:
2233         {
2234                 LyXText * lt = bv_->getLyXText();
2235
2236                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2237                 if (lt->cursor.par()->isRightToLeftPar(buffer_->params))
2238                         lt->cursorRightOneWord(bv_);
2239                 else
2240                         lt->cursorLeftOneWord(bv_);
2241                 finishUndo();
2242                 moveCursorUpdate(true);
2243                 owner_->showState();
2244         }
2245         break;
2246
2247         case LFUN_BEGINNINGBUFSEL:
2248         {
2249                 LyXText * lt = bv_->getLyXText();
2250
2251                 if (lt->inset_owner)
2252                         break;
2253                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2254                 lt->cursorTop(bv_);
2255                 finishUndo();
2256                 moveCursorUpdate(true);
2257                 owner_->showState();
2258         }
2259         break;
2260
2261         case LFUN_ENDBUFSEL:
2262         {
2263                 LyXText * lt = bv_->getLyXText();
2264
2265                 if (lt->inset_owner)
2266                         break;
2267                 update(lt,
2268                        BufferView::SELECT|BufferView::FITCUR);
2269                 lt->cursorBottom(bv_);
2270                 finishUndo();
2271                 moveCursorUpdate(true);
2272                 owner_->showState();
2273         }
2274         break;
2275
2276                 // --- text changing commands ------------------------
2277         case LFUN_BREAKLINE:
2278         {
2279                 LyXText * lt = bv_->getLyXText();
2280
2281                 beforeChange(lt);
2282                 lt->insertChar(bv_, Paragraph::META_NEWLINE);
2283                 update(lt,
2284                        BufferView::SELECT
2285                        | BufferView::FITCUR
2286                        | BufferView::CHANGE);
2287                 lt->setCursor(bv_, lt->cursor.par(), lt->cursor.pos());
2288                 moveCursorUpdate(false);
2289         }
2290         break;
2291
2292         case LFUN_PROTECTEDSPACE:
2293         {
2294                 LyXText * lt = bv_->getLyXText();
2295                 LyXLayout_ptr const & style = lt->cursor.par()->layout();
2296
2297                 if (style->free_spacing) {
2298                         lt->insertChar(bv_, ' ');
2299                         update(lt,
2300                                BufferView::SELECT
2301                                | BufferView::FITCUR
2302                                | BufferView::CHANGE);
2303                 } else {
2304                         specialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
2305                 }
2306                 moveCursorUpdate(false);
2307         }
2308         break;
2309
2310         case LFUN_SETMARK:
2311         {
2312                 LyXText * lt = bv_->getLyXText();
2313
2314                 if (lt->selection.mark()) {
2315                         beforeChange(lt);
2316                         update(lt,
2317                                BufferView::SELECT
2318                                | BufferView::FITCUR);
2319                         owner_->getLyXFunc()->setMessage(N_("Mark removed"));
2320                 } else {
2321                         beforeChange(lt);
2322                         lt->selection.mark(true);
2323                         update(lt,
2324                                BufferView::SELECT
2325                                | BufferView::FITCUR);
2326                         owner_->getLyXFunc()->setMessage(N_("Mark set"));
2327                 }
2328                 lt->selection.cursor = lt->cursor;
2329         }
2330         break;
2331
2332         case LFUN_DELETE:
2333         {
2334                 LyXText * lt = bv_->getLyXText();
2335
2336                 if (!lt->selection.set()) {
2337                         lt->Delete(bv_);
2338                         lt->selection.cursor = lt->cursor;
2339                         update(lt,
2340                                BufferView::SELECT
2341                                | BufferView::FITCUR
2342                                | BufferView::CHANGE);
2343                         // It is possible to make it a lot faster still
2344                         // just comment out the line below...
2345                         showCursor();
2346                 } else {
2347                         bv_->cut(false);
2348                 }
2349                 moveCursorUpdate(false);
2350                 owner_->showState();
2351                 setState();
2352         }
2353         break;
2354
2355         case LFUN_DELETE_SKIP:
2356         {
2357                 LyXText * lt = bv_->getLyXText();
2358
2359                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
2360
2361                 LyXCursor cursor = lt->cursor;
2362
2363                 if (!lt->selection.set()) {
2364                         if (cursor.pos() == cursor.par()->size()) {
2365                                 lt->cursorRight(bv_);
2366                                 cursor = lt->cursor;
2367                                 if (cursor.pos() == 0
2368                                     && !(cursor.par()->params().spaceTop()
2369                                          == VSpace (VSpace::NONE))) {
2370                                         lt->setParagraph
2371                                                 (bv_,
2372                                                  cursor.par()->params().lineTop(),
2373                                                  cursor.par()->params().lineBottom(),
2374                                                  cursor.par()->params().pagebreakTop(),
2375                                                  cursor.par()->params().pagebreakBottom(),
2376                                                  VSpace(VSpace::NONE),
2377                                                  cursor.par()->params().spaceBottom(),
2378                                                  cursor.par()->params().spacing(),
2379                                                  cursor.par()->params().align(),
2380                                                  cursor.par()->params().labelWidthString(), 0);
2381                                         lt->cursorLeft(bv_);
2382                                         update(lt,
2383                                                BufferView::SELECT
2384                                                | BufferView::FITCUR
2385                                                | BufferView::CHANGE);
2386                                 } else {
2387                                         lt->cursorLeft(bv_);
2388                                         lt->Delete(bv_);
2389                                         lt->selection.cursor = lt->cursor;
2390                                         update(lt,
2391                                                BufferView::SELECT
2392                                                | BufferView::FITCUR
2393                                                | BufferView::CHANGE);
2394                                 }
2395                         } else {
2396                                 lt->Delete(bv_);
2397                                 lt->selection.cursor = lt->cursor;
2398                                 update(lt,
2399                                        BufferView::SELECT
2400                                        | BufferView::FITCUR
2401                                        | BufferView::CHANGE);
2402                         }
2403                 } else {
2404                         bv_->cut(false);
2405                 }
2406         }
2407         break;
2408
2409         /* -------> Delete word forward. */
2410         case LFUN_DELETE_WORD_FORWARD:
2411                 update(bv_->getLyXText(), BufferView::SELECT|BufferView::FITCUR);
2412                 bv_->getLyXText()->deleteWordForward(bv_);
2413                 update(bv_->getLyXText(), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2414                 moveCursorUpdate(false);
2415                 owner_->showState();
2416                 break;
2417
2418                 /* -------> Delete word backward. */
2419         case LFUN_DELETE_WORD_BACKWARD:
2420         {
2421                 LyXText * lt = bv_->getLyXText();
2422
2423                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2424                 lt->deleteWordBackward(bv_);
2425                 update(lt,
2426                        BufferView::SELECT
2427                        | BufferView::FITCUR
2428                        | BufferView::CHANGE);
2429                 moveCursorUpdate(false);
2430                 owner_->showState();
2431         }
2432         break;
2433
2434                 /* -------> Kill to end of line. */
2435         case LFUN_DELETE_LINE_FORWARD:
2436         {
2437                 LyXText * lt = bv_->getLyXText();
2438
2439                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2440                 lt->deleteLineForward(bv_);
2441                 update(lt,
2442                        BufferView::SELECT
2443                        | BufferView::FITCUR
2444                        | BufferView::CHANGE);
2445                 moveCursorUpdate(false);
2446         }
2447         break;
2448
2449                 /* -------> Set mark off. */
2450         case LFUN_MARK_OFF:
2451         {
2452                 LyXText * lt = bv_->getLyXText();
2453
2454                 beforeChange(lt);
2455                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2456                 lt->selection.cursor = lt->cursor;
2457                 owner_->getLyXFunc()->setMessage(N_("Mark off"));
2458         }
2459         break;
2460
2461                 /* -------> Set mark on. */
2462         case LFUN_MARK_ON:
2463         {
2464                 LyXText * lt = bv_->getLyXText();
2465
2466                 beforeChange(lt);
2467                 lt->selection.mark(true);
2468                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2469                 lt->selection.cursor = lt->cursor;
2470                 owner_->getLyXFunc()->setMessage(N_("Mark on"));
2471         }
2472         break;
2473
2474         case LFUN_BACKSPACE:
2475         {
2476                 LyXText * lt = bv_->getLyXText();
2477
2478                 if (!lt->selection.set()) {
2479                         if (owner_->getIntl()->getTransManager().backspace()) {
2480                                 lt->backspace(bv_);
2481                                 lt->selection.cursor = lt->cursor;
2482                                 update(lt,
2483                                        BufferView::SELECT
2484                                        | BufferView::FITCUR
2485                                        | BufferView::CHANGE);
2486                                 // It is possible to make it a lot faster still
2487                                 // just comment out the line below...
2488                                 showCursor();
2489                         }
2490                 } else {
2491                         bv_->cut(false);
2492                 }
2493                 owner_->showState();
2494                 setState();
2495         }
2496         break;
2497
2498         case LFUN_BACKSPACE_SKIP:
2499         {
2500                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
2501                 LyXText * lt = bv_->getLyXText();
2502
2503                 LyXCursor cursor = lt->cursor;
2504
2505                 if (!lt->selection.set()) {
2506                         if (cursor.pos() == 0
2507                             && !(cursor.par()->params().spaceTop()
2508                                  == VSpace (VSpace::NONE))) {
2509                                 lt->setParagraph
2510                                         (bv_,
2511                                          cursor.par()->params().lineTop(),
2512                                          cursor.par()->params().lineBottom(),
2513                                          cursor.par()->params().pagebreakTop(),
2514                                          cursor.par()->params().pagebreakBottom(),
2515                                          VSpace(VSpace::NONE), cursor.par()->params().spaceBottom(),
2516                                          cursor.par()->params().spacing(),
2517                                          cursor.par()->params().align(),
2518                                          cursor.par()->params().labelWidthString(), 0);
2519                                 update(lt,
2520                                        BufferView::SELECT
2521                                        | BufferView::FITCUR
2522                                        | BufferView::CHANGE);
2523                         } else {
2524                                 lt->backspace(bv_);
2525                                 lt->selection.cursor = cursor;
2526                                 update(lt,
2527                                        BufferView::SELECT
2528                                        | BufferView::FITCUR
2529                                        | BufferView::CHANGE);
2530                         }
2531                 } else
2532                         bv_->cut(false);
2533         }
2534         break;
2535
2536         case LFUN_BREAKPARAGRAPH:
2537         {
2538                 LyXText * lt = bv_->getLyXText();
2539
2540                 beforeChange(lt);
2541                 lt->breakParagraph(bv_, 0);
2542                 update(lt,
2543                        BufferView::SELECT
2544                        | BufferView::FITCUR
2545                        | BufferView::CHANGE);
2546                 lt->selection.cursor = lt->cursor;
2547                 setState();
2548                 owner_->showState();
2549                 break;
2550         }
2551
2552         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
2553         {
2554                 LyXText * lt = bv_->getLyXText();
2555
2556                 beforeChange(lt);
2557                 lt->breakParagraph(bv_, 1);
2558                 update(lt,
2559                        BufferView::SELECT
2560                        | BufferView::FITCUR
2561                        | BufferView::CHANGE);
2562                 lt->selection.cursor = lt->cursor;
2563                 setState();
2564                 owner_->showState();
2565                 break;
2566         }
2567
2568         case LFUN_BREAKPARAGRAPH_SKIP:
2569         {
2570                 // When at the beginning of a paragraph, remove
2571                 // indentation and add a "defskip" at the top.
2572                 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
2573                 LyXText * lt = bv_->getLyXText();
2574
2575                 LyXCursor cursor = lt->cursor;
2576
2577                 beforeChange(lt);
2578                 if (cursor.pos() == 0) {
2579                         if (cursor.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
2580                                 lt->setParagraph
2581                                         (bv_,
2582                                          cursor.par()->params().lineTop(),
2583                                          cursor.par()->params().lineBottom(),
2584                                          cursor.par()->params().pagebreakTop(),
2585                                          cursor.par()->params().pagebreakBottom(),
2586                                          VSpace(VSpace::DEFSKIP), cursor.par()->params().spaceBottom(),
2587                                          cursor.par()->params().spacing(),
2588                                          cursor.par()->params().align(),
2589                                          cursor.par()->params().labelWidthString(), 1);
2590                                 //update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2591                         }
2592                 }
2593                 else {
2594                         lt->breakParagraph(bv_, 0);
2595                         //update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2596                 }
2597
2598                 update(lt,
2599                        BufferView::SELECT
2600                        | BufferView::FITCUR
2601                        | BufferView::CHANGE);
2602                 lt->selection.cursor = cursor;
2603                 setState();
2604                 owner_->showState();
2605         }
2606         break;
2607
2608         case LFUN_PARAGRAPH_SPACING:
2609         {
2610                 LyXText * lt = bv_->getLyXText();
2611
2612                 Paragraph * par = lt->cursor.par();
2613                 Spacing::Space cur_spacing = par->params().spacing().getSpace();
2614                 float cur_value = 1.0;
2615                 if (cur_spacing == Spacing::Other) {
2616                         cur_value = par->params().spacing().getValue();
2617                 }
2618
2619                 istringstream istr(argument.c_str());
2620
2621                 string tmp;
2622                 istr >> tmp;
2623                 Spacing::Space new_spacing = cur_spacing;
2624                 float new_value = cur_value;
2625                 if (tmp.empty()) {
2626                         lyxerr << "Missing argument to `paragraph-spacing'"
2627                                << endl;
2628                 } else if (tmp == "single") {
2629                         new_spacing = Spacing::Single;
2630                 } else if (tmp == "onehalf") {
2631                         new_spacing = Spacing::Onehalf;
2632                 } else if (tmp == "double") {
2633                         new_spacing = Spacing::Double;
2634                 } else if (tmp == "other") {
2635                         new_spacing = Spacing::Other;
2636                         float tmpval = 0.0;
2637                         istr >> tmpval;
2638                         lyxerr << "new_value = " << tmpval << endl;
2639                         if (tmpval != 0.0)
2640                                 new_value = tmpval;
2641                 } else if (tmp == "default") {
2642                         new_spacing = Spacing::Default;
2643                 } else {
2644                         lyxerr << _("Unknown spacing argument: ")
2645                                << argument << endl;
2646                 }
2647                 if (cur_spacing != new_spacing || cur_value != new_value) {
2648                         par->params().spacing(Spacing(new_spacing, new_value));
2649                         lt->redoParagraph(bv_);
2650                         update(lt,
2651                                BufferView::SELECT
2652                                | BufferView::FITCUR
2653                                | BufferView::CHANGE);
2654                 }
2655         }
2656         break;
2657
2658         case LFUN_INSET_TOGGLE:
2659         {
2660                 LyXText * lt = bv_->getLyXText();
2661                 hideCursor();
2662                 beforeChange(lt);
2663                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2664                 lt->toggleInset(bv_);
2665                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2666                 setState();
2667         }
2668                 break;
2669
2670         case LFUN_QUOTE:
2671                 smartQuote();
2672                 break;
2673
2674         case LFUN_HTMLURL:
2675         case LFUN_URL:
2676         {
2677                 InsetCommandParams p;
2678                 if (action == LFUN_HTMLURL)
2679                         p.setCmdName("htmlurl");
2680                 else
2681                         p.setCmdName("url");
2682                 owner_->getDialogs()->createUrl(p.getAsString());
2683         }
2684         break;
2685
2686         case LFUN_INSERT_URL:
2687         {
2688                 InsetCommandParams p;
2689                 p.setFromString(argument);
2690
2691                 InsetUrl * inset = new InsetUrl(p);
2692                 if (!insertInset(inset))
2693                         delete inset;
2694                 else
2695                         updateInset(inset, true);
2696         }
2697         break;
2698
2699         case LFUN_INSET_ERT:
2700                 insertAndEditInset(new InsetERT(buffer_->params));
2701                 break;
2702
2703         case LFUN_INSET_EXTERNAL:
2704                 insertAndEditInset(new InsetExternal);
2705                 break;
2706
2707         case LFUN_INSET_FOOTNOTE:
2708                 insertAndEditInset(new InsetFoot(buffer_->params));
2709                 break;
2710
2711         case LFUN_INSET_MARGINAL:
2712                 insertAndEditInset(new InsetMarginal(buffer_->params));
2713                 break;
2714
2715         case LFUN_INSET_MINIPAGE:
2716                 insertAndEditInset(new InsetMinipage(buffer_->params));
2717                 break;
2718
2719         case LFUN_INSERT_NOTE:
2720                 insertAndEditInset(new InsetNote(buffer_->params));
2721                 break;
2722
2723         case LFUN_INSET_FLOAT:
2724                 // check if the float type exist
2725                 if (floatList.typeExist(argument)) {
2726                         insertAndEditInset(new InsetFloat(buffer_->params,
2727                                                           argument));
2728                 } else {
2729                         lyxerr << "Non-existent float type: "
2730                                << argument << endl;
2731                 }
2732                 break;
2733
2734         case LFUN_INSET_WIDE_FLOAT:
2735                 // check if the float type exist
2736                 if (floatList.typeExist(argument)) {
2737                         InsetFloat * new_inset =
2738                                 new InsetFloat(buffer_->params, argument);
2739                         new_inset->wide(true);
2740                         insertAndEditInset(new_inset);
2741                 } else {
2742                         lyxerr << "Non-existent float type: "
2743                                << argument << endl;
2744                 }
2745                 break;
2746
2747 #if 0
2748         case LFUN_INSET_LIST:
2749                 insertAndEditInset(new InsetList);
2750                 break;
2751
2752         case LFUN_INSET_THEOREM:
2753                 insertAndEditInset(new InsetTheorem);
2754                 break;
2755 #endif
2756
2757         case LFUN_INSET_CAPTION:
2758         {
2759                 // Do we have a locking inset...
2760                 if (bv_->theLockingInset()) {
2761                         lyxerr << "Locking inset code: "
2762                                << static_cast<int>(bv_->theLockingInset()->lyxCode());
2763                         InsetCaption * new_inset =
2764                                 new InsetCaption(buffer_->params);
2765                         new_inset->setOwner(bv_->theLockingInset());
2766                         new_inset->setAutoBreakRows(true);
2767                         new_inset->setDrawFrame(0, InsetText::LOCKED);
2768                         new_inset->setFrameColor(0, LColor::captionframe);
2769                         if (insertInset(new_inset))
2770                                 new_inset->edit(bv_);
2771                         else
2772                                 delete new_inset;
2773                 }
2774         }
2775         break;
2776
2777         case LFUN_INSET_TABULAR:
2778         {
2779                 int r = 2;
2780                 int c = 2;
2781                 if (!argument.empty())
2782                         ::sscanf(argument.c_str(),"%d%d", &r, &c);
2783                 InsetTabular * new_inset =
2784                         new InsetTabular(*buffer_, r, c);
2785                 bool const rtl =
2786                         bv_->getLyXText()->real_current_font.isRightToLeft();
2787                 if (!open_new_inset(new_inset, rtl))
2788                         delete new_inset;
2789         }
2790         break;
2791
2792         // --- lyxserver commands ----------------------------
2793
2794         case LFUN_CHARATCURSOR:
2795         {
2796                 pos_type pos = bv_->getLyXText()->cursor.pos();
2797                 if (pos < bv_->getLyXText()->cursor.par()->size())
2798                         owner_->getLyXFunc()->setMessage(
2799                                 tostr(bv_->getLyXText()->cursor.par()->getChar(pos)));
2800                 else
2801                         owner_->getLyXFunc()->setMessage("EOF");
2802         }
2803         break;
2804
2805         case LFUN_GETXY:
2806                 owner_->getLyXFunc()->setMessage(tostr(bv_->getLyXText()->cursor.x())
2807                                                  + ' '
2808                                                  + tostr(bv_->getLyXText()->cursor.y()));
2809                 break;
2810
2811         case LFUN_SETXY:
2812         {
2813                 int x = 0;
2814                 int y = 0;
2815                 if (::sscanf(argument.c_str(), " %d %d", &x, &y) != 2) {
2816                         lyxerr << "SETXY: Could not parse coordinates in '"
2817                                << argument << std::endl;
2818                 }
2819                 bv_->getLyXText()->setCursorFromCoordinates(bv_, x, y);
2820         }
2821         break;
2822
2823         case LFUN_GETLAYOUT:
2824                 owner_->getLyXFunc()->setMessage(tostr(bv_->getLyXText()->cursor.par()->layout()));
2825                 break;
2826
2827         case LFUN_GETFONT:
2828         {
2829                 LyXFont & font = bv_->getLyXText()->current_font;
2830                 if (font.shape() == LyXFont::ITALIC_SHAPE)
2831                         owner_->getLyXFunc()->setMessage("E");
2832                 else if (font.shape() == LyXFont::SMALLCAPS_SHAPE)
2833                         owner_->getLyXFunc()->setMessage("N");
2834                 else
2835                         owner_->getLyXFunc()->setMessage("0");
2836
2837         }
2838         break;
2839
2840         // --- accented characters ---------------------------
2841
2842         case LFUN_UMLAUT:
2843         case LFUN_CIRCUMFLEX:
2844         case LFUN_GRAVE:
2845         case LFUN_ACUTE:
2846         case LFUN_TILDE:
2847         case LFUN_CEDILLA:
2848         case LFUN_MACRON:
2849         case LFUN_DOT:
2850         case LFUN_UNDERDOT:
2851         case LFUN_UNDERBAR:
2852         case LFUN_CARON:
2853         case LFUN_SPECIAL_CARON:
2854         case LFUN_BREVE:
2855         case LFUN_TIE:
2856         case LFUN_HUNG_UMLAUT:
2857         case LFUN_CIRCLE:
2858         case LFUN_OGONEK:
2859                 if (argument.empty()) {
2860                         // As always...
2861                         owner_->getLyXFunc()->handleKeyFunc(action);
2862                 } else {
2863                         owner_->getLyXFunc()->handleKeyFunc(action);
2864                         owner_->getIntl()->getTransManager()
2865                                 .TranslateAndInsert(argument[0], bv_->getLyXText());
2866                         update(bv_->getLyXText(),
2867                                BufferView::SELECT
2868                                | BufferView::FITCUR
2869                                | BufferView::CHANGE);
2870                 }
2871                 break;
2872
2873         case LFUN_MATH:
2874                 mathDispatch(bv_, argument);
2875                 break;
2876
2877         case LFUN_MATH_MACRO:
2878                 mathDispatchMathMacro(bv_, argument);
2879                 break;
2880
2881         case LFUN_MATH_DELIM:
2882                 mathDispatchMathDelim(bv_, argument);
2883                 break;
2884
2885         case LFUN_INSERT_MATRIX:
2886                 mathDispatchInsertMatrix(bv_, argument);
2887                 break;
2888
2889         case LFUN_INSERT_MATH:
2890                 mathDispatchInsertMath(bv_, argument);
2891                 break;
2892
2893         case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
2894                 mathDispatchMathImportSelection(bv_, argument);
2895                 break;
2896
2897         case LFUN_MATH_DISPLAY:          // Open or create a displayed math inset
2898                 mathDispatchMathDisplay(bv_, argument);
2899                 break;
2900
2901         case LFUN_MATH_MODE:             // Open or create an inlined math inset
2902                 mathDispatchMathMode(bv_, argument);
2903                 break;
2904
2905         case LFUN_GREEK:                 // Insert a single greek letter
2906                 mathDispatchGreek(bv_, argument);
2907                 break;
2908
2909         case LFUN_CITATION_INSERT:
2910         {
2911                 InsetCommandParams p;
2912                 p.setFromString(argument);
2913
2914                 InsetCitation * inset = new InsetCitation(p);
2915                 if (!insertInset(inset))
2916                         delete inset;
2917                 else
2918                         updateInset(inset, true);
2919         }
2920         break;
2921
2922         case LFUN_INSERT_BIBTEX:
2923         {
2924                 // ale970405+lasgoutt970425
2925                 // The argument can be up to two tokens separated
2926                 // by a space. The first one is the bibstyle.
2927                 string const db       = token(argument, ' ', 0);
2928                 string const bibstyle = token(argument, ' ', 1);
2929
2930                 InsetCommandParams p("BibTeX", db, bibstyle);
2931                 InsetBibtex * inset = new InsetBibtex(p);
2932
2933                 if (insertInset(inset)) {
2934                         if (argument.empty())
2935                                 inset->edit(bv_);
2936                 } else
2937                         delete inset;
2938         }
2939         break;
2940
2941         // BibTeX data bases
2942         case LFUN_BIBDB_ADD:
2943         {
2944                 InsetBibtex * inset =
2945                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2946                 if (inset) {
2947                         inset->addDatabase(argument);
2948                 }
2949         }
2950         break;
2951
2952         case LFUN_BIBDB_DEL:
2953         {
2954                 InsetBibtex * inset =
2955                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2956                 if (inset) {
2957                         inset->delDatabase(argument);
2958                 }
2959         }
2960         break;
2961
2962         case LFUN_BIBTEX_STYLE:
2963         {
2964                 InsetBibtex * inset =
2965                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2966                 if (inset) {
2967                         inset->setOptions(argument);
2968                 }
2969         }
2970         break;
2971
2972         case LFUN_INDEX_INSERT:
2973         {
2974                 string entry = argument;
2975                 if (entry.empty()) {
2976                         entry = bv_->getLyXText()->getStringToIndex(bv_);
2977                 }
2978
2979                 if (entry.empty()) {
2980                         owner_->getDialogs()->createIndex();
2981                         break;
2982                 }
2983
2984                 InsetIndex * inset = new InsetIndex(InsetCommandParams("index", entry));
2985
2986                 if (!insertInset(inset)) {
2987                         delete inset;
2988                 } else {
2989                         updateInset(inset, true);
2990                 }
2991         }
2992         break;
2993
2994         case LFUN_INDEX_PRINT:
2995         {
2996                 InsetCommandParams p("printindex");
2997                 Inset * inset = new InsetPrintIndex(p);
2998                 if (!insertInset(inset, tclass.defaultLayoutName()))
2999                         delete inset;
3000         }
3001         break;
3002
3003         case LFUN_PARENTINSERT:
3004         {
3005                 InsetCommandParams p("lyxparent", argument);
3006                 Inset * inset = new InsetParent(p, *buffer_);
3007                 if (!insertInset(inset, tclass.defaultLayoutName()))
3008                         delete inset;
3009         }
3010
3011         break;
3012
3013         case LFUN_CHILD_INSERT:
3014         {
3015                 InsetInclude::Params p;
3016                 p.cparams.setFromString(argument);
3017                 p.masterFilename_ = buffer_->fileName();
3018
3019                 InsetInclude * inset = new InsetInclude(p);
3020                 if (!insertInset(inset))
3021                         delete inset;
3022                 else {
3023                         updateInset(inset, true);
3024                         bv_->owner()->getDialogs()->showInclude(inset);
3025                 }
3026         }
3027         break;
3028
3029         case LFUN_FLOAT_LIST:
3030                 if (floatList.typeExist(argument)) {
3031                         Inset * inset = new InsetFloatList(argument);
3032                         if (!insertInset(inset, tclass.defaultLayoutName()))
3033                                 delete inset;
3034                 } else {
3035                         lyxerr << "Non-existent float type: "
3036                                << argument << endl;
3037                 }
3038                 break;
3039
3040         case LFUN_THESAURUS_ENTRY:
3041         {
3042                 string arg = argument;
3043
3044                 if (arg.empty()) {
3045                         arg = bv_->getLyXText()->selectionAsString(buffer_,
3046                                                                    false);
3047
3048                         // FIXME
3049                         if (arg.size() > 100 || arg.empty()) {
3050                                 // Get word or selection
3051                                 bv_->getLyXText()->selectWordWhenUnderCursor(bv_, LyXText::WHOLE_WORD);
3052                                 arg = bv_->getLyXText()->selectionAsString(buffer_, false);
3053                                 // FIXME: where is getLyXText()->unselect(bv_) ?
3054                         }
3055                 }
3056
3057                 bv_->owner()->getDialogs()->showThesaurus(arg);
3058         }
3059                 break;
3060
3061         case LFUN_SELFINSERT:
3062         {
3063                 if (argument.empty()) break;
3064
3065                 /* Automatically delete the currently selected
3066                  * text and replace it with what is being
3067                  * typed in now. Depends on lyxrc settings
3068                  * "auto_region_delete", which defaults to
3069                  * true (on). */
3070
3071                 LyXText * lt = bv_->getLyXText();
3072
3073                 if (lyxrc.auto_region_delete) {
3074                         if (lt->selection.set()) {
3075                                 lt->cutSelection(bv_, false, false);
3076                                 bv_->update(lt,
3077                                             BufferView::SELECT
3078                                             | BufferView::FITCUR
3079                                             | BufferView::CHANGE);
3080                         }
3081                         workarea().haveSelection(false);
3082                 }
3083
3084                 beforeChange(lt);
3085                 LyXFont const old_font(lt->real_current_font);
3086
3087                 string::const_iterator cit = argument.begin();
3088                 string::const_iterator end = argument.end();
3089                 for (; cit != end; ++cit) {
3090                         owner_->getIntl()->getTransManager().
3091                                 TranslateAndInsert(*cit, lt);
3092                 }
3093
3094                 bv_->update(lt,
3095                             BufferView::SELECT
3096                             | BufferView::FITCUR
3097                             | BufferView::CHANGE);
3098
3099                 lt->selection.cursor = lt->cursor;
3100                 moveCursorUpdate(false);
3101
3102                 // real_current_font.number can change so we need to
3103                 // update the minibuffer
3104                 if (old_font != lt->real_current_font)
3105                         owner_->showState();
3106                 //return string();
3107         }
3108         break;
3109
3110         case LFUN_DATE_INSERT:  // jdblair: date-insert cmd
3111         {
3112                 time_t now_time_t = time(NULL);
3113                 struct tm * now_tm = localtime(&now_time_t);
3114                 setlocale(LC_TIME, "");
3115                 string arg;
3116                 if (!argument.empty())
3117                         arg = argument;
3118                 else
3119                         arg = lyxrc.date_insert_format;
3120                 char datetmp[32];
3121                 int const datetmp_len =
3122                         ::strftime(datetmp, 32, arg.c_str(), now_tm);
3123
3124                 LyXText * lt = bv_->getLyXText();
3125
3126                 for (int i = 0; i < datetmp_len; i++) {
3127                         lt->insertChar(bv_, datetmp[i]);
3128                         update(lt,
3129                                BufferView::SELECT
3130                                | BufferView::FITCUR
3131                                | BufferView::CHANGE);
3132                 }
3133
3134                 lt->selection.cursor = lt->cursor;
3135                 moveCursorUpdate(false);
3136         }
3137         break;
3138
3139         case LFUN_UNKNOWN_ACTION:
3140                 owner_->getLyXFunc()->setErrorMessage(N_("Unknown function!"));
3141                 break;
3142
3143         default:
3144                 return false;
3145         } // end of switch
3146
3147         return true;
3148 }
3149
3150
3151 void BufferView::Pimpl::newline()
3152 {
3153         if (available()) {
3154                 LyXText * lt = bv_->getLyXText();
3155                 hideCursor();
3156                 update(lt,
3157                        BufferView::SELECT
3158                        | BufferView::FITCUR);
3159                 lt->insertChar(bv_, Paragraph::META_NEWLINE);
3160                 update(lt,
3161                        BufferView::SELECT
3162                        | BufferView::FITCUR
3163                        | BufferView::CHANGE);
3164         }
3165 }
3166
3167
3168 void BufferView::Pimpl::hfill()
3169 {
3170         if (available()) {
3171                 LyXText * lt = bv_->getLyXText();
3172                 hideCursor();
3173                 update(lt,
3174                        BufferView::SELECT
3175                        | BufferView::FITCUR);
3176                 lt->insertChar(bv_, Paragraph::META_HFILL);
3177                 update(lt,
3178                        BufferView::SELECT
3179                        | BufferView::FITCUR
3180                        | BufferView::CHANGE);
3181         }
3182 }
3183
3184
3185 void BufferView::Pimpl::specialChar(InsetSpecialChar::Kind kind)
3186 {
3187         if (available()) {
3188                 LyXText * lt = bv_->getLyXText();
3189
3190                 hideCursor();
3191                 update(lt, BufferView::SELECT|BufferView::FITCUR);
3192                 InsetSpecialChar * new_inset =
3193                         new InsetSpecialChar(kind);
3194                 if (!insertInset(new_inset))
3195                         delete new_inset;
3196                 else
3197                         updateInset(new_inset, true);
3198         }
3199 }
3200
3201
3202 void BufferView::Pimpl::smartQuote()
3203 {
3204         LyXText const * lt = bv_->getLyXText();
3205         Paragraph const * par = lt->cursor.par();
3206         pos_type pos = lt->cursor.pos();
3207         char c;
3208
3209         if (!pos
3210             || (par->isInset(pos - 1)
3211                 && par->getInset(pos - 1)->isSpace()))
3212                 c = ' ';
3213         else
3214                 c = par->getChar(pos - 1);
3215
3216
3217         hideCursor();
3218
3219         LyXLayout_ptr const & style = par->layout();
3220
3221         if (style->pass_thru ||
3222                 (!insertInset(new InsetQuotes(c, bv_->buffer()->params))))
3223                 bv_->owner()->getLyXFunc()->dispatch(LFUN_SELFINSERT, "\"");
3224 }
3225
3226
3227 void BufferView::Pimpl::insertAndEditInset(Inset * inset)
3228 {
3229         if (insertInset(inset))
3230                 inset->edit(bv_);
3231         else
3232                 delete inset;
3233 }
3234
3235
3236 // Open and lock an updatable inset
3237 bool BufferView::Pimpl::open_new_inset(UpdatableInset * new_inset, bool behind)
3238 {
3239         LyXText * lt = bv_->getLyXText();
3240
3241         beforeChange(lt);
3242         finishUndo();
3243         if (!insertInset(new_inset)) {
3244                 delete new_inset;
3245                 return false;
3246         }
3247         new_inset->edit(bv_, !behind);
3248         return true;
3249 }
3250
3251
3252 bool BufferView::Pimpl::insertInset(Inset * inset, string const & lout)
3253 {
3254         // if we are in a locking inset we should try to insert the
3255         // inset there otherwise this is a illegal function now
3256         if (bv_->theLockingInset()) {
3257                 if (bv_->theLockingInset()->insetAllowed(inset))
3258                     return bv_->theLockingInset()->insertInset(bv_, inset);
3259                 return false;
3260         }
3261
3262         // not quite sure if we want this...
3263         setCursorParUndo(bv_);
3264         freezeUndo();
3265
3266         beforeChange(bv_->text);
3267         if (!lout.empty()) {
3268                 update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
3269                 bv_->text->breakParagraph(bv_);
3270                 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
3271
3272                 if (bv_->text->cursor.par()->size()) {
3273                         bv_->text->cursorLeft(bv_);
3274
3275                         bv_->text->breakParagraph(bv_);
3276                         update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
3277                 }
3278
3279                 string lres = lout;
3280                 LyXTextClass const & tclass =
3281                         textclasslist[buffer_->params.textclass];
3282                 bool hasLayout = tclass.hasLayout(lres);
3283                 string lay = tclass.defaultLayoutName();
3284
3285                 if (hasLayout != false) {
3286                         // layout found
3287                         lay = lres;
3288                 } else {
3289                         // layout not fount using default
3290                         lay = tclass.defaultLayoutName();
3291                 }
3292
3293                 bv_->text->setLayout(bv_, lay);
3294
3295                 bv_->text->setParagraph(bv_, 0, 0,
3296                                    0, 0,
3297                                    VSpace(VSpace::NONE), VSpace(VSpace::NONE),
3298                                    Spacing(),
3299                                    LYX_ALIGN_LAYOUT,
3300                                    string(),
3301                                    0);
3302                 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
3303         }
3304
3305         bv_->text->insertInset(bv_, inset);
3306         update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
3307
3308         unFreezeUndo();
3309         return true;
3310 }
3311
3312
3313 void BufferView::Pimpl::updateInset(Inset * inset, bool mark_dirty)
3314 {
3315         if (!inset || !available())
3316                 return;
3317
3318         // first check for locking insets
3319         if (bv_->theLockingInset()) {
3320                 if (bv_->theLockingInset() == inset) {
3321                         if (bv_->text->updateInset(bv_, inset)) {
3322                                 update();
3323                                 if (mark_dirty) {
3324                                         buffer_->markDirty();
3325                                 }
3326                                 updateScrollbar();
3327                                 return;
3328                         }
3329                 } else if (bv_->theLockingInset()->updateInsetInInset(bv_, inset)) {
3330                         if (bv_->text->updateInset(bv_,  bv_->theLockingInset())) {
3331                                 update();
3332                                 if (mark_dirty) {
3333                                         buffer_->markDirty();
3334                                 }
3335                                 updateScrollbar();
3336                                 return;
3337                         }
3338                 }
3339         }
3340
3341         // then check if the inset is a top_level inset (has no owner)
3342         // if yes do the update as always otherwise we have to update the
3343         // toplevel inset where this inset is inside
3344         Inset * tl_inset = inset;
3345         while(tl_inset->owner())
3346                 tl_inset = tl_inset->owner();
3347         hideCursor();
3348         if (tl_inset == inset) {
3349                 update(bv_->text, BufferView::UPDATE);
3350                 if (bv_->text->updateInset(bv_, inset)) {
3351                         if (mark_dirty) {
3352                                 update(bv_->text,
3353                                        BufferView::SELECT
3354                                        | BufferView::FITCUR
3355                                        | BufferView::CHANGE);
3356                         } else {
3357                                 update(bv_->text, SELECT);
3358                         }
3359                         return;
3360                 }
3361         } else if (static_cast<UpdatableInset *>(tl_inset)
3362                            ->updateInsetInInset(bv_, inset))
3363         {
3364                         if (bv_->text->updateInset(bv_,  tl_inset)) {
3365                                 update();
3366                                 updateScrollbar();
3367                         }
3368         }
3369 }
3370
3371
3372 void BufferView::Pimpl::gotoInset(vector<Inset::Code> const & codes,
3373                                   bool same_content)
3374 {
3375         if (!available()) return;
3376
3377         hideCursor();
3378         beforeChange(bv_->text);
3379         update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
3380
3381         LyXCursor const & cursor = bv_->text->cursor;
3382
3383         string contents;
3384         if (same_content &&
3385             cursor.par()->isInset(cursor.pos())) {
3386                 Inset const * inset = cursor.par()->getInset(cursor.pos());
3387                 if (find(codes.begin(), codes.end(), inset->lyxCode())
3388                     != codes.end())
3389                         contents =
3390                                 static_cast<InsetCommand const *>(inset)->getContents();
3391         }
3392
3393
3394         if (!bv_->text->gotoNextInset(bv_, codes, contents)) {
3395                 if (bv_->text->cursor.pos()
3396                     || bv_->text->cursor.par() != bv_->text->ownerParagraph()) {
3397                         LyXCursor tmp = bv_->text->cursor;
3398                         bv_->text->cursor.par(bv_->text->ownerParagraph());
3399                         bv_->text->cursor.pos(0);
3400                         if (!bv_->text->gotoNextInset(bv_, codes, contents)) {
3401                                 bv_->text->cursor = tmp;
3402                                 bv_->owner()->message(_("No more insets"));
3403                         }
3404                 } else {
3405                         bv_->owner()->message(_("No more insets"));
3406                 }
3407         }
3408         update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
3409         bv_->text->selection.cursor = bv_->text->cursor;
3410 }
3411
3412
3413 void BufferView::Pimpl::gotoInset(Inset::Code code, bool same_content)
3414 {
3415         gotoInset(vector<Inset::Code>(1, code), same_content);
3416 }