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