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