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