]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
split the new.ui into three files for easier overriding
[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 #include "paragraph_funcs.h"
31
32 #include "frontends/Alert.h"
33 #include "frontends/Dialogs.h"
34 #include "frontends/LyXView.h"
35 #include "frontends/WorkArea.h"
36 #include "frontends/screen.h"
37
38 #include "insets/insetcommand.h" // ChangeRefs
39 #include "insets/inseterror.h"
40 #include "insets/updatableinset.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 #include "support/BoostFormat.h"
48
49 #include <fstream>
50
51 extern BufferList bufferlist;
52
53 using lyx::pos_type;
54
55 using std::pair;
56 using std::endl;
57 using std::ifstream;
58 using std::vector;
59 using std::find;
60 using std::count_if;
61
62
63 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
64                        int width, int height)
65         : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height))
66 {
67         text = 0;
68 }
69
70
71 BufferView::~BufferView()
72 {
73         delete text;
74         delete pimpl_;
75 }
76
77
78 Buffer * BufferView::buffer() const
79 {
80         return pimpl_->buffer_;
81 }
82
83
84 LyXScreen & BufferView::screen() const
85 {
86         return pimpl_->screen();
87 }
88
89
90 LyXView * BufferView::owner() const
91 {
92         return pimpl_->owner_;
93 }
94
95
96 Painter & BufferView::painter() const
97 {
98         return pimpl_->painter();
99 }
100
101
102 void BufferView::buffer(Buffer * b)
103 {
104         pimpl_->buffer(b);
105 }
106
107
108 void BufferView::reload()
109 {
110         string const fn = buffer()->fileName();
111         if (bufferlist.close(buffer(), false))
112                 buffer(bufferlist.loadLyXFile(fn));
113 }
114
115
116 void BufferView::resize()
117 {
118         if (pimpl_->buffer_) {
119                 pimpl_->resizeCurrentBuffer();
120         }
121 }
122
123
124 void BufferView::repaint()
125 {
126         pimpl_->repaint();
127 }
128
129
130 bool BufferView::fitCursor()
131 {
132         return pimpl_->fitCursor();
133 }
134
135
136 void BufferView::update()
137 {
138         pimpl_->update();
139 }
140
141
142 void BufferView::updateScrollbar()
143 {
144         pimpl_->updateScrollbar();
145 }
146
147
148 void BufferView::scrollDocView(int value)
149 {
150         pimpl_->scrollDocView(value);
151 }
152
153
154 void BufferView::redoCurrentBuffer()
155 {
156         pimpl_->redoCurrentBuffer();
157 }
158
159
160 bool BufferView::available() const
161 {
162         return pimpl_->available();
163 }
164
165
166 Change const BufferView::getCurrentChange()
167 {
168         return pimpl_->getCurrentChange();
169 }
170
171
172 void BufferView::beforeChange(LyXText * text)
173 {
174         pimpl_->beforeChange(text);
175 }
176
177
178 void BufferView::savePosition(unsigned int i)
179 {
180         pimpl_->savePosition(i);
181 }
182
183
184 void BufferView::restorePosition(unsigned int i)
185 {
186         pimpl_->restorePosition(i);
187 }
188
189
190 bool BufferView::isSavedPosition(unsigned int i)
191 {
192         return pimpl_->isSavedPosition(i);
193 }
194
195
196 void BufferView::update(LyXText * text, UpdateCodes f)
197 {
198         pimpl_->update(text, f);
199 }
200
201
202 void BufferView::update(UpdateCodes f)
203 {
204         pimpl_->update(f);
205 }
206
207
208 void BufferView::switchKeyMap()
209 {
210         pimpl_->switchKeyMap();
211 }
212
213
214 void BufferView::insetUnlock()
215 {
216         pimpl_->insetUnlock();
217 }
218
219
220 int BufferView::workWidth() const
221 {
222         return pimpl_->workarea().workWidth();
223 }
224
225
226 void BufferView::showCursor()
227 {
228         pimpl_->showCursor();
229 }
230
231
232 void BufferView::hideCursor()
233 {
234         pimpl_->hideCursor();
235 }
236
237
238 void BufferView::toggleSelection(bool b)
239 {
240         pimpl_->toggleSelection(b);
241 }
242
243
244 void BufferView::toggleToggle()
245 {
246         pimpl_->toggleToggle();
247 }
248
249
250 void BufferView::center()
251 {
252         pimpl_->center();
253 }
254
255
256 string const BufferView::getClipboard() const
257 {
258         return pimpl_->workarea().getClipboard();
259 }
260
261
262 void BufferView::stuffClipboard(string const & stuff) const
263 {
264         pimpl_->stuffClipboard(stuff);
265 }
266
267
268 BufferView::UpdateCodes operator|(BufferView::UpdateCodes uc1,
269                                   BufferView::UpdateCodes uc2)
270 {
271         return static_cast<BufferView::UpdateCodes>
272                 (static_cast<int>(uc1) | static_cast<int>(uc2));
273 }
274
275
276 bool BufferView::dispatch(FuncRequest const & ev)
277 {
278         return pimpl_->dispatch(ev);
279 }
280
281
282 void BufferView::scroll(int lines)
283 {
284         pimpl_->scroll(lines);
285 }
286
287
288 // Inserts a file into current document
289 bool BufferView::insertLyXFile(string const & filen)
290         //
291         // Copyright CHT Software Service GmbH
292         // Uwe C. Schroeder
293         //
294         // Insert a LyXformat - file into current buffer
295         //
296         // Moved from lyx_cb.C (Lgb)
297 {
298         if (filen.empty())
299                 return false;
300
301         string const fname = MakeAbsPath(filen);
302
303         // check if file exist
304         FileInfo const fi(fname);
305
306         if (!fi.readable()) {
307                 string const file = MakeDisplayPath(fname, 50);
308 #if USE_BOOST_FORMAT
309                 boost::format fmt(_("The specified document\n%1$s\ncould not be read."));
310                 fmt % file;
311                 string text = fmt.str();
312 #else
313                 string text = _("The specified document\n");
314                 text += file + _(" could not be read.");
315 #endif
316                 Alert::error(_("Could not read document"), text);
317                 return false;
318         }
319
320         beforeChange(text);
321
322         ifstream ifs(fname.c_str());
323         if (!ifs) {
324                 string const file = MakeDisplayPath(fname, 50);
325 #if USE_BOOST_FORMAT
326                 boost::format fmt(_("Could not open the specified document\n%1$s."));
327                 fmt % file;
328                 string text = fmt.str();
329 #else
330                 string text = _("Could not open the specified document\n");
331                 text += file + ".";
332 #endif
333                 Alert::error(_("Could not open file"), text);
334                 return false;
335         }
336
337         int const c = ifs.peek();
338
339         LyXLex lex(0, 0);
340         lex.setStream(ifs);
341
342         bool res = true;
343
344         if (c == '#') {
345                 // FIXME: huh ? No we won't !
346                 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
347                 res = buffer()->readFile(lex, fname, ParagraphList::iterator(text->cursor.par()));
348         } else {
349                 lyxerr[Debug::INFO] << "Will insert file without header"
350                                     << endl;
351                 res = buffer()->readBody(lex, ParagraphList::iterator(text->cursor.par()));
352         }
353
354         resize();
355         return res;
356 }
357
358
359 bool BufferView::removeAutoInsets()
360 {
361         // keep track of which pos and par the cursor was on
362         Paragraph * cursor_par = &*text->cursor.par();
363         Paragraph * cursor_par_prev = cursor_par ? cursor_par->previous() : 0;
364         Paragraph * cursor_par_next = cursor_par ? cursor_par->next() : 0;
365         pos_type cursor_pos = text->cursor.pos();
366
367         bool found = false;
368
369         // Trap the deletion of the paragraph the cursor is in.
370         // Iterate until we find a paragraph that won't be immediately deleted.
371         // In reality this should mean we only execute the body of the while
372         // loop once at most.  However for safety we iterate rather than just
373         // make this an if () conditional.
374         while ((cursor_par_prev || cursor_par_next)
375                && text->setCursor(
376                                   cursor_par_prev ? cursor_par_prev : cursor_par_next,
377                                   0)) {
378                 // We just removed cursor_par so have to fix the "cursor"
379                 if (cursor_par_prev) {
380                         // '.' = cursor_par
381                         //  a -> a.
382                         // .
383                         cursor_par = cursor_par_prev;
384                         cursor_pos = cursor_par->size();
385                 } else {
386                         // .  -> .a
387                         //  a
388                         cursor_par = cursor_par_next;
389                         cursor_pos = 0;
390                 }
391                 cursor_par_prev = cursor_par->previous();
392                 cursor_par_next = cursor_par->next();
393         }
394
395         // Iterate through the paragraphs removing autoDelete insets as we go.
396         // If the paragraph ends up empty after all the autoDelete insets are
397         // removed that paragraph will be removed by the next setCursor() call.
398         ParIterator it = buffer()->par_iterator_begin();
399         ParIterator end = buffer()->par_iterator_end();
400         for (; it != end; ++it) {
401                 Paragraph * par = *it;
402                 Paragraph * par_prev = par ? par->previous() : 0;
403                 bool removed = false;
404
405                 if (text->setCursor(par, 0)
406                     && cursor_par == par_prev) {
407                         // The previous setCursor line was deleted and that
408                         // was the cursor_par line.  This can only happen if an
409                         // error box was the sole item on cursor_par.
410                         // It is possible for cursor_par_prev to be stray if
411                         // the line it pointed to only had a error box on it
412                         // so we have to set it to a known correct value.
413                         // This is often the same value it already had.
414                         cursor_par_prev = par->previous();
415                         if (cursor_par_prev) {
416                                 // '|' = par, '.' = cursor_par, 'E' = error box
417                                 // First step below may occur before while{}
418                                 //  a    |a      a     a     a.
419                                 //  E -> .E -> |.E -> .  -> |b
420                                 // .      b      b    |b
421                                 //  b
422                                 cursor_par = cursor_par_prev;
423                                 cursor_pos = cursor_par_prev->size();
424                                 cursor_par_prev = cursor_par->previous();
425                                 // cursor_par_next remains the same
426                         } else if (cursor_par_next) {
427                                 // First step below may occur before while{}
428                                 // .
429                                 //  E -> |.E -> |.  -> . -> .|a
430                                 //  a      a      a    |a
431                                 cursor_par = cursor_par_next;
432                                 cursor_pos = 0;
433                                 // cursor_par_prev remains unset
434                                 cursor_par_next = cursor_par->next();
435                         } else {
436                                 // I can't find a way to trigger this
437                                 // so it should be unreachable code
438                                 // unless the buffer is corrupted.
439                                 lyxerr << "BufferView::removeAutoInsets() is bad\n";
440                         }
441                 }
442
443                 InsetList::iterator pit = par->insetlist.begin();
444                 InsetList::iterator pend = par->insetlist.end();
445
446                 while (pit != pend) {
447                         if (pit.getInset()->autoDelete()) {
448                                 removed = true;
449                                 pos_type const pos = pit.getPos();
450
451                                 par->erase(pos);
452                                 // We just invalidated par's inset iterators so
453                                 // we get the next valid iterator position
454                                 pit = par->insetlist.insetIterator(pos);
455                                 // and ensure we have a valid end iterator.
456                                 pend = par->insetlist.end();
457
458                                 if (cursor_par == par) {
459                                         // update the saved cursor position
460                                         if (cursor_pos > pos)
461                                                 --cursor_pos;
462                                 }
463                         } else {
464                                 ++pit;
465                         }
466                 }
467                 if (removed) {
468                         found = true;
469                         text->redoParagraph();
470                 }
471         }
472
473         // It is possible that the last line is empty if it was cursor_par
474         // and/or only had an error inset on it.  So we set the cursor to the
475         // start of the doc to force its removal and ensure a valid saved cursor
476         if (text->setCursor(&*text->ownerParagraphs().begin(), 0)
477             && 0 == cursor_par_next) {
478                 cursor_par = cursor_par_prev;
479                 cursor_pos = cursor_par->size();
480         } else if (cursor_pos > cursor_par->size()) {
481                 // Some C-Enter lines were removed by the setCursor call which
482                 // then invalidated cursor_pos. It could still be "wrong" because
483                 // the cursor may appear to have jumped but since we collapsed
484                 // some C-Enter lines this should be a reasonable compromise.
485                 cursor_pos = cursor_par->size();
486         }
487
488         // restore the original cursor in its corrected location.
489         text->setCursorIntern(cursor_par, cursor_pos);
490
491         return found;
492 }
493
494
495 void BufferView::insertErrors(TeXErrors & terr)
496 {
497         // Save the cursor position
498         LyXCursor cursor = text->cursor;
499
500         TeXErrors::Errors::const_iterator cit = terr.begin();
501         TeXErrors::Errors::const_iterator end = terr.end();
502         for (; cit != end; ++cit) {
503                 string const desctext(cit->error_desc);
504                 string const errortext(cit->error_text);
505                 string const msgtxt = desctext + '\n' + errortext;
506                 int const errorrow = cit->error_in_line;
507
508                 // Insert error string for row number
509                 int tmpid = -1;
510                 int tmppos = -1;
511
512                 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
513                         buffer()->texrow.increasePos(tmpid, tmppos);
514                 }
515
516                 Paragraph * texrowpar = 0;
517
518                 if (tmpid == -1) {
519                         texrowpar = &*text->ownerParagraphs().begin();
520                         tmppos = 0;
521                 } else {
522                         texrowpar = buffer()->getParFromID(tmpid);
523                 }
524
525                 if (texrowpar == 0)
526                         continue;
527
528                 freezeUndo();
529                 InsetError * new_inset = new InsetError(msgtxt);
530                 text->setCursorIntern(texrowpar, tmppos);
531                 text->insertInset(new_inset);
532                 text->fullRebreak();
533                 unFreezeUndo();
534         }
535         // Restore the cursor position
536         text->setCursorIntern(cursor.par(), cursor.pos());
537 }
538
539
540 void BufferView::setCursorFromRow(int row)
541 {
542         int tmpid = -1;
543         int tmppos = -1;
544
545         buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
546
547         Paragraph * texrowpar;
548
549         if (tmpid == -1) {
550                 texrowpar = &*text->ownerParagraphs().begin();
551                 tmppos = 0;
552         } else {
553                 texrowpar = buffer()->getParFromID(tmpid);
554         }
555         text->setCursor(texrowpar, tmppos);
556 }
557
558
559 bool BufferView::insertInset(Inset * inset, string const & lout)
560 {
561         return pimpl_->insertInset(inset, lout);
562 }
563
564
565 void 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;
577                 }
578         }
579 }
580
581
582 void BufferView::undo()
583 {
584         if (!available())
585                 return;
586
587         owner()->message(_("Undo"));
588         hideCursor();
589         beforeChange(text);
590         update(text, BufferView::SELECT);
591         if (!textUndo(this))
592                 owner()->message(_("No further undo information"));
593         else
594                 update(text, BufferView::SELECT);
595         switchKeyMap();
596 }
597
598
599 void BufferView::redo()
600 {
601         if (!available())
602                 return;
603
604         owner()->message(_("Redo"));
605         hideCursor();
606         beforeChange(text);
607         update(text, BufferView::SELECT);
608         if (!textRedo(this))
609                 owner()->message(_("No further redo information"));
610         else
611                 update(text, BufferView::SELECT);
612         switchKeyMap();
613 }
614
615
616 void BufferView::copyEnvironment()
617 {
618         if (available()) {
619                 text->copyEnvironmentType();
620                 owner()->message(_("Paragraph environment type copied"));
621         }
622 }
623
624
625 void BufferView::pasteEnvironment()
626 {
627         if (available()) {
628                 text->pasteEnvironmentType();
629                 owner()->message(_("Paragraph environment type set"));
630                 update(text, BufferView::SELECT);
631         }
632 }
633
634
635 // these functions are for the spellchecker
636 WordLangTuple const BufferView::nextWord(float & value)
637 {
638         if (!available()) {
639                 value = 1;
640                 return WordLangTuple();
641         }
642
643         return text->selectNextWordToSpellcheck(value);
644 }
645
646
647 void BufferView::selectLastWord()
648 {
649         if (!available())
650                 return;
651
652         LyXCursor cur = text->selection.cursor;
653         hideCursor();
654         beforeChange(text);
655         text->selection.cursor = cur;
656         text->selectSelectedWord();
657         toggleSelection(false);
658         update(text, BufferView::SELECT);
659 }
660
661
662 void BufferView::endOfSpellCheck()
663 {
664         if (!available()) return;
665
666         hideCursor();
667         beforeChange(text);
668         text->selectSelectedWord();
669         text->clearSelection();
670         update(text, BufferView::SELECT);
671 }
672
673
674 void BufferView::replaceWord(string const & replacestring)
675 {
676         if (!available())
677                 return;
678
679         LyXText * tt = getLyXText();
680         hideCursor();
681         update(tt, BufferView::SELECT);
682
683         // clear the selection (if there is any)
684         toggleSelection(false);
685         update(tt, BufferView::SELECT);
686
687         // clear the selection (if there is any)
688         toggleSelection(false);
689         tt->replaceSelectionWithString(replacestring);
690
691         tt->setSelectionRange(replacestring.length());
692
693         // Go back so that replacement string is also spellchecked
694         for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
695                 tt->cursorLeft(this);
696         }
697         update(tt, BufferView::SELECT);
698
699         // FIXME: should be done through LFUN
700         buffer()->markDirty();
701         fitCursor();
702 }
703 // End of spellchecker stuff
704
705
706 bool BufferView::lockInset(UpdatableInset * inset)
707 {
708         if (!inset)
709                 return false;
710         // don't relock if we're already locked
711         if (theLockingInset() == inset)
712                 return true;
713         if (!theLockingInset()) {
714                 // first check if it's the inset under the cursor we want lock
715                 // should be most of the time
716                 if (text->cursor.pos() < text->cursor.par()->size()
717                     && text->cursor.par()->getChar(text->cursor.pos()) ==
718                     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                                               outerFont(c.par()));
973         return font.language()->encoding();
974 }
975
976
977 void BufferView::haveSelection(bool sel)
978 {
979         pimpl_->workarea().haveSelection(sel);
980 }
981
982
983 int BufferView::workHeight() const
984 {
985         return pimpl_->workarea().workHeight();
986 }