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