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