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