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