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