]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
431585ba7b8879a85b20cc1a8aecf621932ae331
[lyx.git] / src / BufferView.C
1 /**
2  * \file BufferView.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alfredo Braunstein
7  * \author Lars Gullik Bjønnes
8  * \author John Levon
9  * \author André Pönitz
10  * \author Jürgen Vigna
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "BufferView.h"
18
19 #include "buffer.h"
20 #include "buffer_funcs.h"
21 #include "bufferlist.h"
22 #include "bufferparams.h"
23 #include "coordcache.h"
24 #include "CutAndPaste.h"
25 #include "debug.h"
26 #include "dispatchresult.h"
27 #include "errorlist.h"
28 #include "factory.h"
29 #include "FloatList.h"
30 #include "funcrequest.h"
31 #include "FuncStatus.h"
32 #include "gettext.h"
33 #include "intl.h"
34 #include "insetiterator.h"
35 #include "language.h"
36 #include "LaTeXFeatures.h"
37 #include "lyx_cb.h" // added for Dispatch functions
38 #include "lyx_main.h"
39 #include "lyxfind.h"
40 #include "lyxfunc.h"
41 #include "lyxlayout.h"
42 #include "lyxtext.h"
43 #include "lyxtextclass.h"
44 #include "lyxrc.h"
45 #include "session.h"
46 #include "paragraph.h"
47 #include "paragraph_funcs.h"
48 #include "ParagraphParameters.h"
49 #include "pariterator.h"
50 #include "texrow.h"
51 #include "toc.h"
52 #include "undo.h"
53 #include "vspace.h"
54 #include "WordLangTuple.h"
55 #include "metricsinfo.h"
56
57 #include "insets/insetbibtex.h"
58 #include "insets/insetcommand.h" // ChangeRefs
59 #include "insets/insetref.h"
60 #include "insets/insettext.h"
61
62 #include "frontends/Alert.h"
63 #include "frontends/Clipboard.h"
64 #include "frontends/Dialogs.h"
65 #include "frontends/FileDialog.h"
66 #include "frontends/font_metrics.h"
67 #include "frontends/Gui.h"
68 #include "frontends/LyXView.h"
69 #include "frontends/Selection.h"
70
71 #include "graphics/Previews.h"
72
73 #include "support/convert.h"
74 #include "support/filefilterlist.h"
75 #include "support/filetools.h"
76 #include "support/package.h"
77 #include "support/types.h"
78
79 #include <boost/bind.hpp>
80 #include <boost/current_function.hpp>
81
82 #include <functional>
83 #include <vector>
84
85
86 using lyx::frontend::Clipboard;
87 using lyx::frontend::Gui;
88
89 using lyx::docstring;
90 using lyx::pos_type;
91
92 using lyx::support::addPath;
93 using lyx::support::bformat;
94 using lyx::support::FileFilterList;
95 using lyx::support::fileSearch;
96 using lyx::support::isDirWriteable;
97 using lyx::support::makeDisplayPath;
98 using lyx::support::makeAbsPath;
99 using lyx::support::package;
100
101 using std::distance;
102 using std::endl;
103 using std::istringstream;
104 using std::find;
105 using std::make_pair;
106 using std::min;
107 using std::max;
108 using std::mem_fun_ref;
109 using std::string;
110 using std::vector;
111
112 extern BufferList bufferlist;
113
114
115 namespace {
116
117 unsigned int const saved_positions_num = 20;
118
119
120 /// Return an inset of this class if it exists at the current cursor position
121 template <class T>
122 T * getInsetByCode(LCursor & cur, InsetBase::Code code)
123 {
124         T * inset = 0;
125         DocIterator it = cur;
126         if (it.nextInset() &&
127             it.nextInset()->lyxCode() == code) {
128                 inset = static_cast<T*>(it.nextInset());
129         }
130         return inset;
131 }
132
133 } // anon namespace
134
135
136 BufferView::BufferView(LyXView * owner)
137         : owner_(owner), buffer_(0), wh_(0),
138           cursor_(*this),
139           multiparsel_cache_(false), anchor_ref_(0), offset_ref_(0)
140 {
141         xsel_cache_.set = false;
142
143         saved_positions.resize(saved_positions_num);
144         // load saved bookmarks
145         lyx::Session::BookmarkList & bmList = LyX::ref().session().loadBookmarks();
146         for (lyx::Session::BookmarkList::iterator bm = bmList.begin();
147                 bm != bmList.end(); ++bm)
148                 if (bm->get<0>() < saved_positions_num)
149                         saved_positions[bm->get<0>()] = Position( bm->get<1>(), bm->get<2>(), bm->get<3>() );
150         // and then clear them
151         bmList.clear();
152 }
153
154
155 BufferView::~BufferView()
156 {
157 }
158
159
160 void BufferView::unsetXSel()
161 {
162         xsel_cache_.set = false;
163 }
164
165
166 Buffer * BufferView::buffer() const
167 {
168         return buffer_;
169 }
170
171
172 LyXView * BufferView::owner() const
173 {
174         return owner_;
175 }
176
177
178 void BufferView::setBuffer(Buffer * b)
179 {
180         lyxerr[Debug::INFO] << BOOST_CURRENT_FUNCTION
181                             << "[ b = " << b << "]" << endl;
182
183         if (buffer_) {
184                 // Save the actual cursor position and anchor inside the
185                 // buffer so that it can be restored in case we rechange
186                 // to this buffer later on.
187                 buffer_->saveCursor(cursor_.selectionBegin(),
188                                     cursor_.selectionEnd());
189                 // current buffer is going to be switched-off, save cursor pos
190                 LyX::ref().session().saveFilePosition(buffer_->fileName(),
191                         boost::tie(cursor_.pit(), cursor_.pos()) );
192         }
193
194         // If we're quitting lyx, don't bother updating stuff
195         if (quitting) {
196                 buffer_ = 0;
197                 return;
198         }
199
200         // If we are closing current buffer, switch to the first in
201         // buffer list.
202         if (!b) {
203                 lyxerr[Debug::INFO] << BOOST_CURRENT_FUNCTION
204                                     << " No Buffer!" << endl;
205                 // We are closing the buffer, use the first buffer as current
206                 buffer_ = bufferlist.first();
207         } else {
208                 // Set current buffer
209                 buffer_ = b;
210         }
211
212         // Reset old cursor
213         cursor_ = LCursor(*this);
214         anchor_ref_ = 0;
215         offset_ref_ = 0;
216
217         if (buffer_) {
218                 lyxerr[Debug::INFO] << BOOST_CURRENT_FUNCTION
219                                     << "Buffer addr: " << buffer_ << endl;
220                 cursor_.push(buffer_->inset());
221                 cursor_.resetAnchor();
222                 buffer_->text().init(this);
223                 buffer_->text().setCurrentFont(cursor_);
224                 if (buffer_->getCursor().size() > 0 &&
225                     buffer_->getAnchor().size() > 0)
226                 {
227                         cursor_.setCursor(buffer_->getAnchor().asDocIterator(&(buffer_->inset())));
228                         cursor_.resetAnchor();
229                         cursor_.setCursor(buffer_->getCursor().asDocIterator(&(buffer_->inset())));
230                         cursor_.setSelection();
231                 }
232         }
233
234         update();
235
236         if (buffer_ && lyx::graphics::Previews::status() != LyXRC::PREVIEW_OFF)
237                 lyx::graphics::Previews::get().generateBufferPreviews(*buffer_);
238 }
239
240
241 bool BufferView::loadLyXFile(string const & filename, bool tolastfiles)
242 {
243         // Get absolute path of file and add ".lyx"
244         // to the filename if necessary
245         string s = fileSearch(string(), filename, "lyx");
246
247         bool const found = !s.empty();
248
249         if (!found)
250                 s = filename;
251
252         // File already open?
253         if (bufferlist.exists(s)) {
254                 docstring const file = makeDisplayPath(s, 20);
255                 docstring text = bformat(_("The document %1$s is already "
256                                                      "loaded.\n\nDo you want to revert "
257                                                      "to the saved version?"), file);
258                 int const ret = Alert::prompt(_("Revert to saved document?"),
259                         text, 0, 1,  _("&Revert"), _("&Switch to document"));
260
261                 if (ret != 0) {
262                         setBuffer(bufferlist.getBuffer(s));
263                         return true;
264                 }
265                 // FIXME: should be LFUN_REVERT
266                 if (!bufferlist.close(bufferlist.getBuffer(s), false))
267                         return false;
268                 // Fall through to new load. (Asger)
269         }
270
271         Buffer * b = 0;
272
273         if (found) {
274                 b = bufferlist.newBuffer(s);
275                 if (!::loadLyXFile(b, s)) {
276                         bufferlist.release(b);
277                         return false;
278                 }
279         } else {
280                 docstring text = bformat(_("The document %1$s does not yet "
281                                                      "exist.\n\nDo you want to create "
282                                                      "a new document?"), lyx::from_utf8(s));
283                 int const ret = Alert::prompt(_("Create new document?"),
284                          text, 0, 1, _("&Create"), _("Cancel"));
285
286                 if (ret == 0) {
287                         b = newFile(s, string(), true);
288                         if (!b)
289                                 return false;
290                 } else
291                         return false;
292         }
293
294         setBuffer(b);
295         // Send the "errors" signal in case of parsing errors
296         b->errors("Parse");
297
298         // scroll to the position when the file was last closed
299         if (lyxrc.use_lastfilepos) {
300                 lyx::pit_type pit;
301                 lyx::pos_type pos;
302                 boost::tie(pit, pos) = LyX::ref().session().loadFilePosition(s);
303                 // I am not sure how to separate the following part to a function
304                 // so I will leave this to Lars.
305                 //
306                 // check pit since the document may be externally changed.
307                 if ( static_cast<size_t>(pit) < b->paragraphs().size() ) {
308                         ParIterator it = b->par_iterator_begin();
309                         ParIterator const end = b->par_iterator_end();
310                         for (; it != end; ++it)
311                                 if (it.pit() == pit) {
312                                         // restored pos may be bigger than it->size
313                                         setCursor(makeDocIterator(it, min(pos, it->size())));
314                                         update(Update::FitCursor);
315                                         break;
316                                 }
317                 }
318         }
319
320         if (tolastfiles)
321                 LyX::ref().session().addLastFile(b->fileName());
322
323         return true;
324 }
325
326
327 void BufferView::reload()
328 {
329         string const fn = buffer_->fileName();
330         if (bufferlist.close(buffer_, false))
331                 loadLyXFile(fn);
332 }
333
334
335 void BufferView::resize()
336 {
337         if (!buffer_)
338                 return;
339
340         lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION << endl;
341
342         buffer_->text().init(this);
343         update();
344         switchKeyMap();
345 }
346
347
348 bool BufferView::fitCursor()
349 {
350         if (bv_funcs::status(this, cursor_) == bv_funcs::CUR_INSIDE) {
351                 LyXFont const font = cursor_.getFont();
352                 int const asc = font_metrics::maxAscent(font);
353                 int const des = font_metrics::maxDescent(font);
354                 Point const p = bv_funcs::getPos(cursor_, cursor_.boundary());
355                 if (p.y_ - asc >= 0 && p.y_ + des < height_)
356                         return false;
357         }
358         center();
359         return true;
360 }
361
362
363 bool BufferView::multiParSel()
364 {
365         if (!cursor_.selection())
366                 return false;
367         bool ret = multiparsel_cache_;
368         multiparsel_cache_ = cursor_.selBegin().pit() != cursor_.selEnd().pit();
369         // Either this, or previous selection spans paragraphs
370         return ret || multiparsel_cache_;
371 }
372
373
374 bool BufferView::update(Update::flags flags)
375 {
376         // This is close to a hot-path.
377         if (lyxerr.debugging(Debug::DEBUG)) {
378                 lyxerr[Debug::DEBUG]
379                         << BOOST_CURRENT_FUNCTION
380                         << "[fitcursor = " << (flags & Update::FitCursor)
381                         << ", forceupdate = " << (flags & Update::Force)
382                         << ", singlepar = " << (flags & Update::SinglePar)
383                         << "]  buffer: " << buffer_ << endl;
384         }
385
386         // Check needed to survive LyX startup
387         if (!buffer_)
388                 return false;
389
390         // Update macro store
391         buffer_->buildMacros();
392
393         // First drawing step
394         updateMetrics(flags & Update::SinglePar);
395
396         // The second drawing step is done in WorkArea::redraw() if needed.
397         bool const need_second_step =
398                 (flags & (Update::Force | Update::FitCursor | Update::MultiParSel))
399                 && (fitCursor() || multiParSel());
400
401         return need_second_step;
402 }
403
404
405 void BufferView::updateScrollbar()
406 {
407         if (!buffer_) {
408                 lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
409                                      << " no text in updateScrollbar" << endl;
410                 scrollbarParameters_.reset();
411                 return;
412         }
413
414         LyXText & t = buffer_->text();
415         int const parsize = int(t.paragraphs().size() - 1);
416         if (anchor_ref_ >  parsize)  {
417                 anchor_ref_ = parsize;
418                 offset_ref_ = 0;
419         }
420
421         lyxerr[Debug::GUI]
422                 << BOOST_CURRENT_FUNCTION
423                 << " Updating scrollbar: height: " << t.paragraphs().size()
424                 << " curr par: " << cursor_.bottom().pit()
425                 << " default height " << defaultRowHeight() << endl;
426
427         // It would be better to fix the scrollbar to understand
428         // values in [0..1] and divide everything by wh
429
430         // estimated average paragraph height:
431         if (wh_ == 0)
432                 wh_ = height_ / 4;
433         int h = t.getPar(anchor_ref_).height();
434
435         // Normalize anchor/offset (MV):
436         while (offset_ref_ > h && anchor_ref_ < parsize) {
437                 anchor_ref_++;
438                 offset_ref_ -= h;
439                 h = t.getPar(anchor_ref_).height();
440         }
441         // Look at paragraph heights on-screen
442         int sumh = 0;
443         int nh = 0;
444         for (lyx::pit_type pit = anchor_ref_; pit <= parsize; ++pit) {
445                 if (sumh > height_)
446                         break;
447                 int const h2 = t.getPar(pit).height();
448                 sumh += h2;
449                 nh++;
450         }
451         int const hav = sumh / nh;
452         // More realistic average paragraph height
453         if (hav > wh_)
454                 wh_ = hav;
455
456         scrollbarParameters_.height = (parsize + 1) * wh_;
457         scrollbarParameters_.position = anchor_ref_ * wh_ + int(offset_ref_ * wh_ / float(h));
458         scrollbarParameters_.lineScrollHeight = int(wh_ * defaultRowHeight() / float(h));
459 }
460
461
462 ScrollbarParameters const & BufferView::scrollbarParameters() const
463 {
464         return scrollbarParameters_;
465 }
466
467
468 void BufferView::scrollDocView(int value)
469 {
470         lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
471                            << "[ value = " << value << "]" << endl;
472
473         if (!buffer_)
474                 return;
475
476         LyXText & t = buffer_->text();
477
478         float const bar = value / float(wh_ * t.paragraphs().size());
479
480         anchor_ref_ = int(bar * t.paragraphs().size());
481         if (anchor_ref_ >  int(t.paragraphs().size()) - 1)
482                 anchor_ref_ = int(t.paragraphs().size()) - 1;
483         t.redoParagraph(anchor_ref_);
484         int const h = t.getPar(anchor_ref_).height();
485         offset_ref_ = int((bar * t.paragraphs().size() - anchor_ref_) * h);
486 }
487
488
489 void BufferView::setCursorFromScrollbar()
490 {
491         LyXText & t = buffer_->text();
492
493         int const height = 2 * defaultRowHeight();
494         int const first = height;
495         int const last = height_ - height;
496         LCursor & cur = cursor_;
497
498         bv_funcs::CurStatus st = bv_funcs::status(this, cur);
499
500         switch (st) {
501         case bv_funcs::CUR_ABOVE:
502                 t.setCursorFromCoordinates(cur, 0, first);
503                 cur.clearSelection();
504                 break;
505         case bv_funcs::CUR_BELOW:
506                 t.setCursorFromCoordinates(cur, 0, last);
507                 cur.clearSelection();
508                 break;
509         case bv_funcs::CUR_INSIDE:
510                 int const y = bv_funcs::getPos(cur, cur.boundary()).y_;
511                 int const newy = min(last, max(y, first));
512                 if (y != newy) {
513                         cur.reset(buffer_->inset());
514                         t.setCursorFromCoordinates(cur, 0, newy);
515                 }
516         }
517 }
518
519
520 bool BufferView::available() const
521 {
522         return buffer_;
523 }
524
525
526 Change const BufferView::getCurrentChange()
527 {
528         if (!buffer_->params().tracking_changes || !cursor_.selection())
529                 return Change(Change::UNCHANGED);
530
531         DocIterator dit = cursor_.selectionBegin();
532         return dit.paragraph().lookupChange(dit.pos());
533 }
534
535
536 void BufferView::savePosition(unsigned int i)
537 {
538         if (i >= saved_positions_num)
539                 return;
540         BOOST_ASSERT(cursor_.inTexted());
541         saved_positions[i] = Position(buffer_->fileName(),
542                                       cursor_.paragraph().id(),
543                                       cursor_.pos());
544         if (i > 0)
545                 // emit message signal.
546                 message(bformat(_("Saved bookmark %1$d"), i));
547 }
548
549
550 void BufferView::restorePosition(unsigned int i)
551 {
552         if (i >= saved_positions_num)
553                 return;
554
555         string const fname = saved_positions[i].filename;
556
557         cursor_.clearSelection();
558
559         if (fname != buffer_->fileName()) {
560                 Buffer * b = 0;
561                 if (bufferlist.exists(fname))
562                         b = bufferlist.getBuffer(fname);
563                 else {
564                         b = bufferlist.newBuffer(fname);
565                         // Don't ask, just load it
566                         ::loadLyXFile(b, fname);
567                 }
568                 if (b)
569                         setBuffer(b);
570         }
571
572         ParIterator par = buffer_->getParFromID(saved_positions[i].par_id);
573         if (par == buffer_->par_iterator_end())
574                 return;
575
576         setCursor(makeDocIterator(par, min(par->size(), saved_positions[i].par_pos)));
577
578         if (i > 0)
579                 // emit message signal.
580                 message(bformat(_("Moved to bookmark %1$d"), i));
581 }
582
583
584 bool BufferView::isSavedPosition(unsigned int i)
585 {
586         return i < saved_positions_num && !saved_positions[i].filename.empty();
587 }
588
589 void BufferView::saveSavedPositions()
590 {
591         // save bookmarks. It is better to use the pit interface
592         // but I do not know how to effectively convert between
593         // par_id and pit.
594         for (unsigned int i=1; i < saved_positions_num; ++i) {
595                 if ( isSavedPosition(i) )
596                         LyX::ref().session().saveBookmark( boost::tie(
597                                 i,
598                                 saved_positions[i].filename,
599                                 saved_positions[i].par_id,
600                                 saved_positions[i].par_pos) );
601         }
602 }
603
604 void BufferView::switchKeyMap()
605 {
606         if (!lyxrc.rtl_support)
607                 return;
608
609         Intl & intl = owner_->getIntl();
610         if (getLyXText()->real_current_font.isRightToLeft()) {
611                 if (intl.keymap == Intl::PRIMARY)
612                         intl.keyMapSec();
613         } else {
614                 if (intl.keymap == Intl::SECONDARY)
615                         intl.keyMapPrim();
616         }
617 }
618
619
620 int BufferView::workWidth() const
621 {
622         return width_;
623 }
624
625
626 void BufferView::center()
627 {
628         CursorSlice & bot = cursor_.bottom();
629         lyx::pit_type const pit = bot.pit();
630         bot.text()->redoParagraph(pit);
631         Paragraph const & par = bot.text()->paragraphs()[pit];
632         anchor_ref_ = pit;
633         offset_ref_ = bv_funcs::coordOffset(cursor_, cursor_.boundary()).y_
634                 + par.ascent() - height_ / 2;
635 }
636
637
638 FuncStatus BufferView::getStatus(FuncRequest const & cmd)
639 {
640         FuncStatus flag;
641
642         switch (cmd.action) {
643
644         case LFUN_UNDO:
645                 flag.enabled(!buffer_->undostack().empty());
646                 break;
647         case LFUN_REDO:
648                 flag.enabled(!buffer_->redostack().empty());
649                 break;
650         case LFUN_FILE_INSERT:
651         case LFUN_FILE_INSERT_ASCII_PARA:
652         case LFUN_FILE_INSERT_ASCII:
653         case LFUN_BOOKMARK_SAVE:
654                 // FIXME: Actually, these LFUNS should be moved to LyXText
655                 flag.enabled(cursor_.inTexted());
656                 break;
657         case LFUN_FONT_STATE:
658         case LFUN_LABEL_INSERT:
659         case LFUN_PARAGRAPH_GOTO:
660         // FIXME handle non-trivially
661         case LFUN_OUTLINE_UP:
662         case LFUN_OUTLINE_DOWN:
663         case LFUN_OUTLINE_IN:
664         case LFUN_OUTLINE_OUT:
665         case LFUN_NOTE_NEXT:
666         case LFUN_REFERENCE_NEXT:
667         case LFUN_WORD_FIND:
668         case LFUN_WORD_REPLACE:
669         case LFUN_MARK_OFF:
670         case LFUN_MARK_ON:
671         case LFUN_MARK_TOGGLE:
672         case LFUN_SCREEN_RECENTER:
673         case LFUN_BIBTEX_DATABASE_ADD:
674         case LFUN_BIBTEX_DATABASE_DEL:
675         case LFUN_WORDS_COUNT:
676         case LFUN_NEXT_INSET_TOGGLE:
677                 flag.enabled(true);
678                 break;
679
680         case LFUN_LABEL_GOTO: {
681                 flag.enabled(!cmd.argument().empty()
682                     || getInsetByCode<InsetRef>(cursor_, InsetBase::REF_CODE));
683                 break;
684         }
685
686         case LFUN_BOOKMARK_GOTO:
687                 flag.enabled(isSavedPosition(convert<unsigned int>(lyx::to_utf8(cmd.argument()))));
688                 break;
689         case LFUN_CHANGES_TRACK:
690                 flag.enabled(true);
691                 flag.setOnOff(buffer_->params().tracking_changes);
692                 break;
693
694         case LFUN_CHANGES_OUTPUT: {
695                 OutputParams runparams;
696                 LaTeXFeatures features(*buffer_, buffer_->params(), runparams);
697                 flag.enabled(buffer_ && buffer_->params().tracking_changes
698                         && features.isAvailable("dvipost"));
699                 flag.setOnOff(buffer_->params().output_changes);
700                 break;
701         }
702
703         case LFUN_CHANGES_MERGE:
704         case LFUN_CHANGE_ACCEPT: // what about these two
705         case LFUN_CHANGE_REJECT: // what about these two
706         case LFUN_ALL_CHANGES_ACCEPT:
707         case LFUN_ALL_CHANGES_REJECT:
708                 flag.enabled(buffer_ && buffer_->params().tracking_changes);
709                 break;
710
711         case LFUN_BUFFER_TOGGLE_COMPRESSION: {
712                 flag.setOnOff(buffer_->params().compressed);
713                 break;
714         }
715
716         default:
717                 flag.enabled(false);
718         }
719
720         return flag;
721 }
722
723
724 bool BufferView::dispatch(FuncRequest const & cmd)
725 {
726         //lyxerr << BOOST_CURRENT_FUNCTION
727         //       << [ cmd = " << cmd << "]" << endl;
728
729         // Make sure that the cached BufferView is correct.
730         lyxerr[Debug::ACTION] << BOOST_CURRENT_FUNCTION
731                 << " action[" << cmd.action << ']'
732                 << " arg[" << lyx::to_utf8(cmd.argument()) << ']'
733                 << " x[" << cmd.x << ']'
734                 << " y[" << cmd.y << ']'
735                 << " button[" << cmd.button() << ']'
736                 << endl;
737
738         LCursor & cur = cursor_;
739
740         switch (cmd.action) {
741
742         case LFUN_UNDO:
743                 if (buffer_) {
744                         cur.message(_("Undo"));
745                         cur.clearSelection();
746                         if (!textUndo(*this))
747                                 cur.message(_("No further undo information"));
748                         update();
749                         switchKeyMap();
750                 }
751                 break;
752
753         case LFUN_REDO:
754                 if (buffer_) {
755                         cur.message(_("Redo"));
756                         cur.clearSelection();
757                         if (!textRedo(*this))
758                                 cur.message(_("No further redo information"));
759                         update();
760                         switchKeyMap();
761                 }
762                 break;
763
764         case LFUN_FILE_INSERT:
765                 // FIXME: We don't know the encoding of filenames
766                 menuInsertLyXFile(lyx::to_utf8(cmd.argument()));
767                 break;
768
769         case LFUN_FILE_INSERT_ASCII_PARA:
770                 // FIXME: We don't know the encoding of filenames
771                 insertAsciiFile(this, lyx::to_utf8(cmd.argument()), true);
772                 break;
773
774         case LFUN_FILE_INSERT_ASCII:
775                 // FIXME: We don't know the encoding of filenames
776                 insertAsciiFile(this, lyx::to_utf8(cmd.argument()), false);
777                 break;
778
779         case LFUN_FONT_STATE:
780                 cur.message(lyx::from_utf8(cur.currentState()));
781                 break;
782
783         case LFUN_BOOKMARK_SAVE:
784                 savePosition(convert<unsigned int>(lyx::to_utf8(cmd.argument())));
785                 break;
786
787         case LFUN_BOOKMARK_GOTO:
788                 restorePosition(convert<unsigned int>(lyx::to_utf8(cmd.argument())));
789                 break;
790
791         case LFUN_LABEL_GOTO: {
792                 string label = lyx::to_utf8(cmd.argument());
793                 if (label.empty()) {
794                         InsetRef * inset =
795                                 getInsetByCode<InsetRef>(cursor_,
796                                                          InsetBase::REF_CODE);
797                         if (inset) {
798                                 label = inset->getContents();
799                                 savePosition(0);
800                         }
801                 }
802
803                 if (!label.empty())
804                         gotoLabel(label);
805                 break;
806         }
807
808         case LFUN_PARAGRAPH_GOTO: {
809                 int const id = convert<int>(lyx::to_utf8(cmd.argument()));
810                 ParIterator par = buffer_->getParFromID(id);
811                 if (par == buffer_->par_iterator_end()) {
812                         lyxerr[Debug::INFO] << "No matching paragraph found! ["
813                                             << id << ']' << endl;
814                         break;
815                 } else {
816                         lyxerr[Debug::INFO] << "Paragraph " << par->id()
817                                             << " found." << endl;
818                 }
819
820                 // Set the cursor
821                 setCursor(makeDocIterator(par, 0));
822
823                 update();
824                 switchKeyMap();
825                 break;
826         }
827
828         case LFUN_OUTLINE_UP:
829                 lyx::toc::outline(lyx::toc::Up, cursor_);
830                 cursor_.text()->setCursor(cursor_, cursor_.pit(), 0);
831                 updateLabels(*buffer_);
832                 break;
833         case LFUN_OUTLINE_DOWN:
834                 lyx::toc::outline(lyx::toc::Down, cursor_);
835                 cursor_.text()->setCursor(cursor_, cursor_.pit(), 0);
836                 updateLabels(*buffer_);
837                 break;
838         case LFUN_OUTLINE_IN:
839                 lyx::toc::outline(lyx::toc::In, cursor_);
840                 updateLabels(*buffer_);
841                 break;
842         case LFUN_OUTLINE_OUT:
843                 lyx::toc::outline(lyx::toc::Out, cursor_);
844                 updateLabels(*buffer_);
845                 break;
846
847         case LFUN_NOTE_NEXT:
848                 bv_funcs::gotoInset(this, InsetBase::NOTE_CODE, false);
849                 break;
850
851         case LFUN_REFERENCE_NEXT: {
852                 vector<InsetBase_code> tmp;
853                 tmp.push_back(InsetBase::LABEL_CODE);
854                 tmp.push_back(InsetBase::REF_CODE);
855                 bv_funcs::gotoInset(this, tmp, true);
856                 break;
857         }
858
859         case LFUN_CHANGES_TRACK:
860                 trackChanges();
861                 break;
862
863         case LFUN_CHANGES_OUTPUT: {
864                 bool const state = buffer_->params().output_changes;
865                 buffer_->params().output_changes = !state;
866                 break;
867         }
868
869         case LFUN_CHANGES_MERGE:
870                 if (lyx::find::findNextChange(this))
871                         owner_->getDialogs().show("changes");
872                 break;
873
874         case LFUN_ALL_CHANGES_ACCEPT: {
875                 cursor_.reset(buffer_->inset());
876 #ifdef WITH_WARNINGS
877 #warning FIXME changes
878 #endif
879                 while (lyx::find::findNextChange(this))
880                         getLyXText()->acceptChange(cursor_);
881                 update();
882                 break;
883         }
884
885         case LFUN_ALL_CHANGES_REJECT: {
886                 cursor_.reset(buffer_->inset());
887 #ifdef WITH_WARNINGS
888 #warning FIXME changes
889 #endif
890                 while (lyx::find::findNextChange(this))
891                         getLyXText()->rejectChange(cursor_);
892                 break;
893         }
894
895         case LFUN_WORD_FIND:
896                 lyx::find::find(this, cmd);
897                 break;
898
899         case LFUN_WORD_REPLACE:
900                 lyx::find::replace(this, cmd);
901                 break;
902
903         case LFUN_MARK_OFF:
904                 cur.clearSelection();
905                 cur.resetAnchor();
906                 cur.message(lyx::from_utf8(N_("Mark off")));
907                 break;
908
909         case LFUN_MARK_ON:
910                 cur.clearSelection();
911                 cur.mark() = true;
912                 cur.resetAnchor();
913                 cur.message(lyx::from_utf8(N_("Mark on")));
914                 break;
915
916         case LFUN_MARK_TOGGLE:
917                 cur.clearSelection();
918                 if (cur.mark()) {
919                         cur.mark() = false;
920                         cur.message(lyx::from_utf8(N_("Mark removed")));
921                 } else {
922                         cur.mark() = true;
923                         cur.message(lyx::from_utf8(N_("Mark set")));
924                 }
925                 cur.resetAnchor();
926                 break;
927
928         case LFUN_SCREEN_RECENTER:
929                 center();
930                 break;
931
932         case LFUN_BIBTEX_DATABASE_ADD: {
933                 LCursor tmpcur = cursor_;
934                 bv_funcs::findInset(tmpcur, InsetBase::BIBTEX_CODE, false);
935                 InsetBibtex * inset = getInsetByCode<InsetBibtex>(tmpcur,
936                                                 InsetBase::BIBTEX_CODE);
937                 if (inset) {
938                         if (inset->addDatabase(lyx::to_utf8(cmd.argument())))
939                                 buffer_->updateBibfilesCache();
940                 }
941                 break;
942         }
943
944         case LFUN_BIBTEX_DATABASE_DEL: {
945                 LCursor tmpcur = cursor_;
946                 bv_funcs::findInset(tmpcur, InsetBase::BIBTEX_CODE, false);
947                 InsetBibtex * inset = getInsetByCode<InsetBibtex>(tmpcur,
948                                                 InsetBase::BIBTEX_CODE);
949                 if (inset) {
950                         if (inset->delDatabase(lyx::to_utf8(cmd.argument())))
951                                 buffer_->updateBibfilesCache();
952                 }
953                 break;
954         }
955
956         case LFUN_WORDS_COUNT: {
957                 DocIterator from, to;
958                 if (cur.selection()) {
959                         from = cur.selectionBegin();
960                         to = cur.selectionEnd();
961                 } else {
962                         from = doc_iterator_begin(buffer_->inset());
963                         to = doc_iterator_end(buffer_->inset());
964                 }
965                 int const count = countWords(from, to);
966                 docstring message;
967                 if (count != 1) {
968                         if (cur.selection())
969                                 message = bformat(_("%1$d words in selection."),
970                                           count);
971                                 else
972                                         message = bformat(_("%1$d words in document."),
973                                                           count);
974                 }
975                 else {
976                         if (cur.selection())
977                                 message = _("One word in selection.");
978                         else
979                                 message = _("One word in document.");
980                 }
981
982                 Alert::information(_("Count words"), message);
983         }
984                 break;
985
986         case LFUN_BUFFER_TOGGLE_COMPRESSION:
987                 // turn compression on/off
988                 buffer_->params().compressed = !buffer_->params().compressed;
989                 break;
990
991         case LFUN_NEXT_INSET_TOGGLE: {
992                 // this is the real function we want to invoke
993                 FuncRequest tmpcmd = FuncRequest(LFUN_INSET_TOGGLE, cmd.origin);
994                 // if there is an inset at cursor, see whether it
995                 // wants to toggle.
996                 InsetBase * inset = cur.nextInset();
997                 if (inset && inset->isActive()) {
998                         LCursor tmpcur = cur;
999                         tmpcur.pushLeft(*inset);
1000                         inset->dispatch(tmpcur, tmpcmd);
1001                         if (tmpcur.result().dispatched()) {
1002                                 cur.dispatched();
1003                         }
1004                 }
1005                 // if it did not work, try the underlying inset.
1006                 if (!cur.result().dispatched())
1007                         cur.dispatch(tmpcmd);
1008
1009                 if (cur.result().dispatched())
1010                         cur.clearSelection();
1011
1012                 break;
1013         }
1014
1015         default:
1016                 return false;
1017         }
1018
1019         return true;
1020 }
1021
1022
1023 void BufferView::selectionRequested()
1024 {
1025         static docstring sel;
1026
1027         if (!buffer_)
1028                 return;
1029
1030         LCursor & cur = cursor_;
1031
1032         if (!cur.selection()) {
1033                 xsel_cache_.set = false;
1034                 return;
1035         }
1036
1037         if (!xsel_cache_.set ||
1038             cur.top() != xsel_cache_.cursor ||
1039             cur.anchor_.top() != xsel_cache_.anchor)
1040         {
1041                 xsel_cache_.cursor = cur.top();
1042                 xsel_cache_.anchor = cur.anchor_.top();
1043                 xsel_cache_.set = cur.selection();
1044                 sel = cur.selectionAsString(false);
1045                 if (!sel.empty())
1046                         owner_->gui().selection().put(sel);
1047         }
1048 }
1049
1050
1051 void BufferView::selectionLost()
1052 {
1053         if (buffer_) {
1054                 cursor_.clearSelection();
1055                 xsel_cache_.set = false;
1056         }
1057 }
1058
1059
1060 void BufferView::workAreaResize(int width, int height)
1061 {
1062         bool const widthChange = width != width_;
1063         bool const heightChange = height != height_;
1064
1065         // Update from work area
1066         width_ = width;
1067         height_ = height;
1068
1069         if (buffer_ && widthChange) {
1070                 // The WorkArea content needs a resize
1071                 resize();
1072         }
1073
1074         if (widthChange || heightChange)
1075                 update();
1076 }
1077
1078
1079 bool BufferView::workAreaDispatch(FuncRequest const & cmd0)
1080 {
1081         //lyxerr << BOOST_CURRENT_FUNCTION << "[ cmd0 " << cmd0 << "]" << endl;
1082
1083         // This is only called for mouse related events including
1084         // LFUN_FILE_OPEN generated by drag-and-drop.
1085         FuncRequest cmd = cmd0;
1086
1087         if (!buffer_)
1088                 return false;
1089
1090         LCursor cur(*this);
1091         cur.push(buffer_->inset());
1092         cur.selection() = cursor_.selection();
1093
1094         // Doesn't go through lyxfunc, so we need to update
1095         // the layout choice etc. ourselves
1096
1097         // E.g. Qt mouse press when no buffer
1098         if (!buffer_)
1099                 return false;
1100
1101         // Either the inset under the cursor or the
1102         // surrounding LyXText will handle this event.
1103
1104         // Build temporary cursor.
1105         cmd.y = min(max(cmd.y, -1), height_);
1106         InsetBase * inset = buffer_->text().editXY(cur, cmd.x, cmd.y);
1107         //lyxerr << BOOST_CURRENT_FUNCTION
1108         //       << " * hit inset at tip: " << inset << endl;
1109         //lyxerr << BOOST_CURRENT_FUNCTION
1110         //       << " * created temp cursor:" << cur << endl;
1111
1112         // Put anchor at the same position.
1113         cur.resetAnchor();
1114
1115         // Try to dispatch to an non-editable inset near this position
1116         // via the temp cursor. If the inset wishes to change the real
1117         // cursor it has to do so explicitly by using
1118         //  cur.bv().cursor() = cur;  (or similar)
1119         if (inset)
1120                 inset->dispatch(cur, cmd);
1121
1122         // Now dispatch to the temporary cursor. If the real cursor should
1123         // be modified, the inset's dispatch has to do so explicitly.
1124         if (!cur.result().dispatched())
1125                 cur.dispatch(cmd);
1126
1127         if (cur.result().dispatched()) {
1128                 // Redraw if requested or necessary.
1129                 if (cur.result().update())
1130                         update(Update::FitCursor | Update::Force);
1131                 else
1132                         update(Update::FitCursor | Update::MultiParSel);
1133         }
1134
1135         return true;
1136 }
1137
1138
1139 void BufferView::scroll(int lines)
1140 {
1141 //      if (!buffer_)
1142 //              return;
1143 //
1144 //      LyXText const * t = &buffer_->text();
1145 //      int const line_height = defaultRowHeight();
1146 //
1147 //      // The new absolute coordinate
1148 //      int new_top_y = top_y() + lines * line_height;
1149 //
1150 //      // Restrict to a valid value
1151 //      new_top_y = std::min(t->height() - 4 * line_height, new_top_y);
1152 //      new_top_y = std::max(0, new_top_y);
1153 //
1154 //      scrollDocView(new_top_y);
1155 //
1156 //      // Update the scrollbar.
1157 //      workArea_->setScrollbarParams(t->height(), top_y(), defaultRowHeight());}
1158 }
1159
1160
1161 void BufferView::setCursorFromRow(int row)
1162 {
1163         int tmpid = -1;
1164         int tmppos = -1;
1165
1166         buffer_->texrow().getIdFromRow(row, tmpid, tmppos);
1167
1168         if (tmpid == -1)
1169                 buffer_->text().setCursor(cursor_, 0, 0);
1170         else
1171                 buffer_->text().setCursor(cursor_, buffer_->getParFromID(tmpid).pit(), tmppos);
1172 }
1173
1174
1175 void BufferView::gotoLabel(string const & label)
1176 {
1177         for (InsetIterator it = inset_iterator_begin(buffer_->inset()); it; ++it) {
1178                 vector<string> labels;
1179                 it->getLabelList(*buffer_, labels);
1180                 if (find(labels.begin(),labels.end(),label) != labels.end()) {
1181                         setCursor(it);
1182                         update();
1183                         return;
1184                 }
1185         }
1186 }
1187
1188
1189 LyXText * BufferView::getLyXText()
1190 {
1191         LyXText * text = cursor_.innerText();
1192         BOOST_ASSERT(text);
1193         return text;
1194 }
1195
1196
1197 LyXText const * BufferView::getLyXText() const
1198 {
1199         LyXText const * text = cursor_.innerText();
1200         BOOST_ASSERT(text);
1201         return text;
1202 }
1203
1204
1205 int BufferView::workHeight() const
1206 {
1207         return height_;
1208 }
1209
1210
1211 LyXText * BufferView::text() const
1212 {
1213         return buffer_ ? &buffer_->text() : 0;
1214 }
1215
1216
1217 void BufferView::setCursor(DocIterator const & dit)
1218 {
1219         size_t const n = dit.depth();
1220         for (size_t i = 0; i < n; ++i)
1221                 dit[i].inset().edit(cursor_, true);
1222
1223         cursor_.setCursor(dit);
1224         cursor_.selection() = false;
1225 }
1226
1227
1228 void BufferView::mouseSetCursor(LCursor & cur)
1229 {
1230         BOOST_ASSERT(&cur.bv() == this);
1231
1232         // Has the cursor just left the inset?
1233         bool badcursor = false;
1234         if (&cursor_.inset() != &cur.inset())
1235                 badcursor = cursor_.inset().notifyCursorLeaves(cursor_);
1236
1237         // do the dEPM magic if needed
1238         // FIXME: move this to InsetText::notifyCursorLeaves?
1239         if (!badcursor && cursor_.inTexted())
1240                 cursor_.text()->deleteEmptyParagraphMechanism(cur, cursor_);
1241
1242         cursor_ = cur;
1243         cursor_.clearSelection();
1244         cursor_.setTargetX();
1245         finishUndo();
1246
1247 }
1248
1249
1250 void BufferView::putSelectionAt(DocIterator const & cur,
1251                                 int length, bool backwards)
1252 {
1253         cursor_.clearSelection();
1254
1255         setCursor(cur);
1256
1257         if (length) {
1258                 if (backwards) {
1259                         cursor_.pos() += length;
1260                         cursor_.setSelection(cursor_, -length);
1261                 } else
1262                         cursor_.setSelection(cursor_, length);
1263         }
1264 }
1265
1266
1267 LCursor & BufferView::cursor()
1268 {
1269         return cursor_;
1270 }
1271
1272
1273 LCursor const & BufferView::cursor() const
1274 {
1275         return cursor_;
1276 }
1277
1278
1279 lyx::pit_type BufferView::anchor_ref() const
1280 {
1281         return anchor_ref_;
1282 }
1283
1284
1285 int BufferView::offset_ref() const
1286 {
1287         return offset_ref_;
1288 }
1289
1290
1291 ViewMetricsInfo const & BufferView::viewMetricsInfo()
1292 {
1293         return metrics_info_;
1294 }
1295
1296
1297 void BufferView::updateMetrics(bool singlepar)
1298 {
1299         // Remove old position cache
1300         theCoords.clear();
1301         LyXText & buftext = buffer_->text();
1302         lyx::pit_type size = int(buftext.paragraphs().size());
1303
1304         if (anchor_ref_ > int(buftext.paragraphs().size() - 1)) {
1305                 anchor_ref_ = int(buftext.paragraphs().size() - 1);
1306                 offset_ref_ = 0;
1307         }
1308
1309         lyx::pit_type const pit = anchor_ref_;
1310         int pit1 = pit;
1311         int pit2 = pit;
1312         size_t const npit = buftext.paragraphs().size();
1313
1314         // Rebreak anchor paragraph. In Single Paragraph mode, rebreak only
1315         // the (main text, not inset!) paragraph containing the cursor.
1316         // (if this paragraph contains insets etc., rebreaking will
1317         // recursively descend)
1318         if (!singlepar || pit == cursor_.bottom().pit())
1319                 buftext.redoParagraph(pit);
1320         int y0 = buftext.getPar(pit).ascent() - offset_ref_;
1321
1322         // Redo paragraphs above anchor if necessary; again, in Single Par
1323         // mode, only if we encounter the (main text) one having the cursor.
1324         int y1 = y0;
1325         while (y1 > 0 && pit1 > 0) {
1326                 y1 -= buftext.getPar(pit1).ascent();
1327                 --pit1;
1328                 if (!singlepar || pit1 == cursor_.bottom().pit())
1329                         buftext.redoParagraph(pit1);
1330                 y1 -= buftext.getPar(pit1).descent();
1331         }
1332
1333
1334         // Take care of ascent of first line
1335         y1 -= buftext.getPar(pit1).ascent();
1336
1337         // Normalize anchor for next time
1338         anchor_ref_ = pit1;
1339         offset_ref_ = -y1;
1340
1341         // Grey at the beginning is ugly
1342         if (pit1 == 0 && y1 > 0) {
1343                 y0 -= y1;
1344                 y1 = 0;
1345                 anchor_ref_ = 0;
1346         }
1347
1348         // Redo paragraphs below the anchor if necessary. Single par mode:
1349         // only the one containing the cursor if encountered.
1350         int y2 = y0;
1351         while (y2 < height_ && pit2 < int(npit) - 1) {
1352                 y2 += buftext.getPar(pit2).descent();
1353                 ++pit2;
1354                 if (!singlepar || pit2 == cursor_.bottom().pit())
1355                         buftext.redoParagraph(pit2);
1356                 y2 += buftext.getPar(pit2).ascent();
1357         }
1358
1359         // Take care of descent of last line
1360         y2 += buftext.getPar(pit2).descent();
1361
1362         // The coordinates of all these paragraphs are correct, cache them
1363         int y = y1;
1364         CoordCache::InnerParPosCache & parPos = theCoords.parPos()[&buftext];
1365         for (lyx::pit_type pit = pit1; pit <= pit2; ++pit) {
1366                 Paragraph const & par = buftext.getPar(pit);
1367                 y += par.ascent();
1368                 parPos[pit] = Point(0, y);
1369                 if (singlepar && pit == cursor_.bottom().pit()) {
1370                         // In Single Paragraph mode, collect here the
1371                         // y1 and y2 of the (one) paragraph the cursor is in
1372                         y1 = y - par.ascent();
1373                         y2 = y + par.descent();
1374                 }
1375                 y += par.descent();
1376         }
1377
1378         if (singlepar) {
1379                 // collect cursor paragraph iter bounds
1380                 pit1 = cursor_.bottom().pit();
1381                 pit2 = cursor_.bottom().pit();
1382         }
1383
1384         lyxerr[Debug::DEBUG]
1385                 << BOOST_CURRENT_FUNCTION
1386                 << " y1: " << y1
1387                 << " y2: " << y2
1388                 << " pit1: " << pit1
1389                 << " pit2: " << pit2
1390                 << " npit: " << npit
1391                 << " singlepar: " << singlepar
1392                 << "size: " << size
1393                 << endl;
1394
1395         metrics_info_ = ViewMetricsInfo(pit1, pit2, y1, y2, singlepar, size);
1396 }
1397
1398
1399 void BufferView::menuInsertLyXFile(string const & filenm)
1400 {
1401         BOOST_ASSERT(cursor_.inTexted());
1402         string filename = filenm;
1403
1404         if (filename.empty()) {
1405                 // Launch a file browser
1406                 string initpath = lyxrc.document_path;
1407
1408                 if (available()) {
1409                         string const trypath = buffer_->filePath();
1410                         // If directory is writeable, use this as default.
1411                         if (isDirWriteable(trypath))
1412                                 initpath = trypath;
1413                 }
1414
1415                 FileDialog fileDlg(lyx::to_utf8(_("Select LyX document to insert")),
1416                         LFUN_FILE_INSERT,
1417                         make_pair(string(lyx::to_utf8(_("Documents|#o#O"))),
1418                                   string(lyxrc.document_path)),
1419                         make_pair(string(lyx::to_utf8(_("Examples|#E#e"))),
1420                                   string(addPath(package().system_support(), "examples"))));
1421
1422                 FileDialog::Result result =
1423                         fileDlg.open(initpath,
1424                                      FileFilterList(lyx::to_utf8(_("LyX Documents (*.lyx)"))),
1425                                      string());
1426
1427                 if (result.first == FileDialog::Later)
1428                         return;
1429
1430                 filename = result.second;
1431
1432                 // check selected filename
1433                 if (filename.empty()) {
1434                         // emit message signal.
1435                         message(_("Canceled."));
1436                         return;
1437                 }
1438         }
1439
1440         // Get absolute path of file and add ".lyx"
1441         // to the filename if necessary
1442         filename = fileSearch(string(), filename, "lyx");
1443
1444         docstring const disp_fn = makeDisplayPath(filename);
1445         // emit message signal.
1446         message(bformat(_("Inserting document %1$s..."), disp_fn));
1447
1448         docstring res;
1449         Buffer buf("", false);
1450         if (::loadLyXFile(&buf, makeAbsPath(filename))) {
1451                 ErrorList & el = buffer_->errorList("Parse");
1452                 // Copy the inserted document error list into the current buffer one.
1453                 el = buf.errorList("Parse");
1454                 lyx::cap::pasteParagraphList(cursor_, buf.paragraphs(),
1455                                              buf.params().textclass, el);
1456                 res = _("Document %1$s inserted.");
1457         } else
1458                 res = _("Could not insert document %1$s");
1459
1460         // emit message signal.
1461         message(bformat(res, disp_fn));
1462         buffer_->errors("Parse");
1463         resize();
1464 }
1465
1466
1467 void BufferView::trackChanges()
1468 {
1469         bool const tracking = buffer_->params().tracking_changes;
1470
1471         if (!tracking) {
1472                 for_each(buffer_->par_iterator_begin(),
1473                          buffer_->par_iterator_end(),
1474                          bind(&Paragraph::trackChanges, _1, Change::UNCHANGED));
1475                 buffer_->params().tracking_changes = true;
1476
1477                 // We cannot allow undos beyond the freeze point
1478                 buffer_->undostack().clear();
1479         } else {
1480                 cursor_.setCursor(doc_iterator_begin(buffer_->inset()));
1481                 if (lyx::find::findNextChange(this)) {
1482                         owner_->getDialogs().show("changes");
1483                         return;
1484                 }
1485
1486                 for_each(buffer_->par_iterator_begin(),
1487                          buffer_->par_iterator_end(),
1488                          mem_fun_ref(&Paragraph::untrackChanges));
1489
1490                 buffer_->params().tracking_changes = false;
1491         }
1492
1493         buffer_->redostack().clear();
1494 }