[NEW_FEATURE] refine "open ANSI files as UTF8 w/o BOM" feature.

git-svn-id: svn://svn.tuxfamily.org/svnroot/notepadplus/repository@398 f5eea248-9336-0410-98b8-ebc06183d4e3
pull/343/head^2
donho 2009-01-17 16:46:49 +00:00
parent 2818386fb2
commit 1e27703297
9 changed files with 96 additions and 37 deletions

View File

@ -2862,6 +2862,10 @@ void NppParameters::feedGUIParameters(TiXmlNode *node)
if (element->Attribute(TEXT("lang"), &i))
_nppGUI._newDocDefaultSettings._lang = (LangType)i;
if (val = element->Attribute(TEXT("openAnsiAsUTF8")))
_nppGUI._newDocDefaultSettings._openAnsiAsUtf8 = (lstrcmp(val, TEXT("yes")) == 0);
}
else if (!lstrcmp(nm, TEXT("langsExcluded")))
{
@ -3687,6 +3691,7 @@ bool NppParameters::writeGUIParams()
element->SetAttribute(TEXT("format"), _nppGUI._newDocDefaultSettings._format);
element->SetAttribute(TEXT("encoding"), _nppGUI._newDocDefaultSettings._encoding);
element->SetAttribute(TEXT("lang"), _nppGUI._newDocDefaultSettings._lang);
element->SetAttribute(TEXT("openAnsiAsUTF8"), _nppGUI._newDocDefaultSettings._openAnsiAsUtf8?TEXT("yes"):TEXT("no"));
newDocDefaultSettingsExist = true;
}
else if (!lstrcmp(nm, TEXT("langsExcluded")))
@ -3868,6 +3873,7 @@ bool NppParameters::writeGUIParams()
GUIConfigElement->SetAttribute(TEXT("format"), _nppGUI._newDocDefaultSettings._format);
GUIConfigElement->SetAttribute(TEXT("encoding"), _nppGUI._newDocDefaultSettings._encoding);
GUIConfigElement->SetAttribute(TEXT("lang"), _nppGUI._newDocDefaultSettings._lang);
GUIConfigElement->SetAttribute(TEXT("openAnsiAsUTF8"), _nppGUI._newDocDefaultSettings._openAnsiAsUtf8?TEXT("yes"):TEXT("no"));
}
if (!langsExcludedLstExist)

View File

@ -56,7 +56,7 @@ const int TAB_MULTILINE = 128; // 1000 0000
const int TAB_HIDE = 256; //1 0000 0000
enum formatType {WIN_FORMAT, MAC_FORMAT, UNIX_FORMAT};
enum UniMode {uni8Bit=0, uniUTF8=1, uni16BE=2, uni16LE=3, uniCookie=4, uniEnd};
enum UniMode {uni8Bit=0, uniUTF8=1, uni16BE=2, uni16LE=3, uniCookie=4, uni7Bit=5, uniEnd};
enum ChangeDetect {cdDisabled=0, cdEnabled=1, cdAutoUpdate=2, cdGo2end=3, cdAutoUpdateGo2end=4};
enum BackupFeature {bak_none = 0, bak_simple = 1, bak_verbose = 2};
enum OpenSaveDirSetting {dir_followCurrent = 0, dir_last = 1, dir_userDef = 2};
@ -476,8 +476,9 @@ struct NewDocDefaultSettings
{
formatType _format;
UniMode _encoding;
bool _openAnsiAsUtf8;
LangType _lang;
NewDocDefaultSettings():_format(WIN_FORMAT), _encoding(uni8Bit), _lang(L_TXT){};
NewDocDefaultSettings():_format(WIN_FORMAT), _encoding(uni8Bit), _openAnsiAsUtf8(false), _lang(L_TXT){};
};
struct LangMenuItem {

View File

@ -413,14 +413,18 @@ BufferID FileManager::loadFile(const TCHAR * filename, Document doc) {
}
UniMode encoding = UnicodeConvertor.getEncoding();
if (encoding == uni8Bit)
if (encoding == uni7Bit)
{
NppParameters *pNppParamInst = NppParameters::getInstance();
const NewDocDefaultSettings & ndds = (pNppParamInst->getNppGUI()).getNewDocDefaultSettings();
if (ndds._encoding == uniCookie)
if (ndds._openAnsiAsUtf8)
{
encoding = uniCookie;
}
else
{
encoding = uni8Bit;
}
}
buf->setUnicodeMode(encoding);

View File

@ -48,10 +48,14 @@ Utf8_16_Read::~Utf8_16_Read()
}
}
int Utf8_16_Read::isUTF8_16()
// Returned value :
// 0 : utf8
// 1 : 7bits
// 2 : 8bits
u78 Utf8_16_Read::utf8_7bits_8bits()
{
int rv=1;
int ASCII7only=1;
int rv = 1;
int ASCII7only = 1;
utf8 *sx = (utf8 *)m_pBuf;
utf8 *endx = sx + m_nLen;
@ -59,8 +63,8 @@ int Utf8_16_Read::isUTF8_16()
{
if (!*sx)
{ // For detection, we'll say that NUL means not UTF8
ASCII7only=0;
rv=0;
ASCII7only = 0;
rv = 0;
break;
}
else if (*sx < 0x80)
@ -100,7 +104,11 @@ int Utf8_16_Read::isUTF8_16()
break;
}
}
return(ASCII7only?0:rv);
if (ASCII7only)
return ascii7bits;
if (rv)
return utf8NoBOM;
return ascii8bits;
}
size_t Utf8_16_Read::convert(char* buf, size_t len)
@ -122,6 +130,7 @@ size_t Utf8_16_Read::convert(char* buf, size_t len)
switch (m_eEncoding)
{
case uni7Bit:
case uni8Bit:
case uniCookie: {
// Do nothing, pass through
@ -179,17 +188,31 @@ void Utf8_16_Read::determineEncoding()
if (m_nLen > 1)
{
if (m_pBuf[0] == k_Boms[uni16BE][0] && m_pBuf[1] == k_Boms[uni16BE][1]) {
if (m_pBuf[0] == k_Boms[uni16BE][0] && m_pBuf[1] == k_Boms[uni16BE][1])
{
m_eEncoding = uni16BE;
m_nSkip = 2;
} else if (m_pBuf[0] == k_Boms[uni16LE][0] && m_pBuf[1] == k_Boms[uni16LE][1]) {
}
else if (m_pBuf[0] == k_Boms[uni16LE][0] && m_pBuf[1] == k_Boms[uni16LE][1])
{
m_eEncoding = uni16LE;
m_nSkip = 2;
} else if (m_nLen > 2 && m_pBuf[0] == k_Boms[uniUTF8][0] && m_pBuf[1] == k_Boms[uniUTF8][1] && m_pBuf[2] == k_Boms[uniUTF8][2]) {
}
else if (m_nLen > 2 && m_pBuf[0] == k_Boms[uniUTF8][0] &&
m_pBuf[1] == k_Boms[uniUTF8][1] && m_pBuf[2] == k_Boms[uniUTF8][2])
{
m_eEncoding = uniUTF8;
m_nSkip = 3;
} else if (isUTF8_16()) {
m_eEncoding = uniCookie;
}
else
{
u78 detectedEncoding = utf8_7bits_8bits();
if (detectedEncoding == utf8NoBOM)
m_eEncoding = uniCookie;
else if (detectedEncoding == ascii7bits)
m_eEncoding = uni7Bit;
else //(detectedEncoding == ascii8bits)
m_eEncoding = uni8Bit;
m_nSkip = 0;
}
}
@ -253,6 +276,7 @@ size_t Utf8_16_Write::fwrite(const void* p, size_t _size)
switch (m_eEncoding)
{
case uni7Bit:
case uni8Bit:
case uniCookie:
case uniUTF8: {
@ -301,6 +325,7 @@ size_t Utf8_16_Write::convert(char* p, size_t _size)
switch (m_eEncoding)
{
case uni7Bit:
case uni8Bit:
case uniCookie: {
// Normal write

View File

@ -102,6 +102,7 @@ protected:
};
// Reads UTF16 and outputs UTF8
enum u78 {utf8NoBOM=0, ascii7bits=1, ascii8bits=2};
class Utf8_16_Read : public Utf8_16 {
public:
Utf8_16_Read();
@ -114,7 +115,7 @@ public:
size_t calcCurPos(size_t pos);
protected:
void determineEncoding();
int isUTF8_16();
u78 utf8_7bits_8bits();
private:
UniMode m_eEncoding;
ubyte* m_pBuf;

View File

@ -129,30 +129,32 @@ BEGIN
EDITTEXT IDC_EDIT_SESSIONFILEEXT,298,164,67,14,ES_AUTOHSCROLL
END
IDD_PREFERENCE_NEWDOCSETTING_BOX DIALOGEX 0, 0, 370, 180
STYLE DS_SETFONT | DS_FIXEDSYS | WS_CHILD
FONT 8, "MS Shell Dlg", 0, 0, 0x1
BEGIN
GROUPBOX "Format",IDC_FORMAT_GB_STATIC,225,13,110,55,BS_CENTER
CONTROL "Windows",IDC_RADIO_F_WIN,"Button",BS_AUTORADIOBUTTON | WS_GROUP,233,23,48,10
CONTROL "Unix",IDC_RADIO_F_UNIX,"Button",BS_AUTORADIOBUTTON,233,36,56,10
CONTROL "Mac",IDC_RADIO_F_MAC,"Button",BS_AUTORADIOBUTTON,233,50,60,10
GROUPBOX "Encoding",IDC_ENCODING_STATIC,57,12,140,83,BS_CENTER
CONTROL "ANSI",IDC_RADIO_ANSI,"Button",BS_AUTORADIOBUTTON | WS_GROUP,64,22,80,10
CONTROL "UTF-8 without BOM",IDC_RADIO_UTF8SANSBOM,"Button",BS_AUTORADIOBUTTON,64,36,128,10
CONTROL "UTF-8",IDC_RADIO_UTF8,"Button",BS_AUTORADIOBUTTON,64,50,62,10
CONTROL "UCS-2 big endian",IDC_RADIO_UCS2BIG,"Button",BS_AUTORADIOBUTTON,64,64,103,10
CONTROL "UCS-2 small endian",IDC_RADIO_UCS2SMALL,"Button",BS_AUTORADIOBUTTON,64,78,102,10
RTEXT "Default Language :",IDC_DEFAULTLANG_STATIC,203,81,77,8
COMBOBOX IDC_COMBO_DEFAULTLANG,285,79,60,140,CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP
GROUPBOX "New Document",IDC_NEWDOCUMENT_GR_STATIC,47,2,304,104,BS_CENTER
GROUPBOX "File Open/Save Directory",IDC_OPENSAVEDIR_GR_STATIC,47,114,304,61,BS_CENTER
CONTROL "Follow the current document",IDC_OPENSAVEDIR_FOLLOWCURRENT_RADIO,"Button",BS_AUTORADIOBUTTON | WS_GROUP,64,126,200,10
CONTROL "Remember the last operation directory",IDC_OPENSAVEDIR_REMEMBERLAST_RADIO,"Button",BS_AUTORADIOBUTTON,64,141,217,10
GROUPBOX "Format",IDC_FORMAT_GB_STATIC,225,12,110,55,BS_CENTER
CONTROL "Windows",IDC_RADIO_F_WIN,"Button",BS_AUTORADIOBUTTON | WS_GROUP,233,25,48,10
CONTROL "Unix",IDC_RADIO_F_UNIX,"Button",BS_AUTORADIOBUTTON,233,38,56,10
CONTROL "Mac",IDC_RADIO_F_MAC,"Button",BS_AUTORADIOBUTTON,233,52,60,10
GROUPBOX "Encoding",IDC_ENCODING_STATIC,57,11,144,91,BS_CENTER
CONTROL "ANSI",IDC_RADIO_ANSI,"Button",BS_AUTORADIOBUTTON | WS_GROUP,64,20,80,10
CONTROL "UTF-8 without BOM",IDC_RADIO_UTF8SANSBOM,"Button",BS_AUTORADIOBUTTON,64,34,128,10
CONTROL "UTF-8",IDC_RADIO_UTF8,"Button",BS_AUTORADIOBUTTON,64,59,62,10
CONTROL "UCS-2 big endian",IDC_RADIO_UCS2BIG,"Button",BS_AUTORADIOBUTTON,64,73,103,10
CONTROL "UCS-2 small endian",IDC_RADIO_UCS2SMALL,"Button",BS_AUTORADIOBUTTON,64,87,102,10
RTEXT "Default Language :",IDC_DEFAULTLANG_STATIC,203,83,77,8
COMBOBOX IDC_COMBO_DEFAULTLANG,285,81,60,140,CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP
GROUPBOX "New Document",IDC_NEWDOCUMENT_GR_STATIC,47,2,304,108,BS_CENTER
GROUPBOX "File Open/Save Directory",IDC_OPENSAVEDIR_GR_STATIC,47,117,304,61,BS_CENTER
CONTROL "Follow the current document",IDC_OPENSAVEDIR_FOLLOWCURRENT_RADIO,
"Button",BS_AUTORADIOBUTTON | WS_GROUP,64,129,200,10
CONTROL "Remember the last operation directory",IDC_OPENSAVEDIR_REMEMBERLAST_RADIO,
"Button",BS_AUTORADIOBUTTON,64,144,217,10
CONTROL "",IDC_OPENSAVEDIR_ALWAYSON_RADIO,"Button",BS_AUTORADIOBUTTON,64,156,11,10
EDITTEXT IDC_OPENSAVEDIR_ALWAYSON_EDIT,80,154,179,14,ES_AUTOHSCROLL
PUSHBUTTON "...",IDD_OPENSAVEDIR_ALWAYSON_BROWSE_BUTTON,266,153,16,14
EDITTEXT IDC_OPENSAVEDIR_ALWAYSON_EDIT,80,157,179,14,ES_AUTOHSCROLL
PUSHBUTTON "...",IDD_OPENSAVEDIR_ALWAYSON_BROWSE_BUTTON,266,156,16,14
CONTROL "Apply while open ANSI file",IDC_CHECK_OPENANSIASUTF8,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,74,46,124,10
END
IDD_PREFERENCE_LANG_BOX DIALOGEX 0, 0, 370, 180

View File

@ -866,8 +866,9 @@ BOOL CALLBACK DefaultNewDocDlg::run_dlgProc(UINT Message, WPARAM wParam, LPARAM
default : //uni8Bit
ID2Check = IDC_RADIO_ANSI;
}
::SendDlgItemMessage(_hSelf, ID2Check, BM_SETCHECK, BST_CHECKED, 0);
::SendDlgItemMessage(_hSelf, ID2Check, BM_SETCHECK, BST_CHECKED, 0);
::SendDlgItemMessage(_hSelf, IDC_CHECK_OPENANSIASUTF8, BM_SETCHECK, (ID2Check == IDC_RADIO_UTF8SANSBOM && ndds._openAnsiAsUtf8)?BST_CHECKED:BST_UNCHECKED, 0);
::EnableWindow(::GetDlgItem(_hSelf, IDC_CHECK_OPENANSIASUTF8), ID2Check == IDC_RADIO_UTF8SANSBOM);
int index = 0;
for (int i = L_TXT ; i < pNppParam->L_END ; i++)
{
@ -939,18 +940,31 @@ BOOL CALLBACK DefaultNewDocDlg::run_dlgProc(UINT Message, WPARAM wParam, LPARAM
{
case IDC_RADIO_UCS2BIG:
ndds._encoding = uni16BE;
ndds._openAnsiAsUtf8 = false;
makeOpenAnsiAsUtf8(false);
return TRUE;
case IDC_RADIO_UCS2SMALL:
ndds._encoding = uni16LE;
ndds._openAnsiAsUtf8 = false;
makeOpenAnsiAsUtf8(false);
return TRUE;
case IDC_RADIO_UTF8:
ndds._encoding = uniUTF8;
ndds._openAnsiAsUtf8 = false;
makeOpenAnsiAsUtf8(false);
return TRUE;
case IDC_RADIO_UTF8SANSBOM:
ndds._encoding = uniCookie;
makeOpenAnsiAsUtf8(true);
return TRUE;
case IDC_RADIO_ANSI:
ndds._encoding = uni8Bit;
ndds._openAnsiAsUtf8 = false;
makeOpenAnsiAsUtf8(false);
return TRUE;
case IDC_CHECK_OPENANSIASUTF8 :
ndds._openAnsiAsUtf8 = (BST_CHECKED == ::SendMessage(::GetDlgItem(_hSelf, IDC_CHECK_OPENANSIASUTF8), BM_GETCHECK, 0, 0));
return TRUE;

View File

@ -81,6 +81,11 @@ public :
DefaultNewDocDlg() {};
private :
std::vector<LangID_Name> _langList;
void makeOpenAnsiAsUtf8(bool doIt){
if (!doIt)
::SendDlgItemMessage(_hSelf, IDC_CHECK_OPENANSIASUTF8, BM_SETCHECK, BST_UNCHECKED, 0);
::EnableWindow(::GetDlgItem(_hSelf, IDC_CHECK_OPENANSIASUTF8), doIt);
};
BOOL CALLBACK run_dlgProc(UINT Message, WPARAM wParam, LPARAM lParam);
};

View File

@ -129,6 +129,7 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#define IDC_OPENSAVEDIR_ALWAYSON_EDIT (IDD_PREFERENCE_NEWDOCSETTING_BOX + 17)
#define IDD_OPENSAVEDIR_ALWAYSON_BROWSE_BUTTON (IDD_PREFERENCE_NEWDOCSETTING_BOX + 18)
#define IDC_NEWDOCUMENT_GR_STATIC (IDD_PREFERENCE_NEWDOCSETTING_BOX + 19)
#define IDC_CHECK_OPENANSIASUTF8 (IDD_PREFERENCE_NEWDOCSETTING_BOX + 20)
#define IDD_PREFERENCE_LANG_BOX 6500 //(IDD_PREFERENCE_BOX + 500)
#define IDC_LIST_ENABLEDLANG (IDD_PREFERENCE_LANG_BOX + 1)