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