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