[NEW] File Encoding is remembered in the session.

git-svn-id: svn://svn.tuxfamily.org/svnroot/notepadplus/repository/trunk@567 f5eea248-9336-0410-98b8-ebc06183d4e3
This commit is contained in:
Don Ho 2009-11-22 01:52:07 +00:00
parent 10e6cbfb61
commit de6f421c9c
9 changed files with 178 additions and 95 deletions

View File

@ -117,12 +117,7 @@ public:
const wchar_t * strW = char2wchar(txt2Encode, fromCodepage); const wchar_t * strW = char2wchar(txt2Encode, fromCodepage);
return wchar2char(strW, toCodepage); return wchar2char(strW, toCodepage);
}; };
/*
const char * encodeFromUtf8To(const char *txt2Encode) const {
const wchar_t * strW = wmc->char2wchar(txt2Encode, SC_CP_UTF8);
return wchar2char(strW, toCodepage);
};
*/
protected: protected:
WcharMbcsConvertor() : _multiByteStr(NULL), _wideCharStr(NULL), _multiByteAllocLen(0), _wideCharAllocLen(0), initSize(1024) { WcharMbcsConvertor() : _multiByteStr(NULL), _wideCharStr(NULL), _multiByteAllocLen(0), _wideCharAllocLen(0), initSize(1024) {
}; };

View File

@ -567,7 +567,7 @@ bool Notepad_plus::loadSession(Session & session)
continue; //skip session files, not supporting recursive sessions continue; //skip session files, not supporting recursive sessions
} }
if (PathFileExists(pFn)) { if (PathFileExists(pFn)) {
lastOpened = doOpen(pFn); lastOpened = doOpen(pFn, false, session._mainViewFiles[i]._encoding);
} else { } else {
lastOpened = BUFFER_INVALID; lastOpened = BUFFER_INVALID;
} }
@ -585,6 +585,7 @@ bool Notepad_plus::loadSession(Session & session)
Buffer * buf = MainFileManager->getBufferByID(lastOpened); Buffer * buf = MainFileManager->getBufferByID(lastOpened);
buf->setPosition(session._mainViewFiles[i], &_mainEditView); buf->setPosition(session._mainViewFiles[i], &_mainEditView);
buf->setLangType(typeToSet, pLn); buf->setLangType(typeToSet, pLn);
buf->setEncoding(session._mainViewFiles[i]._encoding);
//Force in the document so we can add the markers //Force in the document so we can add the markers
//Dont use default methods because of performance //Dont use default methods because of performance
@ -617,7 +618,7 @@ bool Notepad_plus::loadSession(Session & session)
continue; //skip session files, not supporting recursive sessions continue; //skip session files, not supporting recursive sessions
} }
if (PathFileExists(pFn)) { if (PathFileExists(pFn)) {
lastOpened = doOpen(pFn); lastOpened = doOpen(pFn, false, session._subViewFiles[k]._encoding);
//check if already open in main. If so, clone //check if already open in main. If so, clone
if (_mainDocTab.getIndexByBuffer(lastOpened) != -1) { if (_mainDocTab.getIndexByBuffer(lastOpened) != -1) {
loadBufferIntoView(lastOpened, SUB_VIEW); loadBufferIntoView(lastOpened, SUB_VIEW);
@ -646,6 +647,7 @@ bool Notepad_plus::loadSession(Session & session)
} }
} }
buf->setLangType(typeToSet, pLn); buf->setLangType(typeToSet, pLn);
buf->setEncoding(session._subViewFiles[k]._encoding);
//Force in the document so we can add the markers //Force in the document so we can add the markers
//Dont use default methods because of performance //Dont use default methods because of performance
@ -688,7 +690,7 @@ bool Notepad_plus::loadSession(Session & session)
return allSessionFilesLoaded; return allSessionFilesLoaded;
} }
BufferID Notepad_plus::doOpen(const TCHAR *fileName, bool isReadOnly) BufferID Notepad_plus::doOpen(const TCHAR *fileName, bool isReadOnly, int encoding)
{ {
TCHAR longFileName[MAX_PATH]; TCHAR longFileName[MAX_PATH];
@ -775,7 +777,7 @@ BufferID Notepad_plus::doOpen(const TCHAR *fileName, bool isReadOnly)
scnN.nmhdr.idFrom = NULL; scnN.nmhdr.idFrom = NULL;
_pluginsManager.notify(&scnN); _pluginsManager.notify(&scnN);
BufferID buffer = MainFileManager->loadFile(longFileName); BufferID buffer = MainFileManager->loadFile(longFileName, NULL, encoding);
if (buffer != BUFFER_INVALID) if (buffer != BUFFER_INVALID)
{ {
_isFileOpening = true; _isFileOpening = true;
@ -2079,7 +2081,7 @@ void Notepad_plus::checkDocState()
enableCommand(IDM_FILE_RENAME, isFileExisting, MENU); enableCommand(IDM_FILE_RENAME, isFileExisting, MENU);
enableConvertMenuItems(curBuf->getFormat()); enableConvertMenuItems(curBuf->getFormat());
checkUnicodeMenuItems(curBuf->getUnicodeMode()); checkUnicodeMenuItems(/*curBuf->getUnicodeMode()*/);
checkLangsMenu(-1); checkLangsMenu(-1);
} }
@ -3113,7 +3115,29 @@ void Notepad_plus::setDisplayFormat(formatType f)
_statusBar.setText(str.c_str(), STATUSBAR_EOF_FORMAT); _statusBar.setText(str.c_str(), STATUSBAR_EOF_FORMAT);
} }
void Notepad_plus::setUniModeText(/*UniMode um*/) int Notepad_plus::getCmdIDFromEncoding(int encoding) const
{
bool found = false;
size_t nbItem = sizeof(encoding_table)/sizeof(int);
size_t i = 0;
for ( ; i < nbItem ; i++)
{
if (encoding_table[i] == encoding)
{
found = true;
break;
}
}
if (!found)
{
printStr(TEXT("Encoding problem. Encoding is not added in encoding_table?"));
return -1;
}
return i+IDM_FORMAT_ENCODE;
}
void Notepad_plus::setUniModeText()
{ {
Buffer *buf = _pEditView->getCurrentBuffer(); Buffer *buf = _pEditView->getCurrentBuffer();
int encoding = buf->getEncoding(); int encoding = buf->getEncoding();
@ -3143,26 +3167,12 @@ void Notepad_plus::setUniModeText(/*UniMode um*/)
} }
else else
{ {
bool found = false; int cmdID = getCmdIDFromEncoding(encoding);
size_t nbItem = sizeof(encoding_table)/sizeof(int); if (cmdID == -1)
size_t i = 0;
for ( ; i < nbItem ; i++)
{
if (encoding_table[i] == encoding)
{
found = true;
break;
}
}
if (!found)
{
printStr(TEXT("Encoding problem. Encoding is not added in encoding_table?"));
return; return;
}
const int itemSize = 64; const int itemSize = 64;
TCHAR uniModeText[itemSize]; TCHAR uniModeText[itemSize];
::GetMenuString(_mainMenuHandle, i+IDM_FORMAT_ENCODE, uniModeText, itemSize, MF_BYCOMMAND); ::GetMenuString(_mainMenuHandle, cmdID, uniModeText, itemSize, MF_BYCOMMAND);
uniModeTextString = uniModeText; uniModeTextString = uniModeText;
} }
_statusBar.setText(uniModeTextString.c_str(), STATUSBAR_UNICODE_TYPE); _statusBar.setText(uniModeTextString.c_str(), STATUSBAR_UNICODE_TYPE);
@ -4414,13 +4424,48 @@ void Notepad_plus::command(int id)
um = uni8Bit; um = uni8Bit;
} }
if (buf->getUnicodeMode() != um) if (buf->getEncoding() != -1)
{ {
buf->setUnicodeMode(um); if (buf->isDirty())
if (shoulBeDirty) {
buf->setDirty(true); int answer = ::MessageBox(NULL, TEXT("You should save the current modification.\rAll the saved modifications can not be undone.\r\rContinue?"), TEXT("Save Current Modification"), MB_YESNO);
if (answer == IDYES)
{
fileSave();
_pEditView->execute(SCI_EMPTYUNDOBUFFER);
}
else
return;
}
if (_pEditView->execute(SCI_CANUNDO) == TRUE)
{
int answer = ::MessageBox(NULL, TEXT("All the saved modifications can not be undone.\r\rContinue?"), TEXT("Lose Undo Ability Waning"), MB_YESNO);
if (answer == IDYES)
{
// Do nothing
}
else
return;
}
buf->setEncoding(-1);
if (um == uni8Bit)
_pEditView->execute(SCI_SETCODEPAGE, CP_ACP);
else
buf->setUnicodeMode(um);
fileReload();
}
else
{
if (buf->getUnicodeMode() != um)
{
buf->setUnicodeMode(um);
if (shoulBeDirty)
buf->setDirty(true);
}
} }
buf->setEncoding(-1);
break; break;
} }
@ -4455,7 +4500,7 @@ void Notepad_plus::command(int id)
if (answer == IDYES) if (answer == IDYES)
{ {
fileSave(); fileSave();
_pEditView->execute(SCI_EMPTYUNDOBUFFER); _pEditView->execute(SCI_EMPTYUNDOBUFFER);
} }
else else
return; return;
@ -4463,7 +4508,7 @@ void Notepad_plus::command(int id)
if (_pEditView->execute(SCI_CANUNDO) == TRUE) if (_pEditView->execute(SCI_CANUNDO) == TRUE)
{ {
int answer = ::MessageBox(NULL, TEXT("All the saved modifications can not be undone.\r\rContinue?"), TEXT("Loss Undo Ability Waning"), MB_YESNO); int answer = ::MessageBox(NULL, TEXT("All the saved modifications can not be undone.\r\rContinue?"), TEXT("Lose Undo Ability Waning"), MB_YESNO);
if (answer == IDYES) if (answer == IDYES)
{ {
// Do nothing // Do nothing
@ -4474,21 +4519,9 @@ void Notepad_plus::command(int id)
if (!buf->isDirty()) if (!buf->isDirty())
{ {
int len = _pEditView->getCurrentDocLen(); Buffer *buf = _pEditView->getCurrentBuffer();
char *content = new char[len+1];
_pEditView->execute(SCI_GETTEXT, len+1, (LPARAM)content);
WcharMbcsConvertor *wmc = WcharMbcsConvertor::getInstance();
const char *newContent = wmc->encode(encoding_table[index], SC_CP_UTF8, content);
_pEditView->execute(SCI_SETCODEPAGE, SC_CP_UTF8);
_pEditView->execute(SCI_SETTEXT, 0, (LPARAM)newContent);
Buffer *buf = _pEditView->getCurrentBuffer();
buf->setEncoding(encoding_table[index]); buf->setEncoding(encoding_table[index]);
delete [] content; fileReload();
buf->setUnicodeMode(uniEnd);
_pEditView->execute(SCI_EMPTYUNDOBUFFER);
buf->setDirty(false);
} }
break; break;
} }
@ -6206,7 +6239,7 @@ void Notepad_plus::dynamicCheckMenuAndTB() const
//Format conversion //Format conversion
enableConvertMenuItems(_pEditView->getCurrentBuffer()->getFormat()); enableConvertMenuItems(_pEditView->getCurrentBuffer()->getFormat());
checkUnicodeMenuItems(_pEditView->getCurrentBuffer()->getUnicodeMode()); checkUnicodeMenuItems(/*_pEditView->getCurrentBuffer()->getUnicodeMode()*/);
//Syncronized scrolling //Syncronized scrolling
} }
@ -6218,8 +6251,12 @@ void Notepad_plus::enableConvertMenuItems(formatType f) const
enableCommand(IDM_FORMAT_TOMAC, (f != MAC_FORMAT), MENU); enableCommand(IDM_FORMAT_TOMAC, (f != MAC_FORMAT), MENU);
} }
void Notepad_plus::checkUnicodeMenuItems(UniMode um) const void Notepad_plus::checkUnicodeMenuItems(/*UniMode um*/) const
{ {
Buffer *buf = _pEditView->getCurrentBuffer();
UniMode um = buf->getUnicodeMode();
int encoding = buf->getEncoding();
int id = -1; int id = -1;
switch (um) switch (um)
{ {
@ -6229,13 +6266,37 @@ void Notepad_plus::checkUnicodeMenuItems(UniMode um) const
case uniCookie : id = IDM_FORMAT_AS_UTF_8; break; case uniCookie : id = IDM_FORMAT_AS_UTF_8; break;
case uni8Bit : id = IDM_FORMAT_ANSI; break; case uni8Bit : id = IDM_FORMAT_ANSI; break;
} }
if (id == -1) //um == uni16BE_NoBOM || um == uni16LE_NoBOM
{ if (encoding == -1)
::CheckMenuRadioItem(_mainMenuHandle, IDM_FORMAT_ANSI, IDM_FORMAT_AS_UTF_8, IDM_FORMAT_ANSI, MF_BYCOMMAND); {
::CheckMenuItem(_mainMenuHandle, IDM_FORMAT_ANSI, MF_UNCHECKED | MF_BYCOMMAND); // Uncheck all in the sub encoding menu
} ::CheckMenuRadioItem(_mainMenuHandle, IDM_FORMAT_ENCODE, IDM_FORMAT_ENCODE_END, IDM_FORMAT_ENCODE, MF_BYCOMMAND);
else ::CheckMenuItem(_mainMenuHandle, IDM_FORMAT_ENCODE, MF_UNCHECKED | MF_BYCOMMAND);
::CheckMenuRadioItem(_mainMenuHandle, IDM_FORMAT_ANSI, IDM_FORMAT_AS_UTF_8, id, MF_BYCOMMAND);
if (id == -1) //um == uni16BE_NoBOM || um == uni16LE_NoBOM
{
// Uncheck all in the main encoding menu
::CheckMenuRadioItem(_mainMenuHandle, IDM_FORMAT_ANSI, IDM_FORMAT_AS_UTF_8, IDM_FORMAT_ANSI, MF_BYCOMMAND);
::CheckMenuItem(_mainMenuHandle, IDM_FORMAT_ANSI, MF_UNCHECKED | MF_BYCOMMAND);
}
else
{
::CheckMenuRadioItem(_mainMenuHandle, IDM_FORMAT_ANSI, IDM_FORMAT_AS_UTF_8, id, MF_BYCOMMAND);
}
}
else
{
int cmdID = getCmdIDFromEncoding(encoding);
if (cmdID == -1)
return;
// Uncheck all in the main encoding menu
::CheckMenuRadioItem(_mainMenuHandle, IDM_FORMAT_ANSI, IDM_FORMAT_AS_UTF_8, IDM_FORMAT_ANSI, MF_BYCOMMAND);
::CheckMenuItem(_mainMenuHandle, IDM_FORMAT_ANSI, MF_UNCHECKED | MF_BYCOMMAND);
// Check the encoding item
::CheckMenuRadioItem(_mainMenuHandle, IDM_FORMAT_ENCODE, IDM_FORMAT_ENCODE_END, cmdID, MF_BYCOMMAND);
}
} }
void Notepad_plus::showAutoComp() { void Notepad_plus::showAutoComp() {
@ -10282,7 +10343,7 @@ void Notepad_plus::getCurrentOpenedFiles(Session & session)
generic_string languageName = getLangFromMenu(buf); generic_string languageName = getLangFromMenu(buf);
const TCHAR *langName = languageName.c_str(); const TCHAR *langName = languageName.c_str();
sessionFileInfo sfi(buf->getFullPathName(), langName, buf->getPosition(&_mainEditView)); sessionFileInfo sfi(buf->getFullPathName(), langName, buf->getEncoding(), buf->getPosition(&_mainEditView));
//_mainEditView.activateBuffer(buf->getID()); //_mainEditView.activateBuffer(buf->getID());
_invisibleEditView.execute(SCI_SETDOCPOINTER, 0, buf->getDocument()); _invisibleEditView.execute(SCI_SETDOCPOINTER, 0, buf->getDocument());
@ -10308,7 +10369,7 @@ void Notepad_plus::getCurrentOpenedFiles(Session & session)
generic_string languageName = getLangFromMenu( buf ); generic_string languageName = getLangFromMenu( buf );
const TCHAR *langName = languageName.c_str(); const TCHAR *langName = languageName.c_str();
sessionFileInfo sfi(buf->getFullPathName(), langName, buf->getPosition(&_subEditView)); sessionFileInfo sfi(buf->getFullPathName(), langName, buf->getEncoding(), buf->getPosition(&_subEditView));
_invisibleEditView.execute(SCI_SETDOCPOINTER, 0, buf->getDocument()); _invisibleEditView.execute(SCI_SETDOCPOINTER, 0, buf->getDocument());
int maxLine = _invisibleEditView.execute(SCI_GETLINECOUNT); int maxLine = _invisibleEditView.execute(SCI_GETLINECOUNT);
@ -10632,8 +10693,8 @@ void Notepad_plus::notifyBufferChanged(Buffer * buffer, int mask) {
if (mask & (BufferChangeFormat|BufferChangeLanguage|BufferChangeUnicode)) if (mask & (BufferChangeFormat|BufferChangeLanguage|BufferChangeUnicode))
{ {
updateStatusBar(); updateStatusBar();
checkUnicodeMenuItems(buffer->getUnicodeMode()); checkUnicodeMenuItems(/*buffer->getUnicodeMode()*/);
setUniModeText(/*buffer->getUnicodeMode()*/); setUniModeText();
setDisplayFormat(buffer->getFormat()); setDisplayFormat(buffer->getFormat());
enableConvertMenuItems(buffer->getFormat()); enableConvertMenuItems(buffer->getFormat());
} }
@ -10657,8 +10718,8 @@ void Notepad_plus::notifyBufferActivated(BufferID bufid, int view) {
dynamicCheckMenuAndTB(); dynamicCheckMenuAndTB();
setLangStatus(buf->getLangType()); setLangStatus(buf->getLangType());
updateStatusBar(); updateStatusBar();
checkUnicodeMenuItems(buf->getUnicodeMode()); checkUnicodeMenuItems(/*buf->getUnicodeMode()*/);
setUniModeText(/*buf->getUnicodeMode()*/); setUniModeText();
setDisplayFormat(buf->getFormat()); setDisplayFormat(buf->getFormat());
enableConvertMenuItems(buf->getFormat()); enableConvertMenuItems(buf->getFormat());
generic_string dir(buf->getFullPathName()); generic_string dir(buf->getFullPathName());

View File

@ -191,7 +191,7 @@ public:
// fileOperations // fileOperations
//The doXXX functions apply to a single buffer and dont need to worry about views, with the excpetion of doClose, since closing one view doesnt have to mean the document is gone //The doXXX functions apply to a single buffer and dont need to worry about views, with the excpetion of doClose, since closing one view doesnt have to mean the document is gone
BufferID doOpen(const TCHAR *fileName, bool isReadOnly = false); BufferID doOpen(const TCHAR *fileName, bool isReadOnly = false, int encoding = -1);
bool doReload(BufferID id, bool alert = true); bool doReload(BufferID id, bool alert = true);
bool doSave(BufferID, const TCHAR * filename, bool isSaveCopy = false); bool doSave(BufferID, const TCHAR * filename, bool isSaveCopy = false);
void doClose(BufferID, int whichOne); void doClose(BufferID, int whichOne);
@ -458,7 +458,7 @@ private:
void getMainClientRect(RECT & rc) const; void getMainClientRect(RECT & rc) const;
void dynamicCheckMenuAndTB() const; void dynamicCheckMenuAndTB() const;
void enableConvertMenuItems(formatType f) const; void enableConvertMenuItems(formatType f) const;
void checkUnicodeMenuItems(UniMode um) const; void checkUnicodeMenuItems(/*UniMode um*/) const;
generic_string getLangDesc(LangType langType, bool shortDesc = false); generic_string getLangDesc(LangType langType, bool shortDesc = false);
@ -467,8 +467,8 @@ private:
}; };
void setDisplayFormat(formatType f); void setDisplayFormat(formatType f);
int getCmdIDFromEncoding(int encoding) const;
void setUniModeText(/*UniMode um*/); void setUniModeText();
void checkLangsMenu(int id) const ; void checkLangsMenu(int id) const ;
void setLanguage(LangType langType); void setLanguage(LangType langType);

View File

@ -403,7 +403,7 @@ BEGIN
MENUITEM "Cyrillic(Windows)", IDM_FORMAT_WIN1251 MENUITEM "Cyrillic(Windows)", IDM_FORMAT_WIN1251
MENUITEM "Central European(Windows)", IDM_FORMAT_WIN1250 MENUITEM "Central European(Windows)", IDM_FORMAT_WIN1250
MENUITEM "Chinese Traditional(Big5)", IDM_FORMAT_BIG5 MENUITEM "Chinese Traditional(Big5)", IDM_FORMAT_BIG5
MENUITEM "Chinese Simplified(GB2312)", IDM_FORMAT_GB2312 MENUITEM "Chinese Simplified(GB)", IDM_FORMAT_GB2312
MENUITEM "Greek(Windows)", IDM_FORMAT_WIN1253 MENUITEM "Greek(Windows)", IDM_FORMAT_WIN1253
MENUITEM "Hebrew(iso8859-8)", IDM_FORMAT_ISO_8859_8 MENUITEM "Hebrew(iso8859-8)", IDM_FORMAT_ISO_8859_8
MENUITEM "Hebrew(Windows)", IDM_FORMAT_WIN1255 MENUITEM "Hebrew(Windows)", IDM_FORMAT_WIN1255

View File

@ -1456,7 +1456,9 @@ bool NppParameters::getSessionFromXmlTree(TiXmlDocument *pSessionDoc, Session *p
const TCHAR *langName; const TCHAR *langName;
langName = (childNode->ToElement())->Attribute(TEXT("lang")); langName = (childNode->ToElement())->Attribute(TEXT("lang"));
sessionFileInfo sfi( fileName, langName, position ); int encoding = -1;
const TCHAR *encStr = (childNode->ToElement())->Attribute(TEXT("encoding"), &encoding);
sessionFileInfo sfi(fileName, langName, encStr?encoding:-1, position);
for (TiXmlNode *markNode = childNode->FirstChildElement(TEXT("Mark")); for (TiXmlNode *markNode = childNode->FirstChildElement(TEXT("Mark"));
markNode ; markNode ;
@ -1501,7 +1503,10 @@ bool NppParameters::getSessionFromXmlTree(TiXmlDocument *pSessionDoc, Session *p
const TCHAR *langName; const TCHAR *langName;
langName = (childNode->ToElement())->Attribute(TEXT("lang")); langName = (childNode->ToElement())->Attribute(TEXT("lang"));
sessionFileInfo sfi( fileName, langName, position ); int encoding = -1;
(childNode->ToElement())->Attribute(TEXT("encoding"), &encoding);
sessionFileInfo sfi(fileName, langName, encoding, position);
for (TiXmlNode *markNode = childNode->FirstChildElement(TEXT("Mark")); for (TiXmlNode *markNode = childNode->FirstChildElement(TEXT("Mark"));
markNode ; markNode ;
@ -2092,6 +2097,7 @@ void NppParameters::writeSession(const Session & session, const TCHAR *fileName)
(fileNameNode->ToElement())->SetAttribute(TEXT("endPos"), session._mainViewFiles[i]._endPos); (fileNameNode->ToElement())->SetAttribute(TEXT("endPos"), session._mainViewFiles[i]._endPos);
(fileNameNode->ToElement())->SetAttribute(TEXT("selMode"), session._mainViewFiles[i]._selMode); (fileNameNode->ToElement())->SetAttribute(TEXT("selMode"), session._mainViewFiles[i]._selMode);
(fileNameNode->ToElement())->SetAttribute(TEXT("lang"), session._mainViewFiles[i]._langName.c_str()); (fileNameNode->ToElement())->SetAttribute(TEXT("lang"), session._mainViewFiles[i]._langName.c_str());
(fileNameNode->ToElement())->SetAttribute(TEXT("encoding"), session._mainViewFiles[i]._encoding);
(fileNameNode->ToElement())->SetAttribute(TEXT("filename"), session._mainViewFiles[i]._fileName.c_str()); (fileNameNode->ToElement())->SetAttribute(TEXT("filename"), session._mainViewFiles[i]._fileName.c_str());
for (size_t j = 0 ; j < session._mainViewFiles[i].marks.size() ; j++) for (size_t j = 0 ; j < session._mainViewFiles[i].marks.size() ; j++)
@ -2115,6 +2121,7 @@ void NppParameters::writeSession(const Session & session, const TCHAR *fileName)
(fileNameNode->ToElement())->SetAttribute(TEXT("endPos"), session._subViewFiles[i]._endPos); (fileNameNode->ToElement())->SetAttribute(TEXT("endPos"), session._subViewFiles[i]._endPos);
(fileNameNode->ToElement())->SetAttribute(TEXT("selMode"), session._subViewFiles[i]._selMode); (fileNameNode->ToElement())->SetAttribute(TEXT("selMode"), session._subViewFiles[i]._selMode);
(fileNameNode->ToElement())->SetAttribute(TEXT("lang"), session._subViewFiles[i]._langName.c_str()); (fileNameNode->ToElement())->SetAttribute(TEXT("lang"), session._subViewFiles[i]._langName.c_str());
(fileNameNode->ToElement())->SetAttribute(TEXT("encoding"), session._subViewFiles[i]._encoding);
(fileNameNode->ToElement())->SetAttribute(TEXT("filename"), session._subViewFiles[i]._fileName.c_str()); (fileNameNode->ToElement())->SetAttribute(TEXT("filename"), session._subViewFiles[i]._fileName.c_str());
for (size_t j = 0 ; j < session._subViewFiles[i].marks.size() ; j++) for (size_t j = 0 ; j < session._subViewFiles[i].marks.size() ; j++)

View File

@ -126,20 +126,17 @@ struct Position
}; };
struct sessionFileInfo : public Position { struct sessionFileInfo : public Position {
sessionFileInfo(const TCHAR *fn) { sessionFileInfo(const TCHAR *fn, const TCHAR *ln, int encoding, Position pos) : _encoding(encoding), Position(pos) {
if (fn) _fileName = fn;
};
sessionFileInfo(const TCHAR *fn, const TCHAR *ln, Position pos) : Position(pos) {
if (fn) _fileName = fn; if (fn) _fileName = fn;
if (ln) _langName = ln; if (ln) _langName = ln;
}; };
sessionFileInfo(generic_string fn) : _fileName(fn){}; sessionFileInfo(generic_string fn) : _fileName(fn), _encoding(-1){};
sessionFileInfo(generic_string fn, Position pos) : Position(pos), _fileName(fn){};
generic_string _fileName; generic_string _fileName;
generic_string _langName; generic_string _langName;
vector<size_t> marks; vector<size_t> marks;
int _encoding;
}; };
struct Session { struct Session {

View File

@ -463,7 +463,8 @@ void FileManager::closeBuffer(BufferID id, ScintillaEditView * identifier) {
} }
} }
BufferID FileManager::loadFile(const TCHAR * filename, Document doc) { BufferID FileManager::loadFile(const TCHAR * filename, Document doc, int encoding)
{
bool ownDoc = false; bool ownDoc = false;
if (doc == NULL) if (doc == NULL)
{ {
@ -475,7 +476,7 @@ BufferID FileManager::loadFile(const TCHAR * filename, Document doc) {
::GetFullPathName(filename, MAX_PATH, fullpath, NULL); ::GetFullPathName(filename, MAX_PATH, fullpath, NULL);
::GetLongPathName(fullpath, fullpath, MAX_PATH); ::GetLongPathName(fullpath, fullpath, MAX_PATH);
Utf8_16_Read UnicodeConvertor; //declare here so we can get information after loading is done Utf8_16_Read UnicodeConvertor; //declare here so we can get information after loading is done
bool res = loadFileData(doc, fullpath, &UnicodeConvertor, L_TXT); bool res = loadFileData(doc, fullpath, &UnicodeConvertor, L_TXT, encoding);
if (res) if (res)
{ {
Buffer * newBuf = new Buffer(this, _nextBufferID, doc, DOC_REGULAR, fullpath); Buffer * newBuf = new Buffer(this, _nextBufferID, doc, DOC_REGULAR, fullpath);
@ -523,12 +524,13 @@ BufferID FileManager::loadFile(const TCHAR * filename, Document doc) {
} }
} }
bool FileManager::reloadBuffer(BufferID id) { bool FileManager::reloadBuffer(BufferID id)
{
Buffer * buf = getBufferByID(id); Buffer * buf = getBufferByID(id);
Document doc = buf->getDocument(); Document doc = buf->getDocument();
Utf8_16_Read UnicodeConvertor; Utf8_16_Read UnicodeConvertor;
buf->_canNotify = false; //disable notify during file load, we dont want dirty to be triggered buf->_canNotify = false; //disable notify during file load, we dont want dirty to be triggered
bool res = loadFileData(doc, buf->getFullPathName(), &UnicodeConvertor, buf->getLangType()); bool res = loadFileData(doc, buf->getFullPathName(), &UnicodeConvertor, buf->getLangType(), buf->getEncoding());
buf->_canNotify = true; buf->_canNotify = true;
if (res) { if (res) {
if (UnicodeConvertor.getNewBuf()) { if (UnicodeConvertor.getNewBuf()) {
@ -537,12 +539,12 @@ bool FileManager::reloadBuffer(BufferID id) {
buf->determinateFormat(""); buf->determinateFormat("");
} }
buf->setUnicodeMode(UnicodeConvertor.getEncoding()); buf->setUnicodeMode(UnicodeConvertor.getEncoding());
// buf->setNeedsLexing(true);
} }
return res; return res;
} }
bool FileManager::reloadBufferDeferred(BufferID id) { bool FileManager::reloadBufferDeferred(BufferID id)
{
Buffer * buf = getBufferByID(id); Buffer * buf = getBufferByID(id);
buf->setDeferredReload(); buf->setDeferredReload();
return true; return true;
@ -694,7 +696,7 @@ BufferID FileManager::bufferFromDocument(Document doc, bool dontIncrease, bool d
return id; return id;
} }
bool FileManager::loadFileData(Document doc, const TCHAR * filename, Utf8_16_Read * UnicodeConvertor, LangType language) bool FileManager::loadFileData(Document doc, const TCHAR * filename, Utf8_16_Read * UnicodeConvertor, LangType language, int encoding)
{ {
const int blockSize = 128 * 1024; //128 kB const int blockSize = 128 * 1024; //128 kB
char data[blockSize]; char data[blockSize];
@ -705,17 +707,22 @@ bool FileManager::loadFileData(Document doc, const TCHAR * filename, Utf8_16_Rea
//Setup scratchtilla for new filedata //Setup scratchtilla for new filedata
_pscratchTilla->execute(SCI_SETDOCPOINTER, 0, doc); _pscratchTilla->execute(SCI_SETDOCPOINTER, 0, doc);
bool ro = _pscratchTilla->execute(SCI_GETREADONLY) != 0; bool ro = _pscratchTilla->execute(SCI_GETREADONLY) != 0;
if (ro) { if (ro)
{
_pscratchTilla->execute(SCI_SETREADONLY, false); _pscratchTilla->execute(SCI_SETREADONLY, false);
} }
_pscratchTilla->execute(SCI_CLEARALL); _pscratchTilla->execute(SCI_CLEARALL);
if (language < L_EXTERNAL) {
WcharMbcsConvertor *wmc = WcharMbcsConvertor::getInstance();
if (language < L_EXTERNAL)
{
_pscratchTilla->execute(SCI_SETLEXER, ScintillaEditView::langNames[language].lexerID); _pscratchTilla->execute(SCI_SETLEXER, ScintillaEditView::langNames[language].lexerID);
} else { }
else
{
int id = language - L_EXTERNAL; int id = language - L_EXTERNAL;
TCHAR * name = NppParameters::getInstance()->getELCFromIndex(id)._name; TCHAR * name = NppParameters::getInstance()->getELCFromIndex(id)._name;
#ifdef UNICODE #ifdef UNICODE
WcharMbcsConvertor *wmc = WcharMbcsConvertor::getInstance();
const char *pName = wmc->wchar2char(name, CP_ACP); const char *pName = wmc->wchar2char(name, CP_ACP);
#else #else
const char *pName = name; const char *pName = name;
@ -723,14 +730,29 @@ bool FileManager::loadFileData(Document doc, const TCHAR * filename, Utf8_16_Rea
_pscratchTilla->execute(SCI_SETLEXERLANGUAGE, 0, (LPARAM)pName); _pscratchTilla->execute(SCI_SETLEXERLANGUAGE, 0, (LPARAM)pName);
} }
if (encoding != -1)
{
_pscratchTilla->execute(SCI_SETCODEPAGE, SC_CP_UTF8);
}
bool success = true; bool success = true;
__try { __try {
size_t lenFile = 0; size_t lenFile = 0;
size_t lenConvert = 0; //just in case conversion results in 0, but file not empty size_t lenConvert = 0; //just in case conversion results in 0, but file not empty
do { do {
lenFile = fread(data, 1, blockSize, fp); lenFile = fread(data, 1, blockSize, fp);
lenConvert = UnicodeConvertor->convert(data, lenFile); if (encoding != -1)
_pscratchTilla->execute(SCI_APPENDTEXT, lenConvert, (LPARAM)(UnicodeConvertor->getNewBuf())); {
WcharMbcsConvertor *wmc = WcharMbcsConvertor::getInstance();
const char *newData = wmc->encode(encoding, SC_CP_UTF8, data);
_pscratchTilla->execute(SCI_APPENDTEXT, strlen(newData), (LPARAM)newData);
}
else
{
lenConvert = UnicodeConvertor->convert(data, lenFile);
_pscratchTilla->execute(SCI_APPENDTEXT, lenConvert, (LPARAM)(UnicodeConvertor->getNewBuf()));
}
} while (lenFile > 0); } while (lenFile > 0);
} __except(filter(GetExceptionCode(), GetExceptionInformation())) { } __except(filter(GetExceptionCode(), GetExceptionInformation())) {
printStr(TEXT("File is too big to be opened by Notepad++")); printStr(TEXT("File is too big to be opened by Notepad++"));

View File

@ -79,7 +79,7 @@ public:
void addBufferReference(BufferID id, ScintillaEditView * identifer); //called by Scintilla etc indirectly void addBufferReference(BufferID id, ScintillaEditView * identifer); //called by Scintilla etc indirectly
BufferID loadFile(const TCHAR * filename, Document doc = NULL); //ID == BUFFER_INVALID on failure. If Doc == NULL, a new file is created, otherwise data is loaded in given document BufferID loadFile(const TCHAR * filename, Document doc = NULL, int encoding = -1); //ID == BUFFER_INVALID on failure. If Doc == NULL, a new file is created, otherwise data is loaded in given document
BufferID newEmptyDocument(); BufferID newEmptyDocument();
//create Buffer from existing Scintilla, used from new Scintillas. If dontIncrease = true, then the new document number isnt increased afterwards. //create Buffer from existing Scintilla, used from new Scintillas. If dontIncrease = true, then the new document number isnt increased afterwards.
//usefull for temporary but neccesary docs //usefull for temporary but neccesary docs
@ -121,7 +121,7 @@ private:
BufferID _nextBufferID; BufferID _nextBufferID;
size_t _nrBufs; size_t _nrBufs;
bool loadFileData(Document doc, const TCHAR * filename, Utf8_16_Read * UnicodeConvertor, LangType language); bool loadFileData(Document doc, const TCHAR * filename, Utf8_16_Read * UnicodeConvertor, LangType language, int encoding = -1);
}; };
#define MainFileManager FileManager::getInstance() #define MainFileManager FileManager::getInstance()

View File

@ -238,7 +238,7 @@
#define IDM_FORMAT_CONV2_UCS_2BE (IDM_FORMAT + 12) #define IDM_FORMAT_CONV2_UCS_2BE (IDM_FORMAT + 12)
#define IDM_FORMAT_CONV2_UCS_2LE (IDM_FORMAT + 13) #define IDM_FORMAT_CONV2_UCS_2LE (IDM_FORMAT + 13)
#define IDM_FORMAT_ENCODE (IDM_FORMAT + 20) #define IDM_FORMAT_ENCODE (IDM_FORMAT + 20)
#define IDM_FORMAT_WIN1250 (IDM_FORMAT_ENCODE + 0) #define IDM_FORMAT_WIN1250 (IDM_FORMAT_ENCODE + 0)
#define IDM_FORMAT_WIN1251 (IDM_FORMAT_ENCODE + 1) #define IDM_FORMAT_WIN1251 (IDM_FORMAT_ENCODE + 1)
#define IDM_FORMAT_WIN1252 (IDM_FORMAT_ENCODE + 2) #define IDM_FORMAT_WIN1252 (IDM_FORMAT_ENCODE + 2)
@ -254,6 +254,7 @@
#define IDM_FORMAT_EUC_KR (IDM_FORMAT_ENCODE + 12) #define IDM_FORMAT_EUC_KR (IDM_FORMAT_ENCODE + 12)
#define IDM_FORMAT_TIS_620 (IDM_FORMAT_ENCODE + 13) #define IDM_FORMAT_TIS_620 (IDM_FORMAT_ENCODE + 13)
#define IDM_FORMAT_ISO_8859_8 (IDM_FORMAT_ENCODE + 14) #define IDM_FORMAT_ISO_8859_8 (IDM_FORMAT_ENCODE + 14)
#define IDM_FORMAT_ENCODE_END IDM_FORMAT_ISO_8859_8
#define IDM_LANG (IDM + 6000) #define IDM_LANG (IDM + 6000)
#define IDM_LANGSTYLE_CONFIG_DLG (IDM_LANG + 1) #define IDM_LANGSTYLE_CONFIG_DLG (IDM_LANG + 1)