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