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