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