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