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