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