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