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