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