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