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