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