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