X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2FDocIterator.cpp;h=ec85886657ffdbd5518e095f2437d5064b86943c;hb=c7d29be153debac82e3d2e8865fcc849f0a5f40d;hp=38f2393224b1e37b1839537f12ea6b85008ec8d8;hpb=fc7fb6a5642bf8163276797c1602dd1395e2aef7;p=lyx.git diff --git a/src/DocIterator.cpp b/src/DocIterator.cpp index 38f2393224..ec85886657 100644 --- a/src/DocIterator.cpp +++ b/src/DocIterator.cpp @@ -15,7 +15,12 @@ #include "DocIterator.h" #include "Buffer.h" +#include "BufferParams.h" +#include "Encoding.h" +#include "Font.h" #include "InsetList.h" +#include "Language.h" +#include "LaTeXFeatures.h" #include "Paragraph.h" #include "LyXRC.h" #include "Text.h" @@ -618,11 +623,7 @@ void DocIterator::sanitize() for (size_t i = 0, n = sl.size(); i != n; ++i) { if (inset == 0) { // FIXME - LYXERR0(" Should not happen, but does e.g. after " - "C-n C-l C-z S-C-z\n" - << " or when a Buffer has been concurrently edited by two views" - << '\n' << "dit: " << *this << '\n' - << " lastpos: " << slices_[i].lastpos()); + LYXERR0("Null inset on cursor stack."); fixIfBroken(); break; } @@ -688,6 +689,74 @@ void DocIterator::append(DocIterator::idx_type idx, pos_type pos) } +Encoding const * DocIterator::getEncoding() const +{ + if (empty()) + return 0; + + BufferParams const & bp = buffer()->params(); + if (bp.useNonTeXFonts) + return encodings.fromLyXName("utf8-plain"); + + CursorSlice const & sl = innerTextSlice(); + Text const & text = *sl.text(); + Language const * lang = + text.getPar(sl.pit()).getFont(bp, sl.pos(), + text.outerFont(sl.pit())).language(); + // If we have a custom encoding for the buffer, we do not switch encoding ... + bool const customenc = + bp.inputenc != "auto" && bp.inputenc != "default"; + // ... except for non-CJKutf8 CJK (see output_latex::switchEncoding()) + bool const cjk_non_utf8 = + bp.encoding().name() != "utf8-cjk" + || !LaTeXFeatures::isAvailable("CJKutf8"); + Encoding const * enc = + (customenc + && (lang->encoding()->package() != Encoding::CJK || !cjk_non_utf8)) + ? &bp.encoding() : lang->encoding(); + + // Some insets force specific encodings sometimes (e.g., listings in + // multibyte context forces singlebyte). + if (inset().forcedEncoding(enc, encodings.fromLyXName("iso8859-1"))) { + // Get the language outside the inset + size_t const n = depth(); + for (size_t i = 0; i < n; ++i) { + Text const & otext = *slices_[i].text(); + Language const * olang = + otext.getPar(slices_[i].pit()).getFont(bp, slices_[i].pos(), + otext.outerFont(slices_[i].pit())).language(); + Encoding const * oenc = olang->encoding(); + if (oenc->name() != "inherit") + return inset().forcedEncoding(enc, oenc); + } + // Fall back to buffer encoding if no outer lang was found. + return inset().forcedEncoding(enc, &bp.encoding()); + } + + // Inherited encoding (latex_language) is determined by the context + // Look for the first outer encoding that is not itself "inherit" + if (lang->encoding()->name() == "inherit") { + size_t const n = depth(); + for (size_t i = 0; i < n; ++i) { + Text const & otext = *slices_[i].text(); + Language const * olang = + otext.getPar(slices_[i].pit()).getFont(bp, slices_[i].pos(), + otext.outerFont(slices_[i].pit())).language(); + // Again, if we have a custom encoding, this is used + // instead of the language's. + Encoding const * oenc = + (customenc + && (olang->encoding()->package() != Encoding::CJK || !cjk_non_utf8)) + ? &bp.encoding() : olang->encoding(); + if (olang->encoding()->name() != "inherit") + return oenc; + } + } + + return enc; +} + + ostream & operator<<(ostream & os, DocIterator const & dit) { for (size_t i = 0, n = dit.depth(); i != n; ++i)