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