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