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