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