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