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