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