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