Fix encoding not sync (on status bar) after reloading

Extracted parts of FileManager::reloadBuffer and FileManager::loadFile
to a separate function, so that both exhibit the same feature level of
EOL/encoding detection. reloadBuffer() used to have less logic than loadFile() and incorrectly handled UTF-8 detection when the file was ANSI

Fixes #2637, fixes #2843, closes #4124
This commit is contained in:
Silent 2018-01-12 17:57:41 +01:00 committed by Don HO
parent 926a5f5300
commit 2c4a389f55
2 changed files with 29 additions and 32 deletions

View File

@ -601,31 +601,13 @@ BufferID FileManager::loadFile(const TCHAR * filename, Document doc, int encodin
Buffer* buf = _buffers.at(_nbBufs - 1);
// restore the encoding (ANSI based) while opening the existing file
NppParameters *pNppParamInst = NppParameters::getInstance();
const NewDocDefaultSettings & ndds = (pNppParamInst->getNppGUI()).getNewDocDefaultSettings();
buf->setUnicodeMode(ndds._unicodeMode);
buf->setEncoding(-1);
// if no file extension, and the language has been detected, we use the detected value
if ((buf->getLangType() == L_TEXT) && (detectedLang != L_TEXT))
buf->setLangType(detectedLang);
if (encoding == -1)
{
UniMode um = UnicodeConvertor.getEncoding();
if (um == uni7Bit)
um = (ndds._openAnsiAsUtf8) ? uniCookie : uni8Bit;
buf->setUnicodeMode(um);
}
else // encoding != -1
{
// Test if encoding is set to UTF8 w/o BOM (usually for utf8 indicator of xml or html)
buf->setEncoding((encoding == SC_CP_UTF8)?-1:encoding);
buf->setUnicodeMode(uniCookie);
}
buf->setEolFormat(bkformat);
setLoadedBufferEncodingAndEol(buf, UnicodeConvertor, encoding, bkformat);
//determine buffer properties
++_nextBufferID;
@ -660,24 +642,38 @@ bool FileManager::reloadBuffer(BufferID id)
if (res)
{
if (encoding == -1)
{
buf->setUnicodeMode(UnicodeConvertor.getEncoding());
}
else
{
buf->setEncoding(encoding);
buf->setUnicodeMode(uniCookie);
}
// Since the buffer will be reloaded from the disk, EOL might have been changed
if (bkformat != EolType::unknown)
buf->setEolFormat(bkformat);
setLoadedBufferEncodingAndEol(buf, UnicodeConvertor, encoding, bkformat);
}
return res;
}
void FileManager::setLoadedBufferEncodingAndEol(Buffer* buf, const Utf8_16_Read& UnicodeConvertor, int encoding, EolType bkformat)
{
if (encoding == -1)
{
NppParameters *pNppParamInst = NppParameters::getInstance();
const NewDocDefaultSettings & ndds = (pNppParamInst->getNppGUI()).getNewDocDefaultSettings();
UniMode um = UnicodeConvertor.getEncoding();
if (um == uni7Bit)
um = (ndds._openAnsiAsUtf8) ? uniCookie : uni8Bit;
buf->setUnicodeMode(um);
}
else
{
// Test if encoding is set to UTF8 w/o BOM (usually for utf8 indicator of xml or html)
buf->setEncoding((encoding == SC_CP_UTF8)?-1:encoding);
buf->setUnicodeMode(uniCookie);
}
// Since the buffer will be reloaded from the disk, EOL might have been changed
if (bkformat != EolType::unknown)
buf->setEolFormat(bkformat);
}
bool FileManager::reloadBufferDeferred(BufferID id)
{
Buffer* buf = getBufferByID(id);

View File

@ -97,6 +97,7 @@ public:
BufferID getBufferFromName(const TCHAR * name);
BufferID getBufferFromDocument(Document doc);
void setLoadedBufferEncodingAndEol(Buffer* buf, const Utf8_16_Read& UnicodeConvertor, int encoding, EolType bkformat);
bool reloadBuffer(BufferID id);
bool reloadBufferDeferred(BufferID id);
bool saveBuffer(BufferID id, const TCHAR* filename, bool isCopy = false, generic_string * error_msg = NULL);