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