]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
Oops...
[lyx.git] / src / BufferView.C
1 /**
2  * \file BufferView.C
3  * Copyright 1995-2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author unknown
7  * \author John Levon <moz@compsoc.man.ac.uk>
8  */
9
10 #include <config.h>
11
12 #include "BufferView.h"
13 #include "BufferView_pimpl.h"
14
15 #include "BufferView_pimpl.h"
16 #include "LaTeX.h"
17 #include "WordLangTuple.h"
18 #include "buffer.h"
19 #include "bufferlist.h"
20 #include "debug.h"
21 #include "gettext.h"
22 #include "iterators.h"
23 #include "language.h"
24 #include "lyxcursor.h"
25 #include "lyxlex.h"
26 #include "lyxtext.h"
27 #include "undo_funcs.h"
28 #include "changes.h"
29
30 #include "frontends/Alert.h"
31 #include "frontends/Dialogs.h"
32 #include "frontends/LyXView.h"
33 #include "frontends/WorkArea.h"
34 #include "frontends/screen.h"
35
36 #include "insets/insetcommand.h" // ChangeRefs
37 #include "insets/inseterror.h"
38 #include "insets/updatableinset.h"
39
40 #include "support/FileInfo.h"
41 #include "support/filetools.h"
42 #include "support/lyxfunctional.h" // equal_1st_in_pair
43 #include "support/types.h"
44 #include "support/lyxalgo.h" // lyx_count
45
46 #include <fstream>
47
48 extern BufferList bufferlist;
49
50 using lyx::pos_type;
51
52 using std::pair;
53 using std::endl;
54 using std::ifstream;
55 using std::vector;
56 using std::find;
57 using std::count_if;
58
59
60 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
61                        int width, int height)
62         : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height))
63 {
64         text = 0;
65 }
66
67
68 BufferView::~BufferView()
69 {
70         delete text;
71         delete pimpl_;
72 }
73
74
75 Buffer * BufferView::buffer() const
76 {
77         return pimpl_->buffer_;
78 }
79
80
81 LyXScreen & BufferView::screen() const
82 {
83         return pimpl_->screen();
84 }
85
86
87 LyXView * BufferView::owner() const
88 {
89         return pimpl_->owner_;
90 }
91
92
93 Painter & BufferView::painter() const
94 {
95         return pimpl_->painter();
96 }
97
98
99 void BufferView::buffer(Buffer * b)
100 {
101         pimpl_->buffer(b);
102 }
103
104
105 void BufferView::resize()
106 {
107         if (pimpl_->buffer_) {
108                 pimpl_->resizeCurrentBuffer();
109         }
110 }
111
112
113 void BufferView::repaint()
114 {
115         pimpl_->repaint();
116 }
117
118
119 bool BufferView::fitCursor()
120 {
121         return pimpl_->fitCursor();
122 }
123
124
125 void BufferView::update()
126 {
127         pimpl_->update();
128 }
129
130
131 void BufferView::updateScrollbar()
132 {
133         pimpl_->updateScrollbar();
134 }
135
136
137 void BufferView::scrollDocView(int value)
138 {
139         pimpl_->scrollDocView(value);
140 }
141
142
143 void BufferView::redoCurrentBuffer()
144 {
145         pimpl_->redoCurrentBuffer();
146 }
147
148
149 bool BufferView::available() const
150 {
151         return pimpl_->available();
152 }
153
154
155 Change const BufferView::getCurrentChange()
156 {
157         return pimpl_->getCurrentChange();
158 }
159
160
161 void BufferView::beforeChange(LyXText * text)
162 {
163         pimpl_->beforeChange(text);
164 }
165
166
167 void BufferView::savePosition(unsigned int i)
168 {
169         pimpl_->savePosition(i);
170 }
171
172
173 void BufferView::restorePosition(unsigned int i)
174 {
175         pimpl_->restorePosition(i);
176 }
177
178
179 bool BufferView::isSavedPosition(unsigned int i)
180 {
181         return pimpl_->isSavedPosition(i);
182 }
183
184
185 void BufferView::update(LyXText * text, UpdateCodes f)
186 {
187         pimpl_->update(text, f);
188 }
189
190
191 void BufferView::switchKeyMap()
192 {
193         pimpl_->switchKeyMap();
194 }
195
196
197 void BufferView::insetUnlock()
198 {
199         pimpl_->insetUnlock();
200 }
201
202
203 int BufferView::workWidth() const
204 {
205         return pimpl_->workarea().workWidth();
206 }
207
208
209 void BufferView::showCursor()
210 {
211         pimpl_->showCursor();
212 }
213
214
215 void BufferView::hideCursor()
216 {
217         pimpl_->hideCursor();
218 }
219
220
221 void BufferView::toggleSelection(bool b)
222 {
223         pimpl_->toggleSelection(b);
224 }
225
226
227 void BufferView::toggleToggle()
228 {
229         pimpl_->toggleToggle();
230 }
231
232
233 void BufferView::center()
234 {
235         pimpl_->center();
236 }
237
238
239 string const BufferView::getClipboard() const
240 {
241         return pimpl_->workarea().getClipboard();
242 }
243
244
245 void BufferView::stuffClipboard(string const & stuff) const
246 {
247         pimpl_->stuffClipboard(stuff);
248 }
249
250
251 BufferView::UpdateCodes operator|(BufferView::UpdateCodes uc1,
252                                   BufferView::UpdateCodes uc2)
253 {
254         return static_cast<BufferView::UpdateCodes>
255                 (static_cast<int>(uc1) | static_cast<int>(uc2));
256 }
257
258
259 bool BufferView::dispatch(FuncRequest const & ev)
260 {
261         return pimpl_->dispatch(ev);
262 }
263
264
265 void BufferView::scroll(int lines)
266 {
267         pimpl_->scroll(lines);
268 }
269
270
271 // Inserts a file into current document
272 bool BufferView::insertLyXFile(string const & filen)
273         //
274         // Copyright CHT Software Service GmbH
275         // Uwe C. Schroeder
276         //
277         // Insert a LyXformat - file into current buffer
278         //
279         // Moved from lyx_cb.C (Lgb)
280 {
281         if (filen.empty())
282                 return false;
283
284         string const fname = MakeAbsPath(filen);
285
286         // check if file exist
287         FileInfo const fi(fname);
288
289         if (!fi.readable()) {
290                 Alert::alert(_("Error!"),
291                            _("Specified file is unreadable: "),
292                            MakeDisplayPath(fname, 50));
293                 return false;
294         }
295
296         beforeChange(text);
297
298         ifstream ifs(fname.c_str());
299         if (!ifs) {
300                 Alert::err_alert(_("Error! Cannot open specified file:"),
301                            MakeDisplayPath(fname, 50));
302                 return false;
303         }
304
305         int const c = ifs.peek();
306
307         LyXLex lex(0, 0);
308         lex.setStream(ifs);
309
310         bool res = true;
311
312         if (c == '#') {
313                 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
314                 res = buffer()->readFile(lex, fname, text->cursor.par());
315         } else {
316                 lyxerr[Debug::INFO] << "Will insert file without header"
317                                     << endl;
318                 res = buffer()->readLyXformat2(lex, text->cursor.par());
319         }
320
321         resize();
322         return res;
323 }
324
325
326 bool BufferView::removeAutoInsets()
327 {
328         // keep track of which pos and par the cursor was on
329         Paragraph * cursor_par = text->cursor.par();
330         Paragraph * cursor_par_prev = cursor_par ? cursor_par->previous() : 0;
331         Paragraph * cursor_par_next = cursor_par ? cursor_par->next() : 0;
332         pos_type cursor_pos = text->cursor.pos();
333
334         bool found = false;
335
336         // Trap the deletion of the paragraph the cursor is in.
337         // Iterate until we find a paragraph that won't be immediately deleted.
338         // In reality this should mean we only execute the body of the while
339         // loop once at most.  However for safety we iterate rather than just
340         // make this an if () conditional.
341         while ((cursor_par_prev || cursor_par_next)
342                && text->setCursor(this,
343                                   cursor_par_prev ? cursor_par_prev : cursor_par_next,
344                                   0)) {
345                 // We just removed cursor_par so have to fix the "cursor"
346                 if (cursor_par_prev) {
347                         // '.' = cursor_par
348                         //  a -> a.
349                         // .
350                         cursor_par = cursor_par_prev;
351                         cursor_pos = cursor_par->size();
352                 } else {
353                         // .  -> .a
354                         //  a
355                         cursor_par = cursor_par_next;
356                         cursor_pos = 0;
357                 }
358                 cursor_par_prev = cursor_par->previous();
359                 cursor_par_next = cursor_par->next();
360         }
361
362         // Iterate through the paragraphs removing autoDelete insets as we go.
363         // If the paragraph ends up empty after all the autoDelete insets are
364         // removed that paragraph will be removed by the next setCursor() call.
365         ParIterator it = buffer()->par_iterator_begin();
366         ParIterator end = buffer()->par_iterator_end();
367         for (; it != end; ++it) {
368                 Paragraph * par = *it;
369                 Paragraph * par_prev = par ? par->previous() : 0;
370                 bool removed = false;
371
372                 if (text->setCursor(this, par, 0)
373                     && cursor_par == par_prev) {
374                         // The previous setCursor line was deleted and that
375                         // was the cursor_par line.  This can only happen if an
376                         // error box was the sole item on cursor_par.
377                         // It is possible for cursor_par_prev to be stray if
378                         // the line it pointed to only had a error box on it
379                         // so we have to set it to a known correct value.
380                         // This is often the same value it already had.
381                         cursor_par_prev = par->previous();
382                         if (cursor_par_prev) {
383                                 // '|' = par, '.' = cursor_par, 'E' = error box
384                                 // First step below may occur before while{}
385                                 //  a    |a      a     a     a.
386                                 //  E -> .E -> |.E -> .  -> |b
387                                 // .      b      b    |b
388                                 //  b
389                                 cursor_par = cursor_par_prev;
390                                 cursor_pos = cursor_par_prev->size();
391                                 cursor_par_prev = cursor_par->previous();
392                                 // cursor_par_next remains the same
393                         } else if (cursor_par_next) {
394                                 // First step below may occur before while{}
395                                 // .
396                                 //  E -> |.E -> |.  -> . -> .|a
397                                 //  a      a      a    |a
398                                 cursor_par = cursor_par_next;
399                                 cursor_pos = 0;
400                                 // cursor_par_prev remains unset
401                                 cursor_par_next = cursor_par->next();
402                         } else {
403                                 // I can't find a way to trigger this
404                                 // so it should be unreachable code
405                                 // unless the buffer is corrupted.
406                                 lyxerr << "BufferView::removeAutoInsets() is bad\n";
407                         }
408                 }
409
410                 InsetList::iterator pit = par->insetlist.begin();
411                 InsetList::iterator pend = par->insetlist.end();
412
413                 while (pit != pend) {
414                         if (pit.getInset()->autoDelete()) {
415                                 removed = true;
416                                 pos_type const pos = pit.getPos();
417
418                                 par->erase(pos);
419                                 // We just invalidated par's inset iterators so
420                                 // we get the next valid iterator position
421                                 pit = par->insetlist.insetIterator(pos);
422                                 // and ensure we have a valid end iterator.
423                                 pend = par->insetlist.end();
424
425                                 if (cursor_par == par) {
426                                         // update the saved cursor position
427                                         if (cursor_pos > pos)
428                                                 --cursor_pos;
429                                 }
430                         } else {
431                                 ++pit;
432                         }
433                 }
434                 if (removed) {
435                         found = true;
436                         text->redoParagraph(this);
437                 }
438         }
439
440         // It is possible that the last line is empty if it was cursor_par
441         // and/or only had an error inset on it.  So we set the cursor to the
442         // start of the doc to force its removal and ensure a valid saved cursor
443         if (text->setCursor(this, text->ownerParagraph(), 0)
444             && 0 == cursor_par_next) {
445                 cursor_par = cursor_par_prev;
446                 cursor_pos = cursor_par->size();
447         } else if (cursor_pos > cursor_par->size()) {
448                 // Some C-Enter lines were removed by the setCursor call which
449                 // then invalidated cursor_pos. It could still be "wrong" because
450                 // the cursor may appear to have jumped but since we collapsed
451                 // some C-Enter lines this should be a reasonable compromise.
452                 cursor_pos = cursor_par->size();
453         }
454
455         // restore the original cursor in its corrected location.
456         text->setCursorIntern(this, cursor_par, cursor_pos);
457
458         return found;
459 }
460
461
462 void BufferView::insertErrors(TeXErrors & terr)
463 {
464         // Save the cursor position
465         LyXCursor cursor = text->cursor;
466
467         TeXErrors::Errors::const_iterator cit = terr.begin();
468         TeXErrors::Errors::const_iterator end = terr.end();
469         for (; cit != end; ++cit) {
470                 string const desctext(cit->error_desc);
471                 string const errortext(cit->error_text);
472                 string const msgtxt = desctext + '\n' + errortext;
473                 int const errorrow = cit->error_in_line;
474
475                 // Insert error string for row number
476                 int tmpid = -1;
477                 int tmppos = -1;
478
479                 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
480                         buffer()->texrow.increasePos(tmpid, tmppos);
481                 }
482
483                 Paragraph * texrowpar = 0;
484
485                 if (tmpid == -1) {
486                         texrowpar = text->ownerParagraph();
487                         tmppos = 0;
488                 } else {
489                         texrowpar = buffer()->getParFromID(tmpid);
490                 }
491
492                 if (texrowpar == 0)
493                         continue;
494
495                 freezeUndo();
496                 InsetError * new_inset = new InsetError(msgtxt);
497                 text->setCursorIntern(this, texrowpar, tmppos);
498                 text->insertInset(this, new_inset);
499                 text->fullRebreak(this);
500                 unFreezeUndo();
501         }
502         // Restore the cursor position
503         text->setCursorIntern(this, cursor.par(), cursor.pos());
504 }
505
506
507 void BufferView::setCursorFromRow(int row)
508 {
509         int tmpid = -1;
510         int tmppos = -1;
511
512         buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
513
514         Paragraph * texrowpar;
515
516         if (tmpid == -1) {
517                 texrowpar = text->ownerParagraph();
518                 tmppos = 0;
519         } else {
520                 texrowpar = buffer()->getParFromID(tmpid);
521         }
522         text->setCursor(this, texrowpar, tmppos);
523 }
524
525
526 bool BufferView::insertInset(Inset * inset, string const & lout)
527 {
528         return pimpl_->insertInset(inset, lout);
529 }
530
531
532 // This is also a buffer property (ale)
533 // Not so sure about that. a goto Label function can not be buffer local, just
534 // think how this will work in a multiwindow/buffer environment, all the
535 // cursors in all the views showing this buffer will move. (Lgb)
536 // OK, then no cursor action should be allowed in buffer. (ale)
537 bool BufferView::gotoLabel(string const & label)
538 {
539         for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
540              it != buffer()->inset_iterator_end(); ++it) {
541                 vector<string> labels = it->getLabelList();
542                 if (find(labels.begin(),labels.end(),label)
543                      != labels.end()) {
544                         beforeChange(text);
545                         text->setCursor(this, it.getPar(), it.getPos());
546                         text->selection.cursor = text->cursor;
547                         update(text, BufferView::SELECT|BufferView::FITCUR);
548                         return true;
549                 }
550         }
551         return false;
552 }
553
554
555 void BufferView::undo()
556 {
557         if (!available())
558                 return;
559
560         owner()->message(_("Undo"));
561         hideCursor();
562         beforeChange(text);
563         update(text, BufferView::SELECT|BufferView::FITCUR);
564         if (!textUndo(this))
565                 owner()->message(_("No further undo information"));
566         else
567                 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
568         switchKeyMap();
569 }
570
571
572 void BufferView::redo()
573 {
574         if (!available())
575                 return;
576
577         owner()->message(_("Redo"));
578         hideCursor();
579         beforeChange(text);
580         update(text, BufferView::SELECT|BufferView::FITCUR);
581         if (!textRedo(this))
582                 owner()->message(_("No further redo information"));
583         else
584                 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
585         switchKeyMap();
586 }
587
588
589 void BufferView::copyEnvironment()
590 {
591         if (available()) {
592                 text->copyEnvironmentType();
593                 owner()->message(_("Paragraph environment type copied"));
594         }
595 }
596
597
598 void BufferView::pasteEnvironment()
599 {
600         if (available()) {
601                 text->pasteEnvironmentType(this);
602                 owner()->message(_("Paragraph environment type set"));
603                 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
604         }
605 }
606
607
608 // these functions are for the spellchecker
609 WordLangTuple const BufferView::nextWord(float & value)
610 {
611         if (!available()) {
612                 value = 1;
613                 return WordLangTuple();
614         }
615
616         return text->selectNextWordToSpellcheck(this, value);
617 }
618
619
620 void BufferView::selectLastWord()
621 {
622         if (!available())
623                 return;
624
625         LyXCursor cur = text->selection.cursor;
626         hideCursor();
627         beforeChange(text);
628         text->selection.cursor = cur;
629         text->selectSelectedWord(this);
630         toggleSelection(false);
631         update(text, BufferView::SELECT|BufferView::FITCUR);
632 }
633
634
635 void BufferView::endOfSpellCheck()
636 {
637         if (!available()) return;
638
639         hideCursor();
640         beforeChange(text);
641         text->selectSelectedWord(this);
642         text->clearSelection();
643         update(text, BufferView::SELECT|BufferView::FITCUR);
644 }
645
646
647 void BufferView::replaceWord(string const & replacestring)
648 {
649         if (!available())
650                 return;
651
652         LyXText * tt = getLyXText();
653         hideCursor();
654         update(tt, BufferView::SELECT|BufferView::FITCUR);
655
656         // clear the selection (if there is any)
657         toggleSelection(false);
658         update(tt, BufferView::SELECT|BufferView::FITCUR);
659
660         // clear the selection (if there is any)
661         toggleSelection(false);
662         tt->replaceSelectionWithString(this, replacestring);
663
664         tt->setSelectionRange(this, replacestring.length());
665
666         // Go back so that replacement string is also spellchecked
667         for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
668                 tt->cursorLeft(this);
669         }
670         update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
671 }
672 // End of spellchecker stuff
673
674
675 bool BufferView::lockInset(UpdatableInset * inset)
676 {
677         if (!inset)
678                 return false;
679         // don't relock if we're already locked
680         if (theLockingInset() == inset)
681                 return true;
682         if (!theLockingInset()) {
683                 // first check if it's the inset under the cursor we want lock
684                 // should be most of the time
685                 char const c = text->cursor.par()->getChar(text->cursor.pos());
686                 if (c == Paragraph::META_INSET) {
687                         Inset * in = text->cursor.par()->getInset(text->cursor.pos());
688                         if (inset == in) {
689                                 theLockingInset(inset);
690                                 return true;
691                         }
692                 }
693                 // Then do a deep look of the inset and lock the right one
694                 int const id = inset->id();
695                 ParagraphList::iterator pit = buffer()->paragraphs.begin();
696                 ParagraphList::iterator pend = buffer()->paragraphs.end();
697                 for (; pit != pend; ++pit) {
698                         InsetList::iterator it = pit->insetlist.begin();
699                         InsetList::iterator end = pit->insetlist.end();
700                         for (; it != end; ++it) {
701                                 if (it.getInset() == inset) {
702                                         text->setCursorIntern(this, &*pit, it.getPos());
703                                         theLockingInset(inset);
704                                         return true;
705                                 }
706                                 if (it.getInset()->getInsetFromID(id)) {
707                                         text->setCursorIntern(this, &*pit, it.getPos());
708                                         it.getInset()->edit(this);
709                                         return theLockingInset()->lockInsetInInset(this, inset);
710                                 }
711                         }
712                 }
713                 return false;
714         }
715         return theLockingInset()->lockInsetInInset(this, inset);
716 }
717
718
719 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
720 {
721         if (available() && theLockingInset() && !theLockingInset()->nodraw()) {
722                 LyXCursor cursor = text->cursor;
723                 Inset * locking_inset = theLockingInset()->getLockingInset();
724
725                 if ((cursor.pos() - 1 >= 0) &&
726                     cursor.par()->isInset(cursor.pos() - 1) &&
727                     (cursor.par()->getInset(cursor.pos() - 1) ==
728                      locking_inset))
729                         text->setCursor(this, cursor,
730                                         cursor.par(), cursor.pos() - 1);
731                 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
732                 LyXText * txt = getLyXText();
733                 if (locking_inset->isTextInset() &&
734                     locking_inset->lyxCode() != Inset::ERT_CODE &&
735                     (txt->real_current_font.language() !=
736                      buffer()->params.language
737                      || txt->real_current_font.isVisibleRightToLeft()
738                      != buffer()->params.language->RightToLeft()))
739                         shape = (txt->real_current_font.isVisibleRightToLeft())
740                                 ? LyXScreen::REVERSED_L_SHAPE
741                                 : LyXScreen::L_SHAPE;
742                 y += cursor.iy() + theLockingInset()->insetInInsetY();
743                 screen().showManualCursor(text, x, y, asc, desc,
744                                                   shape);
745         }
746 }
747
748
749 void BufferView::hideLockedInsetCursor()
750 {
751         if (theLockingInset() && available()) {
752                 screen().hideCursor();
753         }
754 }
755
756
757 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
758 {
759         if (theLockingInset() && available()) {
760                 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
761                 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
762                         updateScrollbar();
763                         return true;
764                 }
765         }
766         return false;
767 }
768
769
770 int BufferView::unlockInset(UpdatableInset * inset)
771 {
772         if (!inset)
773                 return 0;
774         if (inset && theLockingInset() == inset) {
775                 inset->insetUnlock(this);
776                 theLockingInset(0);
777                 // make sure we update the combo !
778                 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
779                 // Tell the paragraph dialog that we changed paragraph
780                 owner()->getDialogs().updateParagraph();
781                 finishUndo();
782                 return 0;
783         } else if (inset && theLockingInset() &&
784                    theLockingInset()->unlockInsetInInset(this, inset)) {
785                 // Tell the paragraph dialog that we changed paragraph
786                 owner()->getDialogs().updateParagraph();
787                 // owner inset has updated the layout combo
788                 finishUndo();
789                 return 0;
790         }
791         return 1;
792 }
793
794
795 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
796 {
797         if (!theLockingInset())
798                 return; // shouldn't happen
799         if (kind == Undo::EDIT) // in this case insets would not be stored!
800                 kind = Undo::FINISH;
801         setUndo(this, kind,
802                 text->cursor.par(),
803                 text->cursor.par()->next());
804 }
805
806
807 void BufferView::updateInset(Inset * inset, bool mark_dirty)
808 {
809         pimpl_->updateInset(inset, mark_dirty);
810 }
811
812
813 bool BufferView::ChangeInsets(Inset::Code code,
814                               string const & from, string const & to)
815 {
816         bool need_update = false;
817         LyXCursor cursor = text->cursor;
818         LyXCursor tmpcursor = cursor;
819         cursor.par(tmpcursor.par());
820         cursor.pos(tmpcursor.pos());
821
822         ParIterator end = buffer()->par_iterator_end();
823         for (ParIterator it = buffer()->par_iterator_begin();
824              it != end; ++it) {
825                 Paragraph * par = *it;
826                 bool changed_inset = false;
827                 for (InsetList::iterator it2 = par->insetlist.begin();
828                      it2 != par->insetlist.end(); ++it2) {
829                         if (it2.getInset()->lyxCode() == code) {
830                                 InsetCommand * inset = static_cast<InsetCommand *>(it2.getInset());
831                                 if (inset->getContents() == from) {
832                                         inset->setContents(to);
833                                         changed_inset = true;
834                                 }
835                         }
836                 }
837                 if (changed_inset) {
838                         need_update = true;
839
840                         // FIXME
841
842                         // The test it.size()==1 was needed to prevent crashes.
843                         // How to set the cursor corretly when it.size()>1 ??
844                         if (it.size() == 1) {
845                                 text->setCursorIntern(this, par, 0);
846                                 text->redoParagraphs(this, text->cursor,
847                                                      text->cursor.par()->next());
848                                 text->fullRebreak(this);
849                         }
850                 }
851         }
852         text->setCursorIntern(this, cursor.par(), cursor.pos());
853         return need_update;
854 }
855
856
857 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
858 {
859         // Check if the label 'from' appears more than once
860         vector<string> labels = buffer()->getLabelList();
861
862         if (lyx::count(labels.begin(), labels.end(), from) > 1)
863                 return false;
864
865         return ChangeInsets(Inset::REF_CODE, from, to);
866 }
867
868
869 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
870 {
871         typedef pair<string, string> StringPair;
872
873         vector<StringPair> keys;
874         buffer()->fillWithBibKeys(keys);
875         if (count_if(keys.begin(), keys.end(),
876                      lyx::equal_1st_in_pair<StringPair>(from))
877             > 1)
878                 return false;
879
880         return ChangeInsets(Inset::CITE_CODE, from, to);
881 }
882
883
884 UpdatableInset * BufferView::theLockingInset() const
885 {
886         // If NULL is not allowed we should put an Assert here. (Lgb)
887         if (text)
888                 return text->the_locking_inset;
889         return 0;
890 }
891
892
893 void BufferView::theLockingInset(UpdatableInset * inset)
894 {
895         text->the_locking_inset = inset;
896 }
897
898
899 LyXText * BufferView::getLyXText() const
900 {
901         if (theLockingInset()) {
902                 LyXText * txt = theLockingInset()->getLyXText(this, true);
903                 if (txt)
904                         return txt;
905         }
906         return text;
907 }
908
909
910 LyXText * BufferView::getParentText(Inset * inset) const
911 {
912         if (inset->owner()) {
913                 LyXText * txt = inset->getLyXText(this);
914                 inset = inset->owner();
915                 while (inset && inset->getLyXText(this) == txt)
916                         inset = inset->owner();
917                 if (inset)
918                         return inset->getLyXText(this);
919         }
920         return text;
921 }
922
923
924 Language const * BufferView::getParentLanguage(Inset * inset) const
925 {
926         LyXText * text = getParentText(inset);
927         return text->cursor.par()->getFontSettings(buffer()->params,
928                                                    text->cursor.pos()).language();
929 }
930
931
932 Encoding const * BufferView::getEncoding() const
933 {
934         LyXText * t = getLyXText();
935         if (!t)
936                 return 0;
937
938         LyXCursor const & c= t->cursor;
939         LyXFont const font = c.par()->getFont(buffer()->params, c.pos());
940         return font.language()->encoding();
941 }
942
943
944 void BufferView::haveSelection(bool sel)
945 {
946         pimpl_->workarea().haveSelection(sel);
947 }
948
949
950 int BufferView::workHeight() const
951 {
952         return pimpl_->workarea().workHeight();
953 }