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