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