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