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