]> git.lyx.org Git - lyx.git/blob - src/BufferView_pimpl.C
5fc8d5b6338ce503dfebcac7cf127aaf613a5ac4
[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 "frontends/FileDialog.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         LyXParagraph::size_type pos = 0;
285         LyXParagraph::size_type selstartpos = 0;
286         LyXParagraph::size_type selendpos = 0;
287         bool selection = false;
288         bool mark_set  = false;
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
1428                 if (available()) {
1429                         string const trypath = owner_->buffer()->filepath;
1430                         // If directory is writeable, use this as default.
1431                         if (IsDirWriteable(trypath) == 1)
1432                                 initpath = trypath;
1433                 }
1434
1435                 FileDialog fileDlg(bv_->owner(), _("Select LyX document to insert"),
1436                         LFUN_FILE_INSERT,
1437                         make_pair(string(_("Documents")), string(lyxrc.document_path)),
1438                         make_pair(string(_("Examples")), string(AddPath(system_lyxdir, "examples"))));
1439
1440                 FileDialog::Result result = fileDlg.Select(initpath, _("*.lyx| LyX Documents (*.lyx)"));
1441  
1442                 if (result.first == FileDialog::Later)
1443                         return;
1444
1445                 filename = result.second;
1446
1447                 // check selected filename
1448                 if (filename.empty()) {
1449                         owner_->getMiniBuffer()->Set(_("Canceled."));
1450                         return;
1451                 }
1452         }
1453
1454         // get absolute path of file and make sure the filename ends
1455         // with .lyx
1456         filename = MakeAbsPath(filename);
1457         if (!IsLyXFilename(filename))
1458                 filename += ".lyx";
1459
1460         // Inserts document
1461         owner_->getMiniBuffer()->Set(_("Inserting document"),
1462                                      MakeDisplayPath(filename), "...");
1463         bool const res = bv_->insertLyXFile(filename);
1464         if (res) {
1465                 owner_->getMiniBuffer()->Set(_("Document"),
1466                                              MakeDisplayPath(filename),
1467                                              _("inserted."));
1468         } else {
1469                 owner_->getMiniBuffer()->Set(_("Could not insert document"),
1470                                              MakeDisplayPath(filename));
1471         }
1472 }
1473
1474
1475 bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
1476 {
1477         switch (action) {
1478                 // --- Misc -------------------------------------------
1479         case LFUN_APPENDIX:
1480                 if (available()) {
1481                         TEXT(bv_)->toggleAppendix(bv_);
1482                         update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1483                 }
1484                 break;
1485
1486         case LFUN_TOC_INSERT:
1487         case LFUN_LOA_INSERT:
1488         case LFUN_LOF_INSERT:
1489         case LFUN_LOT_INSERT:
1490         {
1491                 InsetCommandParams p;
1492                 
1493                 if (action == LFUN_TOC_INSERT )
1494                         p.setCmdName( "tableofcontents" );
1495                 else if (action == LFUN_LOA_INSERT )
1496                         p.setCmdName( "listofalgorithms" );
1497                 else if (action == LFUN_LOF_INSERT )
1498                         p.setCmdName( "listoffigures" );
1499                 else
1500                         p.setCmdName( "listoftables" );
1501
1502                 Inset * inset = new InsetTOC( p );
1503                 if (!bv_->insertInset( inset, "Standard", true ) )
1504                         delete inset;
1505                 break;
1506         }
1507                 
1508         case LFUN_TABULAR_FEATURE:
1509         case LFUN_SCROLL_INSET:
1510                 // this is not handled here as this funktion is only aktive
1511                 // if we have a locking_inset and that one is (or contains)
1512                 // a tabular-inset
1513                 break;
1514
1515         case LFUN_INSET_GRAPHICS:
1516         {
1517                 Inset * new_inset = new InsetGraphics;
1518                 if (!bv_->insertInset(new_inset)) {
1519                         delete new_inset;
1520                 } else {
1521                         // this is need because you don't use a inset->Edit()
1522                         bv_->updateInset(new_inset, true);
1523                         new_inset->Edit(bv_, 0, 0, 0);
1524                 }
1525                 break;
1526         }
1527                 
1528         case LFUN_PASTE:
1529                 bv_->paste();
1530                 setState();
1531                 break;
1532                 
1533         case LFUN_PASTESELECTION:
1534         {
1535                 bool asPara = false;
1536                 if (argument == "paragraph") asPara = true;
1537                 pasteClipboard(asPara);
1538         }
1539         break;
1540         
1541         case LFUN_CUT:
1542                 bv_->cut();
1543                 break;
1544                 
1545         case LFUN_COPY:
1546                 bv_->copy();
1547                 break;
1548                 
1549         case LFUN_LAYOUT_COPY:
1550                 bv_->copyEnvironment();
1551                 break;
1552                 
1553         case LFUN_LAYOUT_PASTE:
1554                 bv_->pasteEnvironment();
1555                 setState();
1556                 break;
1557                 
1558         case LFUN_GOTOERROR:
1559                 bv_->gotoInset(Inset::ERROR_CODE, false);
1560                 break;
1561                 
1562         case LFUN_GOTONOTE:
1563                 bv_->gotoInset(Inset::IGNORE_CODE, false);
1564                 break;
1565
1566         case LFUN_REFERENCE_GOTO:
1567         {
1568                 vector<Inset::Code> tmp;
1569                 tmp.push_back(Inset::LABEL_CODE);
1570                 tmp.push_back(Inset::REF_CODE);
1571                 bv_->gotoInset(tmp, true);
1572                 break;
1573         }
1574
1575         case LFUN_HYPHENATION:
1576                 bv_->hyphenationPoint();
1577                 break;
1578                 
1579         case LFUN_LDOTS:
1580                 bv_->ldots();
1581                 break;
1582                 
1583         case LFUN_END_OF_SENTENCE:
1584                 bv_->endOfSentenceDot();
1585                 break;
1586
1587         case LFUN_MENU_SEPARATOR:
1588                 bv_->menuSeparator();
1589                 break;
1590                 
1591         case LFUN_HFILL:
1592                 bv_->hfill();
1593                 break;
1594                 
1595         case LFUN_DEPTH:
1596                 changeDepth(bv_, TEXT(bv_), 0);
1597                 break;
1598                 
1599         case LFUN_DEPTH_MIN:
1600                 changeDepth(bv_, TEXT(bv_), -1);
1601                 break;
1602                 
1603         case LFUN_DEPTH_PLUS:
1604                 changeDepth(bv_, TEXT(bv_), 1);
1605                 break;
1606                 
1607         case LFUN_FREE:
1608                 owner_->getDialogs()->setUserFreeFont();
1609                 break;
1610                 
1611         case LFUN_TEX:
1612                 Tex(bv_);
1613                 setState();
1614                 owner_->showState();
1615                 break;
1616
1617         case LFUN_FILE_INSERT:
1618         {
1619                 MenuInsertLyXFile(argument);
1620         }
1621         break;
1622         
1623         case LFUN_FILE_INSERT_ASCII_PARA:
1624                 InsertAsciiFile(bv_, argument, true);
1625                 break;
1626
1627         case LFUN_FILE_INSERT_ASCII:
1628                 InsertAsciiFile(bv_, argument, false);
1629                 break;
1630                 
1631         case LFUN_LAYOUT:
1632         {
1633                 lyxerr.debug() << "LFUN_LAYOUT: (arg) "
1634                                << argument << endl;
1635                 
1636                 // Derive layout number from given argument (string)
1637                 // and current buffer's textclass (number). */    
1638                 LyXTextClassList::ClassList::size_type tclass =
1639                         buffer_->params.textclass;
1640                 pair <bool, LyXTextClass::size_type> layout = 
1641                         textclasslist.NumberOfLayout(tclass, argument);
1642
1643                 // If the entry is obsolete, use the new one instead.
1644                 if (layout.first) {
1645                         string obs = textclasslist.Style(tclass,layout.second)
1646                                 .obsoleted_by();
1647                         if (!obs.empty()) 
1648                                 layout = 
1649                                         textclasslist.NumberOfLayout(tclass, obs);
1650                 }
1651
1652                 // see if we found the layout number:
1653                 if (!layout.first) {
1654                         owner_->getLyXFunc()->setErrorMessage(
1655                                 string(N_("Layout ")) + argument +
1656                                 N_(" not known"));
1657                         break;
1658                 }
1659
1660                 if (current_layout != layout.second) {
1661                         hideCursor();
1662                         current_layout = layout.second;
1663                         update(TEXT(bv_),
1664                                BufferView::SELECT|BufferView::FITCUR);
1665                         TEXT(bv_)->SetLayout(bv_, layout.second);
1666                         owner_->setLayout(layout.second);
1667                         update(TEXT(bv_),
1668                                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1669                         setState();
1670                 }
1671         }
1672         break;
1673
1674         case LFUN_LANGUAGE:
1675                 Lang(bv_, argument);
1676                 setState();
1677                 owner_->showState();
1678                 break;
1679
1680         case LFUN_EMPH:
1681                 Emph(bv_);
1682                 owner_->showState();
1683                 break;
1684
1685         case LFUN_BOLD:
1686                 Bold(bv_);
1687                 owner_->showState();
1688                 break;
1689                 
1690         case LFUN_NOUN:
1691                 Noun(bv_);
1692                 owner_->showState();
1693                 break;
1694                 
1695         case LFUN_CODE:
1696                 Code(bv_);
1697                 owner_->showState();
1698                 break;
1699                 
1700         case LFUN_SANS:
1701                 Sans(bv_);
1702                 owner_->showState();
1703                 break;
1704                 
1705         case LFUN_ROMAN:
1706                 Roman(bv_);
1707                 owner_->showState();
1708                 break;
1709                 
1710         case LFUN_DEFAULT:
1711                 StyleReset(bv_);
1712                 owner_->showState();
1713                 break;
1714                 
1715         case LFUN_UNDERLINE:
1716                 Underline(bv_);
1717                 owner_->showState();
1718                 break;
1719                 
1720         case LFUN_FONT_SIZE:
1721                 FontSize(bv_, argument);
1722                 owner_->showState();
1723                 break;
1724                 
1725         case LFUN_FONT_STATE:
1726                 owner_->getLyXFunc()->setMessage(CurrentState(bv_));
1727                 break;
1728                 
1729         case LFUN_UPCASE_WORD:
1730                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
1731                 TEXT(bv_)->ChangeWordCase(bv_, LyXText::text_uppercase);
1732                 if (TEXT(bv_)->inset_owner)
1733                         bv_->updateInset(TEXT(bv_)->inset_owner, true);
1734                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1735                 break;
1736                 
1737         case LFUN_LOWCASE_WORD:
1738                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
1739                 TEXT(bv_)->ChangeWordCase(bv_, LyXText::text_lowercase);
1740                 if (TEXT(bv_)->inset_owner)
1741                         bv_->updateInset(TEXT(bv_)->inset_owner, true);
1742                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1743                 break;
1744                 
1745         case LFUN_CAPITALIZE_WORD:
1746                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
1747                 TEXT(bv_)->ChangeWordCase(bv_,
1748                                              LyXText::text_capitalization);
1749                 if (TEXT(bv_)->inset_owner)
1750                         bv_->updateInset(TEXT(bv_)->inset_owner, true);
1751                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1752                 break;
1753                 
1754         case LFUN_INSERT_LABEL:
1755                 MenuInsertLabel(bv_, argument);
1756                 break;
1757                 
1758         case LFUN_REF_INSERT:
1759                 if (argument.empty()) {
1760                         InsetCommandParams p("ref");
1761                         owner_->getDialogs()->createRef(p.getAsString());
1762                 } else {
1763                         InsetCommandParams p;
1764                         p.setFromString(argument);
1765
1766                         InsetRef * inset = new InsetRef(p, *buffer_);
1767                         if (!bv_->insertInset(inset))
1768                                 delete inset;
1769                         else
1770                                 bv_->updateInset(inset, true);
1771                 }
1772                 break;
1773
1774         case LFUN_BOOKMARK_SAVE:
1775                 savePosition(strToUnsignedInt(argument));
1776                 break;
1777
1778         case LFUN_BOOKMARK_GOTO:
1779                 restorePosition(strToUnsignedInt(argument));
1780                 break;
1781
1782         case LFUN_REF_GOTO:
1783         {
1784                 string label(argument);
1785                 if (label.empty()) {
1786                         InsetRef * inset = 
1787                                 static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1788                         if (inset) {
1789                                 label = inset->getContents();
1790                                 savePosition(0);
1791                         }
1792                 }
1793                 
1794                 if (!label.empty()) {
1795                         //bv_->savePosition(0);
1796                         if (!bv_->gotoLabel(label))
1797                                 WriteAlert(_("Error"), 
1798                                            _("Couldn't find this label"), 
1799                                            _("in current document."));
1800                 }
1801         }
1802         break;
1803                 
1804                 // --- Cursor Movements -----------------------------
1805         case LFUN_RIGHT:
1806         {
1807                 bool is_rtl = TEXT(bv_)->cursor.par()->isRightToLeftPar(buffer_->params);
1808                 if (!TEXT(bv_)->mark_set)
1809                         beforeChange(TEXT(bv_));
1810                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
1811                 if (is_rtl)
1812                         TEXT(bv_)->CursorLeft(bv_, false);
1813                 if (TEXT(bv_)->cursor.pos() < TEXT(bv_)->cursor.par()->Last()
1814                     && TEXT(bv_)->cursor.par()->GetChar(TEXT(bv_)->cursor.pos())
1815                     == LyXParagraph::META_INSET
1816                     && TEXT(bv_)->cursor.par()->GetInset(TEXT(bv_)->cursor.pos())
1817                     && TEXT(bv_)->cursor.par()->GetInset(TEXT(bv_)->cursor.pos())->Editable() == Inset::HIGHLY_EDITABLE){
1818                         Inset * tmpinset = TEXT(bv_)->cursor.par()->GetInset(TEXT(bv_)->cursor.pos());
1819                         owner_->getLyXFunc()->setMessage(tmpinset->EditMessage());
1820                         int y = 0;
1821                         if (is_rtl) {
1822                                 LyXFont font = 
1823                                         TEXT(bv_)->GetFont(buffer_,
1824                                                               TEXT(bv_)->cursor.par(),
1825                                                               TEXT(bv_)->cursor.pos()); 
1826                                 y = tmpinset->descent(bv_,font);
1827                         }
1828                         tmpinset->Edit(bv_, 0, y, 0);
1829                         break;
1830                 }
1831                 if (!is_rtl)
1832                         TEXT(bv_)->CursorRight(bv_, false);
1833                 TEXT(bv_)->FinishUndo();
1834                 moveCursorUpdate(false);
1835                 owner_->showState();
1836         }
1837         break;
1838                 
1839         case LFUN_LEFT:
1840         {
1841                 // This is soooo ugly. Isn`t it possible to make
1842                 // it simpler? (Lgb)
1843                 bool is_rtl = TEXT(bv_)->cursor.par()->isRightToLeftPar(buffer_->params);
1844                 if (!TEXT(bv_)->mark_set)
1845                         beforeChange(TEXT(bv_));
1846                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
1847                 LyXCursor cur = TEXT(bv_)->cursor;
1848                 if (!is_rtl)
1849                         TEXT(bv_)->CursorLeft(bv_, false);
1850                 if ((is_rtl || cur != TEXT(bv_)->cursor) && // only if really moved!
1851                     TEXT(bv_)->cursor.pos() < TEXT(bv_)->cursor.par()->Last() &&
1852                     (TEXT(bv_)->cursor.par()->GetChar(TEXT(bv_)->cursor.pos()) ==
1853                      LyXParagraph::META_INSET) &&
1854                     TEXT(bv_)->cursor.par()->GetInset(TEXT(bv_)->cursor.pos()) &&
1855                     (TEXT(bv_)->cursor.par()->GetInset(TEXT(bv_)->cursor.pos())->Editable()
1856                      == Inset::HIGHLY_EDITABLE))
1857                 {
1858                         Inset * tmpinset = TEXT(bv_)->cursor.par()->GetInset(TEXT(bv_)->cursor.pos());
1859                         owner_->getLyXFunc()->setMessage(tmpinset->EditMessage());
1860                         LyXFont font = TEXT(bv_)->GetFont(buffer_,
1861                                                              TEXT(bv_)->cursor.par(),
1862                                                              TEXT(bv_)->cursor.pos());
1863                         int y = is_rtl ? 0 
1864                                 : tmpinset->descent(bv_,font);
1865                         tmpinset->Edit(bv_,
1866                                        tmpinset->x() +
1867                                        tmpinset->width(bv_,font),
1868                                        y, 0);
1869                         break;
1870                 }
1871                 if  (is_rtl)
1872                         TEXT(bv_)->CursorRight(bv_, false);
1873
1874                 TEXT(bv_)->FinishUndo();
1875                 moveCursorUpdate(false);
1876                 owner_->showState();
1877         }
1878         break;
1879                 
1880         case LFUN_UP:
1881                 if (!TEXT(bv_)->mark_set)
1882                         beforeChange(TEXT(bv_));
1883                 update(TEXT(bv_), BufferView::UPDATE);
1884                 TEXT(bv_)->CursorUp(bv_);
1885                 TEXT(bv_)->FinishUndo();
1886                 moveCursorUpdate(false);
1887                 owner_->showState();
1888                 break;
1889                 
1890         case LFUN_DOWN:
1891                 if (!TEXT(bv_)->mark_set)
1892                         beforeChange(TEXT(bv_));
1893                 update(TEXT(bv_), BufferView::UPDATE);
1894                 TEXT(bv_)->CursorDown(bv_);
1895                 TEXT(bv_)->FinishUndo();
1896                 moveCursorUpdate(false);
1897                 owner_->showState();
1898                 break;
1899
1900         case LFUN_UP_PARAGRAPH:
1901                 if (!TEXT(bv_)->mark_set)
1902                         beforeChange(TEXT(bv_));
1903                 update(TEXT(bv_), BufferView::UPDATE);
1904                 TEXT(bv_)->CursorUpParagraph(bv_);
1905                 TEXT(bv_)->FinishUndo();
1906                 moveCursorUpdate(false);
1907                 owner_->showState();
1908                 break;
1909                 
1910         case LFUN_DOWN_PARAGRAPH:
1911                 if (!TEXT(bv_)->mark_set)
1912                         beforeChange(TEXT(bv_));
1913                 update(TEXT(bv_), BufferView::UPDATE);
1914                 TEXT(bv_)->CursorDownParagraph(bv_);
1915                 TEXT(bv_)->FinishUndo();
1916                 moveCursorUpdate(false);
1917                 owner_->showState();
1918                 break;
1919                 
1920         case LFUN_PRIOR:
1921                 if (!TEXT(bv_)->mark_set)
1922                         beforeChange(TEXT(bv_));
1923                 update(TEXT(bv_), BufferView::UPDATE);
1924                 cursorPrevious(TEXT(bv_));
1925                 TEXT(bv_)->FinishUndo();
1926                 moveCursorUpdate(false);
1927                 owner_->showState();
1928                 break;
1929                 
1930         case LFUN_NEXT:
1931                 if (!TEXT(bv_)->mark_set)
1932                         beforeChange(TEXT(bv_));
1933                 update(TEXT(bv_), BufferView::UPDATE);
1934                 cursorNext(TEXT(bv_));
1935                 TEXT(bv_)->FinishUndo();
1936                 moveCursorUpdate(false);
1937                 owner_->showState();
1938                 break;
1939                 
1940         case LFUN_HOME:
1941                 if (!TEXT(bv_)->mark_set)
1942                         beforeChange(TEXT(bv_));
1943                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
1944                 TEXT(bv_)->CursorHome(bv_);
1945                 TEXT(bv_)->FinishUndo();
1946                 moveCursorUpdate(false);
1947                 owner_->showState();
1948                 break;
1949                 
1950         case LFUN_END:
1951                 if (!TEXT(bv_)->mark_set)
1952                         beforeChange(TEXT(bv_));
1953                 update(TEXT(bv_),
1954                        BufferView::SELECT|BufferView::FITCUR);
1955                 TEXT(bv_)->CursorEnd(bv_);
1956                 TEXT(bv_)->FinishUndo();
1957                 moveCursorUpdate(false);
1958                 owner_->showState();
1959                 break;
1960                 
1961         case LFUN_SHIFT_TAB:
1962         case LFUN_TAB:
1963                 if (!TEXT(bv_)->mark_set)
1964                         beforeChange(TEXT(bv_));
1965                 update(TEXT(bv_),
1966                        BufferView::SELECT|BufferView::FITCUR);
1967                 TEXT(bv_)->CursorTab(bv_);
1968                 TEXT(bv_)->FinishUndo();
1969                 moveCursorUpdate(false);
1970                 owner_->showState();
1971                 break;
1972                 
1973         case LFUN_WORDRIGHT:
1974                 if (!TEXT(bv_)->mark_set)
1975                         beforeChange(TEXT(bv_));
1976                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
1977                 if (TEXT(bv_)->cursor.par()->isRightToLeftPar(buffer_->params))
1978                         TEXT(bv_)->CursorLeftOneWord(bv_);
1979                 else
1980                         TEXT(bv_)->CursorRightOneWord(bv_);
1981                 TEXT(bv_)->FinishUndo();
1982                 moveCursorUpdate(false);
1983                 owner_->showState();
1984                 break;
1985                 
1986         case LFUN_WORDLEFT:
1987                 if (!TEXT(bv_)->mark_set)
1988                         beforeChange(TEXT(bv_));
1989                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
1990                 if (TEXT(bv_)->cursor.par()->isRightToLeftPar(buffer_->params))
1991                         TEXT(bv_)->CursorRightOneWord(bv_);
1992                 else
1993                         TEXT(bv_)->CursorLeftOneWord(bv_);
1994                 TEXT(bv_)->FinishUndo();
1995                 moveCursorUpdate(false);
1996                 owner_->showState();
1997                 break;
1998                 
1999         case LFUN_BEGINNINGBUF:
2000                 if (!TEXT(bv_)->mark_set)
2001                         beforeChange(TEXT(bv_));
2002                 update(TEXT(bv_),
2003                        BufferView::SELECT|BufferView::FITCUR);
2004                 TEXT(bv_)->CursorTop(bv_);
2005                 TEXT(bv_)->FinishUndo();
2006                 moveCursorUpdate(false);
2007                 owner_->showState();
2008                 break;
2009                 
2010         case LFUN_ENDBUF:
2011                 if (!TEXT(bv_)->mark_set)
2012                         beforeChange(TEXT(bv_));
2013                 update(TEXT(bv_),
2014                        BufferView::SELECT|BufferView::FITCUR);
2015                 TEXT(bv_)->CursorBottom(bv_);
2016                 TEXT(bv_)->FinishUndo();
2017                 moveCursorUpdate(false);
2018                 owner_->showState();
2019                 break;
2020
2021       
2022                 /* cursor selection ---------------------------- */
2023         case LFUN_RIGHTSEL:
2024                 update(TEXT(bv_),
2025                        BufferView::SELECT|BufferView::FITCUR);
2026                 if (TEXT(bv_)->cursor.par()->isRightToLeftPar(buffer_->params))
2027                         TEXT(bv_)->CursorLeft(bv_);
2028                 else
2029                         TEXT(bv_)->CursorRight(bv_);
2030                 TEXT(bv_)->FinishUndo();
2031                 moveCursorUpdate(true);
2032                 owner_->showState();
2033                 break;
2034                 
2035         case LFUN_LEFTSEL:
2036                 update(TEXT(bv_),
2037                        BufferView::SELECT|BufferView::FITCUR);
2038                 if (TEXT(bv_)->cursor.par()->isRightToLeftPar(buffer_->params))
2039                         TEXT(bv_)->CursorRight(bv_);
2040                 else
2041                         TEXT(bv_)->CursorLeft(bv_);
2042                 TEXT(bv_)->FinishUndo();
2043                 moveCursorUpdate(true);
2044                 owner_->showState();
2045                 break;
2046                 
2047         case LFUN_UPSEL:
2048                 update(TEXT(bv_),
2049                        BufferView::SELECT|BufferView::FITCUR);
2050                 TEXT(bv_)->CursorUp(bv_);
2051                 TEXT(bv_)->FinishUndo();
2052                 moveCursorUpdate(true);
2053                 owner_->showState();
2054                 break;
2055                 
2056         case LFUN_DOWNSEL:
2057                 update(TEXT(bv_),
2058                        BufferView::SELECT|BufferView::FITCUR);
2059                 TEXT(bv_)->CursorDown(bv_);
2060                 TEXT(bv_)->FinishUndo();
2061                 moveCursorUpdate(true);
2062                 owner_->showState();
2063                 break;
2064
2065         case LFUN_UP_PARAGRAPHSEL:
2066                 update(TEXT(bv_),
2067                        BufferView::SELECT|BufferView::FITCUR);
2068                 TEXT(bv_)->CursorUpParagraph(bv_);
2069                 TEXT(bv_)->FinishUndo();
2070                 moveCursorUpdate(true);
2071                 owner_->showState();
2072                 break;
2073                 
2074         case LFUN_DOWN_PARAGRAPHSEL:
2075                 update(TEXT(bv_),
2076                        BufferView::SELECT|BufferView::FITCUR);
2077                 TEXT(bv_)->CursorDownParagraph(bv_);
2078                 TEXT(bv_)->FinishUndo();
2079                 moveCursorUpdate(true);
2080                 owner_->showState();
2081                 break;
2082                 
2083         case LFUN_PRIORSEL:
2084                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2085                 cursorPrevious(TEXT(bv_));
2086                 TEXT(bv_)->FinishUndo();
2087                 moveCursorUpdate(true);
2088                 owner_->showState();
2089                 break;
2090                 
2091         case LFUN_NEXTSEL:
2092                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2093                 cursorNext(TEXT(bv_));
2094                 TEXT(bv_)->FinishUndo();
2095                 moveCursorUpdate(true);
2096                 owner_->showState();
2097                 break;
2098                 
2099         case LFUN_HOMESEL:
2100                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2101                 TEXT(bv_)->CursorHome(bv_);
2102                 TEXT(bv_)->FinishUndo();
2103                 moveCursorUpdate(true);
2104                 owner_->showState();
2105                 break;
2106                 
2107         case LFUN_ENDSEL:
2108                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2109                 TEXT(bv_)->CursorEnd(bv_);
2110                 TEXT(bv_)->FinishUndo();
2111                 moveCursorUpdate(true);
2112                 owner_->showState();
2113                 break;
2114                 
2115         case LFUN_WORDRIGHTSEL:
2116                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2117                 if (TEXT(bv_)->cursor.par()->isRightToLeftPar(buffer_->params))
2118                         TEXT(bv_)->CursorLeftOneWord(bv_);
2119                 else
2120                         TEXT(bv_)->CursorRightOneWord(bv_);
2121                 TEXT(bv_)->FinishUndo();
2122                 moveCursorUpdate(true);
2123                 owner_->showState();
2124                 break;
2125                 
2126         case LFUN_WORDLEFTSEL:
2127                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2128                 if (TEXT(bv_)->cursor.par()->isRightToLeftPar(buffer_->params))
2129                         TEXT(bv_)->CursorRightOneWord(bv_);
2130                 else
2131                         TEXT(bv_)->CursorLeftOneWord(bv_);
2132                 TEXT(bv_)->FinishUndo();
2133                 moveCursorUpdate(true);
2134                 owner_->showState();
2135                 break;
2136                 
2137         case LFUN_BEGINNINGBUFSEL:
2138                 if (TEXT(bv_)->inset_owner)
2139                         break;
2140                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2141                 TEXT(bv_)->CursorTop(bv_);
2142                 TEXT(bv_)->FinishUndo();
2143                 moveCursorUpdate(true);
2144                 owner_->showState();
2145                 break;
2146                 
2147         case LFUN_ENDBUFSEL:
2148                 if (TEXT(bv_)->inset_owner)
2149                         break;
2150                 update(TEXT(bv_),
2151                        BufferView::SELECT|BufferView::FITCUR);
2152                 TEXT(bv_)->CursorBottom(bv_);
2153                 TEXT(bv_)->FinishUndo();
2154                 moveCursorUpdate(true);
2155                 owner_->showState();
2156                 break;
2157
2158                 // --- text changing commands ------------------------
2159         case LFUN_BREAKLINE:
2160                 beforeChange(TEXT(bv_));
2161                 TEXT(bv_)->InsertChar(bv_, LyXParagraph::META_NEWLINE);
2162                 update(TEXT(bv_),
2163                        BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2164                 moveCursorUpdate(false);
2165                 break;
2166                 
2167         case LFUN_PROTECTEDSPACE:
2168         {
2169                 LyXLayout const & style =
2170                         textclasslist.Style(buffer_->params.textclass,
2171                                             TEXT(bv_)->cursor.par()->GetLayout());
2172
2173                 if (style.free_spacing) {
2174                         TEXT(bv_)->InsertChar(bv_, ' ');
2175                         update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2176                 } else {
2177                         bv_->protectedBlank(TEXT(bv_));
2178                 }
2179                 moveCursorUpdate(false);
2180         }
2181         break;
2182                 
2183         case LFUN_SETMARK:
2184                 if (TEXT(bv_)->mark_set) {
2185                         beforeChange(TEXT(bv_));
2186                         update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2187                         owner_->getLyXFunc()->setMessage(N_("Mark removed"));
2188                 } else {
2189                         beforeChange(TEXT(bv_));
2190                         TEXT(bv_)->mark_set = 1;
2191                         update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2192                         owner_->getLyXFunc()->setMessage(N_("Mark set"));
2193                 }
2194                 TEXT(bv_)->sel_cursor = TEXT(bv_)->cursor;
2195                 break;
2196                 
2197         case LFUN_DELETE:
2198                 if (!TEXT(bv_)->selection) {
2199                         TEXT(bv_)->Delete(bv_);
2200                         TEXT(bv_)->sel_cursor = 
2201                                 TEXT(bv_)->cursor;
2202                         update(TEXT(bv_),
2203                                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2204                         // It is possible to make it a lot faster still
2205                         // just comment out the line below...
2206                         showCursor();
2207                 } else {
2208                         bv_->cut();
2209                 }
2210                 moveCursorUpdate(false);
2211                 owner_->showState();
2212                 setState();
2213                 break;
2214
2215         case LFUN_DELETE_SKIP:
2216         {
2217                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
2218                 
2219                 LyXCursor cursor = TEXT(bv_)->cursor;
2220
2221                 if (!TEXT(bv_)->selection) {
2222                         if (cursor.pos() == cursor.par()->Last()) {
2223                                 TEXT(bv_)->CursorRight(bv_);
2224                                 cursor = TEXT(bv_)->cursor;
2225                                 if (cursor.pos() == 0
2226                                     && !(cursor.par()->params.spaceTop()
2227                                          == VSpace (VSpace::NONE))) {
2228                                         TEXT(bv_)->SetParagraph
2229                                                 (bv_,
2230                                                  cursor.par()->params.lineTop(),
2231                                                  cursor.par()->params.lineBottom(),
2232                                                  cursor.par()->params.pagebreakTop(), 
2233                                                  cursor.par()->params.pagebreakBottom(),
2234                                                  VSpace(VSpace::NONE), 
2235                                                  cursor.par()->params.spaceBottom(),
2236                                                  cursor.par()->params.align(), 
2237                                                  cursor.par()->params.labelWidthString(), 0);
2238                                         TEXT(bv_)->CursorLeft(bv_);
2239                                         update(TEXT(bv_), 
2240                                                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2241                                 } else {
2242                                         TEXT(bv_)->CursorLeft(bv_);
2243                                         TEXT(bv_)->Delete(bv_);
2244                                         TEXT(bv_)->sel_cursor = 
2245                                                 TEXT(bv_)->cursor;
2246                                         update(TEXT(bv_),
2247                                                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2248                                 }
2249                         } else {
2250                                 TEXT(bv_)->Delete(bv_);
2251                                 TEXT(bv_)->sel_cursor = 
2252                                         TEXT(bv_)->cursor;
2253                                 update(TEXT(bv_),
2254                                        BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2255                         }
2256                 } else {
2257                         bv_->cut();
2258                 }
2259         }
2260         break;
2261
2262         /* -------> Delete word forward. */
2263         case LFUN_DELETE_WORD_FORWARD:
2264                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2265                 TEXT(bv_)->DeleteWordForward(bv_);
2266                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2267                 moveCursorUpdate(false);
2268                 owner_->showState();
2269                 break;
2270
2271                 /* -------> Delete word backward. */
2272         case LFUN_DELETE_WORD_BACKWARD:
2273                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2274                 TEXT(bv_)->DeleteWordBackward(bv_);
2275                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2276                 moveCursorUpdate(false);
2277                 owner_->showState();
2278                 break;
2279                 
2280                 /* -------> Kill to end of line. */
2281         case LFUN_DELETE_LINE_FORWARD:
2282                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2283                 TEXT(bv_)->DeleteLineForward(bv_);
2284                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2285                 moveCursorUpdate(false);
2286                 break;
2287                 
2288                 /* -------> Set mark off. */
2289         case LFUN_MARK_OFF:
2290                 beforeChange(TEXT(bv_));
2291                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2292                 TEXT(bv_)->sel_cursor = TEXT(bv_)->cursor;
2293                 owner_->getLyXFunc()->setMessage(N_("Mark off"));
2294                 break;
2295
2296                 /* -------> Set mark on. */
2297         case LFUN_MARK_ON:
2298                 beforeChange(TEXT(bv_));
2299                 TEXT(bv_)->mark_set = 1;
2300                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR);
2301                 TEXT(bv_)->sel_cursor = TEXT(bv_)->cursor;
2302                 owner_->getLyXFunc()->setMessage(N_("Mark on"));
2303                 break;
2304                 
2305         case LFUN_BACKSPACE:
2306         {
2307                 if (!TEXT(bv_)->selection) {
2308                         if (owner_->getIntl()->getTrans().backspace()) {
2309                                 TEXT(bv_)->Backspace(bv_);
2310                                 TEXT(bv_)->sel_cursor = 
2311                                         TEXT(bv_)->cursor;
2312                                 update(TEXT(bv_),
2313                                        BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2314                                 // It is possible to make it a lot faster still
2315                                 // just comment out the line below...
2316                                 showCursor();
2317                         }
2318                 } else {
2319                         bv_->cut();
2320                 }
2321                 owner_->showState();
2322                 setState();
2323         }
2324         break;
2325
2326         case LFUN_BACKSPACE_SKIP:
2327         {
2328                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
2329                 
2330                 LyXCursor cursor = TEXT(bv_)->cursor;
2331                 
2332                 if (!TEXT(bv_)->selection) {
2333                         if (cursor.pos() == 0 
2334                             && !(cursor.par()->params.spaceTop() 
2335                                  == VSpace (VSpace::NONE))) {
2336                                 TEXT(bv_)->SetParagraph 
2337                                         (bv_,
2338                                          cursor.par()->params.lineTop(),      
2339                                          cursor.par()->params.lineBottom(),
2340                                          cursor.par()->params.pagebreakTop(), 
2341                                          cursor.par()->params.pagebreakBottom(),
2342                                          VSpace(VSpace::NONE), cursor.par()->params.spaceBottom(),
2343                                          cursor.par()->params.align(), 
2344                                          cursor.par()->params.labelWidthString(), 0);
2345                                 update(TEXT(bv_),
2346                                        BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2347                         } else {
2348                                 TEXT(bv_)->Backspace(bv_);
2349                                 TEXT(bv_)->sel_cursor 
2350                                         = cursor;
2351                                 update(TEXT(bv_),
2352                                        BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2353                         }
2354                 } else
2355                         bv_->cut();
2356         }
2357         break;
2358
2359         case LFUN_BREAKPARAGRAPH:
2360         {
2361                 beforeChange(TEXT(bv_));
2362                 TEXT(bv_)->BreakParagraph(bv_, 0);
2363                 update(TEXT(bv_),
2364                        BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2365                 TEXT(bv_)->sel_cursor = TEXT(bv_)->cursor;
2366                 setState();
2367                 owner_->showState();
2368                 break;
2369         }
2370
2371         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
2372         {
2373                 beforeChange(TEXT(bv_));
2374                 TEXT(bv_)->BreakParagraph(bv_, 1);
2375                 update(TEXT(bv_),
2376                        BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2377                 TEXT(bv_)->sel_cursor = TEXT(bv_)->cursor;
2378                 setState();
2379                 owner_->showState();
2380                 break;
2381         }
2382         
2383         case LFUN_BREAKPARAGRAPH_SKIP:
2384         {
2385                 // When at the beginning of a paragraph, remove
2386                 // indentation and add a "defskip" at the top.
2387                 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
2388                 
2389                 LyXCursor cursor = TEXT(bv_)->cursor;
2390                 
2391                 beforeChange(TEXT(bv_));
2392                 if (cursor.pos() == 0) {
2393                         if (cursor.par()->params.spaceTop() == VSpace(VSpace::NONE)) {
2394                                 TEXT(bv_)->SetParagraph
2395                                         (bv_,
2396                                          cursor.par()->params.lineTop(),      
2397                                          cursor.par()->params.lineBottom(),
2398                                          cursor.par()->params.pagebreakTop(), 
2399                                          cursor.par()->params.pagebreakBottom(),
2400                                          VSpace(VSpace::DEFSKIP), cursor.par()->params.spaceBottom(),
2401                                          cursor.par()->params.align(), 
2402                                          cursor.par()->params.labelWidthString(), 1);
2403                                 //update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2404                         } 
2405                 }
2406                 else {
2407                         TEXT(bv_)->BreakParagraph(bv_, 0);
2408                         //update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2409                 }
2410
2411                 update(TEXT(bv_),
2412                        BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2413                 TEXT(bv_)->sel_cursor = cursor;
2414                 setState();
2415                 owner_->showState();
2416         }
2417         break;
2418
2419         case LFUN_PARAGRAPH_SPACING:
2420         {
2421                 LyXParagraph * par = TEXT(bv_)->cursor.par();
2422                 Spacing::Space cur_spacing = par->params.spacing().getSpace();
2423                 float cur_value = 1.0;
2424                 if (cur_spacing == Spacing::Other) {
2425                         cur_value = par->params.spacing().getValue();
2426                 }
2427                 
2428                 istringstream istr(argument.c_str());
2429
2430                 string tmp;
2431                 istr >> tmp;
2432                 Spacing::Space new_spacing = cur_spacing;
2433                 float new_value = cur_value;
2434                 if (tmp.empty()) {
2435                         lyxerr << "Missing argument to `paragraph-spacing'"
2436                                << endl;
2437                 } else if (tmp == "single") {
2438                         new_spacing = Spacing::Single;
2439                 } else if (tmp == "onehalf") {
2440                         new_spacing = Spacing::Onehalf;
2441                 } else if (tmp == "double") {
2442                         new_spacing = Spacing::Double;
2443                 } else if (tmp == "other") {
2444                         new_spacing = Spacing::Other;
2445                         float tmpval = 0.0;
2446                         istr >> tmpval;
2447                         lyxerr << "new_value = " << tmpval << endl;
2448                         if (tmpval != 0.0)
2449                                 new_value = tmpval;
2450                 } else if (tmp == "default") {
2451                         new_spacing = Spacing::Default;
2452                 } else {
2453                         lyxerr << _("Unknown spacing argument: ")
2454                                << argument << endl;
2455                 }
2456                 if (cur_spacing != new_spacing || cur_value != new_value) {
2457                         par->params.spacing(Spacing(new_spacing, new_value));
2458                         TEXT(bv_)->RedoParagraph(bv_);
2459                         update(TEXT(bv_),
2460                                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2461                 }
2462         }
2463         break;
2464         
2465         case LFUN_QUOTE:
2466                 beforeChange(TEXT(bv_));
2467                 TEXT(bv_)->InsertChar(bv_, '\"');  // This " matches the single quote in the code
2468                 update(TEXT(bv_), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2469                 moveCursorUpdate(false);
2470                 break;
2471
2472         case LFUN_HTMLURL:
2473         case LFUN_URL:
2474         {
2475                 InsetCommandParams p;
2476                 if (action == LFUN_HTMLURL)
2477                         p.setCmdName("htmlurl");
2478                 else
2479                         p.setCmdName("url");
2480                 owner_->getDialogs()->createUrl( p.getAsString() );
2481         }
2482         break;
2483                     
2484         case LFUN_INSERT_URL:
2485         {
2486                 InsetCommandParams p;
2487                 p.setFromString( argument );
2488
2489                 InsetUrl * inset = new InsetUrl( p );
2490                 if (!bv_->insertInset(inset))
2491                         delete inset;
2492                 else
2493                         bv_->updateInset( inset, true );
2494         }
2495         break;
2496                     
2497         case LFUN_INSET_TEXT:
2498         {
2499                 InsetText * new_inset = new InsetText;
2500                 if (bv_->insertInset(new_inset))
2501                         new_inset->Edit(bv_, 0, 0, 0);
2502                 else
2503                         delete new_inset;
2504         }
2505         break;
2506         
2507         case LFUN_INSET_ERT:
2508         {
2509                 InsetERT * new_inset = new InsetERT;
2510                 if (bv_->insertInset(new_inset))
2511                         new_inset->Edit(bv_, 0, 0, 0);
2512                 else
2513                         delete new_inset;
2514         }
2515         break;
2516         
2517         case LFUN_INSET_EXTERNAL:
2518         {
2519                 InsetExternal * new_inset = new InsetExternal;
2520                 if (bv_->insertInset(new_inset))
2521                         new_inset->Edit(bv_, 0, 0, 0);
2522                 else
2523                         delete new_inset;
2524         }
2525         break;
2526         
2527         case LFUN_INSET_FOOTNOTE:
2528         {
2529                 InsetFoot * new_inset = new InsetFoot;
2530                 if (bv_->insertInset(new_inset))
2531                         new_inset->Edit(bv_, 0, 0, 0);
2532                 else
2533                         delete new_inset;
2534         }
2535         break;
2536
2537         case LFUN_INSET_MARGINAL:
2538         {
2539                 InsetMarginal * new_inset = new InsetMarginal;
2540                 if (bv_->insertInset(new_inset))
2541                         new_inset->Edit(bv_, 0, 0, 0);
2542                 else
2543                         delete new_inset;
2544         }
2545         break;
2546
2547         case LFUN_INSET_MINIPAGE:
2548         {
2549                 InsetMinipage * new_inset = new InsetMinipage;
2550                 if (bv_->insertInset(new_inset))
2551                         new_inset->Edit(bv_, 0, 0, 0);
2552                 else
2553                         delete new_inset;
2554         }
2555         break;
2556
2557         case LFUN_INSET_FLOAT:
2558         {
2559                 // check if the float type exist
2560                 if (floatList.typeExist(argument)) {
2561                         InsetFloat * new_inset = new InsetFloat(argument);
2562                         if (bv_->insertInset(new_inset))
2563                                 new_inset->Edit(bv_, 0, 0, 0);
2564                         else
2565                                 delete new_inset;
2566                 } else {
2567                         lyxerr << "Non-existant float type: "
2568                                << argument << endl;
2569                 }
2570                 
2571         }
2572         break;
2573
2574         case LFUN_INSET_LIST:
2575         {
2576                 InsetList * new_inset = new InsetList;
2577                 if (bv_->insertInset(new_inset))
2578                         new_inset->Edit(bv_, 0, 0, 0);
2579                 else
2580                         delete new_inset;
2581         }
2582         break;
2583
2584         case LFUN_INSET_THEOREM:
2585         {
2586                 InsetTheorem * new_inset = new InsetTheorem;
2587                 if (bv_->insertInset(new_inset))
2588                         new_inset->Edit(bv_, 0, 0, 0);
2589                 else
2590                         delete new_inset;
2591         }
2592         break;
2593
2594         case LFUN_INSET_CAPTION:
2595         {
2596                 // Do we have a locking inset...
2597                 if (bv_->theLockingInset()) {
2598                         lyxerr << "Locking inset code: "
2599                                << static_cast<int>(bv_->theLockingInset()->LyxCode());
2600                         InsetCaption * new_inset = new InsetCaption;
2601                         new_inset->setOwner(bv_->theLockingInset());
2602                         new_inset->SetAutoBreakRows(true);
2603                         new_inset->SetDrawFrame(0, InsetText::LOCKED);
2604                         new_inset->SetFrameColor(0, LColor::footnoteframe);
2605                         if (bv_->insertInset(new_inset))
2606                                 new_inset->Edit(bv_, 0, 0, 0);
2607                         else
2608                                 delete new_inset;
2609                 }
2610         }
2611         break;
2612         
2613         case LFUN_INSET_TABULAR:
2614         {
2615                 int r = 2, c = 2;
2616                 if (!argument.empty())
2617                         ::sscanf(argument.c_str(),"%d%d", &r, &c);
2618                 InsetTabular * new_inset =
2619                         new InsetTabular(*buffer_, r, c);
2620                 bool rtl =
2621                         TEXT(bv_)->real_current_font.isRightToLeft();
2622                 if (!bv_->open_new_inset(new_inset, rtl))
2623                         delete new_inset;
2624         }
2625         break;
2626
2627         // --- lyxserver commands ----------------------------
2628
2629         case LFUN_CHARATCURSOR:
2630         {
2631                 LyXParagraph::size_type pos = TEXT(bv_)->cursor.pos();
2632                 if (pos < TEXT(bv_)->cursor.par()->size())
2633                         owner_->getLyXFunc()->setMessage(
2634                                 tostr(TEXT(bv_)->cursor.par()->GetChar(pos)));
2635                 else
2636                         owner_->getLyXFunc()->setMessage("EOF");
2637         }
2638         break;
2639         
2640         case LFUN_GETXY:
2641                 owner_->getLyXFunc()->setMessage(tostr(TEXT(bv_)->cursor.x())
2642                                                  + ' '
2643                                                  + tostr(TEXT(bv_)->cursor.y()));
2644                 break;
2645                 
2646         case LFUN_SETXY:
2647         {
2648                 int x;
2649                 int y;
2650                 ::sscanf(argument.c_str(), " %d %d", &x, &y);
2651                 TEXT(bv_)->SetCursorFromCoordinates(bv_, x, y);
2652         }
2653         break;
2654         
2655         case LFUN_GETLAYOUT:
2656                 owner_->getLyXFunc()->setMessage(tostr(TEXT(bv_)->cursor.par()->layout));
2657                 break;
2658                         
2659         case LFUN_GETFONT:
2660         {
2661                 LyXFont & font = TEXT(bv_)->current_font;
2662                 if (font.shape() == LyXFont::ITALIC_SHAPE)
2663                         owner_->getLyXFunc()->setMessage("E");
2664                 else if (font.shape() == LyXFont::SMALLCAPS_SHAPE)
2665                         owner_->getLyXFunc()->setMessage("N");
2666                 else
2667                         owner_->getLyXFunc()->setMessage("0");
2668
2669         }
2670         break;
2671
2672         case LFUN_GETLATEX:
2673         {
2674                 LyXFont & font = TEXT(bv_)->current_font;
2675                 if (font.latex() == LyXFont::ON)
2676                         owner_->getLyXFunc()->setMessage("L");
2677                 else
2678                         owner_->getLyXFunc()->setMessage("0");
2679         }
2680         break;
2681
2682         // --- accented characters ---------------------------
2683                 
2684         case LFUN_UMLAUT:
2685         case LFUN_CIRCUMFLEX:
2686         case LFUN_GRAVE:
2687         case LFUN_ACUTE:
2688         case LFUN_TILDE:
2689         case LFUN_CEDILLA:
2690         case LFUN_MACRON:
2691         case LFUN_DOT:
2692         case LFUN_UNDERDOT:
2693         case LFUN_UNDERBAR:
2694         case LFUN_CARON:
2695         case LFUN_SPECIAL_CARON:
2696         case LFUN_BREVE:
2697         case LFUN_TIE:
2698         case LFUN_HUNG_UMLAUT:
2699         case LFUN_CIRCLE:
2700         case LFUN_OGONEK:
2701                 owner_->getLyXFunc()->handleKeyFunc(action);
2702                 break;
2703         
2704         // --- insert characters ----------------------------------------
2705         
2706         case LFUN_MATH_DELIM:     
2707         case LFUN_INSERT_MATRIX:
2708         {          
2709                 if (available()) { 
2710                         if (bv_->open_new_inset(new InsetFormula(false)))
2711                         {
2712                                 bv_->theLockingInset()
2713                                         ->LocalDispatch(bv_, action, argument);
2714                         }
2715                 }
2716         }          
2717         break;
2718                
2719         case LFUN_INSERT_MATH:
2720         {
2721                 math_insert_symbol(bv_, argument);
2722         }
2723         break;
2724         
2725         case LFUN_MATH_DISPLAY:
2726         {
2727                 if (available())
2728                         bv_->open_new_inset(new InsetFormula(true));
2729                 break;
2730         }
2731                     
2732         case LFUN_MATH_MACRO:
2733         {
2734                 if (available()) {
2735                         string s(argument);
2736                         if (s.empty())
2737                                 owner_->getLyXFunc()->setErrorMessage(N_("Missing argument"));
2738                         else {
2739                                 string const s1 = token(s, ' ', 1);
2740                                 int const na = s1.empty() ? 0 : lyx::atoi(s1);
2741                                 bv_->open_new_inset(new InsetFormulaMacro(token(s, ' ', 0), na));
2742                         }
2743                 }
2744         }
2745         break;
2746
2747         case LFUN_MATH_MODE:   // Open or create a math inset
2748         {               
2749                 if (available())
2750                         bv_->open_new_inset(new InsetFormula);
2751                 owner_->getLyXFunc()->setMessage(N_("Math editor mode"));
2752         }
2753         break;
2754           
2755         case LFUN_CITATION_INSERT:
2756         {
2757                 InsetCommandParams p;
2758                 p.setFromString( argument );
2759
2760                 InsetCitation * inset = new InsetCitation( p );
2761                 if (!bv_->insertInset(inset))
2762                         delete inset;
2763                 else
2764                         bv_->updateInset( inset, true );
2765         }
2766         break;
2767                     
2768         case LFUN_INSERT_BIBTEX:
2769         {   
2770                 // ale970405+lasgoutt970425
2771                 // The argument can be up to two tokens separated 
2772                 // by a space. The first one is the bibstyle.
2773                 string const db       = token(argument, ' ', 0);
2774                 string bibstyle = token(argument, ' ', 1);
2775                 if (bibstyle.empty())
2776                         bibstyle = "plain";
2777
2778                 InsetCommandParams p( "BibTeX", db, bibstyle );
2779                 InsetBibtex * inset = new InsetBibtex(p);
2780                 
2781                 if (bv_->insertInset(inset)) {
2782                         if (argument.empty())
2783                                 inset->Edit(bv_, 0, 0, 0);
2784                 } else
2785                         delete inset;
2786         }
2787         break;
2788                 
2789         // BibTeX data bases
2790         case LFUN_BIBDB_ADD:
2791         {
2792                 InsetBibtex * inset = 
2793                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2794                 if (inset) {
2795                         inset->addDatabase(argument);
2796                 }
2797         }
2798         break;
2799                     
2800         case LFUN_BIBDB_DEL:
2801         {
2802                 InsetBibtex * inset = 
2803                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2804                 if (inset) {
2805                         inset->delDatabase(argument);
2806                 }
2807         }
2808         break;
2809         
2810         case LFUN_BIBTEX_STYLE:
2811         {
2812                 InsetBibtex * inset = 
2813                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2814                 if (inset) {
2815                         inset->setOptions(argument);
2816                 }
2817         }
2818         break;
2819                 
2820         case LFUN_INDEX_CREATE:
2821         {
2822                 InsetCommandParams p( "index" );
2823                 
2824                 if (argument.empty()) {
2825                         // Get the word immediately preceding the cursor
2826                         LyXParagraph::size_type curpos = 
2827                                 TEXT(bv_)->cursor.pos() - 1;
2828
2829                         string curstring;
2830                         if (curpos >= 0 )
2831                                 curstring = TEXT(bv_)
2832                                         ->cursor.par()->GetWord(curpos);
2833
2834                         p.setContents( curstring );
2835                 } else {
2836                         p.setContents( argument );
2837                 }
2838
2839                 owner_->getDialogs()->createIndex( p.getAsString() );
2840         }
2841         break;
2842                     
2843         case LFUN_INDEX_INSERT:
2844         {
2845                 InsetCommandParams p;
2846                 p.setFromString(argument);
2847                 InsetIndex * inset = new InsetIndex(p);
2848
2849                 if (!bv_->insertInset(inset))
2850                         delete inset;
2851                 else
2852                         bv_->updateInset(inset, true);
2853         }
2854         break;
2855                     
2856         case LFUN_INDEX_INSERT_LAST:
2857         {
2858                 // Get word immediately preceding the cursor
2859                 LyXParagraph::size_type curpos = 
2860                         TEXT(bv_)->cursor.pos() - 1;
2861                 // Can't do that at the beginning of a paragraph
2862                 if (curpos < 0) break;
2863
2864                 string const curstring(TEXT(bv_)
2865                                        ->cursor.par()->GetWord(curpos));
2866
2867                 InsetCommandParams p("index", curstring);
2868                 InsetIndex * inset = new InsetIndex(p);
2869
2870                 if (!bv_->insertInset(inset))
2871                         delete inset;
2872                 else
2873                         bv_->updateInset(inset, true);
2874         }
2875         break;
2876
2877         case LFUN_INDEX_PRINT:
2878         {
2879                 InsetCommandParams p("printindex");
2880                 Inset * inset = new InsetPrintIndex(p);
2881                 if (!bv_->insertInset(inset, "Standard", true))
2882                         delete inset;
2883         }
2884         break;
2885
2886         case LFUN_PARENTINSERT:
2887         {
2888                 lyxerr << "arg " << argument << endl;
2889                 InsetCommandParams p( "lyxparent", argument );
2890                 Inset * inset = new InsetParent(p, *buffer_);
2891                 if (!bv_->insertInset(inset, "Standard", true))
2892                         delete inset;
2893         }
2894         break;
2895
2896         case LFUN_CHILD_INSERT:
2897         {
2898                 InsetCommandParams p;
2899                 p.setFromString(argument);
2900
2901                 InsetInclude * inset = new InsetInclude(p, *buffer_);
2902                 if (!bv_->insertInset(inset))
2903                         delete inset;
2904                 else
2905                         bv_->updateInset(inset, true);
2906         }
2907         break; 
2908
2909         case LFUN_INSERT_NOTE:
2910                 bv_->insertNote();
2911                 break;
2912
2913         case LFUN_SELFINSERT:
2914         {
2915                 LyXFont const old_font(TEXT(bv_)->real_current_font);
2916                 for (string::size_type i = 0; i < argument.length(); ++i) {
2917                         TEXT(bv_)->InsertChar(bv_, argument[i]);
2918                         // This needs to be in the loop, or else we
2919                         // won't break lines correctly. (Asger)
2920                         update(TEXT(bv_),
2921                                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2922                 }
2923                 TEXT(bv_)->sel_cursor = TEXT(bv_)->cursor;
2924                 moveCursorUpdate(false);
2925
2926                 // real_current_font.number can change so we need to 
2927                 // update the minibuffer
2928                 if (old_font != TEXT(bv_)->real_current_font)
2929                         owner_->showState();
2930         }
2931         break;
2932
2933         case LFUN_DATE_INSERT:  // jdblair: date-insert cmd
2934         {
2935                 struct tm * now_tm;
2936                 
2937                 time_t now_time_t = time(NULL);
2938                 now_tm = localtime(&now_time_t);
2939                 setlocale(LC_TIME, "");
2940                 string arg;
2941                 if (!argument.empty())
2942                         arg = argument;
2943                 else 
2944                         arg = lyxrc.date_insert_format;
2945                 char datetmp[32];
2946                 int const datetmp_len =
2947                         ::strftime(datetmp, 32, arg.c_str(), now_tm);
2948                 for (int i = 0; i < datetmp_len; i++) {
2949                         TEXT(bv_)->InsertChar(bv_, datetmp[i]);
2950                         update(TEXT(bv_),
2951                                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2952                 }
2953
2954                 TEXT(bv_)->sel_cursor = TEXT(bv_)->cursor;
2955                 moveCursorUpdate(false);
2956         }
2957         break;
2958
2959         case LFUN_UNKNOWN_ACTION:
2960         {
2961                 if (!buffer_) {
2962                         owner_->getLyXFunc()->setErrorMessage(N_("No document open"));
2963                         break;
2964                 }
2965
2966                 if (buffer_->isReadonly()) {
2967                         owner_->getLyXFunc()->setErrorMessage(N_("Document is read only"));
2968                         break;
2969                 }
2970                 if (!argument.empty()) {
2971                         /* Automatically delete the currently selected
2972                          * text and replace it with what is being
2973                          * typed in now. Depends on lyxrc settings
2974                          * "auto_region_delete", which defaults to
2975                          * true (on). */
2976                 
2977                         if (lyxrc.auto_region_delete) {
2978                                 if (TEXT(bv_)->selection){
2979                                         TEXT(bv_)->CutSelection(bv_, false);
2980                                         update(TEXT(bv_),
2981                                                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2982                                 }
2983                         }
2984                         
2985                         beforeChange(TEXT(bv_));
2986                         LyXFont const old_font(TEXT(bv_)->real_current_font);
2987                         for (string::size_type i = 0;
2988                              i < argument.length(); ++i) {
2989                                 if (greek_kb_flag) {
2990                                         if (!math_insert_greek(bv_,
2991                                                                argument[i]))
2992                                                 owner_->getIntl()->getTrans().TranslateAndInsert(argument[i], TEXT(bv_));
2993                                 } else
2994                                         owner_->getIntl()->getTrans().TranslateAndInsert(argument[i], TEXT(bv_));
2995                         }
2996
2997                         update(TEXT(bv_),BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2998
2999                         TEXT(bv_)->sel_cursor = TEXT(bv_)->cursor;
3000                         moveCursorUpdate(false);
3001
3002                         // real_current_font.number can change so we need to
3003                         // update the minibuffer
3004                         if (old_font != TEXT(bv_)->real_current_font)
3005                                 owner_->showState();
3006                 } else {
3007                         // if we don't have an argument there was something
3008                         // strange going on so we just tell this to someone!
3009                         owner_->getLyXFunc()->setErrorMessage(N_("No argument givven"));
3010                 }
3011                 break;
3012         }
3013         default:
3014                 return false;
3015         } // end of switch
3016
3017         return true;
3018 }