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