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