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