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