]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
Alfredo's second patch
[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 #include "paragraph_funcs.h"
31
32 #include "frontends/Alert.h"
33 #include "frontends/Dialogs.h"
34 #include "frontends/LyXView.h"
35 #include "frontends/WorkArea.h"
36 #include "frontends/screen.h"
37
38 #include "insets/insetcommand.h" // ChangeRefs
39 #include "insets/inseterror.h"
40 #include "insets/updatableinset.h"
41
42 #include "support/FileInfo.h"
43 #include "support/filetools.h"
44 #include "support/lyxfunctional.h" // equal_1st_in_pair
45 #include "support/types.h"
46 #include "support/lyxalgo.h" // lyx_count
47 #include "support/BoostFormat.h"
48
49 #include <fstream>
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 file = MakeDisplayPath(fname, 50);
325 #if USE_BOOST_FORMAT
326                 boost::format fmt(_("Could not open the specified document\n%1$s."));
327                 fmt % file;
328                 string text = fmt.str();
329 #else
330                 string text = _("Could not open the specified document\n");
331                 text += file + ".";
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->ownerParagraphs().begin(), 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->ownerParagraphs().begin();
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->ownerParagraphs().begin();
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 // these functions are for the spellchecker
617 WordLangTuple const BufferView::nextWord(float & value)
618 {
619         if (!available()) {
620                 value = 1;
621                 return WordLangTuple();
622         }
623
624         return text->selectNextWordToSpellcheck(value);
625 }
626
627
628 void BufferView::selectLastWord()
629 {
630         if (!available())
631                 return;
632
633         LyXCursor cur = text->selection.cursor;
634         hideCursor();
635         beforeChange(text);
636         text->selection.cursor = cur;
637         text->selectSelectedWord();
638         toggleSelection(false);
639         update(text, BufferView::SELECT);
640 }
641
642
643 void BufferView::endOfSpellCheck()
644 {
645         if (!available()) return;
646
647         hideCursor();
648         beforeChange(text);
649         text->selectSelectedWord();
650         text->clearSelection();
651         update(text, BufferView::SELECT);
652 }
653
654
655 void BufferView::replaceWord(string const & replacestring)
656 {
657         if (!available())
658                 return;
659
660         LyXText * tt = getLyXText();
661         hideCursor();
662         update(tt, BufferView::SELECT);
663
664         // clear the selection (if there is any)
665         toggleSelection(false);
666         update(tt, BufferView::SELECT);
667
668         // clear the selection (if there is any)
669         toggleSelection(false);
670         tt->replaceSelectionWithString(replacestring);
671
672         tt->setSelectionRange(replacestring.length());
673
674         // Go back so that replacement string is also spellchecked
675         for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
676                 tt->cursorLeft(this);
677         }
678         update(tt, BufferView::SELECT);
679
680         // FIXME: should be done through LFUN
681         buffer()->markDirty();
682         fitCursor();
683 }
684 // End of spellchecker stuff
685
686
687 bool BufferView::lockInset(UpdatableInset * inset)
688 {
689         if (!inset)
690                 return false;
691         // don't relock if we're already locked
692         if (theLockingInset() == inset)
693                 return true;
694         if (!theLockingInset()) {
695                 // first check if it's the inset under the cursor we want lock
696                 // should be most of the time
697                 if (text->cursor.pos() < text->cursor.par()->size()
698                     && text->cursor.par()->getChar(text->cursor.pos()) ==
699                     Paragraph::META_INSET) {
700                         Inset * in = text->cursor.par()->getInset(text->cursor.pos());
701                         if (inset == in) {
702                                 theLockingInset(inset);
703                                 return true;
704                         }
705                 }
706                 // Then do a deep look of the inset and lock the right one
707                 int const id = inset->id();
708                 ParagraphList::iterator pit = buffer()->paragraphs.begin();
709                 ParagraphList::iterator pend = buffer()->paragraphs.end();
710                 for (; pit != pend; ++pit) {
711                         InsetList::iterator it = pit->insetlist.begin();
712                         InsetList::iterator end = pit->insetlist.end();
713                         for (; it != end; ++it) {
714                                 if (it.getInset() == inset) {
715                                         text->setCursorIntern(&*pit, it.getPos());
716                                         theLockingInset(inset);
717                                         return true;
718                                 }
719                                 if (it.getInset()->getInsetFromID(id)) {
720                                         text->setCursorIntern(&*pit, it.getPos());
721                                         it.getInset()->edit(this);
722                                         return theLockingInset()->lockInsetInInset(this, inset);
723                                 }
724                         }
725                 }
726                 return false;
727         }
728         return theLockingInset()->lockInsetInInset(this, inset);
729 }
730
731
732 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
733 {
734         if (available() && theLockingInset() && !theLockingInset()->nodraw()) {
735                 LyXCursor cursor = text->cursor;
736                 Inset * locking_inset = theLockingInset()->getLockingInset();
737
738                 if ((cursor.pos() - 1 >= 0) &&
739                     cursor.par()->isInset(cursor.pos() - 1) &&
740                     (cursor.par()->getInset(cursor.pos() - 1) ==
741                      locking_inset))
742                         text->setCursor(cursor,
743                                         cursor.par(), cursor.pos() - 1);
744                 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
745                 LyXText * txt = getLyXText();
746                 if (locking_inset->isTextInset() &&
747                     locking_inset->lyxCode() != Inset::ERT_CODE &&
748                     (txt->real_current_font.language() !=
749                      buffer()->params.language
750                      || txt->real_current_font.isVisibleRightToLeft()
751                      != buffer()->params.language->RightToLeft()))
752                         shape = (txt->real_current_font.isVisibleRightToLeft())
753                                 ? LyXScreen::REVERSED_L_SHAPE
754                                 : LyXScreen::L_SHAPE;
755                 y += cursor.iy() + theLockingInset()->insetInInsetY();
756                 screen().showManualCursor(text, x, y, asc, desc,
757                                                   shape);
758         }
759 }
760
761
762 void BufferView::hideLockedInsetCursor()
763 {
764         if (theLockingInset() && available()) {
765                 screen().hideCursor();
766         }
767 }
768
769
770 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
771 {
772         if (theLockingInset() && available()) {
773                 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
774                 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
775                         updateScrollbar();
776                         return true;
777                 }
778         }
779         return false;
780 }
781
782
783 int BufferView::unlockInset(UpdatableInset * inset)
784 {
785         if (!inset)
786                 return 0;
787         if (inset && theLockingInset() == inset) {
788                 inset->insetUnlock(this);
789                 theLockingInset(0);
790                 // make sure we update the combo !
791                 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
792                 // Tell the paragraph dialog that we changed paragraph
793                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
794                 finishUndo();
795                 return 0;
796         } else if (inset && theLockingInset() &&
797                    theLockingInset()->unlockInsetInInset(this, inset)) {
798                 // Tell the paragraph dialog that we changed paragraph
799                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
800                 // owner inset has updated the layout combo
801                 finishUndo();
802                 return 0;
803         }
804         return 1;
805 }
806
807
808 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
809 {
810         if (!theLockingInset())
811                 return; // shouldn't happen
812         if (kind == Undo::EDIT) // in this case insets would not be stored!
813                 kind = Undo::FINISH;
814         setUndo(this, kind,
815                 text->cursor.par(),
816                 boost::next(text->cursor.par()));
817 }
818
819
820 void BufferView::updateInset(Inset * inset)
821 {
822         pimpl_->updateInset(inset);
823 }
824
825
826 bool BufferView::ChangeInsets(Inset::Code code,
827                               string const & from, string const & to)
828 {
829         bool need_update = false;
830         LyXCursor cursor = text->cursor;
831         LyXCursor tmpcursor = cursor;
832         cursor.par(tmpcursor.par());
833         cursor.pos(tmpcursor.pos());
834
835         ParIterator end = buffer()->par_iterator_end();
836         for (ParIterator it = buffer()->par_iterator_begin();
837              it != end; ++it) {
838                 Paragraph * par = *it;
839                 bool changed_inset = false;
840                 for (InsetList::iterator it2 = par->insetlist.begin();
841                      it2 != par->insetlist.end(); ++it2) {
842                         if (it2.getInset()->lyxCode() == code) {
843                                 InsetCommand * inset = static_cast<InsetCommand *>(it2.getInset());
844                                 if (inset->getContents() == from) {
845                                         inset->setContents(to);
846                                         changed_inset = true;
847                                 }
848                         }
849                 }
850                 if (changed_inset) {
851                         need_update = true;
852
853                         // FIXME
854
855                         // The test it.size()==1 was needed to prevent crashes.
856                         // How to set the cursor corretly when it.size()>1 ??
857                         if (it.size() == 1) {
858                                 text->setCursorIntern(par, 0);
859                                 text->redoParagraphs(text->cursor,
860                                                      text->cursor.par()->next());
861                                 text->fullRebreak();
862                         }
863                 }
864         }
865         text->setCursorIntern(cursor.par(), cursor.pos());
866         return need_update;
867 }
868
869
870 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
871 {
872         // Check if the label 'from' appears more than once
873         vector<string> labels = buffer()->getLabelList();
874
875         if (lyx::count(labels.begin(), labels.end(), from) > 1)
876                 return false;
877
878         return ChangeInsets(Inset::REF_CODE, from, to);
879 }
880
881
882 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
883 {
884         typedef pair<string, string> StringPair;
885
886         vector<StringPair> keys;
887         buffer()->fillWithBibKeys(keys);
888         if (count_if(keys.begin(), keys.end(),
889                      lyx::equal_1st_in_pair<StringPair>(from))
890             > 1)
891                 return false;
892
893         return ChangeInsets(Inset::CITE_CODE, from, to);
894 }
895
896
897 UpdatableInset * BufferView::theLockingInset() const
898 {
899         // If NULL is not allowed we should put an Assert here. (Lgb)
900         if (text)
901                 return text->the_locking_inset;
902         return 0;
903 }
904
905
906 void BufferView::theLockingInset(UpdatableInset * inset)
907 {
908         text->the_locking_inset = inset;
909 }
910
911
912 LyXText * BufferView::getLyXText() const
913 {
914         if (theLockingInset()) {
915                 LyXText * txt = theLockingInset()->getLyXText(this, true);
916                 if (txt)
917                         return txt;
918         }
919         return text;
920 }
921
922
923 LyXText * BufferView::getParentText(Inset * inset) const
924 {
925         if (inset->owner()) {
926                 LyXText * txt = inset->getLyXText(this);
927                 inset = inset->owner();
928                 while (inset && inset->getLyXText(this) == txt)
929                         inset = inset->owner();
930                 if (inset)
931                         return inset->getLyXText(this);
932         }
933         return text;
934 }
935
936
937 Language const * BufferView::getParentLanguage(Inset * inset) const
938 {
939         LyXText * text = getParentText(inset);
940         return text->cursor.par()->getFontSettings(buffer()->params,
941                                                    text->cursor.pos()).language();
942 }
943
944
945 Encoding const * BufferView::getEncoding() const
946 {
947         LyXText * t = getLyXText();
948         if (!t)
949                 return 0;
950
951         LyXCursor const & c = t->cursor;
952         LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
953                                               outerFont(c.par(), t->ownerParagraphs()));
954         return font.language()->encoding();
955 }
956
957
958 void BufferView::haveSelection(bool sel)
959 {
960         pimpl_->workarea().haveSelection(sel);
961 }
962
963
964 int BufferView::workHeight() const
965 {
966         return pimpl_->workarea().workHeight();
967 }