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