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