]> git.lyx.org Git - lyx.git/blob - src/BufferView_pimpl.C
lfun_core.diff, make insetgraphics inline
[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                 buffer_->resizeInsets(bv_);
333         } else {
334                 // See if we have a text in TextCache that fits
335                 // the new buffer_ with the correct width.
336                 bv_->text = textcache.findFit(buffer_, workarea_.workWidth());
337                 if (bv_->text) {
338                         if (lyxerr.debugging()) {
339                                 lyxerr << "Found a LyXText that fits:\n";
340                                 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea_.workWidth(), bv_->text)));
341                         }
342                         // Set the owner of the newly found text
343                         //      bv_->text->owner(bv_);
344                         if (lyxerr.debugging())
345                                 textcache.show(lyxerr, "resizeCurrentBuffer");
346                 } else {
347                         bv_->text = new LyXText(bv_);
348                         bv_->text->init(bv_);
349                         //buffer_->resizeInsets(bv_);
350                 }
351         }
352
353         updateScreen();
354
355         if (par) {
356                 bv_->text->selection.set(true);
357                 // At this point just to avoid the Delete-Empty-Paragraph-
358                 // Mechanism when setting the cursor.
359                 bv_->text->selection.mark(mark_set);
360                 if (selection) {
361                         bv_->text->setCursor(bv_, selstartpar, selstartpos);
362                         bv_->text->selection.cursor = bv_->text->cursor;
363                         bv_->text->setCursor(bv_, selendpar, selendpos);
364                         bv_->text->setSelection(bv_);
365                         bv_->text->setCursor(bv_, par, pos);
366                 } else {
367                         bv_->text->setCursor(bv_, par, pos);
368                         bv_->text->selection.cursor = bv_->text->cursor;
369                         bv_->text->selection.set(false);
370                 }
371                 // remake the inset locking
372                 bv_->theLockingInset(the_locking_inset);
373         }
374
375         bv_->text->first_y = screen_->topCursorVisible(bv_->text);
376
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                         buffer_->resizeInsets(bv_);
1003                 } else if (heightChange) {
1004                         // Rebuild image of current screen
1005                         updateScreen();
1006                         // fitCursor() ensures we don't jump back
1007                         // to the start of the document on vertical
1008                         // resize
1009                         fitCursor();
1010
1011                         // The main window size has changed, repaint most stuff
1012                         redraw();
1013                 } else if (screen_.get())
1014                     screen_->redraw(bv_->text, bv_);
1015         } else {
1016                 // Grey box when we don't have a buffer
1017                 workarea_.greyOut();
1018         }
1019
1020         // always make sure that the scrollbar is sane.
1021         updateScrollbar();
1022         owner_->updateLayoutChoice();
1023         return;
1024 }
1025
1026
1027 void BufferView::Pimpl::update()
1028 {
1029         if (screen_.get() &&
1030                 (!bv_->theLockingInset() || !bv_->theLockingInset()->nodraw()))
1031         {
1032                 LyXText::text_status st = bv_->text->status();
1033                 screen_->update(bv_->text, bv_);
1034                 bool fitc = false;
1035                 while (bv_->text->status() == LyXText::CHANGED_IN_DRAW) {
1036                         if (bv_->text->fullRebreak(bv_)) {
1037                                 st = LyXText::NEED_MORE_REFRESH;
1038                                 bv_->text->setCursor(bv_, bv_->text->cursor.par(),
1039                                                                          bv_->text->cursor.pos());
1040                                 fitc = true;
1041                         }
1042                         bv_->text->status(bv_, st);
1043                         screen_->update(bv_->text, bv_);
1044                 }
1045                 // do this here instead of in the screen::update because of
1046                 // the above loop!
1047                 bv_->text->status(bv_, LyXText::UNCHANGED);
1048                 if (fitc)
1049                         fitCursor();
1050         }
1051 }
1052
1053 // Values used when calling update:
1054 // -3 - update
1055 // -2 - update, move sel_cursor if selection, fitcursor
1056 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
1057 //  0 - update, move sel_cursor if selection, fitcursor 
1058 //  1 - update, move sel_cursor if selection, fitcursor, mark dirty
1059 //  3 - update, move sel_cursor if selection
1060 //
1061 // update -
1062 // a simple redraw of the parts that need refresh
1063 //
1064 // move sel_cursor if selection -
1065 // the text's sel_cursor is moved if there is selection is progress
1066 //
1067 // fitcursor -
1068 // fitCursor() is called and the scrollbar updated
1069 //
1070 // mark dirty -
1071 // the buffer is marked dirty.
1072 //
1073 // enum {
1074 //       UPDATE = 0,
1075 //       SELECT = 1,
1076 //       FITCUR = 2,
1077 //       CHANGE = 4 
1078 // };
1079 //
1080 // UPDATE_ONLY = UPDATE;
1081 // UPDATE_SELECT = UPDATE | SELECT;
1082 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
1083 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
1084 //
1085 // update(-3) -> update(0)         -> update(0) -> update(UPDATE)
1086 // update(-2) -> update(1 + 2)     -> update(3) -> update(SELECT|FITCUR)
1087 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1088 // update(1)  -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1089 // update(3)  -> update(1)         -> update(1) -> update(SELECT)
1090
1091 void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
1092 {
1093         owner_->updateLayoutChoice();
1094
1095         if (!text->selection.set() && (f & SELECT)) {
1096                 text->selection.cursor = text->cursor;
1097         }
1098
1099         text->fullRebreak(bv_);
1100
1101         if (text->inset_owner) {
1102                 text->inset_owner->setUpdateStatus(bv_, InsetText::NONE);
1103             updateInset(text->inset_owner, false);
1104         } else {
1105             update();
1106         }
1107                 
1108         if ((f & FITCUR)) {
1109                 fitCursor();
1110         }
1111
1112         if ((f & CHANGE)) {
1113                 buffer_->markDirty();
1114         }
1115 }
1116
1117
1118 // Callback for cursor timer
1119 void BufferView::Pimpl::cursorToggle()
1120 {
1121         if (!buffer_) {
1122                 cursor_timeout.restart();
1123                 return;
1124         }
1125  
1126         if (!screen_.get()) {
1127                 cursor_timeout.restart();
1128                 return;
1129         }
1130
1131         /* FIXME */
1132         extern void reapSpellchecker(void);
1133         reapSpellchecker();
1134          
1135         if (!bv_->theLockingInset()) {
1136                 screen_->cursorToggle(bv_);
1137         } else {
1138                 bv_->theLockingInset()->toggleInsetCursor(bv_);
1139         }
1140         
1141         cursor_timeout.restart();
1142 }
1143
1144
1145 void BufferView::Pimpl::cursorPrevious(LyXText * text)
1146 {
1147         if (!text->cursor.row()->previous())
1148                 return;
1149         
1150         int y = text->first_y;
1151         Row * cursorrow = text->cursor.row();
1152         
1153         text->setCursorFromCoordinates(bv_, bv_->text->cursor.x_fix(), y);
1154         finishUndo();
1155         // This is to allow jumping over large insets
1156         if ((cursorrow == text->cursor.row()))
1157                 text->cursorUp(bv_);
1158         
1159         if (text->inset_owner ||
1160             text->cursor.row()->height() < workarea_.height())
1161                 screen_->draw(bv_->text, bv_,
1162                               text->cursor.y()
1163                               - text->cursor.row()->baseline()
1164                               + text->cursor.row()->height()
1165                               - workarea_.height() + 1);
1166         updateScrollbar();
1167 }
1168
1169
1170 void BufferView::Pimpl::cursorNext(LyXText * text)
1171 {
1172         if (!text->cursor.row()->next())
1173                 return;
1174         
1175         int y = text->first_y + workarea_.height();
1176 //      if (text->inset_owner)
1177 //              y += bv_->text->first;
1178         text->getRowNearY(y);
1179     
1180         Row * cursorrow = text->cursor.row();
1181         text->setCursorFromCoordinates(bv_, text->cursor.x_fix(), y); // + workarea_->height());
1182         finishUndo();
1183         // This is to allow jumping over large insets
1184         if ((cursorrow == bv_->text->cursor.row()))
1185                 text->cursorDown(bv_);
1186         
1187         if (text->inset_owner ||
1188             text->cursor.row()->height() < workarea_.height())
1189                 screen_->draw(bv_->text, bv_, text->cursor.y() -
1190                               text->cursor.row()->baseline());
1191         updateScrollbar();
1192 }
1193
1194
1195 bool BufferView::Pimpl::available() const
1196 {
1197         if (buffer_ && bv_->text)
1198                 return true;
1199         return false;
1200 }
1201
1202
1203 void BufferView::Pimpl::beforeChange(LyXText * text)
1204 {
1205         toggleSelection();
1206         text->clearSelection();
1207 }
1208
1209
1210 void BufferView::Pimpl::savePosition(unsigned int i)
1211 {
1212         if (i >= saved_positions_num)
1213                 return;
1214         saved_positions[i] = Position(buffer_->fileName(),
1215                                       bv_->text->cursor.par()->id(),
1216                                       bv_->text->cursor.pos());
1217         if (i > 0) {
1218                 ostringstream str;
1219                 str << _("Saved bookmark") << ' ' << i;
1220                 owner_->message(str.str().c_str());
1221         }
1222 }
1223
1224
1225 void BufferView::Pimpl::restorePosition(unsigned int i)
1226 {
1227         if (i >= saved_positions_num)
1228                 return;
1229
1230         string const fname = saved_positions[i].filename;
1231
1232         beforeChange(bv_->text);
1233
1234         if (fname != buffer_->fileName()) {
1235                 Buffer * b = bufferlist.exists(fname) ?
1236                         bufferlist.getBuffer(fname) :
1237                         bufferlist.loadLyXFile(fname); // don't ask, just load it
1238                 if (b != 0) buffer(b);
1239         }
1240
1241         Paragraph * par = buffer_->getParFromID(saved_positions[i].par_id);
1242         if (!par)
1243                 return;
1244
1245         bv_->text->setCursor(bv_, par,
1246                              min(par->size(), saved_positions[i].par_pos));
1247
1248         update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
1249         if (i > 0) {
1250                 ostringstream str;
1251                 str << _("Moved to bookmark") << ' ' << i;
1252                 owner_->message(str.str().c_str());
1253         }
1254 }
1255
1256
1257 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
1258 {
1259         if (i >= saved_positions_num)
1260                 return false;
1261
1262         return !saved_positions[i].filename.empty();
1263 }
1264
1265
1266 void BufferView::Pimpl::setState()
1267 {
1268         if (!lyxrc.rtl_support)
1269                 return;
1270
1271         LyXText * text = bv_->getLyXText();
1272         if (text->real_current_font.isRightToLeft()) {
1273                 if (owner_->getIntl()->keymap == Intl::PRIMARY)
1274                         owner_->getIntl()->KeyMapSec();
1275         } else {
1276                 if (owner_->getIntl()->keymap == Intl::SECONDARY)
1277                         owner_->getIntl()->KeyMapPrim();
1278         }
1279 }
1280
1281
1282 void BufferView::Pimpl::insetSleep()
1283 {
1284         if (bv_->theLockingInset() && !inset_slept) {
1285                 bv_->theLockingInset()->getCursorPos(bv_, bv_->slx, bv_->sly);
1286                 bv_->theLockingInset()->insetUnlock(bv_);
1287                 inset_slept = true;
1288         }
1289 }
1290
1291
1292 void BufferView::Pimpl::insetWakeup()
1293 {
1294         if (bv_->theLockingInset() && inset_slept) {
1295                 bv_->theLockingInset()->edit(bv_, bv_->slx, bv_->sly, 0);
1296                 inset_slept = false;
1297         }
1298 }
1299
1300
1301 void BufferView::Pimpl::insetUnlock()
1302 {
1303         if (bv_->theLockingInset()) {
1304                 if (!inset_slept)
1305                         bv_->theLockingInset()->insetUnlock(bv_);
1306                 bv_->theLockingInset(0);
1307                 finishUndo();
1308                 inset_slept = false;
1309         }
1310 }
1311
1312
1313 bool BufferView::Pimpl::focus() const
1314 {
1315         return workarea_.hasFocus();
1316 }
1317
1318
1319 void BufferView::Pimpl::focus(bool f)
1320 {
1321         if (f) workarea_.setFocus();
1322 }
1323
1324
1325 bool BufferView::Pimpl::active() const
1326 {
1327         return workarea_.active();
1328 }
1329
1330
1331 bool BufferView::Pimpl::belowMouse() const 
1332 {
1333         return workarea_.belowMouse();
1334 }
1335
1336
1337 void BufferView::Pimpl::showCursor()
1338 {
1339         if (screen_.get()) {
1340                 if (bv_->theLockingInset())
1341                         bv_->theLockingInset()->showInsetCursor(bv_);
1342                 else
1343                         screen_->showCursor(bv_->text, bv_);
1344         }
1345 }
1346
1347
1348 void BufferView::Pimpl::hideCursor()
1349 {
1350         if (screen_.get()) {
1351                 if (!bv_->theLockingInset())
1352 //                      bv_->theLockingInset()->hideInsetCursor(bv_);
1353 //              else
1354                         screen_->hideCursor();
1355         }
1356 }
1357
1358
1359 void BufferView::Pimpl::toggleSelection(bool b)
1360 {
1361         if (screen_.get()) {
1362                 if (bv_->theLockingInset())
1363                         bv_->theLockingInset()->toggleSelection(bv_, b);
1364                 screen_->toggleSelection(bv_->text, bv_, b);
1365         }
1366 }
1367
1368
1369 void BufferView::Pimpl::toggleToggle()
1370 {
1371         if (screen_.get())
1372                 screen_->toggleToggle(bv_->text, bv_);
1373 }
1374
1375
1376 void BufferView::Pimpl::center() 
1377 {
1378         beforeChange(bv_->text);
1379         if (bv_->text->cursor.y() > static_cast<int>((workarea_.height() / 2))) {
1380                 screen_->draw(bv_->text, bv_, bv_->text->cursor.y() - workarea_.height() / 2);
1381         } else {
1382                 screen_->draw(bv_->text, bv_, 0);
1383         }
1384         update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
1385         redraw();
1386 }
1387
1388
1389 void BufferView::Pimpl::pasteClipboard(bool asPara) 
1390 {
1391         if (!buffer_) return;
1392
1393         screen_->hideCursor();
1394         beforeChange(bv_->text);
1395         
1396         string const clip(workarea_.getClipboard());
1397         
1398         if (clip.empty()) return;
1399
1400         if (asPara) {
1401                 bv_->getLyXText()->insertStringAsParagraphs(bv_, clip);
1402         } else {
1403                 bv_->getLyXText()->insertStringAsLines(bv_, clip);
1404         }
1405         update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1406 }
1407
1408
1409 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
1410 {
1411         workarea_.putClipboard(stuff);
1412 }
1413
1414
1415 /*
1416  * Dispatch functions for actions which can be valid for BufferView->text
1417  * and/or InsetText->text!!!
1418  */
1419
1420
1421 inline
1422 void BufferView::Pimpl::moveCursorUpdate(bool selecting)
1423 {
1424         LyXText * lt = bv_->getLyXText();
1425         
1426         if (selecting || lt->selection.mark()) {
1427                 lt->setSelection(bv_);
1428                 if (lt->bv_owner)
1429                         toggleToggle();
1430                 else
1431                         updateInset(lt->inset_owner, false);
1432         }
1433         if (lt->bv_owner) {
1434                 update(lt, BufferView::SELECT|BufferView::FITCUR);
1435                 showCursor();
1436         }
1437         
1438         /* ---> Everytime the cursor is moved, show the current font state. */
1439         // should this too me moved out of this func?
1440         //owner->showState();
1441         setState();
1442 }
1443
1444
1445 Inset * BufferView::Pimpl::getInsetByCode(Inset::Code code)
1446 {
1447         LyXCursor cursor = bv_->getLyXText()->cursor;
1448         Buffer::inset_iterator it =
1449                 find_if(Buffer::inset_iterator(
1450                         cursor.par(), cursor.pos()),
1451                         buffer_->inset_iterator_end(),
1452                         lyx::compare_memfun(&Inset::lyxCode, code));
1453         return it != buffer_->inset_iterator_end() ? (*it) : 0;
1454 }
1455
1456
1457 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
1458 {
1459         string filename = filen;
1460
1461         if (filename.empty()) {
1462                 // Launch a file browser
1463                 string initpath = lyxrc.document_path;
1464
1465                 if (available()) {
1466                         string const trypath = owner_->buffer()->filePath();
1467                         // If directory is writeable, use this as default.
1468                         if (IsDirWriteable(trypath))
1469                                 initpath = trypath;
1470                 }
1471
1472                 FileDialog fileDlg(bv_->owner(),
1473                                    _("Select LyX document to insert"),
1474                         LFUN_FILE_INSERT,
1475                         make_pair(string(_("Documents|#o#O")),
1476                                   string(lyxrc.document_path)),
1477                         make_pair(string(_("Examples|#E#e")),
1478                                   string(AddPath(system_lyxdir, "examples"))));
1479
1480                 FileDialog::Result result =
1481                         fileDlg.Select(initpath,
1482                                        _("*.lyx| LyX Documents (*.lyx)"));
1483  
1484                 if (result.first == FileDialog::Later)
1485                         return;
1486
1487                 filename = result.second;
1488
1489                 // check selected filename
1490                 if (filename.empty()) {
1491                         owner_->message(_("Canceled."));
1492                         return;
1493                 }
1494         }
1495
1496         // get absolute path of file and add ".lyx" to the filename if
1497         // necessary
1498         filename = FileSearch(string(), filename, "lyx");
1499
1500         string const disp_fn(MakeDisplayPath(filename));
1501         
1502         ostringstream s1;
1503         s1 << _("Inserting document") << ' '
1504            << disp_fn << " ...";
1505         owner_->message(s1.str().c_str());
1506         bool const res = bv_->insertLyXFile(filename);
1507         if (res) {
1508                 ostringstream str;
1509                 str << _("Document") << ' ' << disp_fn
1510                     << ' ' << _("inserted.");
1511                 owner_->message(str.str().c_str());
1512         } else {
1513                 ostringstream str;
1514                 str << _("Could not insert document") << ' '
1515                     << disp_fn;
1516                 owner_->message(str.str().c_str());
1517         }
1518 }
1519
1520
1521 bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
1522 {
1523         lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch: action["
1524                               << action <<"] arg[" << argument << "]" << endl;
1525         
1526         switch (action) {
1527                 // --- Misc -------------------------------------------
1528         case LFUN_APPENDIX:
1529         {
1530                 if (available()) {
1531                         LyXText * lt = bv_->getLyXText();
1532                         lt->toggleAppendix(bv_);
1533                         update(lt,
1534                                BufferView::SELECT
1535                                | BufferView::FITCUR
1536                                | BufferView::CHANGE);
1537                 }
1538         }
1539         break;
1540
1541         case LFUN_TOC_INSERT:
1542         {
1543                 InsetCommandParams p;
1544                 p.setCmdName("tableofcontents");
1545                 Inset * inset = new InsetTOC(p);
1546                 if (!insertInset(inset, "Standard"))
1547                         delete inset;
1548                 break;
1549         }
1550                 
1551         case LFUN_SCROLL_INSET:
1552                 // this is not handled here as this funktion is only aktive
1553                 // if we have a locking_inset and that one is (or contains)
1554                 // a tabular-inset
1555                 break;
1556
1557         case LFUN_INSET_GRAPHICS:
1558         {
1559                 Inset * new_inset = new InsetGraphics;
1560                 if (!insertInset(new_inset)) {
1561                         delete new_inset;
1562                 } else {
1563                         // this is need because you don't use a inset->Edit()
1564                         updateInset(new_inset, true);
1565                         new_inset->edit(bv_);
1566                 }
1567                 break;
1568         }
1569                 
1570         case LFUN_PASTE:
1571                 bv_->paste();
1572                 setState();
1573                 break;
1574                 
1575         case LFUN_PASTESELECTION:
1576         {
1577                 bool asPara = false;
1578                 if (argument == "paragraph")
1579                         asPara = true;
1580                 pasteClipboard(asPara);
1581         }
1582         break;
1583         
1584         case LFUN_CUT:
1585                 bv_->cut();
1586                 break;
1587                 
1588         case LFUN_COPY:
1589                 bv_->copy();
1590                 break;
1591                 
1592         case LFUN_LAYOUT_COPY:
1593                 bv_->copyEnvironment();
1594                 break;
1595                 
1596         case LFUN_LAYOUT_PASTE:
1597                 bv_->pasteEnvironment();
1598                 setState();
1599                 break;
1600                 
1601         case LFUN_GOTOERROR:
1602                 gotoInset(Inset::ERROR_CODE, false);
1603                 break;
1604                 
1605         case LFUN_GOTONOTE:
1606                 gotoInset(Inset::IGNORE_CODE, false);
1607                 break;
1608
1609         case LFUN_REFERENCE_GOTO:
1610         {
1611                 vector<Inset::Code> tmp;
1612                 tmp.push_back(Inset::LABEL_CODE);
1613                 tmp.push_back(Inset::REF_CODE);
1614                 gotoInset(tmp, true);
1615                 break;
1616         }
1617
1618         case LFUN_HYPHENATION:
1619                 specialChar(InsetSpecialChar::HYPHENATION);
1620                 break;
1621                 
1622         case LFUN_LIGATURE_BREAK:
1623                 specialChar(InsetSpecialChar::LIGATURE_BREAK);
1624                 break;
1625                 
1626         case LFUN_LDOTS:
1627                 specialChar(InsetSpecialChar::LDOTS);
1628                 break;
1629                 
1630         case LFUN_END_OF_SENTENCE:
1631                 specialChar(InsetSpecialChar::END_OF_SENTENCE);
1632                 break;
1633
1634         case LFUN_MENU_SEPARATOR:
1635                 specialChar(InsetSpecialChar::MENU_SEPARATOR);
1636                 break;
1637                 
1638         case LFUN_HFILL:
1639                 hfill();
1640                 break;
1641                 
1642         case LFUN_DEPTH:
1643                 changeDepth(bv_, bv_->getLyXText(), 0);
1644                 break;
1645                 
1646         case LFUN_DEPTH_MIN:
1647                 changeDepth(bv_, bv_->getLyXText(), -1);
1648                 break;
1649                 
1650         case LFUN_DEPTH_PLUS:
1651                 changeDepth(bv_, bv_->getLyXText(), 1);
1652                 break;
1653                 
1654         case LFUN_FREE:
1655                 owner_->getDialogs()->setUserFreeFont();
1656                 break;
1657
1658         case LFUN_FILE_INSERT:
1659                 MenuInsertLyXFile(argument);
1660                 break;
1661         
1662         case LFUN_FILE_INSERT_ASCII_PARA:
1663                 InsertAsciiFile(bv_, argument, true);
1664                 break;
1665
1666         case LFUN_FILE_INSERT_ASCII:
1667                 InsertAsciiFile(bv_, argument, false);
1668                 break;
1669                 
1670         case LFUN_LAYOUT:
1671         {
1672                 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1673                                     << argument << endl;
1674                 
1675                 // Derive layout number from given argument (string)
1676                 // and current buffer's textclass (number). */    
1677                 textclass_type tclass = buffer_->params.textclass;
1678                 bool hasLayout =
1679                         textclasslist[tclass].hasLayout(argument);
1680                 string layout = argument;
1681
1682                 // If the entry is obsolete, use the new one instead.
1683                 if (hasLayout) {
1684                         string const & obs = textclasslist[tclass][layout]
1685                                 .obsoleted_by();
1686                         if (!obs.empty()) 
1687                                 layout = obs;
1688                 }
1689
1690                 if (!hasLayout) {
1691                         owner_->getLyXFunc()->setErrorMessage(
1692                                 string(N_("Layout ")) + argument +
1693                                 N_(" not known"));
1694                         break;
1695                 }
1696
1697                 if (current_layout != layout) {
1698                         LyXText * lt = bv_->getLyXText();
1699                         hideCursor();
1700                         current_layout = layout;
1701                         update(lt,
1702                                BufferView::SELECT
1703                                | BufferView::FITCUR);
1704                         lt->setLayout(bv_, layout);
1705                         owner_->setLayout(layout);
1706                         update(lt,
1707                                BufferView::SELECT
1708                                | BufferView::FITCUR
1709                                | BufferView::CHANGE);
1710                         setState();
1711                 }
1712         }
1713         break;
1714
1715         case LFUN_LANGUAGE:
1716                 lang(bv_, argument);
1717                 setState();
1718                 owner_->showState();
1719                 break;
1720
1721         case LFUN_EMPH:
1722                 emph(bv_);
1723                 owner_->showState();
1724                 break;
1725
1726         case LFUN_BOLD:
1727                 bold(bv_);
1728                 owner_->showState();
1729                 break;
1730                 
1731         case LFUN_NOUN:
1732                 noun(bv_);
1733                 owner_->showState();
1734                 break;
1735                 
1736         case LFUN_CODE:
1737                 code(bv_);
1738                 owner_->showState();
1739                 break;
1740                 
1741         case LFUN_SANS:
1742                 sans(bv_);
1743                 owner_->showState();
1744                 break;
1745                 
1746         case LFUN_ROMAN:
1747                 roman(bv_);
1748                 owner_->showState();
1749                 break;
1750                 
1751         case LFUN_DEFAULT:
1752                 styleReset(bv_);
1753                 owner_->showState();
1754                 break;
1755                 
1756         case LFUN_UNDERLINE:
1757                 underline(bv_);
1758                 owner_->showState();
1759                 break;
1760                 
1761         case LFUN_FONT_SIZE:
1762                 fontSize(bv_, argument);
1763                 owner_->showState();
1764                 break;
1765                 
1766         case LFUN_FONT_STATE:
1767                 owner_->getLyXFunc()->setMessage(currentState(bv_));
1768                 break;
1769                 
1770         case LFUN_UPCASE_WORD:
1771         {
1772                 LyXText * lt = bv_->getLyXText();
1773                 
1774                 update(lt,
1775                        BufferView::SELECT
1776                        | BufferView::FITCUR);
1777                 lt->changeCase(bv_, LyXText::text_uppercase);
1778                 if (lt->inset_owner)
1779                         updateInset(lt->inset_owner, true);
1780                 update(lt,
1781                        BufferView::SELECT
1782                        | BufferView::FITCUR
1783                        | BufferView::CHANGE);
1784         }
1785         break;
1786                 
1787         case LFUN_LOWCASE_WORD:
1788         {
1789                 LyXText * lt = bv_->getLyXText();
1790                 
1791                 update(lt, BufferView::SELECT|BufferView::FITCUR);
1792                 lt->changeCase(bv_, LyXText::text_lowercase);
1793                 if (lt->inset_owner)
1794                         updateInset(lt->inset_owner, true);
1795                 update(lt,
1796                        BufferView::SELECT
1797                        | BufferView::FITCUR
1798                        | BufferView::CHANGE);
1799         }
1800         break;
1801                 
1802         case LFUN_CAPITALIZE_WORD:
1803         {
1804                 LyXText * lt = bv_->getLyXText();
1805                 
1806                 update(lt, BufferView::SELECT|BufferView::FITCUR);
1807                 lt->changeCase(bv_, LyXText::text_capitalization);
1808                 if (lt->inset_owner)
1809                         updateInset(lt->inset_owner, true);
1810                 update(lt,
1811                        BufferView::SELECT
1812                        | BufferView::FITCUR
1813                        | BufferView::CHANGE);
1814         }
1815         break;
1816
1817         case LFUN_TRANSPOSE_CHARS:
1818         {
1819                 LyXText * lt = bv_->getLyXText();
1820                 
1821                 update(lt, BufferView::SELECT|BufferView::FITCUR);
1822                 lt->transposeChars(*bv_);
1823                 if (lt->inset_owner)
1824                         updateInset(lt->inset_owner, true);
1825                 update(lt,
1826                        BufferView::SELECT
1827                        | BufferView::FITCUR
1828                        | BufferView::CHANGE);
1829         }
1830         break;
1831                 
1832                                           
1833         case LFUN_INSERT_LABEL:
1834                 MenuInsertLabel(bv_, argument);
1835                 break;
1836
1837         case LFUN_REF_INSERT:
1838                 if (argument.empty()) {
1839                         InsetCommandParams p("ref");
1840                         owner_->getDialogs()->createRef(p.getAsString());
1841                 } else {
1842                         InsetCommandParams p;
1843                         p.setFromString(argument);
1844
1845                         InsetRef * inset = new InsetRef(p, *buffer_);
1846                         if (!insertInset(inset))
1847                                 delete inset;
1848                         else
1849                                 updateInset(inset, true);
1850                 }
1851                 break;
1852
1853         case LFUN_BOOKMARK_SAVE:
1854                 savePosition(strToUnsignedInt(argument));
1855                 break;
1856
1857         case LFUN_BOOKMARK_GOTO:
1858                 restorePosition(strToUnsignedInt(argument));
1859                 break;
1860
1861         case LFUN_REF_GOTO:
1862         {
1863                 string label(argument);
1864                 if (label.empty()) {
1865                         InsetRef * inset = 
1866                                 static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1867                         if (inset) {
1868                                 label = inset->getContents();
1869                                 savePosition(0);
1870                         }
1871                 }
1872                 
1873                 if (!label.empty()) {
1874                         //bv_->savePosition(0);
1875                         if (!bv_->gotoLabel(label))
1876                                 Alert::alert(_("Error"), 
1877                                            _("Couldn't find this label"), 
1878                                            _("in current document."));
1879                 }
1880         }
1881         break;
1882                 
1883                 // --- Cursor Movements -----------------------------
1884         case LFUN_RIGHT:
1885         {
1886                 LyXText * lt = bv_->getLyXText();
1887                 
1888                 bool is_rtl = lt->cursor.par()->isRightToLeftPar(buffer_->params);
1889                 if (!lt->selection.mark())
1890                         beforeChange(lt);
1891                 update(lt, BufferView::SELECT|BufferView::FITCUR);
1892                 if (is_rtl)
1893                         lt->cursorLeft(bv_, false);
1894                 if (lt->cursor.pos() < lt->cursor.par()->size()
1895                     && lt->cursor.par()->isInset(lt->cursor.pos())
1896                     && isHighlyEditableInset(lt->cursor.par()->getInset(lt->cursor.pos()))) {
1897                         Inset * tmpinset = lt->cursor.par()->getInset(lt->cursor.pos());
1898                         owner_->getLyXFunc()->setMessage(tmpinset->editMessage());
1899                         if (is_rtl)
1900                                 tmpinset->edit(bv_, false);
1901                         else
1902                                 tmpinset->edit(bv_);
1903                         break;
1904                 }
1905                 if (!is_rtl)
1906                         lt->cursorRight(bv_, false);
1907                 finishUndo();
1908                 moveCursorUpdate(false);
1909                 owner_->showState();
1910         }
1911         break;
1912                 
1913         case LFUN_LEFT:
1914         {
1915                 // This is soooo ugly. Isn`t it possible to make
1916                 // it simpler? (Lgb)
1917                 LyXText * lt = bv_->getLyXText();
1918                 bool is_rtl = lt->cursor.par()->isRightToLeftPar(buffer_->params);
1919                 if (!lt->selection.mark())
1920                         beforeChange(lt);
1921                 update(lt, BufferView::SELECT|BufferView::FITCUR);
1922                 LyXCursor const cur = lt->cursor;
1923                 if (!is_rtl)
1924                         lt->cursorLeft(bv_, false);
1925                 if ((is_rtl || cur != lt->cursor) && // only if really moved!
1926                     lt->cursor.pos() < lt->cursor.par()->size() &&
1927                     lt->cursor.par()->isInset(lt->cursor.pos()) &&
1928                     isHighlyEditableInset(lt->cursor.par()->getInset(lt->cursor.pos()))) {
1929                         Inset * tmpinset = lt->cursor.par()->getInset(lt->cursor.pos());
1930                         owner_->getLyXFunc()->setMessage(tmpinset->editMessage());
1931                         if (is_rtl)
1932                                 tmpinset->edit(bv_);
1933                         else
1934                                 tmpinset->edit(bv_, false);
1935                         break;
1936                 }
1937                 if  (is_rtl)
1938                         lt->cursorRight(bv_, false);
1939
1940                 finishUndo();
1941                 moveCursorUpdate(false);
1942                 owner_->showState();
1943         }
1944         break;
1945                 
1946         case LFUN_UP:
1947         {
1948                 LyXText * lt = bv_->getLyXText();
1949                 
1950                 if (!lt->selection.mark())
1951                         beforeChange(lt);
1952                 update(lt, BufferView::UPDATE);
1953                 lt->cursorUp(bv_);
1954                 finishUndo();
1955                 moveCursorUpdate(false);
1956                 owner_->showState();
1957         }
1958         break;
1959                 
1960         case LFUN_DOWN:
1961         {
1962                 LyXText * lt = bv_->getLyXText();
1963                 
1964                 if (!lt->selection.mark())
1965                         beforeChange(lt);
1966                 update(lt, BufferView::UPDATE);
1967                 lt->cursorDown(bv_);
1968                 finishUndo();
1969                 moveCursorUpdate(false);
1970                 owner_->showState();
1971         }
1972         break;
1973
1974         case LFUN_UP_PARAGRAPH:
1975         {
1976                 LyXText * lt = bv_->getLyXText();
1977                 
1978                 if (!lt->selection.mark())
1979                         beforeChange(lt);
1980                 update(lt, BufferView::UPDATE);
1981                 lt->cursorUpParagraph(bv_);
1982                 finishUndo();
1983                 moveCursorUpdate(false);
1984                 owner_->showState();
1985         }
1986         break;
1987                 
1988         case LFUN_DOWN_PARAGRAPH:
1989         {
1990                 LyXText * lt = bv_->getLyXText();
1991                 
1992                 if (!lt->selection.mark())
1993                         beforeChange(lt);
1994                 update(lt, BufferView::UPDATE);
1995                 lt->cursorDownParagraph(bv_);
1996                 finishUndo();
1997                 moveCursorUpdate(false);
1998                 owner_->showState();
1999         }
2000         break;
2001                 
2002         case LFUN_PRIOR:
2003         {
2004                 LyXText * lt = bv_->getLyXText();
2005                 
2006                 if (!lt->selection.mark())
2007                         beforeChange(lt);
2008                 update(lt, BufferView::UPDATE);
2009                 cursorPrevious(lt);
2010                 finishUndo();
2011                 moveCursorUpdate(false);
2012                 owner_->showState();
2013         }
2014         break;
2015                 
2016         case LFUN_NEXT:
2017         {
2018                 LyXText * lt = bv_->getLyXText();
2019                 
2020                 if (!lt->selection.mark())
2021                         beforeChange(lt);
2022                 update(lt, BufferView::UPDATE);
2023                 cursorNext(lt);
2024                 finishUndo();
2025                 moveCursorUpdate(false);
2026                 owner_->showState();
2027         }
2028         break;
2029                 
2030         case LFUN_HOME:
2031         {
2032                 LyXText * lt = bv_->getLyXText();
2033                 
2034                 if (!lt->selection.mark())
2035                         beforeChange(lt);
2036                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2037                 lt->cursorHome(bv_);
2038                 finishUndo();
2039                 moveCursorUpdate(false);
2040                 owner_->showState();
2041         }
2042         break;
2043                 
2044         case LFUN_END:
2045         {
2046                 LyXText * lt = bv_->getLyXText();
2047                 
2048                 if (!lt->selection.mark())
2049                         beforeChange(lt);
2050                 update(lt,
2051                        BufferView::SELECT|BufferView::FITCUR);
2052                 lt->cursorEnd(bv_);
2053                 finishUndo();
2054                 moveCursorUpdate(false);
2055                 owner_->showState();
2056         }
2057         break;
2058                 
2059         case LFUN_SHIFT_TAB:
2060         case LFUN_TAB:
2061         {
2062                 LyXText * lt = bv_->getLyXText();
2063                 
2064                 if (!lt->selection.mark())
2065                         beforeChange(lt);
2066                 update(lt,
2067                        BufferView::SELECT|BufferView::FITCUR);
2068                 lt->cursorTab(bv_);
2069                 finishUndo();
2070                 moveCursorUpdate(false);
2071                 owner_->showState();
2072         }
2073         break;
2074                 
2075         case LFUN_WORDRIGHT:
2076         {
2077                 LyXText * lt = bv_->getLyXText();
2078                 
2079                 if (!lt->selection.mark())
2080                         beforeChange(lt);
2081                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2082                 if (lt->cursor.par()->isRightToLeftPar(buffer_->params))
2083                         lt->cursorLeftOneWord(bv_);
2084                 else
2085                         lt->cursorRightOneWord(bv_);
2086                 finishUndo();
2087                 moveCursorUpdate(false);
2088                 owner_->showState();
2089         }
2090         break;
2091                 
2092         case LFUN_WORDLEFT:
2093         {
2094                 LyXText * lt = bv_->getLyXText();
2095                 
2096                 if (!lt->selection.mark())
2097                         beforeChange(lt);
2098                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2099                 if (lt->cursor.par()->isRightToLeftPar(buffer_->params))
2100                         lt->cursorRightOneWord(bv_);
2101                 else
2102                         lt->cursorLeftOneWord(bv_);
2103                 finishUndo();
2104                 moveCursorUpdate(false);
2105                 owner_->showState();
2106         }
2107         break;
2108                 
2109         case LFUN_BEGINNINGBUF:
2110         {
2111                 LyXText * lt = bv_->getLyXText();
2112                 
2113                 if (!lt->selection.mark())
2114                         beforeChange(lt);
2115                 update(lt,
2116                        BufferView::SELECT|BufferView::FITCUR);
2117                 lt->cursorTop(bv_);
2118                 finishUndo();
2119                 moveCursorUpdate(false);
2120                 owner_->showState();
2121         }
2122         break;
2123                 
2124         case LFUN_ENDBUF:
2125         {
2126                 LyXText * lt = bv_->getLyXText();
2127                 
2128                 if (!lt->selection.mark())
2129                         beforeChange(lt);
2130                 update(lt,
2131                        BufferView::SELECT|BufferView::FITCUR);
2132                 lt->cursorBottom(bv_);
2133                 finishUndo();
2134                 moveCursorUpdate(false);
2135                 owner_->showState();
2136         }
2137         break;
2138       
2139                 /* cursor selection ---------------------------- */
2140         case LFUN_RIGHTSEL:
2141         {
2142                 LyXText * lt = bv_->getLyXText();
2143                 
2144                 update(lt,
2145                        BufferView::SELECT|BufferView::FITCUR);
2146                 if (lt->cursor.par()->isRightToLeftPar(buffer_->params))
2147                         lt->cursorLeft(bv_);
2148                 else
2149                         lt->cursorRight(bv_);
2150                 finishUndo();
2151                 moveCursorUpdate(true);
2152                 owner_->showState();
2153         }
2154         break;
2155                 
2156         case LFUN_LEFTSEL:
2157         {
2158                 LyXText * lt = bv_->getLyXText();
2159                 
2160                 update(lt,
2161                        BufferView::SELECT|BufferView::FITCUR);
2162                 if (lt->cursor.par()->isRightToLeftPar(buffer_->params))
2163                         lt->cursorRight(bv_);
2164                 else
2165                         lt->cursorLeft(bv_);
2166                 finishUndo();
2167                 moveCursorUpdate(true);
2168                 owner_->showState();
2169         }
2170         break;
2171                 
2172         case LFUN_UPSEL:
2173         {
2174                 LyXText * lt = bv_->getLyXText();
2175                 
2176                 update(lt,
2177                        BufferView::SELECT|BufferView::FITCUR);
2178                 lt->cursorUp(bv_);
2179                 finishUndo();
2180                 moveCursorUpdate(true);
2181                 owner_->showState();
2182         }
2183         break;
2184                 
2185         case LFUN_DOWNSEL:
2186         {
2187                 LyXText * lt = bv_->getLyXText();
2188                 
2189                 update(lt,
2190                        BufferView::SELECT|BufferView::FITCUR);
2191                 lt->cursorDown(bv_);
2192                 finishUndo();
2193                 moveCursorUpdate(true);
2194                 owner_->showState();
2195         }
2196         break;
2197
2198         case LFUN_UP_PARAGRAPHSEL:
2199         {
2200                 LyXText * lt = bv_->getLyXText();
2201                 
2202                 update(lt,
2203                        BufferView::SELECT|BufferView::FITCUR);
2204                 lt->cursorUpParagraph(bv_);
2205                 finishUndo();
2206                 moveCursorUpdate(true);
2207                 owner_->showState();
2208         }
2209         break;
2210                 
2211         case LFUN_DOWN_PARAGRAPHSEL:
2212         {
2213                 LyXText * lt = bv_->getLyXText();
2214                 
2215                 update(lt,
2216                        BufferView::SELECT|BufferView::FITCUR);
2217                 lt->cursorDownParagraph(bv_);
2218                 finishUndo();
2219                 moveCursorUpdate(true);
2220                 owner_->showState();
2221         }
2222         break;
2223                 
2224         case LFUN_PRIORSEL:
2225         {
2226                 LyXText * lt = bv_->getLyXText();
2227                 
2228                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2229                 cursorPrevious(lt);
2230                 finishUndo();
2231                 moveCursorUpdate(true);
2232                 owner_->showState();
2233         }
2234         break;
2235                 
2236         case LFUN_NEXTSEL:
2237         {
2238                 LyXText * lt = bv_->getLyXText();
2239                 
2240                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2241                 cursorNext(lt);
2242                 finishUndo();
2243                 moveCursorUpdate(true);
2244                 owner_->showState();
2245         }
2246         break;
2247                 
2248         case LFUN_HOMESEL:
2249         {
2250                 LyXText * lt = bv_->getLyXText();
2251                 
2252                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2253                 lt->cursorHome(bv_);
2254                 finishUndo();
2255                 moveCursorUpdate(true);
2256                 owner_->showState();
2257         }
2258         break;
2259                 
2260         case LFUN_ENDSEL:
2261         {
2262                 LyXText * lt = bv_->getLyXText();
2263
2264                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2265                 lt->cursorEnd(bv_);
2266                 finishUndo();
2267                 moveCursorUpdate(true);
2268                 owner_->showState();
2269         }
2270         break;
2271                 
2272         case LFUN_WORDRIGHTSEL:
2273         {
2274                 LyXText * lt = bv_->getLyXText();
2275                 
2276                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2277                 if (lt->cursor.par()->isRightToLeftPar(buffer_->params))
2278                         lt->cursorLeftOneWord(bv_);
2279                 else
2280                         lt->cursorRightOneWord(bv_);
2281                 finishUndo();
2282                 moveCursorUpdate(true);
2283                 owner_->showState();
2284         }
2285         break;
2286                 
2287         case LFUN_WORDLEFTSEL:
2288         {
2289                 LyXText * lt = bv_->getLyXText();
2290                 
2291                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2292                 if (lt->cursor.par()->isRightToLeftPar(buffer_->params))
2293                         lt->cursorRightOneWord(bv_);
2294                 else
2295                         lt->cursorLeftOneWord(bv_);
2296                 finishUndo();
2297                 moveCursorUpdate(true);
2298                 owner_->showState();
2299         }
2300         break;
2301                 
2302         case LFUN_BEGINNINGBUFSEL:
2303         {
2304                 LyXText * lt = bv_->getLyXText();
2305                 
2306                 if (lt->inset_owner)
2307                         break;
2308                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2309                 lt->cursorTop(bv_);
2310                 finishUndo();
2311                 moveCursorUpdate(true);
2312                 owner_->showState();
2313         }
2314         break;
2315                 
2316         case LFUN_ENDBUFSEL:
2317         {
2318                 LyXText * lt = bv_->getLyXText();
2319                 
2320                 if (lt->inset_owner)
2321                         break;
2322                 update(lt,
2323                        BufferView::SELECT|BufferView::FITCUR);
2324                 lt->cursorBottom(bv_);
2325                 finishUndo();
2326                 moveCursorUpdate(true);
2327                 owner_->showState();
2328         }
2329         break;
2330
2331                 // --- text changing commands ------------------------
2332         case LFUN_BREAKLINE:
2333         {
2334                 LyXText * lt = bv_->getLyXText();
2335
2336                 beforeChange(lt);
2337                 lt->insertChar(bv_, Paragraph::META_NEWLINE);
2338                 update(lt,
2339                        BufferView::SELECT
2340                        | BufferView::FITCUR
2341                        | BufferView::CHANGE);
2342                 moveCursorUpdate(false);
2343         }
2344         break;
2345                 
2346         case LFUN_PROTECTEDSPACE:
2347         {
2348                 LyXText * lt = bv_->getLyXText();
2349
2350                 LyXLayout const & style = textclasslist[buffer_->params.textclass][lt->cursor.par()->layout()];
2351
2352                 if (style.free_spacing) {
2353                         lt->insertChar(bv_, ' ');
2354                         update(lt,
2355                                BufferView::SELECT
2356                                | BufferView::FITCUR
2357                                | BufferView::CHANGE);
2358                 } else {
2359                         specialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
2360                 }
2361                 moveCursorUpdate(false);
2362         }
2363         break;
2364                 
2365         case LFUN_SETMARK:
2366         {
2367                 LyXText * lt = bv_->getLyXText();
2368
2369                 if (lt->selection.mark()) {
2370                         beforeChange(lt);
2371                         update(lt,
2372                                BufferView::SELECT
2373                                | BufferView::FITCUR);
2374                         owner_->getLyXFunc()->setMessage(N_("Mark removed"));
2375                 } else {
2376                         beforeChange(lt);
2377                         lt->selection.mark(true);
2378                         update(lt,
2379                                BufferView::SELECT
2380                                | BufferView::FITCUR);
2381                         owner_->getLyXFunc()->setMessage(N_("Mark set"));
2382                 }
2383                 lt->selection.cursor = lt->cursor;
2384         }
2385         break;
2386                 
2387         case LFUN_DELETE:
2388         {
2389                 LyXText * lt = bv_->getLyXText();
2390
2391                 if (!lt->selection.set()) {
2392                         lt->Delete(bv_);
2393                         lt->selection.cursor = lt->cursor;
2394                         update(lt,
2395                                BufferView::SELECT
2396                                | BufferView::FITCUR
2397                                | BufferView::CHANGE);
2398                         // It is possible to make it a lot faster still
2399                         // just comment out the line below...
2400                         showCursor();
2401                 } else {
2402                         bv_->cut(false);
2403                 }
2404                 moveCursorUpdate(false);
2405                 owner_->showState();
2406                 setState();
2407         }
2408         break;
2409
2410         case LFUN_DELETE_SKIP:
2411         {
2412                 LyXText * lt = bv_->getLyXText();
2413
2414                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
2415                 
2416                 LyXCursor cursor = lt->cursor;
2417
2418                 if (!lt->selection.set()) {
2419                         if (cursor.pos() == cursor.par()->size()) {
2420                                 lt->cursorRight(bv_);
2421                                 cursor = lt->cursor;
2422                                 if (cursor.pos() == 0
2423                                     && !(cursor.par()->params().spaceTop()
2424                                          == VSpace (VSpace::NONE))) {
2425                                         lt->setParagraph
2426                                                 (bv_,
2427                                                  cursor.par()->params().lineTop(),
2428                                                  cursor.par()->params().lineBottom(),
2429                                                  cursor.par()->params().pagebreakTop(), 
2430                                                  cursor.par()->params().pagebreakBottom(),
2431                                                  VSpace(VSpace::NONE), 
2432                                                  cursor.par()->params().spaceBottom(),
2433                                                  cursor.par()->params().spacing(), 
2434                                                  cursor.par()->params().align(), 
2435                                                  cursor.par()->params().labelWidthString(), 0);
2436                                         lt->cursorLeft(bv_);
2437                                         update(lt, 
2438                                                BufferView::SELECT
2439                                                | BufferView::FITCUR
2440                                                | BufferView::CHANGE);
2441                                 } else {
2442                                         lt->cursorLeft(bv_);
2443                                         lt->Delete(bv_);
2444                                         lt->selection.cursor = lt->cursor;
2445                                         update(lt,
2446                                                BufferView::SELECT
2447                                                | BufferView::FITCUR
2448                                                | BufferView::CHANGE);
2449                                 }
2450                         } else {
2451                                 lt->Delete(bv_);
2452                                 lt->selection.cursor = lt->cursor;
2453                                 update(lt,
2454                                        BufferView::SELECT
2455                                        | BufferView::FITCUR
2456                                        | BufferView::CHANGE);
2457                         }
2458                 } else {
2459                         bv_->cut(false);
2460                 }
2461         }
2462         break;
2463
2464         /* -------> Delete word forward. */
2465         case LFUN_DELETE_WORD_FORWARD:
2466                 update(bv_->getLyXText(), BufferView::SELECT|BufferView::FITCUR);
2467                 bv_->getLyXText()->deleteWordForward(bv_);
2468                 update(bv_->getLyXText(), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2469                 moveCursorUpdate(false);
2470                 owner_->showState();
2471                 break;
2472
2473                 /* -------> Delete word backward. */
2474         case LFUN_DELETE_WORD_BACKWARD:
2475         {
2476                 LyXText * lt = bv_->getLyXText();
2477                 
2478                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2479                 lt->deleteWordBackward(bv_);
2480                 update(lt,
2481                        BufferView::SELECT
2482                        | BufferView::FITCUR
2483                        | BufferView::CHANGE);
2484                 moveCursorUpdate(false);
2485                 owner_->showState();
2486         }
2487         break;
2488                 
2489                 /* -------> Kill to end of line. */
2490         case LFUN_DELETE_LINE_FORWARD:
2491         {
2492                 LyXText * lt = bv_->getLyXText();
2493                 
2494                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2495                 lt->deleteLineForward(bv_);
2496                 update(lt,
2497                        BufferView::SELECT
2498                        | BufferView::FITCUR
2499                        | BufferView::CHANGE);
2500                 moveCursorUpdate(false);
2501         }
2502         break;
2503                 
2504                 /* -------> Set mark off. */
2505         case LFUN_MARK_OFF:
2506         {
2507                 LyXText * lt = bv_->getLyXText();
2508                 
2509                 beforeChange(lt);
2510                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2511                 lt->selection.cursor = lt->cursor;
2512                 owner_->getLyXFunc()->setMessage(N_("Mark off"));
2513         }
2514         break;
2515
2516                 /* -------> Set mark on. */
2517         case LFUN_MARK_ON:
2518         {
2519                 LyXText * lt = bv_->getLyXText();
2520                 
2521                 beforeChange(lt);
2522                 lt->selection.mark(true);
2523                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2524                 lt->selection.cursor = lt->cursor;
2525                 owner_->getLyXFunc()->setMessage(N_("Mark on"));
2526         }
2527         break;
2528                 
2529         case LFUN_BACKSPACE:
2530         {
2531                 LyXText * lt = bv_->getLyXText();
2532                 
2533                 if (!lt->selection.set()) {
2534                         if (owner_->getIntl()->getTrans().backspace()) {
2535                                 lt->backspace(bv_);
2536                                 lt->selection.cursor = lt->cursor;
2537                                 update(lt,
2538                                        BufferView::SELECT
2539                                        | BufferView::FITCUR
2540                                        | BufferView::CHANGE);
2541                                 // It is possible to make it a lot faster still
2542                                 // just comment out the line below...
2543                                 showCursor();
2544                         }
2545                 } else {
2546                         bv_->cut(false);
2547                 }
2548                 owner_->showState();
2549                 setState();
2550         }
2551         break;
2552
2553         case LFUN_BACKSPACE_SKIP:
2554         {
2555                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
2556                 LyXText * lt = bv_->getLyXText();
2557                 
2558                 LyXCursor cursor = lt->cursor;
2559                 
2560                 if (!lt->selection.set()) {
2561                         if (cursor.pos() == 0 
2562                             && !(cursor.par()->params().spaceTop() 
2563                                  == VSpace (VSpace::NONE))) {
2564                                 lt->setParagraph 
2565                                         (bv_,
2566                                          cursor.par()->params().lineTop(),      
2567                                          cursor.par()->params().lineBottom(),
2568                                          cursor.par()->params().pagebreakTop(), 
2569                                          cursor.par()->params().pagebreakBottom(),
2570                                          VSpace(VSpace::NONE), cursor.par()->params().spaceBottom(),
2571                                          cursor.par()->params().spacing(), 
2572                                          cursor.par()->params().align(), 
2573                                          cursor.par()->params().labelWidthString(), 0);
2574                                 update(lt,
2575                                        BufferView::SELECT
2576                                        | BufferView::FITCUR
2577                                        | BufferView::CHANGE);
2578                         } else {
2579                                 lt->backspace(bv_);
2580                                 lt->selection.cursor = cursor;
2581                                 update(lt,
2582                                        BufferView::SELECT
2583                                        | BufferView::FITCUR
2584                                        | BufferView::CHANGE);
2585                         }
2586                 } else
2587                         bv_->cut(false);
2588         }
2589         break;
2590
2591         case LFUN_BREAKPARAGRAPH:
2592         {
2593                 LyXText * lt = bv_->getLyXText();
2594                 
2595                 beforeChange(lt);
2596                 lt->breakParagraph(bv_, 0);
2597                 update(lt,
2598                        BufferView::SELECT
2599                        | BufferView::FITCUR
2600                        | BufferView::CHANGE);
2601                 lt->selection.cursor = lt->cursor;
2602                 setState();
2603                 owner_->showState();
2604                 break;
2605         }
2606
2607         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
2608         {
2609                 LyXText * lt = bv_->getLyXText();
2610                 
2611                 beforeChange(lt);
2612                 lt->breakParagraph(bv_, 1);
2613                 update(lt,
2614                        BufferView::SELECT
2615                        | BufferView::FITCUR
2616                        | BufferView::CHANGE);
2617                 lt->selection.cursor = lt->cursor;
2618                 setState();
2619                 owner_->showState();
2620                 break;
2621         }
2622         
2623         case LFUN_BREAKPARAGRAPH_SKIP:
2624         {
2625                 // When at the beginning of a paragraph, remove
2626                 // indentation and add a "defskip" at the top.
2627                 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
2628                 LyXText * lt = bv_->getLyXText();
2629                 
2630                 LyXCursor cursor = lt->cursor;
2631                 
2632                 beforeChange(lt);
2633                 if (cursor.pos() == 0) {
2634                         if (cursor.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
2635                                 lt->setParagraph
2636                                         (bv_,
2637                                          cursor.par()->params().lineTop(),
2638                                          cursor.par()->params().lineBottom(),
2639                                          cursor.par()->params().pagebreakTop(),
2640                                          cursor.par()->params().pagebreakBottom(),
2641                                          VSpace(VSpace::DEFSKIP), cursor.par()->params().spaceBottom(),
2642                                          cursor.par()->params().spacing(),
2643                                          cursor.par()->params().align(),
2644                                          cursor.par()->params().labelWidthString(), 1);
2645                                 //update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2646                         } 
2647                 }
2648                 else {
2649                         lt->breakParagraph(bv_, 0);
2650                         //update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2651                 }
2652
2653                 update(lt,
2654                        BufferView::SELECT
2655                        | BufferView::FITCUR
2656                        | BufferView::CHANGE);
2657                 lt->selection.cursor = cursor;
2658                 setState();
2659                 owner_->showState();
2660         }
2661         break;
2662
2663         case LFUN_PARAGRAPH_SPACING:
2664         {
2665                 LyXText * lt = bv_->getLyXText();
2666                 
2667                 Paragraph * par = lt->cursor.par();
2668                 Spacing::Space cur_spacing = par->params().spacing().getSpace();
2669                 float cur_value = 1.0;
2670                 if (cur_spacing == Spacing::Other) {
2671                         cur_value = par->params().spacing().getValue();
2672                 }
2673                 
2674                 istringstream istr(argument.c_str());
2675
2676                 string tmp;
2677                 istr >> tmp;
2678                 Spacing::Space new_spacing = cur_spacing;
2679                 float new_value = cur_value;
2680                 if (tmp.empty()) {
2681                         lyxerr << "Missing argument to `paragraph-spacing'"
2682                                << endl;
2683                 } else if (tmp == "single") {
2684                         new_spacing = Spacing::Single;
2685                 } else if (tmp == "onehalf") {
2686                         new_spacing = Spacing::Onehalf;
2687                 } else if (tmp == "double") {
2688                         new_spacing = Spacing::Double;
2689                 } else if (tmp == "other") {
2690                         new_spacing = Spacing::Other;
2691                         float tmpval = 0.0;
2692                         istr >> tmpval;
2693                         lyxerr << "new_value = " << tmpval << endl;
2694                         if (tmpval != 0.0)
2695                                 new_value = tmpval;
2696                 } else if (tmp == "default") {
2697                         new_spacing = Spacing::Default;
2698                 } else {
2699                         lyxerr << _("Unknown spacing argument: ")
2700                                << argument << endl;
2701                 }
2702                 if (cur_spacing != new_spacing || cur_value != new_value) {
2703                         par->params().spacing(Spacing(new_spacing, new_value));
2704                         lt->redoParagraph(bv_);
2705                         update(lt,
2706                                BufferView::SELECT
2707                                | BufferView::FITCUR
2708                                | BufferView::CHANGE);
2709                 }
2710         }
2711         break;
2712         
2713         case LFUN_INSET_TOGGLE:
2714         {
2715                 LyXText * lt = bv_->getLyXText();
2716                 hideCursor();
2717                 beforeChange(lt);
2718                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2719                 lt->toggleInset(bv_);
2720                 update(lt, BufferView::SELECT|BufferView::FITCUR);
2721                 setState();
2722         }       
2723                 break;
2724
2725         case LFUN_QUOTE:
2726                 smartQuote();
2727                 break;
2728
2729         case LFUN_HTMLURL:
2730         case LFUN_URL:
2731         {
2732                 InsetCommandParams p;
2733                 if (action == LFUN_HTMLURL)
2734                         p.setCmdName("htmlurl");
2735                 else
2736                         p.setCmdName("url");
2737                 owner_->getDialogs()->createUrl(p.getAsString());
2738         }
2739         break;
2740         
2741         case LFUN_INSERT_URL:
2742         {
2743                 InsetCommandParams p;
2744                 p.setFromString(argument);
2745
2746                 InsetUrl * inset = new InsetUrl(p);
2747                 if (!insertInset(inset))
2748                         delete inset;
2749                 else
2750                         updateInset(inset, true);
2751         }
2752         break;
2753         
2754         case LFUN_INSET_ERT:
2755                 insertAndEditInset(new InsetERT(buffer_->params));
2756                 break;
2757         
2758         case LFUN_INSET_EXTERNAL:
2759                 insertAndEditInset(new InsetExternal);
2760                 break;
2761         
2762         case LFUN_INSET_FOOTNOTE:
2763                 insertAndEditInset(new InsetFoot(buffer_->params));
2764                 break;
2765
2766         case LFUN_INSET_MARGINAL:
2767                 insertAndEditInset(new InsetMarginal(buffer_->params));
2768                 break;
2769
2770         case LFUN_INSET_MINIPAGE:
2771                 insertAndEditInset(new InsetMinipage(buffer_->params));
2772                 break;
2773
2774         case LFUN_INSERT_NOTE:
2775                 insertAndEditInset(new InsetNote(buffer_->params));
2776                 break;
2777
2778         case LFUN_INSET_FLOAT:
2779                 // check if the float type exist
2780                 if (floatList.typeExist(argument)) {
2781                         insertAndEditInset(new InsetFloat(buffer_->params,
2782                                                           argument));
2783                 } else {
2784                         lyxerr << "Non-existent float type: "
2785                                << argument << endl;
2786                 }
2787                 break;
2788
2789         case LFUN_INSET_WIDE_FLOAT:
2790         {
2791                 // check if the float type exist
2792                 if (floatList.typeExist(argument)) {
2793                         InsetFloat * new_inset =
2794                                 new InsetFloat(buffer_->params, argument);
2795                         new_inset->wide(true);
2796                         if (insertInset(new_inset))
2797                                 new_inset->edit(bv_);
2798                         else
2799                                 delete new_inset;
2800                 } else {
2801                         lyxerr << "Non-existent float type: "
2802                                << argument << endl;
2803                 }
2804                 
2805         }
2806         break;
2807
2808 #if 0
2809         case LFUN_INSET_LIST:
2810                 insertAndEditInset(new InsetList);
2811                 break;
2812
2813         case LFUN_INSET_THEOREM:
2814                 insertAndEditInset(new InsetTheorem);
2815                 break;
2816 #endif
2817                 
2818         case LFUN_INSET_CAPTION:
2819         {
2820                 // Do we have a locking inset...
2821                 if (bv_->theLockingInset()) {
2822                         lyxerr << "Locking inset code: "
2823                                << static_cast<int>(bv_->theLockingInset()->lyxCode());
2824                         InsetCaption * new_inset =
2825                                 new InsetCaption(buffer_->params);
2826                         new_inset->setOwner(bv_->theLockingInset());
2827                         new_inset->setAutoBreakRows(true);
2828                         new_inset->setDrawFrame(0, InsetText::LOCKED);
2829                         new_inset->setFrameColor(0, LColor::captionframe);
2830                         if (insertInset(new_inset))
2831                                 new_inset->edit(bv_);
2832                         else
2833                                 delete new_inset;
2834                 }
2835         }
2836         break;
2837         
2838         case LFUN_INSET_TABULAR:
2839         {
2840                 int r = 2;
2841                 int c = 2;
2842                 if (!argument.empty())
2843                         ::sscanf(argument.c_str(),"%d%d", &r, &c);
2844                 InsetTabular * new_inset =
2845                         new InsetTabular(*buffer_, r, c);
2846                 bool const rtl =
2847                         bv_->getLyXText()->real_current_font.isRightToLeft();
2848                 if (!open_new_inset(new_inset, rtl))
2849                         delete new_inset;
2850         }
2851         break;
2852
2853         // --- lyxserver commands ----------------------------
2854
2855         case LFUN_CHARATCURSOR:
2856         {
2857                 pos_type pos = bv_->getLyXText()->cursor.pos();
2858                 if (pos < bv_->getLyXText()->cursor.par()->size())
2859                         owner_->getLyXFunc()->setMessage(
2860                                 tostr(bv_->getLyXText()->cursor.par()->getChar(pos)));
2861                 else
2862                         owner_->getLyXFunc()->setMessage("EOF");
2863         }
2864         break;
2865         
2866         case LFUN_GETXY:
2867                 owner_->getLyXFunc()->setMessage(tostr(bv_->getLyXText()->cursor.x())
2868                                                  + ' '
2869                                                  + tostr(bv_->getLyXText()->cursor.y()));
2870                 break;
2871                 
2872         case LFUN_SETXY:
2873         {
2874                 int x = 0;
2875                 int y = 0;
2876                 if (::sscanf(argument.c_str(), " %d %d", &x, &y) != 2) {
2877                         lyxerr << "SETXY: Could not parse coordinates in '"
2878                                << argument << std::endl;
2879                 }
2880                 bv_->getLyXText()->setCursorFromCoordinates(bv_, x, y);
2881         }
2882         break;
2883         
2884         case LFUN_GETLAYOUT:
2885                 owner_->getLyXFunc()->setMessage(tostr(bv_->getLyXText()->cursor.par()->layout()));
2886                 break;
2887                         
2888         case LFUN_GETFONT:
2889         {
2890                 LyXFont & font = bv_->getLyXText()->current_font;
2891                 if (font.shape() == LyXFont::ITALIC_SHAPE)
2892                         owner_->getLyXFunc()->setMessage("E");
2893                 else if (font.shape() == LyXFont::SMALLCAPS_SHAPE)
2894                         owner_->getLyXFunc()->setMessage("N");
2895                 else
2896                         owner_->getLyXFunc()->setMessage("0");
2897
2898         }
2899         break;
2900
2901         // --- accented characters ---------------------------
2902                 
2903         case LFUN_UMLAUT:
2904         case LFUN_CIRCUMFLEX:
2905         case LFUN_GRAVE:
2906         case LFUN_ACUTE:
2907         case LFUN_TILDE:
2908         case LFUN_CEDILLA:
2909         case LFUN_MACRON:
2910         case LFUN_DOT:
2911         case LFUN_UNDERDOT:
2912         case LFUN_UNDERBAR:
2913         case LFUN_CARON:
2914         case LFUN_SPECIAL_CARON:
2915         case LFUN_BREVE:
2916         case LFUN_TIE:
2917         case LFUN_HUNG_UMLAUT:
2918         case LFUN_CIRCLE:
2919         case LFUN_OGONEK:
2920                 if (argument.empty()) {
2921                         // As always...
2922                         owner_->getLyXFunc()->handleKeyFunc(action);
2923                 } else {
2924                         owner_->getLyXFunc()->handleKeyFunc(action);
2925                         owner_->getIntl()->getTrans()
2926                                 .TranslateAndInsert(argument[0], bv_->getLyXText());
2927                         update(bv_->getLyXText(),
2928                                BufferView::SELECT
2929                                | BufferView::FITCUR
2930                                | BufferView::CHANGE);
2931                 }
2932                 break;
2933         
2934         case LFUN_MATH_MACRO:
2935                 mathDispatchMathMacro(bv_, argument);
2936                 break;
2937
2938         case LFUN_MATH_DELIM:     
2939                 mathDispatchMathDelim(bv_, argument);
2940                 break;
2941
2942         case LFUN_INSERT_MATRIX:
2943                 mathDispatchInsertMatrix(bv_, argument);
2944                 break;
2945
2946         case LFUN_INSERT_MATH:
2947                 mathDispatchInsertMath(bv_, argument);
2948                 break;
2949
2950         case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
2951                 mathDispatchMathImportSelection(bv_, argument);
2952                 break;
2953
2954         case LFUN_MATH_DISPLAY:          // Open or create a displayed math inset
2955                 mathDispatchMathDisplay(bv_, argument);
2956                 break;
2957
2958         case LFUN_MATH_MODE:             // Open or create an inlined math inset 
2959                 mathDispatchMathMode(bv_, argument);
2960                 break;
2961                 
2962         case LFUN_GREEK:                 // Insert a single greek letter
2963                 mathDispatchGreek(bv_, argument);
2964                 break;
2965                 
2966         case LFUN_CITATION_INSERT:
2967         {
2968                 InsetCommandParams p;
2969                 p.setFromString(argument);
2970
2971                 InsetCitation * inset = new InsetCitation(p);
2972                 if (!insertInset(inset))
2973                         delete inset;
2974                 else
2975                         updateInset(inset, true);
2976         }
2977         break;
2978                     
2979         case LFUN_INSERT_BIBTEX:
2980         {   
2981                 // ale970405+lasgoutt970425
2982                 // The argument can be up to two tokens separated 
2983                 // by a space. The first one is the bibstyle.
2984                 string const db       = token(argument, ' ', 0);
2985                 string const bibstyle = token(argument, ' ', 1);
2986
2987                 InsetCommandParams p("BibTeX", db, bibstyle);
2988                 InsetBibtex * inset = new InsetBibtex(p);
2989                 
2990                 if (insertInset(inset)) {
2991                         if (argument.empty())
2992                                 inset->edit(bv_);
2993                 } else
2994                         delete inset;
2995         }
2996         break;
2997                 
2998         // BibTeX data bases
2999         case LFUN_BIBDB_ADD:
3000         {
3001                 InsetBibtex * inset = 
3002                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
3003                 if (inset) {
3004                         inset->addDatabase(argument);
3005                 }
3006         }
3007         break;
3008                     
3009         case LFUN_BIBDB_DEL:
3010         {
3011                 InsetBibtex * inset = 
3012                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
3013                 if (inset) {
3014                         inset->delDatabase(argument);
3015                 }
3016         }
3017         break;
3018         
3019         case LFUN_BIBTEX_STYLE:
3020         {
3021                 InsetBibtex * inset = 
3022                         static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
3023                 if (inset) {
3024                         inset->setOptions(argument);
3025                 }
3026         }
3027         break;
3028                 
3029         case LFUN_INDEX_CREATE:
3030         {
3031                 InsetCommandParams p("index");
3032                 if (argument.empty()) {
3033                         string const idxstring(bv_->getLyXText()->getStringToIndex(bv_));
3034                         p.setContents(idxstring);
3035                 } else {
3036                         p.setContents(argument);
3037                 }
3038
3039                 owner_->getDialogs()->createIndex(p.getAsString());
3040         }
3041         break;
3042                     
3043         case LFUN_INDEX_INSERT:
3044         {
3045                 InsetCommandParams p;
3046                 p.setFromString(argument);
3047                 InsetIndex * inset = new InsetIndex(p);
3048
3049                 if (!insertInset(inset))
3050                         delete inset;
3051                 else
3052                         updateInset(inset, true);
3053         }
3054         break;
3055                     
3056         case LFUN_INDEX_INSERT_LAST:
3057         {
3058                 string const idxstring(bv_->getLyXText()->getStringToIndex(bv_));
3059                 if (!idxstring.empty()) {
3060                         owner_->message(_("Word `")
3061                                         + idxstring + _(("' indexed.")));
3062                         InsetCommandParams p("index", idxstring);
3063                         InsetIndex * inset = new InsetIndex(p);
3064                         
3065                         if (!insertInset(inset))
3066                                 delete inset;
3067                         else
3068                                 updateInset(inset, true);
3069                 }
3070         }
3071         break;
3072
3073         case LFUN_INDEX_PRINT:
3074         {
3075                 InsetCommandParams p("printindex");
3076                 Inset * inset = new InsetPrintIndex(p);
3077                 if (!insertInset(inset, "Standard"))
3078                         delete inset;
3079         }
3080         break;
3081
3082         case LFUN_PARENTINSERT:
3083         {
3084                 lyxerr << "arg " << argument << endl;
3085                 InsetCommandParams p("lyxparent", argument);
3086                 Inset * inset = new InsetParent(p, *buffer_);
3087                 if (!insertInset(inset, "Standard"))
3088                         delete inset;
3089         }
3090                  
3091         break;
3092
3093         case LFUN_CHILD_INSERT:
3094         {
3095                 InsetInclude::Params p;
3096                 p.cparams.setFromString(argument);
3097                 p.masterFilename_ = buffer_->fileName();
3098
3099                 InsetInclude * inset = new InsetInclude(p);
3100                 if (!insertInset(inset))
3101                         delete inset;
3102                 else {
3103                         updateInset(inset, true);
3104                         bv_->owner()->getDialogs()->showInclude(inset);
3105                 }
3106         }
3107         break; 
3108
3109         case LFUN_FLOAT_LIST:
3110         {
3111                 // We should check the argument for validity. (Lgb)
3112                 Inset * inset = new InsetFloatList(argument);
3113                 if (!insertInset(inset, "Standard"))
3114                         delete inset;
3115         }
3116         break;
3117         
3118         case LFUN_THESAURUS_ENTRY:
3119         {
3120                 string arg = argument;
3121
3122                 if (arg.empty()) {
3123                         arg = bv_->getLyXText()->selectionAsString(buffer_,
3124                                                                    false);
3125  
3126                         // FIXME
3127                         if (arg.size() > 100 || arg.empty()) {
3128                                 // Get word or selection
3129                                 bv_->getLyXText()->selectWordWhenUnderCursor(bv_, LyXText::WHOLE_WORD);
3130                                 arg = bv_->getLyXText()->selectionAsString(buffer_, false);
3131                                 // FIXME: where is getLyXText()->unselect(bv_) ?
3132                         }
3133                 }
3134
3135                 bv_->owner()->getDialogs()->showThesaurus(arg);
3136         }
3137                 break;
3138  
3139         case LFUN_SELFINSERT:
3140         {
3141                 if (argument.empty()) break;
3142                 
3143                 /* Automatically delete the currently selected
3144                  * text and replace it with what is being
3145                  * typed in now. Depends on lyxrc settings
3146                  * "auto_region_delete", which defaults to
3147                  * true (on). */
3148
3149                 LyXText * lt = bv_->getLyXText();
3150                 
3151                 if (lyxrc.auto_region_delete) {
3152                         if (lt->selection.set()) {
3153                                 lt->cutSelection(bv_, false, false);
3154                                 bv_->update(lt,
3155                                             BufferView::SELECT
3156                                             | BufferView::FITCUR
3157                                             | BufferView::CHANGE);
3158                         }
3159                 }
3160                 
3161                 beforeChange(lt);
3162                 LyXFont const old_font(lt->real_current_font);
3163                 
3164                 string::const_iterator cit = argument.begin();
3165                 string::const_iterator end = argument.end();
3166                 for (; cit != end; ++cit) {
3167                         owner_->getIntl()->getTrans().TranslateAndInsert(*cit, lt);
3168                 }
3169                 
3170                 bv_->update(lt,
3171                             BufferView::SELECT
3172                             | BufferView::FITCUR
3173                             | BufferView::CHANGE);
3174                 
3175                 lt->selection.cursor = lt->cursor;
3176                 moveCursorUpdate(false);
3177                 
3178                 // real_current_font.number can change so we need to
3179                 // update the minibuffer
3180                 if (old_font != lt->real_current_font)
3181                         owner_->showState();
3182                 //return string();
3183         }
3184         break;
3185
3186         case LFUN_DATE_INSERT:  // jdblair: date-insert cmd
3187         {
3188                 time_t now_time_t = time(NULL);
3189                 struct tm * now_tm = localtime(&now_time_t);
3190                 setlocale(LC_TIME, "");
3191                 string arg;
3192                 if (!argument.empty())
3193                         arg = argument;
3194                 else 
3195                         arg = lyxrc.date_insert_format;
3196                 char datetmp[32];
3197                 int const datetmp_len =
3198                         ::strftime(datetmp, 32, arg.c_str(), now_tm);
3199
3200                 LyXText * lt = bv_->getLyXText();
3201                 
3202                 for (int i = 0; i < datetmp_len; i++) {
3203                         lt->insertChar(bv_, datetmp[i]);
3204                         update(lt,
3205                                BufferView::SELECT
3206                                | BufferView::FITCUR
3207                                | BufferView::CHANGE);
3208                 }
3209
3210                 lt->selection.cursor = lt->cursor;
3211                 moveCursorUpdate(false);
3212         }
3213         break;
3214
3215         case LFUN_UNKNOWN_ACTION:
3216                 owner_->getLyXFunc()->setErrorMessage(N_("Unknown function!"));
3217                 break;
3218         
3219         default:
3220                 return false;
3221         } // end of switch
3222
3223         return true;
3224 }
3225
3226
3227 void BufferView::Pimpl::newline()
3228 {
3229         if (available()) {
3230                 LyXText * lt = bv_->getLyXText();
3231                 hideCursor();
3232                 update(lt,
3233                        BufferView::SELECT
3234                        | BufferView::FITCUR);
3235                 lt->insertChar(bv_, Paragraph::META_NEWLINE);
3236                 update(lt,
3237                        BufferView::SELECT
3238                        | BufferView::FITCUR
3239                        | BufferView::CHANGE);
3240         }
3241 }
3242
3243
3244 void BufferView::Pimpl::hfill()
3245 {
3246         if (available()) {
3247                 LyXText * lt = bv_->getLyXText();
3248                 hideCursor();
3249                 update(lt,
3250                        BufferView::SELECT
3251                        | BufferView::FITCUR);
3252                 lt->insertChar(bv_, Paragraph::META_HFILL);
3253                 update(lt,
3254                        BufferView::SELECT
3255                        | BufferView::FITCUR
3256                        | BufferView::CHANGE);
3257         }
3258 }
3259
3260
3261 void BufferView::Pimpl::specialChar(InsetSpecialChar::Kind kind)
3262 {
3263         if (available()) {
3264                 LyXText * lt = bv_->getLyXText();
3265                 
3266                 hideCursor();
3267                 update(lt, BufferView::SELECT|BufferView::FITCUR);
3268                 InsetSpecialChar * new_inset = 
3269                         new InsetSpecialChar(kind);
3270                 if (!insertInset(new_inset))
3271                         delete new_inset;
3272                 else
3273                         updateInset(new_inset, true);
3274         }
3275 }
3276
3277
3278 void BufferView::Pimpl::smartQuote()
3279 {
3280         LyXText const * lt = bv_->getLyXText();
3281         Paragraph const * par = lt->cursor.par();
3282         pos_type pos = lt->cursor.pos();
3283         char c;
3284
3285         if (!pos
3286             || (par->isInset(pos - 1)
3287                 && par->getInset(pos - 1)->isSpace()))
3288                 c = ' ';
3289         else
3290                 c = par->getChar(pos - 1);
3291                 
3292
3293         hideCursor();
3294
3295         LyXLayout const & style =
3296                 textclasslist[bv_->buffer()->params.textclass][par->layout()];
3297         
3298         if (style.pass_thru ||
3299                 (!insertInset(new InsetQuotes(c, bv_->buffer()->params))))
3300                 bv_->owner()->getLyXFunc()->dispatch(LFUN_SELFINSERT, "\"");
3301 }
3302
3303
3304 void BufferView::Pimpl::insertAndEditInset(Inset * inset)
3305 {
3306         if (insertInset(inset))
3307                 inset->edit(bv_);
3308         else
3309                 delete inset;
3310 }
3311
3312  
3313 // Open and lock an updatable inset
3314 bool BufferView::Pimpl::open_new_inset(UpdatableInset * new_inset, bool behind)
3315 {
3316         LyXText * lt = bv_->getLyXText();
3317         
3318         beforeChange(lt);
3319         finishUndo();
3320         if (!insertInset(new_inset)) {
3321                 delete new_inset;
3322                 return false;
3323         }
3324         new_inset->edit(bv_, !behind);
3325         return true;
3326 }
3327
3328
3329 bool BufferView::Pimpl::insertInset(Inset * inset, string const & lout)
3330 {
3331         // if we are in a locking inset we should try to insert the
3332         // inset there otherwise this is a illegal function now
3333         if (bv_->theLockingInset()) {
3334                 if (bv_->theLockingInset()->insetAllowed(inset))
3335                     return bv_->theLockingInset()->insertInset(bv_, inset);
3336                 return false;
3337         }
3338
3339         // not quite sure if we want this...
3340         setCursorParUndo(bv_);
3341         freezeUndo();
3342         
3343         beforeChange(bv_->text);
3344         if (!lout.empty()) {
3345                 update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
3346                 bv_->text->breakParagraph(bv_);
3347                 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
3348
3349                 if (bv_->text->cursor.par()->size()) {
3350                         bv_->text->cursorLeft(bv_);
3351                         
3352                         bv_->text->breakParagraph(bv_);
3353                         update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
3354                 }
3355
3356                 string lres = lout;
3357                 LyXTextClass const & tclass =
3358                         textclasslist[buffer_->params.textclass];
3359                 bool hasLayout = tclass.hasLayout(lres);
3360                 string lay = tclass.defaultLayoutName();
3361
3362                 if (hasLayout != false) {
3363                         // layout found
3364                         lay = lres;
3365                 } else {
3366                         // layout not fount using default
3367                         lay = tclass.defaultLayoutName();
3368                 }
3369
3370                 bv_->text->setLayout(bv_, lay);
3371                 
3372                 bv_->text->setParagraph(bv_, 0, 0,
3373                                    0, 0,
3374                                    VSpace(VSpace::NONE), VSpace(VSpace::NONE),
3375                                    Spacing(),
3376                                    LYX_ALIGN_LAYOUT, 
3377                                    string(),
3378                                    0);
3379                 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
3380         }
3381         
3382         bv_->text->insertInset(bv_, inset);
3383         update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
3384
3385         unFreezeUndo();
3386         return true;
3387 }
3388
3389
3390 void BufferView::Pimpl::updateInset(Inset * inset, bool mark_dirty)
3391 {
3392         if (!inset || !available())
3393                 return;
3394
3395         // first check for locking insets
3396         if (bv_->theLockingInset()) {
3397                 if (bv_->theLockingInset() == inset) {
3398                         if (bv_->text->updateInset(bv_, inset)) {
3399                                 update();
3400                                 if (mark_dirty) {
3401                                         buffer_->markDirty();
3402                                 }
3403                                 updateScrollbar();
3404                                 return;
3405                         }
3406                 } else if (bv_->theLockingInset()->updateInsetInInset(bv_, inset)) {
3407                         if (bv_->text->updateInset(bv_,  bv_->theLockingInset())) {
3408                                 update();
3409                                 if (mark_dirty) {
3410                                         buffer_->markDirty();
3411                                 }
3412                                 updateScrollbar();
3413                                 return;
3414                         }
3415                 }
3416         }
3417   
3418         // then check if the inset is a top_level inset (has no owner)
3419         // if yes do the update as always otherwise we have to update the
3420         // toplevel inset where this inset is inside
3421         Inset * tl_inset = inset;
3422         while(tl_inset->owner())
3423                 tl_inset = tl_inset->owner();
3424         hideCursor();
3425         if (tl_inset == inset) {
3426                 update(bv_->text, BufferView::UPDATE);
3427                 if (bv_->text->updateInset(bv_, inset)) {
3428                         if (mark_dirty) {
3429                                 update(bv_->text,
3430                                        BufferView::SELECT
3431                                        | BufferView::FITCUR
3432                                        | BufferView::CHANGE);
3433                         } else {
3434                                 update(bv_->text, SELECT);
3435                         }
3436                         return;
3437                 }
3438         } else if (static_cast<UpdatableInset *>(tl_inset)
3439                            ->updateInsetInInset(bv_, inset))
3440         {
3441                         if (bv_->text->updateInset(bv_,  tl_inset)) {
3442                                 update();
3443                                 updateScrollbar();
3444                         }
3445         }
3446 }
3447
3448
3449 void BufferView::Pimpl::gotoInset(vector<Inset::Code> const & codes,
3450                                   bool same_content)
3451 {
3452         if (!available()) return;
3453         
3454         hideCursor();
3455         beforeChange(bv_->text);
3456         update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
3457         
3458         LyXCursor const & cursor = bv_->text->cursor;
3459  
3460         string contents;
3461         if (same_content &&
3462             cursor.par()->isInset(cursor.pos())) {
3463                 Inset const * inset = cursor.par()->getInset(cursor.pos());
3464                 if (find(codes.begin(), codes.end(), inset->lyxCode())
3465                     != codes.end())
3466                         contents =
3467                                 static_cast<InsetCommand const *>(inset)->getContents();
3468         }
3469         
3470  
3471         if (!bv_->text->gotoNextInset(bv_, codes, contents)) {
3472                 if (bv_->text->cursor.pos() 
3473                     || bv_->text->cursor.par() != bv_->text->ownerParagraph()) {
3474                         LyXCursor tmp = bv_->text->cursor;
3475                         bv_->text->cursor.par(bv_->text->ownerParagraph());
3476                         bv_->text->cursor.pos(0);
3477                         if (!bv_->text->gotoNextInset(bv_, codes, contents)) {
3478                                 bv_->text->cursor = tmp;
3479                                 bv_->owner()->message(_("No more insets"));
3480                         }
3481                 } else {
3482                         bv_->owner()->message(_("No more insets"));
3483                 }
3484         }
3485         update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
3486         bv_->text->selection.cursor = bv_->text->cursor;
3487 }
3488
3489
3490 void BufferView::Pimpl::gotoInset(Inset::Code code, bool same_content)
3491 {
3492         gotoInset(vector<Inset::Code>(1, code), same_content);
3493 }