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