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