]> git.lyx.org Git - lyx.git/blob - src/BufferView_pimpl.C
Changed the singleton implementation in GraphicsCache, the former version
[lyx.git] / src / BufferView_pimpl.C
1 #include <config.h>
2
3 #include <unistd.h>
4 #include <sys/wait.h>
5
6 #ifdef __GNUG__
7 #pragma implementation
8 #endif
9
10 #include "BufferView_pimpl.h"
11 #include "WorkArea.h"
12 #include "lyxscreen.h"
13 #include "lyxtext.h"
14 #include "lyxrow.h"
15 #include "LyXView.h"
16 #include "commandtags.h"
17 #include "lyxfunc.h"
18 #include "minibuffer.h"
19 #include "font.h"
20 #include "bufferview_funcs.h"
21 #include "TextCache.h"
22 #include "bufferlist.h"
23 #include "lyx_gui_misc.h"
24 #include "lyxrc.h"
25 #include "intl.h"
26 #include "support/LAssert.h"
27 #include "frontends/Dialogs.h"
28 #include "insets/insetbib.h"
29 #include "insets/insettext.h"
30 /// added for Dispatch functions
31 #include "lyx_cb.h"
32 #include "filedlg.h"
33 #include "lyx_main.h"
34 #include "FloatList.h"
35 #include "support/filetools.h"
36 #include "support/lyxfunctional.h"
37 #include "insets/inseturl.h"
38 #include "insets/insetlatexaccent.h"
39 #include "insets/insettoc.h"
40 #include "insets/insetref.h"
41 #include "insets/insetparent.h"
42 #include "insets/insetindex.h"
43 #include "insets/insetinclude.h"
44 #include "insets/insetcite.h"
45 #include "insets/insetert.h"
46 #include "insets/insetexternal.h"
47 #include "insets/insetgraphics.h"
48 #include "insets/insetfoot.h"
49 #include "insets/insetmarginal.h"
50 #include "insets/insetminipage.h"
51 #include "insets/insetfloat.h"
52 #include "insets/insetlist.h"
53 #include "insets/insettabular.h"
54 #include "insets/insettheorem.h"
55 #include "insets/insetcaption.h"
56 #include "mathed/formulamacro.h"
57 extern LyXTextClass::size_type current_layout;
58 extern void math_insert_symbol(BufferView *, string const &);
59 extern bool math_insert_greek(BufferView *, char);
60 extern int greek_kb_flag;
61
62 #ifdef SIGC_CXX_NAMESPACES
63 using SigC::slot;
64 #endif
65
66 using std::find_if;
67 using std::pair;
68 using std::endl;
69 using std::make_pair;
70 using std::min;
71
72 /* the selection possible is needed, that only motion events are 
73  * used, where the bottom press event was on the drawing area too */
74 bool selection_possible = false;
75
76 extern BufferList bufferlist;
77 extern char ascii_type;
78
79 extern void sigchldhandler(pid_t pid, int * status);
80 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
81
82 const unsigned int saved_positions_num = 20;
83
84 static inline
85 void waitForX()
86 {
87         XSync(fl_get_display(), 0);
88 }
89
90
91 static
92 void SetXtermCursor(Window win)
93 {
94         static Cursor cursor;
95         static bool cursor_undefined = true;
96         if (cursor_undefined){
97                 cursor = XCreateFontCursor(fl_get_display(), XC_xterm);
98                 XFlush(fl_get_display());
99                 cursor_undefined = false;
100         }
101         XDefineCursor(fl_get_display(), win, cursor);
102         XFlush(fl_get_display());
103 }
104
105
106 BufferView::Pimpl::Pimpl(BufferView * b, LyXView * o,
107              int xpos, int ypos, int width, int height)
108         : bv_(b), owner_(o), cursor_timeout(400)
109 {
110         buffer_ = 0;
111         workarea_ = new WorkArea(xpos, ypos, width, height);
112         // Setup the signals
113         workarea_->scrollCB.connect(slot(this, &BufferView::Pimpl::scrollCB));
114         workarea_->workAreaExpose
115                 .connect(slot(this, &BufferView::Pimpl::workAreaExpose));
116         workarea_->workAreaEnter
117                 .connect(slot(this, &BufferView::Pimpl::enterView));
118         workarea_->workAreaLeave
119                 .connect(slot(this, &BufferView::Pimpl::leaveView));
120         workarea_->workAreaButtonPress
121                 .connect(slot(this, &BufferView::Pimpl::workAreaButtonPress));
122         workarea_->workAreaButtonRelease
123                 .connect(slot(this,
124                               &BufferView::Pimpl::workAreaButtonRelease));
125         workarea_->workAreaMotionNotify
126                 .connect(slot(this, &BufferView::Pimpl::workAreaMotionNotify));
127         workarea_->workAreaDoubleClick
128                 .connect(slot(this, &BufferView::Pimpl::doubleClick));
129         workarea_->workAreaTripleClick
130                 .connect(slot(this, &BufferView::Pimpl::tripleClick));
131         workarea_->workAreaKeyPress
132                 .connect(slot(this, &BufferView::Pimpl::workAreaKeyPress));
133         
134         screen_ = 0;
135
136         cursor_timeout.timeout.connect(slot(this,
137                                             &BufferView::Pimpl::cursorToggle));
138         current_scrollbar_value = 0;
139         cursor_timeout.start();
140         workarea_->setFocus();
141         using_xterm_cursor = false;
142         saved_positions.resize(saved_positions_num);
143 }
144
145
146 Painter & BufferView::Pimpl::painter() 
147 {
148         return workarea_->getPainter();
149 }
150
151
152 void BufferView::Pimpl::buffer(Buffer * b)
153 {
154         lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
155                             << b << ")" << endl;
156         if (buffer_) {
157                 bv_->insetSleep();
158                 buffer_->delUser(bv_);
159
160                 // Put the old text into the TextCache, but
161                 // only if the buffer is still loaded.
162                 // Also set the owner of the test to 0
163                 //              bv_->text->owner(0);
164                 textcache.add(buffer_, workarea_->workWidth(), bv_->text);
165                 if (lyxerr.debugging())
166                         textcache.show(lyxerr, "BufferView::buffer");
167                 
168                 bv_->text = 0;
169         }
170
171         // Set current buffer
172         buffer_ = b;
173
174         if (bufferlist.getState() == BufferList::CLOSING) return;
175         
176         // Nuke old image
177         // screen is always deleted when the buffer is changed.
178         delete screen_;
179         screen_ = 0;
180
181         // If we are closing the buffer, use the first buffer as current
182         if (!buffer_) {
183                 buffer_ = bufferlist.first();
184         }
185
186         if (buffer_) {
187                 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
188                 buffer_->addUser(bv_);
189                 // If we don't have a text object for this, we make one
190                 if (bv_->text == 0) {
191                         resizeCurrentBuffer();
192                 } else {
193                         updateScreen();
194                         updateScrollbar();
195                 }
196                 bv_->text->first = screen_->TopCursorVisible(bv_->text);
197                 owner_->updateMenubar();
198                 owner_->updateToolbar();
199                 // Similarly, buffer-dependent dialogs should be updated or
200                 // hidden. This should go here because some dialogs (eg ToC)
201                 // require bv_->text.
202                 owner_->getDialogs()->updateBufferDependent(true);
203                 redraw();
204                 bv_->insetWakeup();
205         } else {
206                 lyxerr[Debug::INFO] << "  No Buffer!" << endl;
207                 owner_->updateMenubar();
208                 owner_->updateToolbar();
209                 owner_->getDialogs()->hideBufferDependent();
210                 updateScrollbar();
211                 workarea_->redraw();
212
213                 // Also remove all remaining text's from the testcache.
214                 // (there should not be any!) (if there is any it is a
215                 // bug!)
216                 if (lyxerr.debugging())
217                         textcache.show(lyxerr, "buffer delete all");
218                 textcache.clear();
219         }
220         // should update layoutchoice even if we don't have a buffer.
221         owner_->updateLayoutChoice();
222         owner_->getMiniBuffer()->Init();
223         owner_->updateWindowTitle();
224 }
225
226
227 void BufferView::Pimpl::resize(int xpos, int ypos, int width, int height)
228 {
229         workarea_->resize(xpos, ypos, width, height);
230         update(bv_->text, SELECT);
231         redraw();
232 }
233
234
235 void BufferView::Pimpl::resize()
236 {
237         if (buffer_)
238                 resizeCurrentBuffer();
239 }
240
241
242 void BufferView::Pimpl::redraw()
243 {
244         lyxerr[Debug::INFO] << "BufferView::redraw()" << endl;
245         workarea_->redraw();
246 }
247
248
249 bool BufferView::Pimpl::fitCursor(LyXText * text)
250 {
251         Assert(screen_);
252  
253         bool ret = screen_->FitCursor(text, bv_);
254         if (ret)
255             updateScrollbar();
256         return ret;
257 }
258
259
260 void BufferView::Pimpl::redoCurrentBuffer()
261 {
262         lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
263         if (buffer_ && bv_->text) {
264                 resize();
265                 owner_->updateLayoutChoice();
266         }
267 }
268
269
270 int BufferView::Pimpl::resizeCurrentBuffer()
271 {
272         lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
273         
274         LyXParagraph * par = 0;
275         LyXParagraph * selstartpar = 0;
276         LyXParagraph * selendpar = 0;
277         UpdatableInset * the_locking_inset = 0;
278         
279         int pos = 0;
280         int selstartpos = 0;
281         int selendpos = 0;
282         int selection = 0;
283         int mark_set = 0;
284
285         ProhibitInput(bv_);
286
287         owner_->getMiniBuffer()->Set(_("Formatting document..."));   
288
289         if (bv_->text) {
290                 par = bv_->text->cursor.par();
291                 pos = bv_->text->cursor.pos();
292                 selstartpar = bv_->text->sel_start_cursor.par();
293                 selstartpos = bv_->text->sel_start_cursor.pos();
294                 selendpar = bv_->text->sel_end_cursor.par();
295                 selendpos = bv_->text->sel_end_cursor.pos();
296                 selection = bv_->text->selection;
297                 mark_set = bv_->text->mark_set;
298                 the_locking_inset = bv_->text->the_locking_inset;
299                 delete bv_->text;
300                 bv_->text = new LyXText(bv_);
301         } else {
302                 // See if we have a text in TextCache that fits
303                 // the new buffer_ with the correct width.
304                 bv_->text = textcache.findFit(buffer_, workarea_->workWidth());
305                 if (bv_->text) {
306                         if (lyxerr.debugging()) {
307                                 lyxerr << "Found a LyXText that fits:\n";
308                                 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea_->workWidth(), bv_->text)));
309                         }
310                         // Set the owner of the newly found text
311                         //      bv_->text->owner(bv_);
312                         if (lyxerr.debugging())
313                                 textcache.show(lyxerr, "resizeCurrentBuffer");
314                 } else {
315                         bv_->text = new LyXText(bv_);
316                 }
317         }
318         updateScreen();
319
320         if (par) {
321                 bv_->text->selection = true;
322                 /* at this point just to avoid the Delete-Empty-Paragraph
323                  * Mechanism when setting the cursor */
324                 bv_->text->mark_set = mark_set;
325                 if (selection) {
326                         bv_->text->SetCursor(bv_, selstartpar, selstartpos);
327                         bv_->text->sel_cursor = bv_->text->cursor;
328                         bv_->text->SetCursor(bv_, selendpar, selendpos);
329                         bv_->text->SetSelection(bv_);
330                         bv_->text->SetCursor(bv_, par, pos);
331                 } else {
332                         bv_->text->SetCursor(bv_, par, pos);
333                         bv_->text->sel_cursor = bv_->text->cursor;
334                         bv_->text->selection = false;
335                 }
336                 // remake the inset locking
337                 bv_->text->the_locking_inset = the_locking_inset;
338         }
339         bv_->text->first = screen_->TopCursorVisible(bv_->text);
340         buffer_->resizeInsets(bv_);
341         // this will scroll the screen such that the cursor becomes visible
342         updateScrollbar();
343         redraw();
344         owner_->getMiniBuffer()->Init();
345         bv_->setState();
346         AllowInput(bv_);
347
348         owner_->getDialogs()->hideSplash();
349  
350         return 0;
351 }
352
353
354 void BufferView::Pimpl::updateScreen()
355 {
356         // Regenerate the screen.
357         delete screen_;
358         screen_ = new LyXScreen(*workarea_);
359 }
360
361
362 void BufferView::Pimpl::updateScrollbar()
363 {
364         /* If the text is smaller than the working area, the scrollbar
365          * maximum must be the working area height. No scrolling will 
366          * be possible */
367
368         if (!buffer_) {
369                 workarea_->setScrollbar(0, 1.0);
370                 return;
371         }
372         
373         static unsigned long max2 = 0;
374         static unsigned long height2 = 0;
375
376         unsigned long cbth = 0;
377         long cbsf = 0;
378
379         if (bv_->text) {
380                 cbth = bv_->text->height;
381                 cbsf = bv_->text->first;
382         }
383
384         // check if anything has changed.
385         if (max2 == cbth &&
386             height2 == workarea_->height() &&
387             current_scrollbar_value == cbsf)
388                 return; // no
389         max2 = cbth;
390         height2 = workarea_->height();
391         current_scrollbar_value = cbsf;
392
393         if (cbth <= height2) { // text is smaller than screen
394                 workarea_->setScrollbar(0, 1.0); // right?
395                 return;
396         }
397
398         long maximum_height = workarea_->height() * 3 / 4 + cbth;
399         long value = cbsf;
400
401         // set the scrollbar
402         double hfloat = workarea_->height();
403         double maxfloat = maximum_height;
404
405         float slider_size = 0.0;
406         int slider_value = value;
407
408         workarea_->setScrollbarBounds(0, bv_->text->height - workarea_->height());
409         double lineh = bv_->text->DefaultHeight();
410         workarea_->setScrollbarIncrements(lineh);
411         if (maxfloat > 0.0) {
412                 if ((hfloat / maxfloat) * float(height2) < 3)
413                         slider_size = 3.0/float(height2);
414                 else
415                         slider_size = hfloat / maxfloat;
416         } else
417                 slider_size = hfloat;
418
419         workarea_->setScrollbar(slider_value, slider_size / workarea_->height());
420 }
421
422
423 // Callback for scrollbar slider
424 void BufferView::Pimpl::scrollCB(double value)
425 {
426         if (!buffer_) return;
427
428         current_scrollbar_value = long(value);
429         if (current_scrollbar_value < 0)
430                 current_scrollbar_value = 0;
431    
432         if (!screen_)
433                 return;
434
435         screen_->Draw(bv_->text, bv_, current_scrollbar_value);
436
437         if (!lyxrc.cursor_follows_scrollbar) {
438                 waitForX();
439                 return;
440         }
441  
442         LyXText * vbt = bv_->text;
443  
444         int const height = vbt->DefaultHeight();
445         int const first = static_cast<int>((bv_->text->first + height));
446         int const last = static_cast<int>((bv_->text->first + workarea_->height() - height));
447
448         if (vbt->cursor.y() < first)
449                 vbt->SetCursorFromCoordinates(bv_, 0, first);
450         else if (vbt->cursor.y() > last)
451                 vbt->SetCursorFromCoordinates(bv_, 0, last);
452
453         waitForX();
454 }
455
456
457 int BufferView::Pimpl::scrollUp(long time)
458 {
459         if (!buffer_) return 0;
460         if (!screen_) return 0;
461    
462         double value = workarea_->getScrollbarValue();
463    
464         if (value == 0) return 0;
465
466         float add_value =  (bv_->text->DefaultHeight()
467                             + float(time) * float(time) * 0.125);
468    
469         if (add_value > workarea_->height())
470                 add_value = float(workarea_->height() -
471                                   bv_->text->DefaultHeight());
472    
473         value -= add_value;
474
475         if (value < 0)
476                 value = 0;
477    
478         workarea_->setScrollbarValue(value);
479    
480         scrollCB(value); 
481         return 0;
482 }
483
484
485 int BufferView::Pimpl::scrollDown(long time)
486 {
487         if (!buffer_) return 0;
488         if (!screen_) return 0;
489    
490         double value= workarea_->getScrollbarValue();
491         pair<float, float> p = workarea_->getScrollbarBounds();
492         double max = p.second;
493         
494         if (value == max) return 0;
495
496         float add_value =  (bv_->text->DefaultHeight()
497                             + float(time) * float(time) * 0.125);
498    
499         if (add_value > workarea_->height())
500                 add_value = float(workarea_->height() -
501                                   bv_->text->DefaultHeight());
502    
503         value += add_value;
504    
505         if (value > max)
506                 value = max;
507
508         workarea_->setScrollbarValue(value);
509         
510         scrollCB(value); 
511         return 0;
512 }
513
514
515 void BufferView::Pimpl::workAreaKeyPress(KeySym keysym, unsigned int state)
516 {
517         bv_->owner()->getLyXFunc()->processKeySym(keysym, state);
518 }
519
520
521 void BufferView::Pimpl::workAreaMotionNotify(int x, int y, unsigned int state)
522 {
523         // Only use motion with button 1
524         if (!(state & Button1MotionMask))
525                 return;
526
527         if (!buffer_ || !screen_) return;
528
529         // Check for inset locking
530         if (bv_->theLockingInset()) {
531                 LyXCursor cursor = bv_->text->cursor;
532                 LyXFont font = bv_->text->GetFont(bv_->buffer(),
533                                                   cursor.par(), cursor.pos());
534                 int width = bv_->theLockingInset()->width(bv_, font);
535                 int inset_x = font.isVisibleRightToLeft()
536                         ? cursor.x() - width : cursor.x();
537                 int start_x = inset_x + bv_->theLockingInset()->scroll();
538                 bv_->theLockingInset()->
539                         InsetMotionNotify(bv_,
540                                           x - start_x,
541                                           y - cursor.y() + bv_->text->first,
542                                           state);
543                 return;
544         }
545    
546         /* The test for not selection possible is needed, that only motion events are 
547          * used, where the bottom press event was on the drawing area too */
548         if (!selection_possible)
549                 return;
550  
551         screen_->HideCursor();
552
553         bv_->text->SetCursorFromCoordinates(bv_, x, y + bv_->text->first);
554       
555         if (!bv_->text->selection)
556                 update(bv_->text, BufferView::UPDATE); // Maybe an empty line was deleted
557       
558         bv_->text->SetSelection(bv_);
559         screen_->ToggleToggle(bv_->text, bv_);
560         fitCursor(bv_->text);
561         screen_->ShowCursor(bv_->text, bv_);
562 }
563
564
565 // Single-click on work area
566 void BufferView::Pimpl::workAreaButtonPress(int xpos, int ypos,
567                                             unsigned int button)
568 {
569         last_click_x = -1;
570         last_click_y = -1;
571
572         if (!buffer_ || !screen_) return;
573
574         Inset * inset_hit = checkInsetHit(bv_->text, xpos, ypos, button);
575
576         // ok ok, this is a hack.
577         if (button == 4 || button == 5) {
578                 switch (button) {
579                 case 4:
580                         scrollUp(lyxrc.wheel_jump); // default 100, set in lyxrc
581                         break;
582                 case 5:
583                         scrollDown(lyxrc.wheel_jump);
584                         break;
585                 }
586         }
587         
588         if (bv_->theLockingInset()) {
589                 // We are in inset locking mode
590                 
591                 /* Check whether the inset was hit. If not reset mode,
592                    otherwise give the event to the inset */
593                 if (inset_hit == bv_->theLockingInset()) {
594                         bv_->theLockingInset()->
595                                 InsetButtonPress(bv_,
596                                                  xpos, ypos,
597                                                  button);
598                         return;
599                 } else {
600                         bv_->unlockInset(bv_->theLockingInset());
601                 }
602         }
603         
604         if (!inset_hit)
605                 selection_possible = true;
606         screen_->HideCursor();
607
608         int const screen_first = bv_->text->first;
609         
610         // Middle button press pastes if we have a selection
611         bool paste_internally = false;
612         if (button == 2
613             && bv_->text->selection) {
614                 owner_->getLyXFunc()->Dispatch(LFUN_COPY);
615                 paste_internally = true;
616         }
617         
618         // Clear the selection
619         screen_->ToggleSelection(bv_->text, bv_);
620         bv_->text->ClearSelection(bv_);
621         bv_->text->FullRebreak(bv_);
622         screen_->Update(bv_->text, bv_);
623         updateScrollbar();
624         
625         // Single left click in math inset?
626         if ((inset_hit != 0) &&
627             (inset_hit->Editable()==Inset::HIGHLY_EDITABLE)) {
628                 // Highly editable inset, like math
629                 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
630                 selection_possible = false;
631                 owner_->updateLayoutChoice();
632                 owner_->getMiniBuffer()->Set(inset->EditMessage());
633                 inset->InsetButtonPress(bv_, xpos, ypos, button);
634                 inset->Edit(bv_, xpos, ypos, button);
635                 return;
636         } 
637         
638         // Right click on a footnote flag opens float menu
639         if (button == 3) { 
640                 selection_possible = false;
641                 return;
642         }
643         
644         if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
645                 bv_->text->SetCursorFromCoordinates(bv_, xpos, ypos + screen_first);
646         bv_->text->FinishUndo();
647         bv_->text->sel_cursor = bv_->text->cursor;
648         bv_->text->cursor.x_fix(bv_->text->cursor.x());
649         
650         owner_->updateLayoutChoice();
651         if (fitCursor(bv_->text)) {
652                 selection_possible = false;
653         }
654         
655         // Insert primary selection with middle mouse
656         // if there is a local selection in the current buffer,
657         // insert this
658         if (button == 2) {
659                 if (paste_internally)
660                         owner_->getLyXFunc()->Dispatch(LFUN_PASTE);
661                 else
662                         owner_->getLyXFunc()->Dispatch(LFUN_PASTESELECTION,
663                                                        "paragraph");
664                 selection_possible = false;
665                 return;
666         }
667 }
668
669
670 void BufferView::Pimpl::doubleClick(int /*x*/, int /*y*/, unsigned int button) 
671 {
672         // select a word
673         if (!buffer_)
674             return;
675
676         LyXText * text = bv_->getLyXText();
677
678         if (text->bv_owner && bv_->theLockingInset())
679             return;
680
681         if (screen_ && button == 1) {
682             if (text->bv_owner) {
683                 screen_->HideCursor();
684                 screen_->ToggleSelection(text, bv_);
685                 text->SelectWord(bv_);
686                 screen_->ToggleSelection(text, bv_, false);
687             } else {
688                 text->SelectWord(bv_);
689             }
690             /* This will fit the cursor on the screen
691              * if necessary */
692             update(text, BufferView::SELECT|BufferView::FITCUR);
693         }
694 }
695
696
697 void BufferView::Pimpl::tripleClick(int /*x*/, int /*y*/, unsigned int button)
698 {
699         // select a line
700         if (buffer_)
701                 return;
702
703         LyXText * text = bv_->getLyXText();
704
705         if (text->bv_owner && bv_->theLockingInset())
706             return;
707
708         if (screen_ && (button == 1)) {
709                 screen_->HideCursor();
710                 screen_->ToggleSelection(text, bv_);
711                 text->CursorHome(bv_);
712                 text->sel_cursor = text->cursor;
713                 text->CursorEnd(bv_);
714                 text->SetSelection(bv_);
715                 screen_->ToggleSelection(text, bv_, false);
716                 /* This will fit the cursor on the screen
717                  * if necessary */
718                 update(text, BufferView::SELECT|BufferView::FITCUR);
719         }
720 }
721
722
723 void BufferView::Pimpl::enterView()
724 {
725         if (active() && available()) {
726                 SetXtermCursor(workarea_->getWin());
727                 using_xterm_cursor = true;
728         }
729 }
730
731
732 void BufferView::Pimpl::leaveView()
733 {
734         if (using_xterm_cursor) {
735                 XUndefineCursor(fl_get_display(), workarea_->getWin());
736                 using_xterm_cursor = false;
737         }
738 }
739
740
741 void BufferView::Pimpl::workAreaButtonRelease(int x, int y,
742                                               unsigned int button)
743 {
744         if (!buffer_ || !screen_) return;
745
746         // If we hit an inset, we have the inset coordinates in these
747         // and inset_hit points to the inset.  If we do not hit an
748         // inset, inset_hit is 0, and inset_x == x, inset_y == y.
749         Inset * inset_hit = checkInsetHit(bv_->text, x, y, button);
750
751         if (bv_->theLockingInset()) {
752                 // We are in inset locking mode.
753
754                 /* LyX does a kind of work-area grabbing for insets.
755                    Only a ButtonPress Event outside the inset will 
756                    force a InsetUnlock. */
757                 bv_->theLockingInset()->
758                         InsetButtonRelease(bv_, x, y, button);
759                 return;
760         }
761         
762         selection_possible = false;
763         
764         if (button >= 2) return;
765
766         bv_->setState();
767         owner_->showState();
768
769         // Did we hit an editable inset?
770         if (inset_hit) {
771                 // Inset like error, notes and figures
772                 selection_possible = false;
773
774                 // CHECK fix this proper in 0.13
775
776                 // Following a ref shouldn't issue
777                 // a push on the undo-stack
778                 // anylonger, now that we have
779                 // keybindings for following
780                 // references and returning from
781                 // references.  IMHO though, it
782                 // should be the inset's own business
783                 // to push or not push on the undo
784                 // stack. They don't *have* to
785                 // alter the document...
786                 // (Joacim)
787                 // ...or maybe the SetCursorParUndo()
788                 // below isn't necessary at all anylonger?
789                 if (inset_hit->LyxCode() == Inset::REF_CODE) {
790                         bv_->text->SetCursorParUndo(bv_->buffer());
791                 }
792
793                 owner_->getMiniBuffer()->Set(inset_hit->EditMessage());
794                 if (inset_hit->Editable()==Inset::HIGHLY_EDITABLE) {
795                         // Highly editable inset, like math
796                         UpdatableInset *inset = (UpdatableInset *)inset_hit;
797                         inset->InsetButtonRelease(bv_, x, y, button);
798                 } else {
799                         inset_hit->InsetButtonRelease(bv_, x, y, button);
800                         inset_hit->Edit(bv_, x, y, button);
801                 }
802                 return;
803         }
804
805         // check whether we want to open a float
806         if (bv_->text) {
807                 bool hit = false;
808                 char c = ' ';
809                 if (bv_->text->cursor.pos() <
810                     bv_->text->cursor.par()->Last()) {
811                         c = bv_->text->cursor.par()->
812                                 GetChar(bv_->text->cursor.pos());
813                 }
814 #ifndef NEW_INSETS
815                if(!bv_->text->selection)
816                 if (c == LyXParagraph::META_FOOTNOTE
817                     || c == LyXParagraph::META_MARGIN
818                     || c == LyXParagraph::META_FIG
819                     || c == LyXParagraph::META_TAB
820                     || c == LyXParagraph::META_WIDE_FIG
821                     || c == LyXParagraph::META_WIDE_TAB
822                     || c == LyXParagraph::META_ALGORITHM){
823                         hit = true;
824                 } else
825 #endif
826                         if (bv_->text->cursor.pos() - 1 >= 0) {
827                         c = bv_->text->cursor.par()->
828                                 GetChar(bv_->text->cursor.pos() - 1);
829 #ifndef NEW_INSETS
830                         if (c == LyXParagraph::META_FOOTNOTE
831                             || c == LyXParagraph::META_MARGIN
832                             || c == LyXParagraph::META_FIG
833                             || c == LyXParagraph::META_TAB
834                             || c == LyXParagraph::META_WIDE_FIG 
835                             || c == LyXParagraph::META_WIDE_TAB
836                             || c == LyXParagraph::META_ALGORITHM){
837                                 // We are one step too far to the right
838                                 bv_->text->CursorLeft(bv_);
839                                 hit = true;
840                         }
841 #endif
842                 }
843                 if (hit == true) {
844 #ifndef NEW_INSETS
845                         bv_->toggleFloat();
846 #endif
847                         selection_possible = false;
848                         return;
849                 }
850         }
851
852 #ifndef NEW_INSETS
853         // Do we want to close a float? (click on the float-label)
854         if (bv_->text->cursor.row()->par()->footnoteflag == 
855             LyXParagraph::OPEN_FOOTNOTE
856             && bv_->text->cursor.row()->previous() &&
857             bv_->text->cursor.row()->previous()->par()->
858             footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
859                 LyXFont font(LyXFont::ALL_SANE);
860                 font.setSize(LyXFont::SIZE_FOOTNOTE);
861
862                 int box_x = 20; // LYX_PAPER_MARGIN;
863                 box_x += lyxfont::width(" wide-tab ", font);
864
865                 unsigned int screen_first = bv_->text->first;
866
867                 if (x < box_x
868                     && y + screen_first > bv_->text->cursor.y() -
869                     bv_->text->cursor.row()->baseline()
870                     && y + screen_first < bv_->text->cursor.y() -
871                     bv_->text->cursor.row()->baseline()
872                     + lyxfont::maxAscent(font) * 1.2 + lyxfont::maxDescent(font) * 1.2) {
873                         bv_->toggleFloat();
874                         selection_possible = false;
875                         return;
876                 }
877         }
878 #endif
879
880         // Maybe we want to edit a bibitem ale970302
881         if (bv_->text->cursor.par()->bibkey && x < 20 + 
882             bibitemMaxWidth(bv_, textclasslist.
883                             TextClass(buffer_->
884                                       params.textclass).defaultfont())) {
885                 bv_->text->cursor.par()->bibkey->Edit(bv_, 0, 0, 0);
886         }
887
888         return;
889 }
890
891
892 /* 
893  * Returns an inset if inset was hit. 0 otherwise.
894  * If hit, the coordinates are changed relative to the inset. 
895  * Otherwise coordinates are not changed, and false is returned.
896  */
897 Inset * BufferView::Pimpl::checkInsetHit(LyXText * text, int & x, int & y,
898                                          unsigned int /* button */)
899 {
900         if (!screen_)
901                 return 0;
902   
903         int y_tmp = y + text->first;
904   
905         LyXCursor cursor;
906         text->SetCursorFromCoordinates(bv_, cursor, x, y_tmp);
907
908         if (cursor.pos() < cursor.par()->Last()
909             && cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET
910             && cursor.par()->GetInset(cursor.pos())
911             && cursor.par()->GetInset(cursor.pos())->Editable()) {
912
913                 // Check whether the inset really was hit
914                 Inset * tmpinset = cursor.par()->GetInset(cursor.pos());
915                 LyXFont font = text->GetFont(bv_->buffer(),
916                                                   cursor.par(), cursor.pos());
917                 int width = tmpinset->width(bv_, font);
918                 int inset_x = font.isVisibleRightToLeft()
919                         ? cursor.x() - width : cursor.x();
920                 int start_x = inset_x + tmpinset->scroll();
921                 int end_x = inset_x + width;
922
923                 if (x > start_x && x < end_x
924                     && y_tmp > cursor.y() - tmpinset->ascent(bv_, font)
925                     && y_tmp < cursor.y() + tmpinset->descent(bv_, font)) {
926                         text->SetCursor(bv_, cursor.par(),cursor.pos(),true);
927                         x = x - start_x;
928                         // The origin of an inset is on the baseline
929                         y = y_tmp - (text->cursor.y()); 
930                         return tmpinset;
931                 }
932         }
933
934         if ((cursor.pos() - 1 >= 0) &&
935             (cursor.par()->GetChar(cursor.pos()-1) == LyXParagraph::META_INSET) &&
936             (cursor.par()->GetInset(cursor.pos() - 1)) &&
937             (cursor.par()->GetInset(cursor.pos() - 1)->Editable())) {
938                 Inset * tmpinset = cursor.par()->GetInset(cursor.pos()-1);
939                 LyXFont font = text->GetFont(bv_->buffer(), cursor.par(),
940                                                   cursor.pos()-1);
941                 int width = tmpinset->width(bv_, font);
942                 int inset_x = font.isVisibleRightToLeft()
943                         ? cursor.x() : cursor.x() - width;
944                 int start_x = inset_x + tmpinset->scroll();
945                 int end_x = inset_x + width;
946
947                 if (x > start_x && x < end_x
948                     && y_tmp > cursor.y() - tmpinset->ascent(bv_, font)
949                     && y_tmp < cursor.y() + tmpinset->descent(bv_, font)) {
950 #if 0
951                         if (move_cursor && (tmpinset != bv_->theLockingInset()))
952 #endif
953                                 text->SetCursor(bv_, cursor.par(),cursor.pos()-1,true);
954                         x = x - start_x;
955                         // The origin of an inset is on the baseline
956                         y = y_tmp - (text->cursor.y()); 
957                         return tmpinset;
958                 }
959         }
960         return 0;
961 }
962
963
964 void BufferView::Pimpl::workAreaExpose()
965 {
966         static int work_area_width = 0;
967         static unsigned int work_area_height = 0;
968
969         bool widthChange = workarea_->workWidth() != work_area_width;
970         bool heightChange = workarea_->height() != work_area_height;
971
972         // update from work area
973         work_area_width = workarea_->workWidth();
974         work_area_height = workarea_->height();
975         if (buffer_ != 0) {
976                 if (widthChange) {
977                         // All buffers need a resize
978                         bufferlist.resize();
979
980                         // Remove all texts from the textcache
981                         // This is not _really_ what we want to do. What
982                         // we really want to do is to delete in textcache
983                         // that does not have a BufferView with matching
984                         // width, but as long as we have only one BufferView
985                         // deleting all gives the same result.
986                         if (lyxerr.debugging())
987                                 textcache.show(lyxerr, "Expose delete all");
988                         textcache.clear();
989                 } else if (heightChange) {
990                         // Rebuild image of current screen
991                         updateScreen();
992                         // fitCursor() ensures we don't jump back
993                         // to the start of the document on vertical
994                         // resize
995                         fitCursor(bv_->text);
996
997                         // The main window size has changed, repaint most stuff
998                         redraw();
999                         // ...including the minibuffer
1000                         owner_->getMiniBuffer()->Init();
1001
1002                 } else if (screen_)
1003                     screen_->Redraw(bv_->text, bv_);
1004         } else {
1005                 // Grey box when we don't have a buffer
1006                 workarea_->greyOut();
1007         }
1008
1009         // always make sure that the scrollbar is sane.
1010         updateScrollbar();
1011         owner_->updateLayoutChoice();
1012         return;
1013 }
1014
1015
1016 void BufferView::Pimpl::update()
1017 {
1018         if (screen_) screen_->Update(bv_->text, bv_);
1019 }
1020
1021 // Values used when calling update:
1022 // -3 - update
1023 // -2 - update, move sel_cursor if selection, fitcursor
1024 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
1025 //  0 - update, move sel_cursor if selection, fitcursor 
1026 //  1 - update, move sel_cursor if selection, fitcursor, mark dirty
1027 //  3 - update, move sel_cursor if selection
1028 //
1029 // update -
1030 // a simple redraw of the parts that need refresh
1031 //
1032 // move sel_cursor if selection -
1033 // the text's sel_cursor is moved if there is selection is progress
1034 //
1035 // fitcursor -
1036 // fitCursor() is called and the scrollbar updated
1037 //
1038 // mark dirty -
1039 // the buffer is marked dirty.
1040 //
1041 // enum {
1042 //       UPDATE = 0,
1043 //       SELECT = 1,
1044 //       FITCUR = 2,
1045 //       CHANGE = 4 
1046 // };
1047 //
1048 // UPDATE_ONLY = UPDATE;
1049 // UPDATE_SELECT = UPDATE | SELECT;
1050 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
1051 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
1052 //
1053 // update(-3) -> update(0)         -> update(0) -> update(UPDATE)
1054 // update(-2) -> update(1 + 2)     -> update(3) -> update(SELECT|FITCUR)
1055 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1056 // update(1)  -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1057 // update(3)  -> update(1)         -> update(1) -> update(SELECT)
1058
1059 void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
1060 {
1061         owner_->updateLayoutChoice();
1062
1063         if (!text->selection && (f & SELECT)) {
1064                 text->sel_cursor = text->cursor;
1065         }
1066
1067         text->FullRebreak(bv_);
1068
1069         if (text->inset_owner) {
1070             text->inset_owner->SetUpdateStatus(bv_, InsetText::NONE);
1071             bv_->updateInset(text->inset_owner, true);
1072         } else
1073             update();
1074
1075         if ((f & FITCUR)) {
1076                 fitCursor(text);
1077         }
1078
1079         if ((f & CHANGE)) {
1080                 if (buffer_->isLyxClean()) {
1081                         buffer_->markDirty();
1082                         owner_->getMiniBuffer()->setTimer(4);
1083                 } else {
1084                         buffer_->markDirty();
1085                 }
1086         }
1087 }
1088
1089
1090 // Callback for cursor timer
1091 void BufferView::Pimpl::cursorToggle()
1092 {
1093         // Quite a nice place for asyncron Inset updating, isn't it?
1094         // Actually no! This is run even if no buffer exist... so (Lgb)
1095         if (!buffer_) {
1096                 cursor_timeout.restart();
1097                 return;
1098         }
1099  
1100         int status = 1;
1101         int pid = waitpid(static_cast<pid_t>(0), &status, WNOHANG);
1102         if (pid == -1) // error find out what is wrong
1103                 ; // ignore it for now.
1104         else if (pid > 0)
1105                 sigchldhandler(pid, &status);
1106
1107         updatelist.update(bv_);
1108         
1109         if (!screen_) {
1110                 cursor_timeout.restart();
1111                 return;
1112         }
1113
1114         if (!bv_->theLockingInset()) {
1115                 screen_->CursorToggle(bv_->text, bv_);
1116         } else {
1117                 bv_->theLockingInset()->ToggleInsetCursor(bv_);
1118         }
1119         
1120         cursor_timeout.restart();
1121 }
1122
1123
1124 void BufferView::Pimpl::cursorPrevious(LyXText * text)
1125 {
1126         if (!text->cursor.row()->previous())
1127                 return;
1128         
1129         int y = text->first;
1130         if (text->inset_owner)
1131                 y += bv_->text->first;
1132         Row * cursorrow = text->cursor.row();
1133         text->SetCursorFromCoordinates(bv_, bv_->text->cursor.x_fix(), y);
1134         bv_->text->FinishUndo();
1135         // This is to allow jumping over large insets
1136         if ((cursorrow == text->cursor.row()))
1137                 text->CursorUp(bv_);
1138         
1139         if (text->inset_owner ||
1140             text->cursor.row()->height() < workarea_->height())
1141                 screen_->Draw(bv_->text, bv_,
1142                               text->cursor.y()
1143                               - text->cursor.row()->baseline()
1144                               + text->cursor.row()->height()
1145                               - workarea_->height() + 1 );
1146         updateScrollbar();
1147 }
1148
1149
1150 void BufferView::Pimpl::cursorNext(LyXText * text)
1151 {
1152         if (!text->cursor.row()->next())
1153                 return;
1154         
1155         int y = text->first + workarea_->height();
1156 //      if (text->inset_owner)
1157 //              y += bv_->text->first;
1158         text->GetRowNearY(y);
1159     
1160         Row * cursorrow = text->cursor.row();
1161         text->SetCursorFromCoordinates(bv_, text->cursor.x_fix(), y); // + workarea_->height());
1162         bv_->text->FinishUndo();
1163         // This is to allow jumping over large insets
1164         if ((cursorrow == bv_->text->cursor.row()))
1165                 text->CursorDown(bv_);
1166         
1167         if (text->inset_owner ||
1168             text->cursor.row()->height() < workarea_->height())
1169                 screen_->Draw(bv_->text, bv_, text->cursor.y() -
1170                               text->cursor.row()->baseline());
1171         updateScrollbar();
1172 }
1173
1174
1175 bool BufferView::Pimpl::available() const
1176 {
1177         if (buffer_ && bv_->text) return true;
1178         return false;
1179 }
1180
1181
1182 void BufferView::Pimpl::beforeChange(LyXText * text)
1183 {
1184         toggleSelection();
1185         text->ClearSelection(bv_);
1186 }
1187
1188
1189 void BufferView::Pimpl::savePosition(unsigned int i)
1190 {
1191         if (i >= saved_positions_num)
1192                 return;
1193         saved_positions[i] = Position(buffer_->fileName(),
1194                                       bv_->text->cursor.par()->id(),
1195                                       bv_->text->cursor.pos());
1196         if (i > 0)
1197                 owner_->getMiniBuffer()->Set(_("Saved bookmark ") + tostr(i));
1198 }
1199
1200
1201 void BufferView::Pimpl::restorePosition(unsigned int i)
1202 {
1203         if (i >= saved_positions_num)
1204                 return;
1205
1206         string fname = saved_positions[i].filename;
1207
1208         beforeChange(bv_->text);
1209
1210         if (fname != buffer_->fileName()) {
1211                 Buffer * b = bufferlist.exists(fname) ?
1212                         bufferlist.getBuffer(fname) :
1213                         bufferlist.loadLyXFile(fname); // don't ask, just load it
1214                 if (b != 0 ) buffer(b);
1215         }
1216
1217         LyXParagraph * par = bv_->text->GetParFromID(saved_positions[i].par_id);
1218         if (!par)
1219                 return;
1220
1221         bv_->text->SetCursor(bv_, par,
1222                              min(par->Last(), saved_positions[i].par_pos));
1223         update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
1224         if (i > 0)
1225                 owner_->getMiniBuffer()->Set(_("Moved to bookmark ") + tostr(i));
1226 }
1227
1228
1229 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
1230 {
1231         if (i >= saved_positions_num)
1232                 return false;
1233
1234         return !saved_positions[i].filename.empty();
1235 }
1236
1237
1238 void BufferView::Pimpl::setState()
1239 {
1240         if (!lyxrc.rtl_support)
1241                 return;
1242
1243         LyXText * text = bv_->getLyXText();
1244         if (text->real_current_font.isRightToLeft() &&
1245             text->real_current_font.latex() != LyXFont::ON) {
1246                 if (owner_->getIntl()->primarykeymap)
1247                         owner_->getIntl()->KeyMapSec();
1248         } else {
1249                 if (!owner_->getIntl()->primarykeymap)
1250                         owner_->getIntl()->KeyMapPrim();
1251         }
1252 }
1253
1254
1255 void BufferView::Pimpl::insetSleep()
1256 {
1257         if (bv_->theLockingInset() && !bv_->inset_slept) {
1258                 bv_->theLockingInset()->GetCursorPos(bv_, bv_->slx, bv_->sly);
1259                 bv_->theLockingInset()->InsetUnlock(bv_);
1260                 bv_->inset_slept = true;
1261         }
1262 }
1263
1264
1265 void BufferView::Pimpl::insetWakeup()
1266 {
1267         if (bv_->theLockingInset() && bv_->inset_slept) {
1268                 bv_->theLockingInset()->Edit(bv_, bv_->slx, bv_->sly, 0);
1269                 bv_->inset_slept = false;
1270         }
1271 }
1272
1273
1274 void BufferView::Pimpl::insetUnlock()
1275 {
1276         if (bv_->theLockingInset()) {
1277                 if (!bv_->inset_slept)
1278                         bv_->theLockingInset()->InsetUnlock(bv_);
1279                 bv_->theLockingInset(0);
1280                 bv_->text->FinishUndo();
1281                 bv_->inset_slept = false;
1282         }
1283 }
1284
1285
1286 bool BufferView::Pimpl::focus() const
1287 {
1288         return workarea_->hasFocus();
1289 }
1290
1291
1292 void BufferView::Pimpl::focus(bool f)
1293 {
1294         if (f) workarea_->setFocus();
1295 }
1296
1297
1298 bool BufferView::Pimpl::active() const
1299 {
1300         return workarea_->active();
1301 }
1302
1303
1304 bool BufferView::Pimpl::belowMouse() const 
1305 {
1306         return workarea_->belowMouse();
1307 }
1308
1309
1310 void BufferView::Pimpl::showCursor()
1311 {
1312         if (screen_)
1313                 screen_->ShowCursor(bv_->text, bv_);
1314 }
1315
1316
1317 void BufferView::Pimpl::hideCursor()
1318 {
1319         if (screen_)
1320                 screen_->HideCursor();
1321 }
1322
1323
1324 void BufferView::Pimpl::toggleSelection(bool b)
1325 {
1326         if (screen_)
1327                 screen_->ToggleSelection(bv_->text, bv_, b);
1328 }
1329
1330
1331 void BufferView::Pimpl::toggleToggle()
1332 {
1333         if (screen_)
1334                 screen_->ToggleToggle(bv_->text, bv_);
1335 }
1336
1337
1338 void BufferView::Pimpl::center() 
1339 {
1340         beforeChange(bv_->text);
1341         if (bv_->text->cursor.y() > static_cast<int>((workarea_->height() / 2))) {
1342                 screen_->Draw(bv_->text, bv_, bv_->text->cursor.y() - workarea_->height() / 2);
1343         } else {
1344                 screen_->Draw(bv_->text, bv_, 0);
1345         }
1346         update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
1347         redraw();
1348 }
1349
1350
1351 void BufferView::Pimpl::pasteClipboard(bool asPara) 
1352 {
1353         if (!buffer_) return;
1354
1355         screen_->HideCursor();
1356         beforeChange(bv_->text);
1357         
1358         string const clip(workarea_->getClipboard());
1359         
1360         if (clip.empty()) return;
1361
1362         if (asPara) {
1363                 bv_->text->InsertStringB(bv_, clip);
1364         } else {
1365                 bv_->text->InsertStringA(bv_, clip);
1366         }
1367         update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1368 }
1369
1370
1371 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
1372 {
1373         workarea_->putClipboard(stuff);
1374 }
1375
1376
1377 /*
1378  * Dispatch functions for actions which can be valid for BufferView->text
1379  * and/or InsetText->text!!!
1380  */
1381 static LyXText * TEXT(BufferView * bv) { return bv->getLyXText(); }
1382
1383 inline
1384 void BufferView::Pimpl::moveCursorUpdate(bool selecting)
1385 {
1386         if (selecting || TEXT(bv_)->mark_set) {
1387                 TEXT(bv_)->SetSelection(bv_);
1388                 if (TEXT(bv_)->bv_owner)
1389                         bv_->toggleToggle();
1390         }
1391         update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
1392         showCursor();
1393         
1394         /* ---> Everytime the cursor is moved, show the current font state. */
1395         // should this too me moved out of this func?
1396         //owner->showState();
1397         bv_->setState();
1398 }
1399
1400
1401 Inset * BufferView::Pimpl::getInsetByCode(Inset::Code code)
1402 {
1403         LyXCursor cursor = TEXT(bv_)->cursor;
1404         Buffer::inset_iterator it =
1405                 find_if(Buffer::inset_iterator(
1406                         cursor.par(), cursor.pos()),
1407                         buffer_->inset_iterator_end(),
1408                         compare_memfun(&Inset::LyxCode, code)
1409                         );
1410         return it != buffer_->inset_iterator_end() ? (*it) : 0;
1411 }
1412
1413
1414 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
1415 {
1416         string filename = filen;
1417
1418         if (filename.empty()) {
1419                 // Launch a file browser
1420                 string initpath = lyxrc.document_path;
1421                 LyXFileDlg fileDlg;
1422
1423                 if (available()) {
1424                         string const trypath = owner_->buffer()->filepath;
1425                         // If directory is writeable, use this as default.
1426                         if (IsDirWriteable(trypath) == 1)
1427                                 initpath = trypath;
1428                 }
1429
1430                 // launches dialog
1431                 ProhibitInput(bv_);
1432                 fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
1433                 fileDlg.SetButton(1, _("Examples"), 
1434                                   AddPath(system_lyxdir, "examples"));
1435                 filename = fileDlg.Select(_("Select Document to Insert"),
1436                                           initpath, "*.lyx");
1437                 AllowInput(bv_);
1438
1439                 // check selected filename
1440                 if (filename.empty()) {
1441                         owner_->getMiniBuffer()->Set(_("Canceled."));
1442                         return;
1443                 }
1444         } 
1445
1446         // get absolute path of file and make sure the filename ends
1447         // with .lyx
1448         filename = MakeAbsPath(filename);
1449         if (!IsLyXFilename(filename))
1450                 filename += ".lyx";
1451
1452         // Inserts document
1453         owner_->getMiniBuffer()->Set(_("Inserting document"),
1454                                      MakeDisplayPath(filename), "...");
1455         bool const res = bv_->insertLyXFile(filename);
1456         if (res) {
1457                 owner_->getMiniBuffer()->Set(_("Document"),
1458                                              MakeDisplayPath(filename),
1459                                              _("inserted."));
1460         } else {
1461                 owner_->getMiniBuffer()->Set(_("Could not insert document"),
1462                                              MakeDisplayPath(filename));
1463         }
1464 }
1465
1466
1467 bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
1468 {
1469         switch (action) {
1470                 // --- Misc -------------------------------------------
1471         case LFUN_APPENDIX:
1472                 if (available()) {
1473                         TEXT(bv_)->toggleAppendix(bv_);
1474                         update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1475                 }
1476                 break;
1477
1478         case LFUN_TOC_INSERT:
1479         case LFUN_LOA_INSERT:
1480         case LFUN_LOF_INSERT:
1481         case LFUN_LOT_INSERT:
1482         {
1483                 InsetCommandParams p;
1484                 
1485                 if (action == LFUN_TOC_INSERT )
1486                         p.setCmdName( "tableofcontents" );
1487                 else if (action == LFUN_LOA_INSERT )
1488                         p.setCmdName( "listofalgorithms" );
1489                 else if (action == LFUN_LOF_INSERT )
1490                         p.setCmdName( "listoffigures" );
1491                 else
1492                         p.setCmdName( "listoftables" );
1493
1494                 Inset * inset = new InsetTOC( p );
1495                 if (!bv_->insertInset( inset, "Standard", true ) )
1496                         delete inset;
1497                 break;
1498         }
1499                 
1500         case LFUN_TABULAR_FEATURE:
1501         case LFUN_SCROLL_INSET:
1502                 // this is not handled here as this funktion is only aktive
1503                 // if we have a locking_inset and that one is (or contains)
1504                 // a tabular-inset
1505                 break;
1506
1507         case LFUN_INSET_GRAPHICS:
1508         {
1509                 Inset * new_inset = new InsetGraphics;
1510                 if (!bv_->insertInset(new_inset)) {
1511                         delete new_inset;
1512                 } else {
1513                         // this is need because you don't use a inset->Edit()
1514                         bv_->updateInset(new_inset, true);
1515                         new_inset->Edit(bv_, 0, 0, 0);
1516                 }
1517                 break;
1518         }
1519                 
1520         case LFUN_PASTE:
1521                 bv_->paste();
1522                 setState();
1523                 break;
1524                 
1525         case LFUN_PASTESELECTION:
1526         {
1527                 bool asPara = false;
1528                 if (argument == "paragraph") asPara = true;
1529                 pasteClipboard(asPara);
1530         }
1531         break;
1532         
1533         case LFUN_CUT:
1534                 bv_->cut();
1535                 break;
1536                 
1537         case LFUN_COPY:
1538                 bv_->copy();
1539                 break;
1540                 
1541         case LFUN_LAYOUT_COPY:
1542                 bv_->copyEnvironment();
1543                 break;
1544                 
1545         case LFUN_LAYOUT_PASTE:
1546                 bv_->pasteEnvironment();
1547                 setState();
1548                 break;
1549                 
1550         case LFUN_GOTOERROR:
1551                 bv_->gotoInset(Inset::ERROR_CODE, false);
1552                 break;
1553                 
1554         case LFUN_GOTONOTE:
1555                 bv_->gotoInset(Inset::IGNORE_CODE, false);
1556                 break;
1557
1558         case LFUN_REFERENCE_GOTO:
1559         {
1560                 vector<Inset::Code> tmp;
1561                 tmp.push_back(Inset::LABEL_CODE);
1562                 tmp.push_back(Inset::REF_CODE);
1563                 bv_->gotoInset(tmp, true);
1564                 break;
1565         }
1566
1567         case LFUN_HYPHENATION:
1568                 bv_->hyphenationPoint();
1569                 break;
1570                 
1571         case LFUN_LDOTS:
1572                 bv_->ldots();
1573                 break;
1574                 
1575         case LFUN_END_OF_SENTENCE:
1576                 bv_->endOfSentenceDot();
1577                 break;
1578
1579         case LFUN_MENU_SEPARATOR:
1580                 bv_->menuSeparator();
1581                 break;
1582                 
1583         case LFUN_HFILL:
1584                 bv_->hfill();
1585                 break;
1586                 
1587         case LFUN_DEPTH:
1588                 changeDepth(bv_, TEXT(bv_), 0);
1589                 break;
1590                 
1591         case LFUN_DEPTH_MIN:
1592                 changeDepth(bv_, TEXT(bv_), -1);
1593                 break;
1594                 
1595         case LFUN_DEPTH_PLUS:
1596                 changeDepth(bv_, TEXT(bv_), 1);
1597                 break;
1598                 
1599         case LFUN_FREE:
1600                 owner_->getDialogs()->setUserFreeFont();
1601                 break;
1602                 
1603         case LFUN_TEX:
1604                 Tex(bv_);
1605                 setState();
1606                 owner_->showState();
1607                 break;
1608
1609         case LFUN_FILE_INSERT:
1610         {
1611                 MenuInsertLyXFile(argument);
1612         }
1613         break;
1614         
1615         case LFUN_FILE_INSERT_ASCII:
1616         {
1617                 bool asPara = (argument == "paragraph");
1618                 InsertAsciiFile(bv_, string(), asPara);
1619         }
1620         break;
1621                 
1622         case LFUN_LAYOUT:
1623         {
1624                 lyxerr.debug() << "LFUN_LAYOUT: (arg) "
1625                                << argument << endl;
1626                 
1627                 // Derive layout number from given argument (string)
1628                 // and current buffer's textclass (number). */    
1629                 LyXTextClassList::ClassList::size_type tclass =
1630                         buffer_->params.textclass;
1631                 pair <bool, LyXTextClass::size_type> layout = 
1632                         textclasslist.NumberOfLayout(tclass, argument);
1633
1634                 // If the entry is obsolete, use the new one instead.
1635                 if (layout.first) {
1636                         string obs = textclasslist.Style(tclass,layout.second)
1637                                 .obsoleted_by();
1638                         if (!obs.empty()) 
1639                                 layout = 
1640                                         textclasslist.NumberOfLayout(tclass, obs);
1641                 }
1642
1643                 // see if we found the layout number:
1644                 if (!layout.first) {
1645                         owner_->getLyXFunc()->setErrorMessage(
1646                                 string(N_("Layout ")) + argument +
1647                                 N_(" not known"));
1648                         break;
1649                 }
1650
1651                 if (current_layout != layout.second) {
1652                         hideCursor();
1653                         current_layout = layout.second;
1654                         update(TEXT(bv_),
1655                                BufferView::SELECT|BufferView::FITCUR);
1656                         TEXT(bv_)->SetLayout(bv_, layout.second);
1657                         owner_->setLayout(layout.second);
1658                         update(TEXT(bv_),
1659                                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1660                         setState();
1661                 }
1662         }
1663         break;
1664
1665         case LFUN_LANGUAGE:
1666                 Lang(bv_, argument);
1667                 setState();
1668                 owner_->showState();
1669                 break;
1670
1671         case LFUN_EMPH:
1672                 Emph(bv_);
1673                 owner_->showState();
1674                 break;
1675
1676         case LFUN_BOLD:
1677                 Bold(bv_);
1678                 owner_->showState();
1679                 break;
1680                 
1681         case LFUN_NOUN:
1682                 Noun(bv_);
1683                 owner_->showState();
1684                 break;
1685                 
1686         case LFUN_CODE:
1687                 Code(bv_);
1688                 owner_->showState();
1689                 break;
1690                 
1691         case LFUN_SANS:
1692                 Sans(bv_);
1693                 owner_->showState();
1694                 break;
1695                 
1696         case LFUN_ROMAN:
1697                 Roman(bv_);
1698                 owner_->showState();
1699                 break;
1700                 
1701         case LFUN_DEFAULT:
1702                 StyleReset(bv_);
1703                 owner_->showState();
1704                 break;
1705                 
1706         case LFUN_UNDERLINE:
1707                 Underline(bv_);
1708                 owner_->showState();
1709                 break;
1710                 
1711         case LFUN_FONT_SIZE:
1712                 FontSize(bv_, argument);
1713                 owner_->showState();
1714                 break;
1715                 
1716         case LFUN_FONT_STATE:
1717                 owner_->getLyXFunc()->setMessage(CurrentState(bv_));
1718                 break;
1719                 
1720         case LFUN_UPCASE_WORD:
1721                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
1722                 TEXT(bv_)->ChangeWordCase(bv_, LyXText::text_uppercase);
1723                 if (TEXT(bv_)->inset_owner)
1724                         bv_->updateInset(TEXT(bv_)->inset_owner, true);
1725                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1726                 break;
1727                 
1728         case LFUN_LOWCASE_WORD:
1729                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
1730                 TEXT(bv_)->ChangeWordCase(bv_, LyXText::text_lowercase);
1731                 if (TEXT(bv_)->inset_owner)
1732                         bv_->updateInset(TEXT(bv_)->inset_owner, true);
1733                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1734                 break;
1735                 
1736         case LFUN_CAPITALIZE_WORD:
1737                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
1738                 TEXT(bv_)->ChangeWordCase(bv_,
1739                                              LyXText::text_capitalization);
1740                 if (TEXT(bv_)->inset_owner)
1741                         bv_->updateInset(TEXT(bv_)->inset_owner, true);
1742                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1743                 break;
1744                 
1745         case LFUN_INSERT_LABEL:
1746                 MenuInsertLabel(bv_, argument);
1747                 break;
1748                 
1749         case LFUN_REF_INSERT:
1750                 if (argument.empty()) {
1751                         InsetCommandParams p("ref");
1752                         owner_->getDialogs()->createRef(p.getAsString());
1753                 } else {
1754                         InsetCommandParams p;
1755                         p.setFromString(argument);
1756
1757                         InsetRef * inset = new InsetRef(p, *buffer_);
1758                         if (!bv_->insertInset(inset))
1759                                 delete inset;
1760                         else
1761                                 bv_->updateInset(inset, true);
1762                 }
1763                 break;
1764
1765         case LFUN_BOOKMARK_SAVE:
1766                 savePosition(strToUnsignedInt(argument));
1767                 break;
1768
1769         case LFUN_BOOKMARK_GOTO:
1770                 restorePosition(strToUnsignedInt(argument));
1771                 break;
1772
1773         case LFUN_REF_GOTO:
1774         {
1775                 string label(argument);
1776                 if (label.empty()) {
1777                         InsetRef * inset = 
1778                                 static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1779                         if (inset) {
1780                                 label = inset->getContents();
1781                                 savePosition(0);
1782                         }
1783                 }
1784                 
1785                 if (!label.empty()) {
1786                         //bv_->savePosition(0);
1787                         if (!bv_->gotoLabel(label))
1788                                 WriteAlert(_("Error"), 
1789                                            _("Couldn't find this label"), 
1790                                            _("in current document."));
1791                 }
1792         }
1793         break;
1794                 
1795                 // --- Cursor Movements -----------------------------
1796         case LFUN_RIGHT:
1797         {
1798                 bool is_rtl = TEXT(bv_)->cursor.par()->isRightToLeftPar(buffer_->params);
1799                 if (!TEXT(bv_)->mark_set)
1800                         beforeChange(TEXT(bv_));
1801                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
1802                 if (is_rtl)
1803                         TEXT(bv_)->CursorLeft(bv_, false);
1804                 if (TEXT(bv_)->cursor.pos() < TEXT(bv_)->cursor.par()->Last()
1805                     && TEXT(bv_)->cursor.par()->GetChar(TEXT(bv_)->cursor.pos())
1806                     == LyXParagraph::META_INSET
1807                     && TEXT(bv_)->cursor.par()->GetInset(TEXT(bv_)->cursor.pos())
1808                     && TEXT(bv_)->cursor.par()->GetInset(TEXT(bv_)->cursor.pos())->Editable() == Inset::HIGHLY_EDITABLE){
1809                         Inset * tmpinset = TEXT(bv_)->cursor.par()->GetInset(TEXT(bv_)->cursor.pos());
1810                         owner_->getLyXFunc()->setMessage(tmpinset->EditMessage());
1811                         int y = 0;
1812                         if (is_rtl) {
1813                                 LyXFont font = 
1814                                         TEXT(bv_)->GetFont(buffer_,
1815                                                               TEXT(bv_)->cursor.par(),
1816                                                               TEXT(bv_)->cursor.pos()); 
1817                                 y = tmpinset->descent(bv_,font);
1818                         }
1819                         tmpinset->Edit(bv_, 0, y, 0);
1820                         break;
1821                 }
1822                 if (!is_rtl)
1823                         TEXT(bv_)->CursorRight(bv_, false);
1824                 TEXT(bv_)->FinishUndo();
1825                 moveCursorUpdate(false);
1826                 owner_->showState();
1827         }
1828         break;
1829                 
1830         case LFUN_LEFT:
1831         {
1832                 // This is soooo ugly. Isn`t it possible to make
1833                 // it simpler? (Lgb)
1834                 bool is_rtl = TEXT(bv_)->cursor.par()->isRightToLeftPar(buffer_->params);
1835                 if (!TEXT(bv_)->mark_set)
1836                         beforeChange(TEXT(bv_));
1837                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
1838                 LyXCursor cur = TEXT(bv_)->cursor;
1839                 if (!is_rtl)
1840                         TEXT(bv_)->CursorLeft(bv_, false);
1841                 if ((is_rtl || cur != TEXT(bv_)->cursor) && // only if really moved!
1842                     TEXT(bv_)->cursor.pos() < TEXT(bv_)->cursor.par()->Last() &&
1843                     (TEXT(bv_)->cursor.par()->GetChar(TEXT(bv_)->cursor.pos()) ==
1844                      LyXParagraph::META_INSET) &&
1845                     TEXT(bv_)->cursor.par()->GetInset(TEXT(bv_)->cursor.pos()) &&
1846                     (TEXT(bv_)->cursor.par()->GetInset(TEXT(bv_)->cursor.pos())->Editable()
1847                      == Inset::HIGHLY_EDITABLE))
1848                 {
1849                         Inset * tmpinset = TEXT(bv_)->cursor.par()->GetInset(TEXT(bv_)->cursor.pos());
1850                         owner_->getLyXFunc()->setMessage(tmpinset->EditMessage());
1851                         LyXFont font = TEXT(bv_)->GetFont(buffer_,
1852                                                              TEXT(bv_)->cursor.par(),
1853                                                              TEXT(bv_)->cursor.pos());
1854                         int y = is_rtl ? 0 
1855                                 : tmpinset->descent(bv_,font);
1856                         tmpinset->Edit(bv_,
1857                                        tmpinset->x() +
1858                                        tmpinset->width(bv_,font),
1859                                        y, 0);
1860                         break;
1861                 }
1862                 if  (is_rtl)
1863                         TEXT(bv_)->CursorRight(bv_, false);
1864
1865                 TEXT(bv_)->FinishUndo();
1866                 moveCursorUpdate(false);
1867                 owner_->showState();
1868         }
1869         break;
1870                 
1871         case LFUN_UP:
1872                 if (!TEXT(bv_)->mark_set)
1873                         beforeChange(TEXT(bv_));
1874                 update(TEXT(bv_), BufferView::UPDATE);
1875                 TEXT(bv_)->CursorUp(bv_);
1876                 TEXT(bv_)->FinishUndo();
1877                 moveCursorUpdate(false);
1878                 owner_->showState();
1879                 break;
1880                 
1881         case LFUN_DOWN:
1882                 if (!TEXT(bv_)->mark_set)
1883                         beforeChange(TEXT(bv_));
1884                 update(TEXT(bv_), BufferView::UPDATE);
1885                 TEXT(bv_)->CursorDown(bv_);
1886                 TEXT(bv_)->FinishUndo();
1887                 moveCursorUpdate(false);
1888                 owner_->showState();
1889                 break;
1890
1891         case LFUN_UP_PARAGRAPH:
1892                 if (!TEXT(bv_)->mark_set)
1893                         beforeChange(TEXT(bv_));
1894                 update(TEXT(bv_), BufferView::UPDATE);
1895                 TEXT(bv_)->CursorUpParagraph(bv_);
1896                 TEXT(bv_)->FinishUndo();
1897                 moveCursorUpdate(false);
1898                 owner_->showState();
1899                 break;
1900                 
1901         case LFUN_DOWN_PARAGRAPH:
1902                 if (!TEXT(bv_)->mark_set)
1903                         beforeChange(TEXT(bv_));
1904                 update(TEXT(bv_), BufferView::UPDATE);
1905                 TEXT(bv_)->CursorDownParagraph(bv_);
1906                 TEXT(bv_)->FinishUndo();
1907                 moveCursorUpdate(false);
1908                 owner_->showState();
1909                 break;
1910                 
1911         case LFUN_PRIOR:
1912                 if (!TEXT(bv_)->mark_set)
1913                         beforeChange(TEXT(bv_));
1914                 update(TEXT(bv_), BufferView::UPDATE);
1915                 cursorPrevious(TEXT(bv_));
1916                 TEXT(bv_)->FinishUndo();
1917                 moveCursorUpdate(false);
1918                 owner_->showState();
1919                 break;
1920                 
1921         case LFUN_NEXT:
1922                 if (!TEXT(bv_)->mark_set)
1923                         beforeChange(TEXT(bv_));
1924                 update(TEXT(bv_), BufferView::UPDATE);
1925                 cursorNext(TEXT(bv_));
1926                 TEXT(bv_)->FinishUndo();
1927                 moveCursorUpdate(false);
1928                 owner_->showState();
1929                 break;
1930                 
1931         case LFUN_HOME:
1932                 if (!TEXT(bv_)->mark_set)
1933                         beforeChange(TEXT(bv_));
1934                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
1935                 TEXT(bv_)->CursorHome(bv_);
1936                 TEXT(bv_)->FinishUndo();
1937                 moveCursorUpdate(false);
1938                 owner_->showState();
1939                 break;
1940                 
1941         case LFUN_END:
1942                 if (!TEXT(bv_)->mark_set)
1943                         beforeChange(TEXT(bv_));
1944                 update(TEXT(bv_),
1945                        BufferView::SELECT|BufferView::FITCUR);
1946                 TEXT(bv_)->CursorEnd(bv_);
1947                 TEXT(bv_)->FinishUndo();
1948                 moveCursorUpdate(false);
1949                 owner_->showState();
1950                 break;
1951                 
1952         case LFUN_SHIFT_TAB:
1953         case LFUN_TAB:
1954                 if (!TEXT(bv_)->mark_set)
1955                         beforeChange(TEXT(bv_));
1956                 update(TEXT(bv_),
1957                        BufferView::SELECT|BufferView::FITCUR);
1958                 TEXT(bv_)->CursorTab(bv_);
1959                 TEXT(bv_)->FinishUndo();
1960                 moveCursorUpdate(false);
1961                 owner_->showState();
1962                 break;
1963                 
1964         case LFUN_WORDRIGHT:
1965                 if (!TEXT(bv_)->mark_set)
1966                         beforeChange(TEXT(bv_));
1967                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
1968                 if (TEXT(bv_)->cursor.par()->isRightToLeftPar(buffer_->params))
1969                         TEXT(bv_)->CursorLeftOneWord(bv_);
1970                 else
1971                         TEXT(bv_)->CursorRightOneWord(bv_);
1972                 TEXT(bv_)->FinishUndo();
1973                 moveCursorUpdate(false);
1974                 owner_->showState();
1975                 break;
1976                 
1977         case LFUN_WORDLEFT:
1978                 if (!TEXT(bv_)->mark_set)
1979                         beforeChange(TEXT(bv_));
1980                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
1981                 if (TEXT(bv_)->cursor.par()->isRightToLeftPar(buffer_->params))
1982                         TEXT(bv_)->CursorRightOneWord(bv_);
1983                 else
1984                         TEXT(bv_)->CursorLeftOneWord(bv_);
1985                 TEXT(bv_)->FinishUndo();
1986                 moveCursorUpdate(false);
1987                 owner_->showState();
1988                 break;
1989                 
1990         case LFUN_BEGINNINGBUF:
1991                 if (!TEXT(bv_)->mark_set)
1992                         beforeChange(TEXT(bv_));
1993                 update(TEXT(bv_),
1994                        BufferView::SELECT|BufferView::FITCUR);
1995                 TEXT(bv_)->CursorTop(bv_);
1996                 TEXT(bv_)->FinishUndo();
1997                 moveCursorUpdate(false);
1998                 owner_->showState();
1999                 break;
2000                 
2001         case LFUN_ENDBUF:
2002                 if (!TEXT(bv_)->mark_set)
2003                         beforeChange(TEXT(bv_));
2004                 update(TEXT(bv_),
2005                        BufferView::SELECT|BufferView::FITCUR);
2006                 TEXT(bv_)->CursorBottom(bv_);
2007                 TEXT(bv_)->FinishUndo();
2008                 moveCursorUpdate(false);
2009                 owner_->showState();
2010                 break;
2011
2012       
2013                 /* cursor selection ---------------------------- */
2014         case LFUN_RIGHTSEL:
2015                 update(TEXT(bv_),
2016                        BufferView::SELECT|BufferView::FITCUR);
2017                 if (TEXT(bv_)->cursor.par()->isRightToLeftPar(buffer_->params))
2018                         TEXT(bv_)->CursorLeft(bv_);
2019                 else
2020                         TEXT(bv_)->CursorRight(bv_);
2021                 TEXT(bv_)->FinishUndo();
2022                 moveCursorUpdate(true);
2023                 owner_->showState();
2024                 break;
2025                 
2026         case LFUN_LEFTSEL:
2027                 update(TEXT(bv_),
2028                        BufferView::SELECT|BufferView::FITCUR);
2029                 if (TEXT(bv_)->cursor.par()->isRightToLeftPar(buffer_->params))
2030                         TEXT(bv_)->CursorRight(bv_);
2031                 else
2032                         TEXT(bv_)->CursorLeft(bv_);
2033                 TEXT(bv_)->FinishUndo();
2034                 moveCursorUpdate(true);
2035                 owner_->showState();
2036                 break;
2037                 
2038         case LFUN_UPSEL:
2039                 update(TEXT(bv_),
2040                        BufferView::SELECT|BufferView::FITCUR);
2041                 TEXT(bv_)->CursorUp(bv_);
2042                 TEXT(bv_)->FinishUndo();
2043                 moveCursorUpdate(true);
2044                 owner_->showState();
2045                 break;
2046                 
2047         case LFUN_DOWNSEL:
2048                 update(TEXT(bv_),
2049                        BufferView::SELECT|BufferView::FITCUR);
2050                 TEXT(bv_)->CursorDown(bv_);
2051                 TEXT(bv_)->FinishUndo();
2052                 moveCursorUpdate(true);
2053                 owner_->showState();
2054                 break;
2055
2056         case LFUN_UP_PARAGRAPHSEL:
2057                 update(TEXT(bv_),
2058                        BufferView::SELECT|BufferView::FITCUR);
2059                 TEXT(bv_)->CursorUpParagraph(bv_);
2060                 TEXT(bv_)->FinishUndo();
2061                 moveCursorUpdate(true);
2062                 owner_->showState();
2063                 break;
2064                 
2065         case LFUN_DOWN_PARAGRAPHSEL:
2066                 update(TEXT(bv_),
2067                        BufferView::SELECT|BufferView::FITCUR);
2068                 TEXT(bv_)->CursorDownParagraph(bv_);
2069                 TEXT(bv_)->FinishUndo();
2070                 moveCursorUpdate(true);
2071                 owner_->showState();
2072                 break;
2073                 
2074         case LFUN_PRIORSEL:
2075                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2076                 cursorPrevious(TEXT(bv_));
2077                 TEXT(bv_)->FinishUndo();
2078                 moveCursorUpdate(true);
2079                 owner_->showState();
2080                 break;
2081                 
2082         case LFUN_NEXTSEL:
2083                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2084                 cursorNext(TEXT(bv_));
2085                 TEXT(bv_)->FinishUndo();
2086                 moveCursorUpdate(true);
2087                 owner_->showState();
2088                 break;
2089                 
2090         case LFUN_HOMESEL:
2091                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2092                 TEXT(bv_)->CursorHome(bv_);
2093                 TEXT(bv_)->FinishUndo();
2094                 moveCursorUpdate(true);
2095                 owner_->showState();
2096                 break;
2097                 
2098         case LFUN_ENDSEL:
2099                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2100                 TEXT(bv_)->CursorEnd(bv_);
2101                 TEXT(bv_)->FinishUndo();
2102                 moveCursorUpdate(true);
2103                 owner_->showState();
2104                 break;
2105                 
2106         case LFUN_WORDRIGHTSEL:
2107                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2108                 if (TEXT(bv_)->cursor.par()->isRightToLeftPar(buffer_->params))
2109                         TEXT(bv_)->CursorLeftOneWord(bv_);
2110                 else
2111                         TEXT(bv_)->CursorRightOneWord(bv_);
2112                 TEXT(bv_)->FinishUndo();
2113                 moveCursorUpdate(true);
2114                 owner_->showState();
2115                 break;
2116                 
2117         case LFUN_WORDLEFTSEL:
2118                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2119                 if (TEXT(bv_)->cursor.par()->isRightToLeftPar(buffer_->params))
2120                         TEXT(bv_)->CursorRightOneWord(bv_);
2121                 else
2122                         TEXT(bv_)->CursorLeftOneWord(bv_);
2123                 TEXT(bv_)->FinishUndo();
2124                 moveCursorUpdate(true);
2125                 owner_->showState();
2126                 break;
2127                 
2128         case LFUN_BEGINNINGBUFSEL:
2129                 if (TEXT(bv_)->inset_owner)
2130                         break;
2131                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2132                 TEXT(bv_)->CursorTop(bv_);
2133                 TEXT(bv_)->FinishUndo();
2134                 moveCursorUpdate(true);
2135                 owner_->showState();
2136                 break;
2137                 
2138         case LFUN_ENDBUFSEL:
2139                 if (TEXT(bv_)->inset_owner)
2140                         break;
2141                 update(TEXT(bv_),
2142                        BufferView::SELECT|BufferView::FITCUR);
2143                 TEXT(bv_)->CursorBottom(bv_);
2144                 TEXT(bv_)->FinishUndo();
2145                 moveCursorUpdate(true);
2146                 owner_->showState();
2147                 break;
2148
2149                 // --- text changing commands ------------------------
2150         case LFUN_BREAKLINE:
2151                 beforeChange(TEXT(bv_));
2152                 TEXT(bv_)->InsertChar(bv_, LyXParagraph::META_NEWLINE);
2153                 update(TEXT(bv_),
2154                        BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2155                 moveCursorUpdate(false);
2156                 break;
2157                 
2158         case LFUN_PROTECTEDSPACE:
2159         {
2160                 LyXLayout const & style =
2161                         textclasslist.Style(buffer_->params.textclass,
2162                                             TEXT(bv_)->cursor.par()->GetLayout());
2163
2164                 if (style.free_spacing) {
2165                         TEXT(bv_)->InsertChar(bv_, ' ');
2166                         update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2167                 } else {
2168                         bv_->protectedBlank(TEXT(bv_));
2169                 }
2170                 moveCursorUpdate(false);
2171         }
2172         break;
2173                 
2174         case LFUN_SETMARK:
2175                 if (TEXT(bv_)->mark_set) {
2176                         beforeChange(TEXT(bv_));
2177                         update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2178                         owner_->getLyXFunc()->setMessage(N_("Mark removed"));
2179                 } else {
2180                         beforeChange(TEXT(bv_));
2181                         TEXT(bv_)->mark_set = 1;
2182                         update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2183                         owner_->getLyXFunc()->setMessage(N_("Mark set"));
2184                 }
2185                 TEXT(bv_)->sel_cursor = TEXT(bv_)->cursor;
2186                 break;
2187                 
2188         case LFUN_DELETE:
2189                 if (!TEXT(bv_)->selection) {
2190                         TEXT(bv_)->Delete(bv_);
2191                         TEXT(bv_)->sel_cursor = 
2192                                 TEXT(bv_)->cursor;
2193                         update(TEXT(bv_),
2194                                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2195                         // It is possible to make it a lot faster still
2196                         // just comment out the line below...
2197                         showCursor();
2198                 } else {
2199                         bv_->cut();
2200                 }
2201                 moveCursorUpdate(false);
2202                 owner_->showState();
2203                 setState();
2204                 break;
2205
2206         case LFUN_DELETE_SKIP:
2207         {
2208                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
2209                 
2210                 LyXCursor cursor = TEXT(bv_)->cursor;
2211
2212                 if (!TEXT(bv_)->selection) {
2213                         if (cursor.pos() == cursor.par()->Last()) {
2214                                 TEXT(bv_)->CursorRight(bv_);
2215                                 cursor = TEXT(bv_)->cursor;
2216                                 if (cursor.pos() == 0
2217                                     && !(cursor.par()->added_space_top 
2218                                          == VSpace (VSpace::NONE))) {
2219                                         TEXT(bv_)->SetParagraph
2220                                                 (bv_,
2221                                                  cursor.par()->line_top,
2222                                                  cursor.par()->line_bottom,
2223                                                  cursor.par()->pagebreak_top, 
2224                                                  cursor.par()->pagebreak_bottom,
2225                                                  VSpace(VSpace::NONE), 
2226                                                  cursor.par()->added_space_bottom,
2227                                                  cursor.par()->align, 
2228                                                  cursor.par()->labelwidthstring, 0);
2229                                         TEXT(bv_)->CursorLeft(bv_);
2230                                         update(TEXT(bv_), 
2231                                                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2232                                 } else {
2233                                         TEXT(bv_)->CursorLeft(bv_);
2234                                         TEXT(bv_)->Delete(bv_);
2235                                         TEXT(bv_)->sel_cursor = 
2236                                                 TEXT(bv_)->cursor;
2237                                         update(TEXT(bv_),
2238                                                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2239                                 }
2240                         } else {
2241                                 TEXT(bv_)->Delete(bv_);
2242                                 TEXT(bv_)->sel_cursor = 
2243                                         TEXT(bv_)->cursor;
2244                                 update(TEXT(bv_),
2245                                        BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2246                         }
2247                 } else {
2248                         bv_->cut();
2249                 }
2250         }
2251         break;
2252
2253         /* -------> Delete word forward. */
2254         case LFUN_DELETE_WORD_FORWARD:
2255                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2256                 TEXT(bv_)->DeleteWordForward(bv_);
2257                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2258                 moveCursorUpdate(false);
2259                 owner_->showState();
2260                 break;
2261
2262                 /* -------> Delete word backward. */
2263         case LFUN_DELETE_WORD_BACKWARD:
2264                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2265                 TEXT(bv_)->DeleteWordBackward(bv_);
2266                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2267                 moveCursorUpdate(false);
2268                 owner_->showState();
2269                 break;
2270                 
2271                 /* -------> Kill to end of line. */
2272         case LFUN_DELETE_LINE_FORWARD:
2273                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2274                 TEXT(bv_)->DeleteLineForward(bv_);
2275                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2276                 moveCursorUpdate(false);
2277                 break;
2278                 
2279                 /* -------> Set mark off. */
2280         case LFUN_MARK_OFF:
2281                 beforeChange(TEXT(bv_));
2282                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2283                 TEXT(bv_)->sel_cursor = TEXT(bv_)->cursor;
2284                 owner_->getLyXFunc()->setMessage(N_("Mark off"));
2285                 break;
2286
2287                 /* -------> Set mark on. */
2288         case LFUN_MARK_ON:
2289                 beforeChange(TEXT(bv_));
2290                 TEXT(bv_)->mark_set = 1;
2291                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2292                 TEXT(bv_)->sel_cursor = TEXT(bv_)->cursor;
2293                 owner_->getLyXFunc()->setMessage(N_("Mark on"));
2294                 break;
2295                 
2296         case LFUN_BACKSPACE:
2297         {
2298                 if (!TEXT(bv_)->selection) {
2299                         if (owner_->getIntl()->getTrans().backspace()) {
2300                                 TEXT(bv_)->Backspace(bv_);
2301                                 TEXT(bv_)->sel_cursor = 
2302                                         TEXT(bv_)->cursor;
2303                                 update(TEXT(bv_),
2304                                        BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2305                                 // It is possible to make it a lot faster still
2306                                 // just comment out the line below...
2307                                 showCursor();
2308                         }
2309                 } else {
2310                         bv_->cut();
2311                 }
2312                 owner_->showState();
2313                 setState();
2314         }
2315         break;
2316
2317         case LFUN_BACKSPACE_SKIP:
2318         {
2319                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
2320                 
2321                 LyXCursor cursor = TEXT(bv_)->cursor;
2322                 
2323                 if (!TEXT(bv_)->selection) {
2324                         if (cursor.pos() == 0 
2325                             && !(cursor.par()->added_space_top 
2326                                  == VSpace (VSpace::NONE))) {
2327                                 TEXT(bv_)->SetParagraph 
2328                                         (bv_,
2329                                          cursor.par()->line_top,      
2330                                          cursor.par()->line_bottom,
2331                                          cursor.par()->pagebreak_top, 
2332                                          cursor.par()->pagebreak_bottom,
2333                                          VSpace(VSpace::NONE), cursor.par()->added_space_bottom,
2334                                          cursor.par()->align, 
2335                                          cursor.par()->labelwidthstring, 0);
2336                                 update(TEXT(bv_),
2337                                        BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2338                         } else {
2339                                 TEXT(bv_)->Backspace(bv_);
2340                                 TEXT(bv_)->sel_cursor 
2341                                         = cursor;
2342                                 update(TEXT(bv_),
2343                                        BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2344                         }
2345                 } else
2346                         bv_->cut();
2347         }
2348         break;
2349
2350         case LFUN_BREAKPARAGRAPH:
2351         {
2352                 beforeChange(TEXT(bv_));
2353                 TEXT(bv_)->BreakParagraph(bv_, 0);
2354                 update(TEXT(bv_),
2355                        BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2356                 TEXT(bv_)->sel_cursor = TEXT(bv_)->cursor;
2357                 setState();
2358                 owner_->showState();
2359                 break;
2360         }
2361
2362         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
2363         {
2364                 beforeChange(TEXT(bv_));
2365                 TEXT(bv_)->BreakParagraph(bv_, 1);
2366                 update(TEXT(bv_),
2367                        BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2368                 TEXT(bv_)->sel_cursor = TEXT(bv_)->cursor;
2369                 setState();
2370                 owner_->showState();
2371                 break;
2372         }
2373         
2374         case LFUN_BREAKPARAGRAPH_SKIP:
2375         {
2376                 // When at the beginning of a paragraph, remove
2377                 // indentation and add a "defskip" at the top.
2378                 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
2379                 
2380                 LyXCursor cursor = TEXT(bv_)->cursor;
2381                 
2382                 beforeChange(TEXT(bv_));
2383                 if (cursor.pos() == 0) {
2384                         if (cursor.par()->added_space_top == VSpace(VSpace::NONE)) {
2385                                 TEXT(bv_)->SetParagraph
2386                                         (bv_,
2387                                          cursor.par()->line_top,      
2388                                          cursor.par()->line_bottom,
2389                                          cursor.par()->pagebreak_top, 
2390                                          cursor.par()->pagebreak_bottom,
2391                                          VSpace(VSpace::DEFSKIP), cursor.par()->added_space_bottom,
2392                                          cursor.par()->align, 
2393                                          cursor.par()->labelwidthstring, 1);
2394                                 //update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2395                         } 
2396                 }
2397                 else {
2398                         TEXT(bv_)->BreakParagraph(bv_, 0);
2399                         //update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2400                 }
2401
2402                 update(TEXT(bv_),
2403                        BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2404                 TEXT(bv_)->sel_cursor = cursor;
2405                 setState();
2406                 owner_->showState();
2407         }
2408         break;
2409
2410         case LFUN_PARAGRAPH_SPACING:
2411         {
2412                 LyXParagraph * par = TEXT(bv_)->cursor.par();
2413                 Spacing::Space cur_spacing = par->spacing.getSpace();
2414                 float cur_value = 1.0;
2415                 if (cur_spacing == Spacing::Other) {
2416                         cur_value = par->spacing.getValue();
2417                 }
2418                 
2419                 istringstream istr(argument.c_str());
2420
2421                 string tmp;
2422                 istr >> tmp;
2423                 Spacing::Space new_spacing = cur_spacing;
2424                 float new_value = cur_value;
2425                 if (tmp.empty()) {
2426                         lyxerr << "Missing argument to `paragraph-spacing'"
2427                                << endl;
2428                 } else if (tmp == "single") {
2429                         new_spacing = Spacing::Single;
2430                 } else if (tmp == "onehalf") {
2431                         new_spacing = Spacing::Onehalf;
2432                 } else if (tmp == "double") {
2433                         new_spacing = Spacing::Double;
2434                 } else if (tmp == "other") {
2435                         new_spacing = Spacing::Other;
2436                         float tmpval = 0.0;
2437                         istr >> tmpval;
2438                         lyxerr << "new_value = " << tmpval << endl;
2439                         if (tmpval != 0.0)
2440                                 new_value = tmpval;
2441                 } else if (tmp == "default") {
2442                         new_spacing = Spacing::Default;
2443                 } else {
2444                         lyxerr << _("Unknown spacing argument: ")
2445                                << argument << endl;
2446                 }
2447                 if (cur_spacing != new_spacing || cur_value != new_value) {
2448                         par->spacing.set(new_spacing, new_value);
2449                         TEXT(bv_)->RedoParagraph(bv_);
2450                         update(TEXT(bv_),
2451                                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2452                 }
2453         }
2454         break;
2455         
2456         case LFUN_QUOTE:
2457                 beforeChange(TEXT(bv_));
2458                 TEXT(bv_)->InsertChar(bv_, '\"');  // This " matches the single quote in the code
2459                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2460                 moveCursorUpdate(false);
2461                 break;
2462
2463         case LFUN_HTMLURL:
2464         case LFUN_URL:
2465         {
2466                 InsetCommandParams p;
2467                 if (action == LFUN_HTMLURL)
2468                         p.setCmdName("htmlurl");
2469                 else
2470                         p.setCmdName("url");
2471                 owner_->getDialogs()->createUrl( p.getAsString() );
2472         }
2473         break;
2474                     
2475         case LFUN_INSERT_URL:
2476         {
2477                 InsetCommandParams p;
2478                 p.setFromString( argument );
2479
2480                 InsetUrl * inset = new InsetUrl( p );
2481                 if (!bv_->insertInset(inset))
2482                         delete inset;
2483                 else
2484                         bv_->updateInset( inset, true );
2485         }
2486         break;
2487                     
2488         case LFUN_INSET_TEXT:
2489         {
2490                 InsetText * new_inset = new InsetText;
2491                 if (bv_->insertInset(new_inset))
2492                         new_inset->Edit(bv_, 0, 0, 0);
2493                 else
2494                         delete new_inset;
2495         }
2496         break;
2497         
2498         case LFUN_INSET_ERT:
2499         {
2500                 InsetERT * new_inset = new InsetERT;
2501                 if (bv_->insertInset(new_inset))
2502                         new_inset->Edit(bv_, 0, 0, 0);
2503                 else
2504                         delete new_inset;
2505         }
2506         break;
2507         
2508         case LFUN_INSET_EXTERNAL:
2509         {
2510                 InsetExternal * new_inset = new InsetExternal;
2511                 if (bv_->insertInset(new_inset))
2512                         new_inset->Edit(bv_, 0, 0, 0);
2513                 else
2514                         delete new_inset;
2515         }
2516         break;
2517         
2518         case LFUN_INSET_FOOTNOTE:
2519         {
2520                 InsetFoot * new_inset = new InsetFoot;
2521                 if (bv_->insertInset(new_inset))
2522                         new_inset->Edit(bv_, 0, 0, 0);
2523                 else
2524                         delete new_inset;
2525         }
2526         break;
2527
2528         case LFUN_INSET_MARGINAL:
2529         {
2530                 InsetMarginal * new_inset = new InsetMarginal;
2531                 if (bv_->insertInset(new_inset))
2532                         new_inset->Edit(bv_, 0, 0, 0);
2533                 else
2534                         delete new_inset;
2535         }
2536         break;
2537
2538         case LFUN_INSET_MINIPAGE:
2539         {
2540                 InsetMinipage * new_inset = new InsetMinipage;
2541                 if (bv_->insertInset(new_inset))
2542                         new_inset->Edit(bv_, 0, 0, 0);
2543                 else
2544                         delete new_inset;
2545         }
2546         break;
2547
2548         case LFUN_INSET_FLOAT:
2549         {
2550                 // check if the float type exist
2551                 if (floatList.typeExist(argument)) {
2552                         InsetFloat * new_inset = new InsetFloat(argument);
2553                         if (bv_->insertInset(new_inset))
2554                                 new_inset->Edit(bv_, 0, 0, 0);
2555                         else
2556                                 delete new_inset;
2557                 } else {
2558                         lyxerr << "Non-existant float type: "
2559                                << argument << endl;
2560                 }
2561                 
2562         }
2563         break;
2564
2565         case LFUN_INSET_LIST:
2566         {
2567                 InsetList * new_inset = new InsetList;
2568                 if (bv_->insertInset(new_inset))
2569                         new_inset->Edit(bv_, 0, 0, 0);
2570                 else
2571                         delete new_inset;
2572         }
2573         break;
2574
2575         case LFUN_INSET_THEOREM:
2576         {
2577                 InsetTheorem * new_inset = new InsetTheorem;
2578                 if (bv_->insertInset(new_inset))
2579                         new_inset->Edit(bv_, 0, 0, 0);
2580                 else
2581                         delete new_inset;
2582         }
2583         break;
2584
2585         case LFUN_INSET_CAPTION:
2586         {
2587                 // Do we have a locking inset...
2588                 if (bv_->theLockingInset()) {
2589                         lyxerr << "Locking inset code: "
2590                                << static_cast<int>(bv_->theLockingInset()->LyxCode());
2591                         InsetCaption * new_inset = new InsetCaption;
2592                         new_inset->setOwner(bv_->theLockingInset());
2593                         new_inset->SetAutoBreakRows(true);
2594                         new_inset->SetDrawFrame(0, InsetText::LOCKED);
2595                         new_inset->SetFrameColor(0, LColor::footnoteframe);
2596                         if (bv_->insertInset(new_inset))
2597                                 new_inset->Edit(bv_, 0, 0, 0);
2598                         else
2599                                 delete new_inset;
2600                 }
2601         }
2602         break;
2603         
2604         case LFUN_INSET_TABULAR:
2605         {
2606                 int r = 2, c = 2;
2607                 if (!argument.empty())
2608                         ::sscanf(argument.c_str(),"%d%d", &r, &c);
2609                 InsetTabular * new_inset =
2610                         new InsetTabular(*buffer_, r, c);
2611                 bool rtl =
2612                         TEXT(bv_)->real_current_font.isRightToLeft();
2613                 if (!bv_->open_new_inset(new_inset, rtl))
2614                         delete new_inset;
2615         }
2616         break;
2617
2618         // --- lyxserver commands ----------------------------
2619
2620         case LFUN_CHARATCURSOR:
2621         {
2622                 LyXParagraph::size_type pos = TEXT(bv_)->cursor.pos();
2623                 if (pos < TEXT(bv_)->cursor.par()->size())
2624                         owner_->getLyXFunc()->setMessage(
2625                                 tostr(TEXT(bv_)->cursor.par()->GetChar(pos)));
2626                 else
2627                         owner_->getLyXFunc()->setMessage("EOF");
2628         }
2629         break;
2630         
2631         case LFUN_GETXY:
2632                 owner_->getLyXFunc()->setMessage(tostr(TEXT(bv_)->cursor.x())
2633                                                  + ' '
2634                                                  + tostr(TEXT(bv_)->cursor.y()));
2635                 break;
2636                 
2637         case LFUN_SETXY:
2638         {
2639                 int x;
2640                 int y;
2641                 ::sscanf(argument.c_str(), " %d %d", &x, &y);
2642                 TEXT(bv_)->SetCursorFromCoordinates(bv_, x, y);
2643         }
2644         break;
2645         
2646         case LFUN_GETLAYOUT:
2647                 owner_->getLyXFunc()->setMessage(tostr(TEXT(bv_)->cursor.par()->layout));
2648                 break;
2649                         
2650         case LFUN_GETFONT:
2651         {
2652                 LyXFont & font = TEXT(bv_)->current_font;
2653                 if (font.shape() == LyXFont::ITALIC_SHAPE)
2654                         owner_->getLyXFunc()->setMessage("E");
2655                 else if (font.shape() == LyXFont::SMALLCAPS_SHAPE)
2656                         owner_->getLyXFunc()->setMessage("N");
2657                 else
2658                         owner_->getLyXFunc()->setMessage("0");
2659
2660         }
2661         break;
2662
2663         case LFUN_GETLATEX:
2664         {
2665                 LyXFont & font = TEXT(bv_)->current_font;
2666                 if (font.latex() == LyXFont::ON)
2667                         owner_->getLyXFunc()->setMessage("L");
2668                 else
2669                         owner_->getLyXFunc()->setMessage("0");
2670         }
2671         break;
2672
2673         // --- accented characters ---------------------------
2674                 
2675         case LFUN_UMLAUT:
2676         case LFUN_CIRCUMFLEX:
2677         case LFUN_GRAVE:
2678         case LFUN_ACUTE:
2679         case LFUN_TILDE:
2680         case LFUN_CEDILLA:
2681         case LFUN_MACRON:
2682         case LFUN_DOT:
2683         case LFUN_UNDERDOT:
2684         case LFUN_UNDERBAR:
2685         case LFUN_CARON:
2686         case LFUN_SPECIAL_CARON:
2687         case LFUN_BREVE:
2688         case LFUN_TIE:
2689         case LFUN_HUNG_UMLAUT:
2690         case LFUN_CIRCLE:
2691         case LFUN_OGONEK:
2692                 owner_->getLyXFunc()->handleKeyFunc(action);
2693                 break;
2694         
2695         // --- insert characters ----------------------------------------
2696         
2697         case LFUN_MATH_DELIM:     
2698         case LFUN_INSERT_MATRIX:
2699         {          
2700                 if (available()) { 
2701                         if (bv_->open_new_inset(new InsetFormula(false)))
2702                         {
2703                                 bv_->theLockingInset()
2704                                         ->LocalDispatch(bv_, action, argument);
2705                         }
2706                 }
2707         }          
2708         break;
2709                
2710         case LFUN_INSERT_MATH:
2711         {
2712                 math_insert_symbol(bv_, argument);
2713         }
2714         break;
2715         
2716         case LFUN_MATH_DISPLAY:
2717         {
2718                 if (available())
2719                         bv_->open_new_inset(new InsetFormula(true));
2720                 break;
2721         }
2722                     
2723         case LFUN_MATH_MACRO:
2724         {
2725                 if (available()) {
2726                         string s(argument);
2727                         if (s.empty())
2728                                 owner_->getLyXFunc()->setErrorMessage(N_("Missing argument"));
2729                         else {
2730                                 string const s1 = token(s, ' ', 1);
2731                                 int const na = s1.empty() ? 0 : lyx::atoi(s1);
2732                                 bv_->open_new_inset(new InsetFormulaMacro(token(s, ' ', 0), na));
2733                         }
2734                 }
2735         }
2736         break;
2737
2738         case LFUN_MATH_MODE:   // Open or create a math inset
2739         {               
2740                 if (available())
2741                         bv_->open_new_inset(new InsetFormula);
2742                 owner_->getLyXFunc()->setMessage(N_("Math editor mode"));
2743         }
2744         break;
2745           
2746         case LFUN_CITATION_INSERT:
2747         {
2748                 InsetCommandParams p;
2749                 p.setFromString( argument );
2750
2751                 InsetCitation * inset = new InsetCitation( p );
2752                 if (!bv_->insertInset(inset))
2753                         delete inset;
2754                 else
2755                         bv_->updateInset( inset, true );
2756         }
2757         break;
2758                     
2759         case LFUN_INSERT_BIBTEX:
2760         {   
2761                 // ale970405+lasgoutt970425
2762                 // The argument can be up to two tokens separated 
2763                 // by a space. The first one is the bibstyle.
2764                 string const db       = token(argument, ' ', 0);
2765                 string bibstyle = token(argument, ' ', 1);
2766                 if (bibstyle.empty())
2767                         bibstyle = "plain";
2768
2769                 InsetCommandParams p( "BibTeX", db, bibstyle );
2770                 InsetBibtex * inset = new InsetBibtex(p);
2771                 
2772                 if (bv_->insertInset(inset)) {
2773                         if (argument.empty())
2774                                 inset->Edit(bv_, 0, 0, 0);
2775                 } else
2776                         delete inset;
2777         }
2778         break;
2779                 
2780         // BibTeX data bases
2781         case LFUN_BIBDB_ADD:
2782         {
2783                 InsetBibtex * inset = 
2784                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2785                 if (inset) {
2786                         inset->addDatabase(argument);
2787                 }
2788         }
2789         break;
2790                     
2791         case LFUN_BIBDB_DEL:
2792         {
2793                 InsetBibtex * inset = 
2794                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2795                 if (inset) {
2796                         inset->delDatabase(argument);
2797                 }
2798         }
2799         break;
2800         
2801         case LFUN_BIBTEX_STYLE:
2802         {
2803                 InsetBibtex * inset = 
2804                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2805                 if (inset) {
2806                         inset->setOptions(argument);
2807                 }
2808         }
2809         break;
2810                 
2811         case LFUN_INDEX_CREATE:
2812         {
2813                 InsetCommandParams p( "index" );
2814                 
2815                 if (argument.empty()) {
2816                         // Get the word immediately preceding the cursor
2817                         LyXParagraph::size_type curpos = 
2818                                 TEXT(bv_)->cursor.pos() - 1;
2819
2820                         string curstring;
2821                         if (curpos >= 0 )
2822                                 curstring = TEXT(bv_)
2823                                         ->cursor.par()->GetWord(curpos);
2824
2825                         p.setContents( curstring );
2826                 } else {
2827                         p.setContents( argument );
2828                 }
2829
2830                 owner_->getDialogs()->createIndex( p.getAsString() );
2831         }
2832         break;
2833                     
2834         case LFUN_INDEX_INSERT:
2835         {
2836                 InsetCommandParams p;
2837                 p.setFromString(argument);
2838                 InsetIndex * inset = new InsetIndex(p);
2839
2840                 if (!bv_->insertInset(inset))
2841                         delete inset;
2842                 else
2843                         bv_->updateInset(inset, true);
2844         }
2845         break;
2846                     
2847         case LFUN_INDEX_INSERT_LAST:
2848         {
2849                 // Get word immediately preceding the cursor
2850                 LyXParagraph::size_type curpos = 
2851                         TEXT(bv_)->cursor.pos() - 1;
2852                 // Can't do that at the beginning of a paragraph
2853                 if (curpos < 0) break;
2854
2855                 string const curstring(TEXT(bv_)
2856                                        ->cursor.par()->GetWord(curpos));
2857
2858                 InsetCommandParams p("index", curstring);
2859                 InsetIndex * inset = new InsetIndex(p);
2860
2861                 if (!bv_->insertInset(inset))
2862                         delete inset;
2863                 else
2864                         bv_->updateInset(inset, true);
2865         }
2866         break;
2867
2868         case LFUN_INDEX_PRINT:
2869         {
2870                 InsetCommandParams p("printindex");
2871                 Inset * inset = new InsetPrintIndex(p);
2872                 if (!bv_->insertInset(inset, "Standard", true))
2873                         delete inset;
2874         }
2875         break;
2876
2877         case LFUN_PARENTINSERT:
2878         {
2879                 lyxerr << "arg " << argument << endl;
2880                 InsetCommandParams p( "lyxparent", argument );
2881                 Inset * inset = new InsetParent(p, *buffer_);
2882                 if (!bv_->insertInset(inset, "Standard", true))
2883                         delete inset;
2884         }
2885         break;
2886
2887         case LFUN_CHILD_INSERT:
2888         {
2889                 InsetCommandParams p;
2890                 p.setFromString(argument);
2891
2892                 InsetInclude * inset = new InsetInclude(p, *buffer_);
2893                 if (!bv_->insertInset(inset))
2894                         delete inset;
2895                 else
2896                         bv_->updateInset(inset, true);
2897         }
2898         break; 
2899
2900         case LFUN_INSERT_NOTE:
2901                 bv_->insertNote();
2902                 break;
2903
2904         case LFUN_SELFINSERT:
2905         {
2906                 LyXFont const old_font(TEXT(bv_)->real_current_font);
2907                 for (string::size_type i = 0; i < argument.length(); ++i) {
2908                         TEXT(bv_)->InsertChar(bv_, argument[i]);
2909                         // This needs to be in the loop, or else we
2910                         // won't break lines correctly. (Asger)
2911                         update(TEXT(bv_),
2912                                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2913                 }
2914                 TEXT(bv_)->sel_cursor = TEXT(bv_)->cursor;
2915                 moveCursorUpdate(false);
2916
2917                 // real_current_font.number can change so we need to 
2918                 // update the minibuffer
2919                 if (old_font != TEXT(bv_)->real_current_font)
2920                         owner_->showState();
2921         }
2922         break;
2923
2924         case LFUN_DATE_INSERT:  // jdblair: date-insert cmd
2925         {
2926                 struct tm * now_tm;
2927                 
2928                 time_t now_time_t = time(NULL);
2929                 now_tm = localtime(&now_time_t);
2930                 setlocale(LC_TIME, "");
2931                 string arg;
2932                 if (!argument.empty())
2933                         arg = argument;
2934                 else 
2935                         arg = lyxrc.date_insert_format;
2936                 char datetmp[32];
2937                 int const datetmp_len =
2938                         ::strftime(datetmp, 32, arg.c_str(), now_tm);
2939                 for (int i = 0; i < datetmp_len; i++) {
2940                         TEXT(bv_)->InsertChar(bv_, datetmp[i]);
2941                         update(TEXT(bv_),
2942                                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2943                 }
2944
2945                 TEXT(bv_)->sel_cursor = TEXT(bv_)->cursor;
2946                 moveCursorUpdate(false);
2947         }
2948         break;
2949
2950         case LFUN_UNKNOWN_ACTION:
2951         {
2952                 if (!buffer_) {
2953                         owner_->getLyXFunc()->setErrorMessage(N_("No document open"));
2954                         break;
2955                 }
2956
2957                 if (buffer_->isReadonly()) {
2958                         owner_->getLyXFunc()->setErrorMessage(N_("Document is read only"));
2959                         break;
2960                 }
2961                 if (!argument.empty()) {
2962                         /* Automatically delete the currently selected
2963                          * text and replace it with what is being
2964                          * typed in now. Depends on lyxrc settings
2965                          * "auto_region_delete", which defaults to
2966                          * true (on). */
2967                 
2968                         if (lyxrc.auto_region_delete) {
2969                                 if (TEXT(bv_)->selection){
2970                                         TEXT(bv_)->CutSelection(bv_, false);
2971                                         update(TEXT(bv_),
2972                                                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2973                                 }
2974                         }
2975                         
2976                         beforeChange(TEXT(bv_));
2977                         LyXFont const old_font(TEXT(bv_)->real_current_font);
2978                         for (string::size_type i = 0;
2979                              i < argument.length(); ++i) {
2980                                 if (greek_kb_flag) {
2981                                         if (!math_insert_greek(bv_,
2982                                                                argument[i]))
2983                                                 owner_->getIntl()->getTrans().TranslateAndInsert(argument[i], TEXT(bv_));
2984                                 } else
2985                                         owner_->getIntl()->getTrans().TranslateAndInsert(argument[i], TEXT(bv_));
2986                         }
2987
2988                         update(TEXT(bv_),BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2989
2990                         TEXT(bv_)->sel_cursor = TEXT(bv_)->cursor;
2991                         moveCursorUpdate(false);
2992
2993                         // real_current_font.number can change so we need to
2994                         // update the minibuffer
2995                         if (old_font != TEXT(bv_)->real_current_font)
2996                                 owner_->showState();
2997                 } else {
2998                         // if we don't have an argument there was something
2999                         // strange going on so we just tell this to someone!
3000                         owner_->getLyXFunc()->setErrorMessage(N_("No argument givven"));
3001                 }
3002                 break;
3003         }
3004         default:
3005                 return false;
3006         } // end of switch
3007
3008         return true;
3009 }