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