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