diff --git a/src/Dialogs.c b/src/Dialogs.c index eb31a0933..e53676fac 100644 --- a/src/Dialogs.c +++ b/src/Dialogs.c @@ -1926,11 +1926,17 @@ INT_PTR CALLBACK SelectDefEncodingDlgProc(HWND hwnd,UINT umsg,WPARAM wParam,LPAR { case IDOK: { if (Encoding_GetFromComboboxEx(GetDlgItem(hwnd,IDC_ENCODINGLIST),&pdd->idEncoding)) { - bSkipUnicodeDetection = (IsDlgButtonChecked(hwnd,IDC_NOUNICODEDETECTION) == BST_CHECKED) ? 1 : 0; - bLoadASCIIasUTF8 = (IsDlgButtonChecked(hwnd,IDC_ASCIIASUTF8) == BST_CHECKED) ? 1 : 0; - bLoadNFOasOEM = (IsDlgButtonChecked(hwnd,IDC_NFOASOEM) == BST_CHECKED) ? 1 : 0; - bNoEncodingTags = (IsDlgButtonChecked(hwnd,IDC_ENCODINGFROMFILEVARS) == BST_CHECKED) ? 1 : 0; - EndDialog(hwnd,IDOK); + if (pdd->idEncoding < 0) { + MsgBox(MBWARN,IDS_ERR_ENCODINGNA); + EndDialog(hwnd,IDCANCEL); + } + else { + bSkipUnicodeDetection = (IsDlgButtonChecked(hwnd,IDC_NOUNICODEDETECTION) == BST_CHECKED) ? 1 : 0; + bLoadASCIIasUTF8 = (IsDlgButtonChecked(hwnd,IDC_ASCIIASUTF8) == BST_CHECKED) ? 1 : 0; + bLoadNFOasOEM = (IsDlgButtonChecked(hwnd,IDC_NFOASOEM) == BST_CHECKED) ? 1 : 0; + bNoEncodingTags = (IsDlgButtonChecked(hwnd,IDC_ENCODINGFROMFILEVARS) == BST_CHECKED) ? 1 : 0; + EndDialog(hwnd,IDOK); + } } else PostMessage(hwnd,WM_NEXTDLGCTL,(WPARAM)(GetDlgItem(hwnd,IDC_ENCODINGLIST)),1); @@ -2078,10 +2084,17 @@ INT_PTR CALLBACK SelectEncodingDlgProc(HWND hwnd,UINT umsg,WPARAM wParam,LPARAM { case IDOK: - if (Encoding_GetFromListView(hwndLV,&pdd->idEncoding)) - EndDialog(hwnd,IDOK); + if (Encoding_GetFromListView(hwndLV,&pdd->idEncoding)) { + if (pdd->idEncoding < 0) { + MsgBox(MBWARN,IDS_ERR_ENCODINGNA); + EndDialog(hwnd,IDCANCEL); + } + else + EndDialog(hwnd,IDOK); + } else PostMessage(hwnd,WM_NEXTDLGCTL,(WPARAM)(GetDlgItem(hwnd,IDC_ENCODINGLIST)),1); + break; diff --git a/src/Edit.c b/src/Edit.c index c9a0aa109..17ac1e93e 100644 --- a/src/Edit.c +++ b/src/Edit.c @@ -72,7 +72,6 @@ extern BOOL bAutoStripBlanks; // Default Codepage and Character Set extern int iDefaultEncoding; extern int iDefaultCharSet; -extern BOOL bSkipUnicodeDetection; extern BOOL bLoadASCIIasUTF8; extern BOOL bLoadNFOasOEM; @@ -81,6 +80,8 @@ extern BOOL bVirtualSpaceInRectSelection; extern int iMarkOccurrencesCount; extern int iMarkOccurrencesMaxCount; +extern NP2ENCODING mEncoding[]; + #define DELIM_BUFFER 258 char DelimChars[DELIM_BUFFER] = { '\0' }; char DelimCharsAccel[DELIM_BUFFER] = { '\0' }; @@ -111,152 +112,6 @@ enum SortOrderMask { SORT_COLUMN = 128 }; -int g_DOSEncoding; - -// Supported Encodings -WCHAR wchANSI[16] = { L'\0'}; -WCHAR wchOEM[16] = { L'\0' }; - -NP2ENCODING mEncoding[] = { - { NCP_ANSI | NCP_RECODE, CP_ACP, "ansi,system,ascii,", 61000, L"" }, - { NCP_OEM | NCP_RECODE, CP_OEMCP, "oem,oem,", 61001, L"" }, - { NCP_UNICODE | NCP_UNICODE_BOM, CP_UTF8, "", 61002, L"" }, - { NCP_UNICODE | NCP_UNICODE_REVERSE | NCP_UNICODE_BOM, CP_UTF8, "", 61003, L"" }, - { NCP_UNICODE | NCP_RECODE, CP_UTF8, "utf-16,utf16,unicode,", 61004, L"" }, - { NCP_UNICODE | NCP_UNICODE_REVERSE | NCP_RECODE, CP_UTF8, "utf-16be,utf16be,unicodebe,", 61005, L"" }, - { NCP_UTF8 | NCP_RECODE, CP_UTF8, "utf-8,utf8,", 61006, L"" }, - { NCP_UTF8 | NCP_UTF8_SIGN, CP_UTF8, "utf-8,utf8,", 61007, L"" }, - { NCP_8BIT | NCP_RECODE, CP_UTF7, "utf-7,utf7,", 61008, L"" }, - { NCP_8BIT | NCP_RECODE, 720, "DOS-720,dos720,", 61009, L"" }, - { NCP_8BIT | NCP_RECODE, 28596, "iso-8859-6,iso88596,arabic,csisolatinarabic,ecma114,isoir127,", 61010, L"" }, - { NCP_8BIT | NCP_RECODE, 10004, "x-mac-arabic,xmacarabic,", 61011, L"" }, - { NCP_8BIT | NCP_RECODE, 1256, "windows-1256,windows1256,cp1256", 61012, L"" }, - { NCP_8BIT | NCP_RECODE, 775, "ibm775,ibm775,cp500,", 61013, L"" }, - { NCP_8BIT | NCP_RECODE, 28594, "iso-8859-4,iso88594,csisolatin4,isoir110,l4,latin4,", 61014, L"" }, - { NCP_8BIT | NCP_RECODE, 1257, "windows-1257,windows1257,", 61015, L"" }, - { NCP_8BIT | NCP_RECODE, 852, "ibm852,ibm852,cp852,", 61016, L"" }, - { NCP_8BIT | NCP_RECODE, 28592, "iso-8859-2,iso88592,csisolatin2,isoir101,latin2,l2,", 61017, L"" }, - { NCP_8BIT | NCP_RECODE, 10029, "x-mac-ce,xmacce,", 61018, L"" }, - { NCP_8BIT | NCP_RECODE, 1250, "windows-1250,windows1250,xcp1250,", 61019, L"" }, - { NCP_8BIT | NCP_RECODE, 936, "gb2312,gb2312,chinese,cngb,csgb2312,csgb231280,gb231280,gbk,", 61020, L"" }, - { NCP_8BIT | NCP_RECODE, 10008, "x-mac-chinesesimp,xmacchinesesimp,", 61021, L"" }, - { NCP_8BIT | NCP_RECODE, 950, "big5,big5,cnbig5,csbig5,xxbig5,", 61022, L"" }, - { NCP_8BIT | NCP_RECODE, 10002, "x-mac-chinesetrad,xmacchinesetrad,", 61023, L"" }, - { NCP_8BIT | NCP_RECODE, 10082, "x-mac-croatian,xmaccroatian,", 61024, L"" }, - { NCP_8BIT | NCP_RECODE, 866, "cp866,cp866,ibm866,", 61025, L"" }, - { NCP_8BIT | NCP_RECODE, 28595, "iso-8859-5,iso88595,csisolatin5,csisolatincyrillic,cyrillic,isoir144,", 61026, L"" }, - { NCP_8BIT | NCP_RECODE, 20866, "koi8-r,koi8r,cskoi8r,koi,koi8,", 61027, L"" }, - { NCP_8BIT | NCP_RECODE, 21866, "koi8-u,koi8u,koi8ru,", 61028, L"" }, - { NCP_8BIT | NCP_RECODE, 10007, "x-mac-cyrillic,xmaccyrillic,", 61029, L"" }, - { NCP_8BIT | NCP_RECODE, 1251, "windows-1251,windows1251,xcp1251,", 61030, L"" }, - { NCP_8BIT | NCP_RECODE, 28603, "iso-8859-13,iso885913,", 61031, L"" }, - { NCP_8BIT | NCP_RECODE, 863, "ibm863,ibm863,", 61032, L"" }, - { NCP_8BIT | NCP_RECODE, 737, "ibm737,ibm737,", 61033, L"" }, - { NCP_8BIT | NCP_RECODE, 28597, "iso-8859-7,iso88597,csisolatingreek,ecma118,elot928,greek,greek8,isoir126,", 61034, L"" }, - { NCP_8BIT | NCP_RECODE, 10006, "x-mac-greek,xmacgreek,", 61035, L"" }, - { NCP_8BIT | NCP_RECODE, 1253, "windows-1253,windows1253,", 61036, L"" }, - { NCP_8BIT | NCP_RECODE, 869, "ibm869,ibm869,", 61037, L"" }, - { NCP_8BIT | NCP_RECODE, 862, "DOS-862,dos862,", 61038, L"" }, - { NCP_8BIT | NCP_RECODE, 38598, "iso-8859-8-i,iso88598i,logical,", 61039, L"" }, - { NCP_8BIT | NCP_RECODE, 28598, "iso-8859-8,iso88598,csisolatinhebrew,hebrew,isoir138,visual,", 61040, L"" }, - { NCP_8BIT | NCP_RECODE, 10005, "x-mac-hebrew,xmachebrew,", 61041, L"" }, - { NCP_8BIT | NCP_RECODE, 1255, "windows-1255,windows1255,", 61042, L"" }, - { NCP_8BIT | NCP_RECODE, 861, "ibm861,ibm861,", 61043, L"" }, - { NCP_8BIT | NCP_RECODE, 10079, "x-mac-icelandic,xmacicelandic,", 61044, L"" }, - { NCP_8BIT | NCP_RECODE, 10001, "x-mac-japanese,xmacjapanese,", 61045, L"" }, - { NCP_8BIT | NCP_RECODE, 932, "shift_jis,shiftjis,shiftjs,csshiftjis,cswindows31j,mskanji,xmscp932,xsjis,", 61046, L"" }, - { NCP_8BIT | NCP_RECODE, 10003, "x-mac-korean,xmackorean,", 61047, L"" }, - { NCP_8BIT | NCP_RECODE, 949, "windows-949,windows949,ksc56011987,csksc5601,euckr,isoir149,korean,ksc56011989", 61048, L"" }, - { NCP_8BIT | NCP_RECODE, 28593, "iso-8859-3,iso88593,latin3,isoir109,l3,", 61049, L"" }, - { NCP_8BIT | NCP_RECODE, 28605, "iso-8859-15,iso885915,latin9,l9,", 61050, L"" }, - { NCP_8BIT | NCP_RECODE, 865, "ibm865,ibm865,", 61051, L"" }, - { NCP_8BIT | NCP_RECODE, 437, "ibm437,ibm437,437,cp437,cspc8,codepage437,", 61052, L"" }, - { NCP_8BIT | NCP_RECODE, 858, "ibm858,ibm858,ibm00858,", 61053, L"" }, - { NCP_8BIT | NCP_RECODE, 860, "ibm860,ibm860,", 61054, L"" }, - { NCP_8BIT | NCP_RECODE, 10010, "x-mac-romanian,xmacromanian,", 61055, L"" }, - { NCP_8BIT | NCP_RECODE, 10021, "x-mac-thai,xmacthai,", 61056, L"" }, - { NCP_8BIT | NCP_RECODE, 874, "windows-874,windows874,dos874,iso885911,tis620,", 61057, L"" }, - { NCP_8BIT | NCP_RECODE, 857, "ibm857,ibm857,", 61058, L"" }, - { NCP_8BIT | NCP_RECODE, 28599, "iso-8859-9,iso88599,latin5,isoir148,l5,", 61059, L"" }, - { NCP_8BIT | NCP_RECODE, 10081, "x-mac-turkish,xmacturkish,", 61060, L"" }, - { NCP_8BIT | NCP_RECODE, 1254, "windows-1254,windows1254,", 61061, L"" }, - { NCP_8BIT | NCP_RECODE, 10017, "x-mac-ukrainian,xmacukrainian,", 61062, L"" }, - { NCP_8BIT | NCP_RECODE, 1258, "windows-1258,windows-258,", 61063, L"" }, - { NCP_8BIT | NCP_RECODE, 850, "ibm850,ibm850,", 61064, L"" }, - { NCP_8BIT | NCP_RECODE, 28591, "iso-8859-1,iso88591,cp819,latin1,ibm819,isoir100,latin1,l1,", 61065, L"" }, - { NCP_8BIT | NCP_RECODE, 10000, "macintosh,macintosh,", 61066, L"" }, - { NCP_8BIT | NCP_RECODE, 1252, "windows-1252,windows1252,cp367,cp819,ibm367,us,xansi,", 61067, L"" }, - { NCP_8BIT | NCP_RECODE, 37, "ebcdic-cp-us,ebcdiccpus,ebcdiccpca,ebcdiccpwt,ebcdiccpnl,ibm037,cp037,", 61068, L"" }, - { NCP_8BIT | NCP_RECODE, 500, "x-ebcdic-international,xebcdicinternational,", 61069, L"" }, - { NCP_8BIT | NCP_RECODE, 875, "x-EBCDIC-GreekModern,xebcdicgreekmodern,", 61070, L"" }, - { NCP_8BIT | NCP_RECODE, 1026, "CP1026,cp1026,csibm1026,ibm1026,", 61071, L"" }, - //{ NCP_8BIT|NCP_RECODE, 870, "CP870,cp870,ebcdiccproece,ebcdiccpyu,csibm870,ibm870,", 00000, L"" }, // IBM EBCDIC (Multilingual Latin-2) - //{ NCP_8BIT|NCP_RECODE, 1047, "IBM01047,ibm01047,", 00000, L"" }, // IBM EBCDIC (Open System Latin-1) - //{ NCP_8BIT|NCP_RECODE, 1140, "x-ebcdic-cp-us-euro,xebcdiccpuseuro,", 00000, L"" }, // IBM EBCDIC (US-Canada-Euro) - //{ NCP_8BIT|NCP_RECODE, 1141, "x-ebcdic-germany-euro,xebcdicgermanyeuro,", 00000, L"" }, // IBM EBCDIC (Germany-Euro) - //{ NCP_8BIT|NCP_RECODE, 1142, "x-ebcdic-denmarknorway-euro,xebcdicdenmarknorwayeuro,", 00000, L"" }, // IBM EBCDIC (Denmark-Norway-Euro) - //{ NCP_8BIT|NCP_RECODE, 1143, "x-ebcdic-finlandsweden-euro,xebcdicfinlandswedeneuro,", 00000, L"" }, // IBM EBCDIC (Finland-Sweden-Euro) - //{ NCP_8BIT|NCP_RECODE, 1144, "x-ebcdic-italy-euro,xebcdicitalyeuro,", 00000, L"" }, // IBM EBCDIC (Italy-Euro) - //{ NCP_8BIT|NCP_RECODE, 1145, "x-ebcdic-spain-euro,xebcdicspaineuro,", 00000, L"" }, // IBM EBCDIC (Spain-Latin America-Euro) - //{ NCP_8BIT|NCP_RECODE, 1146, "x-ebcdic-uk-euro,xebcdicukeuro,", 00000, L"" }, // IBM EBCDIC (UK-Euro) - //{ NCP_8BIT|NCP_RECODE, 1147, "x-ebcdic-france-euro,xebcdicfranceeuro,", 00000, L"" }, // IBM EBCDIC (France-Euro) - //{ NCP_8BIT|NCP_RECODE, 1148, "x-ebcdic-international-euro,xebcdicinternationaleuro,", 00000, L"" }, // IBM EBCDIC (International-Euro) - //{ NCP_8BIT|NCP_RECODE, 1149, "x-ebcdic-icelandic-euro,xebcdicicelandiceuro,", 00000, L"" }, // IBM EBCDIC (Icelandic-Euro) - //{ NCP_8BIT|NCP_RECODE, 1361, "johab,johab,", 00000, L"" }, // Korean (Johab) - //{ NCP_8BIT|NCP_RECODE, 20273, "x-EBCDIC-Germany,xebcdicgermany,", 00000, L"" }, // IBM EBCDIC (Germany) - //{ NCP_8BIT|NCP_RECODE, 20277, "x-EBCDIC-DenmarkNorway,xebcdicdenmarknorway,ebcdiccpdk,ebcdiccpno,", 00000, L"" }, // IBM EBCDIC (Denmark-Norway) - //{ NCP_8BIT|NCP_RECODE, 20278, "x-EBCDIC-FinlandSweden,xebcdicfinlandsweden,ebcdicpfi,ebcdiccpse,", 00000, L"" }, // IBM EBCDIC (Finland-Sweden) - //{ NCP_8BIT|NCP_RECODE, 20280, "x-EBCDIC-Italy,xebcdicitaly,", 00000, L"" }, // IBM EBCDIC (Italy) - //{ NCP_8BIT|NCP_RECODE, 20284, "x-EBCDIC-Spain,xebcdicspain,ebcdiccpes,", 00000, L"" }, // IBM EBCDIC (Spain-Latin America) - //{ NCP_8BIT|NCP_RECODE, 20285, "x-EBCDIC-UK,xebcdicuk,ebcdiccpgb,", 00000, L"" }, // IBM EBCDIC (UK) - //{ NCP_8BIT|NCP_RECODE, 20290, "x-EBCDIC-JapaneseKatakana,xebcdicjapanesekatakana,", 00000, L"" }, // IBM EBCDIC (Japanese Katakana) - //{ NCP_8BIT|NCP_RECODE, 20297, "x-EBCDIC-France,xebcdicfrance,ebcdiccpfr,", 00000, L"" }, // IBM EBCDIC (France) - //{ NCP_8BIT|NCP_RECODE, 20420, "x-EBCDIC-Arabic,xebcdicarabic,ebcdiccpar1,", 00000, L"" }, // IBM EBCDIC (Arabic) - //{ NCP_8BIT|NCP_RECODE, 20423, "x-EBCDIC-Greek,xebcdicgreek,ebcdiccpgr,", 00000, L"" }, // IBM EBCDIC (Greek) - //{ NCP_8BIT|NCP_RECODE, 20424, "x-EBCDIC-Hebrew,xebcdichebrew,ebcdiccphe,", 00000, L"" }, // IBM EBCDIC (Hebrew) - //{ NCP_8BIT|NCP_RECODE, 20833, "x-EBCDIC-KoreanExtended,xebcdickoreanextended,", 00000, L"" }, // IBM EBCDIC (Korean Extended) - //{ NCP_8BIT|NCP_RECODE, 20838, "x-EBCDIC-Thai,xebcdicthai,ibmthai,csibmthai,", 00000, L"" }, // IBM EBCDIC (Thai) - //{ NCP_8BIT|NCP_RECODE, 20871, "x-EBCDIC-Icelandic,xebcdicicelandic,ebcdiccpis,", 00000, L"" }, // IBM EBCDIC (Icelandic) - //{ NCP_8BIT|NCP_RECODE, 20880, "x-EBCDIC-CyrillicRussian,xebcdiccyrillicrussian,ebcdiccyrillic,", 00000, L"" }, // IBM EBCDIC (Cyrillic Russian) - //{ NCP_8BIT|NCP_RECODE, 20905, "x-EBCDIC-Turkish,xebcdicturkish,ebcdiccptr,", 00000, L"" }, // IBM EBCDIC (Turkish) - //{ NCP_8BIT|NCP_RECODE, 20924, "IBM00924,ibm00924,ebcdiclatin9euro,", 00000, L"" }, // IBM EBCDIC (Open System-Euro Latin-1) - //{ NCP_8BIT|NCP_RECODE, 21025, "x-EBCDIC-CyrillicSerbianBulgarian,xebcdiccyrillicserbianbulgarian,", 00000, L"" }, // IBM EBCDIC (Cyrillic Serbian-Bulgarian) - //{ NCP_8BIT|NCP_RECODE, 50930, "x-EBCDIC-JapaneseAndKana,xebcdicjapaneseandkana,", 00000, L"" }, // IBM EBCDIC (Japanese and Japanese Katakana) - //{ NCP_8BIT|NCP_RECODE, 50931, "x-EBCDIC-JapaneseAndUSCanada,xebcdicjapaneseanduscanada,", 00000, L"" }, // IBM EBCDIC (Japanese and US-Canada) - //{ NCP_8BIT|NCP_RECODE, 50933, "x-EBCDIC-KoreanAndKoreanExtended,xebcdickoreanandkoreanextended,", 00000, L"" }, // IBM EBCDIC (Korean and Korean Extended) - //{ NCP_8BIT|NCP_RECODE, 50935, "x-EBCDIC-SimplifiedChinese,xebcdicsimplifiedchinese,", 00000, L"" }, // IBM EBCDIC (Chinese Simplified) - //{ NCP_8BIT|NCP_RECODE, 50937, "x-EBCDIC-TraditionalChinese,xebcdictraditionalchinese,", 00000, L"" }, // IBM EBCDIC (Chinese Traditional) - //{ NCP_8BIT|NCP_RECODE, 50939, "x-EBCDIC-JapaneseAndJapaneseLatin,xebcdicjapaneseandjapaneselatin,", 00000, L"" }, // IBM EBCDIC (Japanese and Japanese-Latin) - //{ NCP_8BIT|NCP_RECODE, 20105, "x-IA5,xia5,", 00000, L"" }, // Western European (IA5) - //{ NCP_8BIT|NCP_RECODE, 20106, "x-IA5-German,xia5german,", 00000, L"" }, // German (IA5) - //{ NCP_8BIT|NCP_RECODE, 20107, "x-IA5-Swedish,xia5swedish,", 00000, L"" }, // Swedish (IA5) - //{ NCP_8BIT|NCP_RECODE, 20108, "x-IA5-Norwegian,xia5norwegian,", 00000, L"" }, // Norwegian (IA5) - //{ NCP_8BIT|NCP_RECODE, 20936, "x-cp20936,xcp20936,", 00000, L"" }, // Chinese Simplified (GB2312) - //{ NCP_8BIT|NCP_RECODE, 20932, "euc-jp,,", 00000, L"" }, // Japanese (JIS X 0208-1990 & 0212-1990) - //{ NCP_8BIT|NCP_RECODE, 50220, "iso-2022-jp,iso2022jp,", 00000, L"" }, // Japanese (JIS) - //{ NCP_8BIT|NCP_RECODE, 50221, "csISO2022JP,csiso2022jp,", 00000, L"" }, // Japanese (JIS-Allow 1 byte Kana) - //{ NCP_8BIT|NCP_RECODE, 50222, "_iso-2022-jp$SIO,iso2022jpSIO,", 00000, L"" }, // Japanese (JIS-Allow 1 byte Kana - SO/SI) - //{ NCP_8BIT|NCP_RECODE, 50225, "iso-2022-kr,iso2022kr,csiso2022kr,", 00000, L"" }, // Korean (ISO-2022-KR) - //{ NCP_8BIT|NCP_RECODE, 50227, "x-cp50227,xcp50227,", 00000, L"" }, // Chinese Simplified (ISO-2022) - //{ NCP_8BIT|NCP_RECODE, 50229, "iso-2022-cn,iso2022cn,", 00000, L"" }, // Chinese Traditional (ISO-2022) - //{ NCP_8BIT|NCP_RECODE, 20000, "x-Chinese-CNS,xchinesecns,", 00000, L"" }, // Chinese Traditional (CNS) - //{ NCP_8BIT|NCP_RECODE, 20002, "x-Chinese-Eten,xchineseeten,", 00000, L"" }, // Chinese Traditional (Eten) - //{ NCP_8BIT|NCP_RECODE, 51932, "euc-jp,eucjp,xeuc,xeucjp,", 00000, L"" }, // Japanese (EUC) - //{ NCP_8BIT|NCP_RECODE, 51936, "euc-cn,euccn,xeuccn,", 00000, L"" }, // Chinese Simplified (EUC) - //{ NCP_8BIT|NCP_RECODE, 51949, "euc-kr,euckr,cseuckr,", 00000, L"" }, // Korean (EUC) - //{ NCP_8BIT|NCP_RECODE, 52936, "hz-gb-2312,hzgb2312,hz,", 00000, L"" }, // Chinese Simplified (HZ-GB2312) - { NCP_8BIT | NCP_RECODE, 54936, "gb18030,gb18030,", 61072, L"" } // Chinese Simplified (GB18030) - //{ NCP_8BIT|NCP_RECODE, 57002, "x-iscii-de,xisciide,", 00000, L"" }, // ISCII Devanagari - //{ NCP_8BIT|NCP_RECODE, 57003, "x-iscii-be,xisciibe,", 00000, L"" }, // ISCII Bengali - //{ NCP_8BIT|NCP_RECODE, 57004, "x-iscii-ta,xisciita,", 00000, L"" }, // ISCII Tamil - //{ NCP_8BIT|NCP_RECODE, 57005, "x-iscii-te,xisciite,", 00000, L"" }, // ISCII Telugu - //{ NCP_8BIT|NCP_RECODE, 57006, "x-iscii-as,xisciias,", 00000, L"" }, // ISCII Assamese - //{ NCP_8BIT|NCP_RECODE, 57007, "x-iscii-or,xisciior,", 00000, L"" }, // ISCII Oriya - //{ NCP_8BIT|NCP_RECODE, 57008, "x-iscii-ka,xisciika,", 00000, L"" }, // ISCII Kannada - //{ NCP_8BIT|NCP_RECODE, 57009, "x-iscii-ma,xisciima,", 00000, L"" }, // ISCII Malayalam - //{ NCP_8BIT|NCP_RECODE, 57010, "x-iscii-gu,xisciigu,", 00000, L"" }, // ISCII Gujarathi - //{ NCP_8BIT|NCP_RECODE, 57011, "x-iscii-pa,xisciipa,", 00000, L"" }, // ISCII Panjabi -}; extern LPMRULIST mruFind; @@ -825,758 +680,6 @@ int EditDetectEOLMode(HWND hwnd,char* lpData,DWORD cbData) -//============================================================================= -// -// Encoding Helper Functions -// - -int Encoding_Current(int iEncoding) -{ - static int CurrentEncoding = CPI_NONE; - - if (iEncoding >= 0) { - if (Encoding_IsValid(iEncoding)) - CurrentEncoding = iEncoding; - else - CurrentEncoding = CPI_UTF8; - } - return CurrentEncoding; -} - - -int Encoding_Source(int iSrcEncoding) -{ - static int SourceEncoding = CPI_NONE; - - if (iSrcEncoding >= 0) { - if (Encoding_IsValid(iSrcEncoding)) - SourceEncoding = iSrcEncoding; - else - SourceEncoding = CPI_UTF8; - } - else if (iSrcEncoding == CPI_NONE) { - SourceEncoding = CPI_NONE; - } - return SourceEncoding; -} - - -int Encoding_SrcWeak(int iSrcWeakEnc) -{ - static int SourceWeakEncoding = CPI_NONE; - - if (iSrcWeakEnc >= 0) { - if (Encoding_IsValid(iSrcWeakEnc)) - SourceWeakEncoding = iSrcWeakEnc; - else - SourceWeakEncoding = CPI_UTF8; - } - else if (iSrcWeakEnc == CPI_NONE) { - SourceWeakEncoding = CPI_NONE; - } - return SourceWeakEncoding; -} - - -BOOL Encoding_HasChanged(int iOriginalEncoding) -{ - static int OriginalEncoding = CPI_NONE; - - if (iOriginalEncoding >= CPI_NONE) { - OriginalEncoding = iOriginalEncoding; - } - return (BOOL)(OriginalEncoding != Encoding_Current(CPI_GET)); -} - - -void Encoding_InitDefaults() -{ - const UINT uCodePageMBCS[20] = { - 42, // (Symbol) - 50220,50221,50222,50225,50227,50229, // (Chinese, Japanese, Korean) - 54936, // (GB18030) - 57002,57003,57004,57005,57006,57007,57008,57009,57010,57011, // (ISCII) - 65000, // (UTF-7) - 65001 // (UTF-8) - }; - - mEncoding[CPI_ANSI_DEFAULT].uCodePage = GetACP(); // set ANSI system CP - StringCchPrintf(wchANSI,COUNTOF(wchANSI),L" (CP-%u)",mEncoding[CPI_ANSI_DEFAULT].uCodePage); - - for (int i = CPI_UTF7 + 1; i < COUNTOF(mEncoding); ++i) { - if (Encoding_IsValid(i) && (mEncoding[i].uCodePage == mEncoding[CPI_ANSI_DEFAULT].uCodePage)) { - mEncoding[i].uFlags |= NCP_ANSI; - if (mEncoding[i].uFlags & NCP_8BIT) - mEncoding[CPI_ANSI_DEFAULT].uFlags |= NCP_8BIT; - break; - } - } - - mEncoding[CPI_OEM].uCodePage = GetOEMCP(); - StringCchPrintf(wchOEM, COUNTOF(wchOEM), L" (CP-%u)", mEncoding[CPI_OEM].uCodePage); - - for (int i = CPI_UTF7 + 1; i < COUNTOF(mEncoding); ++i) { - if (Encoding_IsValid(i) && (mEncoding[i].uCodePage == mEncoding[CPI_OEM].uCodePage)) { - mEncoding[i].uFlags |= NCP_OEM; - if (mEncoding[i].uFlags & NCP_8BIT) - mEncoding[CPI_OEM].uFlags |= NCP_8BIT; - break; - } - } - - // multi byte character sets - for (int i = 0; i < COUNTOF(mEncoding); ++i) { - for (int k = 0; k < COUNTOF(uCodePageMBCS); k++) { - if (mEncoding[i].uCodePage == uCodePageMBCS[k]) { - mEncoding[i].uFlags |= NCP_MBCS; - } - } - } - - g_DOSEncoding = CPI_OEM; - // Try to set the DOS encoding to DOS-437 if the default OEMCP is not DOS-437 - if (mEncoding[g_DOSEncoding].uCodePage != 437) - { - for (int i = CPI_UTF7 + 1; i < COUNTOF(mEncoding); ++i) { - if (Encoding_IsValid(i) && (mEncoding[i].uCodePage == 437)) { - g_DOSEncoding = i; - break; - } - } - } -} - - -int Encoding_MapIniSetting(BOOL bLoad,int iSetting) { - if (bLoad) { - switch (iSetting) { - case -1: return CPI_NONE; - case 0: return CPI_ANSI_DEFAULT; - case 1: return CPI_UNICODEBOM; - case 2: return CPI_UNICODEBEBOM; - case 3: return CPI_UTF8; - case 4: return CPI_UTF8SIGN; - case 5: return CPI_OEM; - case 6: return CPI_UNICODE; - case 7: return CPI_UNICODEBE; - case 8: return CPI_UTF7; - default: { - for (int i = CPI_UTF7 + 1; i < COUNTOF(mEncoding); i++) { - if ((mEncoding[i].uCodePage == (UINT)iSetting) && Encoding_IsValid(i)) - return(i); - } - return CPI_ANSI_DEFAULT; - } - } - } - else { - switch (iSetting) { - case CPI_NONE: return -1; - case CPI_ANSI_DEFAULT: return 0; - case CPI_UNICODEBOM: return 1; - case CPI_UNICODEBEBOM: return 2; - case CPI_UTF8: return 3; - case CPI_UTF8SIGN: return 4; - case CPI_OEM: return 5; - case CPI_UNICODE: return 6; - case CPI_UNICODEBE: return 7; - case CPI_UTF7: return 8; - default: { - if (Encoding_IsValid(iSetting)) - return(mEncoding[iSetting].uCodePage); - else - return CPI_ANSI_DEFAULT; - } - } - } -} - - -void Encoding_GetLabel(int iEncoding) -{ - if (mEncoding[iEncoding].wchLabel[0] == L'\0') - { - WCHAR wch1[128] = { L'\0' }; - WCHAR wch2[128] = { L'\0' }; - GetString(mEncoding[iEncoding].idsName,wch1,COUNTOF(wch1)); - WCHAR *pwsz = StrChr(wch1, L';'); - if (pwsz) { - pwsz = StrChr(CharNext(pwsz), L';'); - if (pwsz) { - pwsz = CharNext(pwsz); - } - } - if (!pwsz) - pwsz = wch1; - - StringCchCopyN(wch2, COUNTOF(wch2), pwsz, COUNTOF(wch1)); - - if (Encoding_IsANSI(iEncoding)) - StringCchCatN(wch2, COUNTOF(wch2), wchANSI, COUNTOF(wchANSI)); - else if (Encoding_IsOEM(iEncoding)) - StringCchCatN(wch2, COUNTOF(wch2), wchOEM, COUNTOF(wchOEM)); - - StringCchCopyN(mEncoding[iEncoding].wchLabel,COUNTOF(mEncoding[iEncoding].wchLabel), - wch2,COUNTOF(mEncoding[iEncoding].wchLabel)); - } -} - - -int Encoding_MatchW(LPCWSTR pwszTest) { - char tchTest[256] = { '\0' }; - WideCharToMultiByteStrg(CP_ACP,pwszTest,tchTest); - return(Encoding_MatchA(tchTest)); -} - - -int Encoding_MatchA(char *pchTest) { - char chTest[256] = { '\0' }; - char *pchSrc = pchTest; - char *pchDst = chTest; - *pchDst++ = ','; - while (*pchSrc) { - if (IsCharAlphaNumericA(*pchSrc)) - *pchDst++ = *CharLowerA(pchSrc); - pchSrc++; - } - *pchDst++ = ','; - *pchDst = 0; - for (int i = 0; i < COUNTOF(mEncoding); i++) { - if (StrStrIA(mEncoding[i].pszParseNames,chTest)) { - CPINFO cpi; - if ((mEncoding[i].uFlags & NCP_INTERNAL) || - IsValidCodePage(mEncoding[i].uCodePage) && - GetCPInfo(mEncoding[i].uCodePage,&cpi)) - return(i); - else - return(-1); - } - } - return(-1); -} - - -int Encoding_GetByCodePage(UINT cp) -{ - for (int i = 0; i < COUNTOF(mEncoding); i++) { - if (cp == mEncoding[i].uCodePage) { - return i; - } - } - return CPI_ANSI_DEFAULT; -} - - -BOOL Encoding_IsValid(int iTestEncoding) { - CPINFO cpi; - if ((iTestEncoding >= 0) && (iTestEncoding < COUNTOF(mEncoding))) { - if ((mEncoding[iTestEncoding].uFlags & NCP_INTERNAL) || - IsValidCodePage(mEncoding[iTestEncoding].uCodePage) && - GetCPInfo(mEncoding[iTestEncoding].uCodePage,&cpi)) { - return(TRUE); - } - } - return(FALSE); -} - - -typedef struct _ee { - int id; - WCHAR wch[256]; -} ENCODINGENTRY, *PENCODINGENTRY; - -int CmpEncoding(const void *s1, const void *s2) { - return StrCmp(((PENCODINGENTRY)s1)->wch,((PENCODINGENTRY)s2)->wch); -} - -void Encoding_AddToListView(HWND hwnd,int idSel,BOOL bRecodeOnly) -{ - int i; - int iSelItem = -1; - LVITEM lvi; - WCHAR wchBuf[256] = { L'\0' }; - - PENCODINGENTRY pEE = LocalAlloc(LPTR,COUNTOF(mEncoding) * sizeof(ENCODINGENTRY)); - for (i = 0; i < COUNTOF(mEncoding); i++) { - pEE[i].id = i; - GetString(mEncoding[i].idsName,pEE[i].wch,COUNTOF(pEE[i].wch)); - } - qsort(pEE,COUNTOF(mEncoding),sizeof(ENCODINGENTRY),CmpEncoding); - - ZeroMemory(&lvi,sizeof(LVITEM)); - lvi.mask = LVIF_PARAM | LVIF_TEXT | LVIF_IMAGE; - lvi.pszText = wchBuf; - - for (i = 0; i < COUNTOF(mEncoding); i++) { - - int id = pEE[i].id; - if (!bRecodeOnly || (mEncoding[id].uFlags & NCP_RECODE)) { - - lvi.iItem = ListView_GetItemCount(hwnd); - - WCHAR *pwsz = StrChr(pEE[i].wch, L';'); - if (pwsz) { - StringCchCopyN(wchBuf,COUNTOF(wchBuf),CharNext(pwsz),COUNTOF(wchBuf)); - pwsz = StrChr(wchBuf, L';'); - if (pwsz) - *pwsz = 0; - } - else - StringCchCopyN(wchBuf,COUNTOF(wchBuf),pEE[i].wch,COUNTOF(wchBuf)); - - if (Encoding_IsANSI(id)) - StringCchCatN(wchBuf,COUNTOF(wchBuf),wchANSI,COUNTOF(wchANSI)); - else if (Encoding_IsOEM(id)) - StringCchCatN(wchBuf,COUNTOF(wchBuf),wchOEM,COUNTOF(wchOEM)); - - if (Encoding_IsValid(id)) - lvi.iImage = 0; - else - lvi.iImage = 1; - - lvi.lParam = (LPARAM)id; - ListView_InsertItem(hwnd,&lvi); - - if (idSel == id) - iSelItem = lvi.iItem; - } - } - - LocalFree(pEE); - - if (iSelItem != -1) { - ListView_SetItemState(hwnd,iSelItem,LVIS_SELECTED|LVIS_FOCUSED,LVIS_SELECTED|LVIS_FOCUSED); - ListView_EnsureVisible(hwnd,iSelItem,FALSE); - } - else { - ListView_SetItemState(hwnd,0,LVIS_FOCUSED,LVIS_FOCUSED); - ListView_EnsureVisible(hwnd,0,FALSE); - } -} - - -BOOL Encoding_GetFromListView(HWND hwnd,int *pidEncoding) -{ - LVITEM lvi; - - lvi.iItem = ListView_GetNextItem(hwnd,-1,LVNI_ALL | LVNI_SELECTED); - lvi.iSubItem = 0; - lvi.mask = LVIF_PARAM; - - if (ListView_GetItem(hwnd,&lvi)) { - if (Encoding_IsValid((int)lvi.lParam)) { - *pidEncoding = (int)lvi.lParam; - return (TRUE); - } - else - MsgBox(MBWARN,IDS_ERR_ENCODINGNA); - } - return(FALSE); -} - - -void Encoding_AddToComboboxEx(HWND hwnd,int idSel,BOOL bRecodeOnly) -{ - int i; - int iSelItem = -1; - COMBOBOXEXITEM cbei; - WCHAR wchBuf[256] = { L'\0' }; - - PENCODINGENTRY pEE = LocalAlloc(LPTR,COUNTOF(mEncoding) * sizeof(ENCODINGENTRY)); - for (i = 0; i < COUNTOF(mEncoding); i++) { - pEE[i].id = i; - GetString(mEncoding[i].idsName,pEE[i].wch,COUNTOF(pEE[i].wch)); - } - qsort(pEE,COUNTOF(mEncoding),sizeof(ENCODINGENTRY),CmpEncoding); - - ZeroMemory(&cbei,sizeof(COMBOBOXEXITEM)); - cbei.mask = CBEIF_TEXT | CBEIF_IMAGE | CBEIF_SELECTEDIMAGE | CBEIF_LPARAM; - cbei.pszText = wchBuf; - cbei.cchTextMax = COUNTOF(wchBuf); - cbei.iImage = 0; - cbei.iSelectedImage = 0; - - for (i = 0; i < COUNTOF(mEncoding); i++) { - - int id = pEE[i].id; - if (!bRecodeOnly || (mEncoding[id].uFlags & NCP_RECODE)) { - - CPINFO cpi; - - cbei.iItem = SendMessage(hwnd,CB_GETCOUNT,0,0); - - WCHAR *pwsz = StrChr(pEE[i].wch, L';'); - if (pwsz) { - StringCchCopyN(wchBuf,COUNTOF(wchBuf),CharNext(pwsz),COUNTOF(wchBuf)); - pwsz = StrChr(wchBuf, L';'); - if (pwsz) - *pwsz = 0; - } - else - StringCchCopyN(wchBuf,COUNTOF(wchBuf),pEE[i].wch,COUNTOF(wchBuf)); - - if (Encoding_IsANSI(id)) - StringCchCatN(wchBuf,COUNTOF(wchBuf),wchANSI,COUNTOF(wchANSI)); - else if (id == CPI_OEM) - StringCchCatN(wchBuf,COUNTOF(wchBuf),wchOEM,COUNTOF(wchOEM)); - - if ((mEncoding[id].uFlags & NCP_INTERNAL) || - (IsValidCodePage(mEncoding[id].uCodePage) && - GetCPInfo(mEncoding[id].uCodePage,&cpi))) - cbei.iImage = 0; - else - cbei.iImage = 1; - - cbei.lParam = (LPARAM)id; - SendMessage(hwnd,CBEM_INSERTITEM,0,(LPARAM)&cbei); - - if (idSel == id) - iSelItem = (int)cbei.iItem; - } - } - - LocalFree(pEE); - - if (iSelItem != -1) - SendMessage(hwnd,CB_SETCURSEL,(WPARAM)iSelItem,0); -} - - -BOOL Encoding_GetFromComboboxEx(HWND hwnd,int *pidEncoding) -{ - COMBOBOXEXITEM cbei; - - cbei.iItem = SendMessage(hwnd,CB_GETCURSEL,0,0); - cbei.mask = CBEIF_LPARAM; - - if (SendMessage(hwnd,CBEM_GETITEM,0,(LPARAM)&cbei)) { - if (Encoding_IsValid((int)cbei.lParam)) { - *pidEncoding = (int)cbei.lParam; - return (TRUE); - } - else - MsgBox(MBWARN,IDS_ERR_ENCODINGNA); - } - return(FALSE); -} - - -BOOL Encoding_IsDefault(int iEncoding) -{ - return (mEncoding[iEncoding].uFlags & NCP_DEFAULT); -} - -BOOL Encoding_IsANSI(int iEncoding) -{ - return (mEncoding[iEncoding].uFlags & NCP_ANSI); -} - -BOOL Encoding_IsOEM(int iEncoding) -{ - return (mEncoding[iEncoding].uFlags & NCP_OEM); -} - -UINT Encoding_SciGetCodePage(HWND hwnd) -{ - // remove internal support for Chinese, Japan, Korean DBCS use UTF-8 instead - /* - int cp = (UINT)SendMessage(hwnd,SCI_GETCODEPAGE,0,0); - if (cp == 932 || cp == 936 || cp == 949 || cp == 950) { - return cp; - } - return (cp == 0) ? CP_ACP : CP_UTF8; - */ - UNUSED(hwnd); - return CP_UTF8; -} - - -int Encoding_SciMappedCodePage(int iEncoding) -{ - // remove internal support for Chinese, Japan, Korean DBCS use UTF-8 instead - /* - if (Encoding_IsValid(iEncoding)) { - // check for Chinese, Japan, Korean DBCS code pages and switch accordingly - int cp = (int)mEncoding[iEncoding].uCodePage; - if (cp == 932 || cp == 936 || cp == 949 || cp == 950) { - return cp; - } - } - */ - UNUSED(iEncoding); - return SC_CP_UTF8; -} - - -void Encoding_SciSetCodePage(HWND hwnd, int iEncoding) -{ - int cp = Encoding_SciMappedCodePage(iEncoding); - SendMessage(hwnd,SCI_SETCODEPAGE,(WPARAM)cp,0); - // charsets can be changed via styles schema - /* - int charset = SC_CHARSET_ANSI; - switch (cp) { - case 932: - charset = SC_CHARSET_SHIFTJIS; - break; - case 936: - charset = SC_CHARSET_GB2312; - break; - case 949: - charset = SC_CHARSET_HANGUL; - break; - case 950: - charset = SC_CHARSET_CHINESEBIG5; - break; - default: - charset = iDefaultCharSet; - break; - } - SendMessage(hwnd,SCI_STYLESETCHARACTERSET,(WPARAM)STYLE_DEFAULT,(LPARAM)charset); - */ -} - - -BOOL IsUnicode(const char* pBuffer,int cb,LPBOOL lpbBOM,LPBOOL lpbReverse) -{ - int i = 0xFFFF; - - BOOL bIsTextUnicode; - - BOOL bHasBOM; - BOOL bHasRBOM; - - if (!pBuffer || cb < 2) - return FALSE; - - if (!bSkipUnicodeDetection) - bIsTextUnicode = IsTextUnicode(pBuffer,cb,&i); - else - bIsTextUnicode = FALSE; - - bHasBOM = (*((UNALIGNED PWCHAR)pBuffer) == 0xFEFF); - bHasRBOM = (*((UNALIGNED PWCHAR)pBuffer) == 0xFFFE); - - if (i == 0xFFFF) // i doesn't seem to have been modified ... - i = 0; - - if (bIsTextUnicode || bHasBOM || bHasRBOM || - ((i & (IS_TEXT_UNICODE_UNICODE_MASK | IS_TEXT_UNICODE_REVERSE_MASK)) && - !((i & IS_TEXT_UNICODE_UNICODE_MASK) && (i & IS_TEXT_UNICODE_REVERSE_MASK)) && - !(i & IS_TEXT_UNICODE_ODD_LENGTH) && - !(i & IS_TEXT_UNICODE_ILLEGAL_CHARS && !(i & IS_TEXT_UNICODE_REVERSE_SIGNATURE)) && - !((i & IS_TEXT_UNICODE_REVERSE_MASK) == IS_TEXT_UNICODE_REVERSE_STATISTICS))) { - - if (lpbBOM) - *lpbBOM = (bHasBOM || bHasRBOM || - (i & (IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE))) - ? TRUE : FALSE; - - if (lpbReverse) - *lpbReverse = (bHasRBOM || (i & IS_TEXT_UNICODE_REVERSE_MASK)) ? TRUE : FALSE; - - return TRUE; - } - - else - - return FALSE; -} - - -BOOL IsUTF8(const char* pTest,int nLength) -{ - static int byte_class_table[256] = { - /* 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F */ - /* 00 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - /* 10 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - /* 20 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - /* 30 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - /* 40 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - /* 50 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - /* 60 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - /* 70 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - /* 80 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - /* 90 */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - /* A0 */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - /* B0 */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - /* C0 */ 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - /* D0 */ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - /* E0 */ 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 7, 7, - /* F0 */ 9,10,10,10,11, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 - /* 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F */ }; - - /* state table */ - typedef enum { - kSTART = 0,kA,kB,kC,kD,kE,kF,kG,kERROR,kNumOfStates } utf8_state; - - static utf8_state state_table[] = { - /* kSTART, kA, kB, kC, kD, kE, kF, kG, kERROR */ - /* 0x00-0x7F: 0 */ kSTART, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, - /* 0x80-0x8F: 1 */ kERROR, kSTART, kA, kERROR, kA, kB, kERROR, kB, kERROR, - /* 0x90-0x9f: 2 */ kERROR, kSTART, kA, kERROR, kA, kB, kB, kERROR, kERROR, - /* 0xa0-0xbf: 3 */ kERROR, kSTART, kA, kA, kERROR, kB, kB, kERROR, kERROR, - /* 0xc0-0xc1, 0xf5-0xff: 4 */ kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, - /* 0xc2-0xdf: 5 */ kA, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, - /* 0xe0: 6 */ kC, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, - /* 0xe1-0xec, 0xee-0xef: 7 */ kB, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, - /* 0xed: 8 */ kD, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, - /* 0xf0: 9 */ kF, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, - /* 0xf1-0xf3: 10 */ kE, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, - /* 0xf4: 11 */ kG, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR }; - - #define BYTE_CLASS(b) (byte_class_table[(unsigned char)b]) - #define NEXT_STATE(b,cur) (state_table[(BYTE_CLASS(b) * kNumOfStates) + (cur)]) - - utf8_state current = kSTART; - int i; - - const char* pt = pTest; - int len = nLength; - - for(i = 0; i < len ; i++, pt++) { - - current = NEXT_STATE(*pt,current); - if (kERROR == current) - break; - } - - return (current == kSTART) ? TRUE : FALSE; - } - - -BOOL IsUTF7(const char* pTest,int nLength) -{ - int i; - const char *pt = pTest; - - for (i = 0; i < nLength; i++) { - if (*pt & 0x80 || !*pt) - return FALSE; - pt++; - } - - return TRUE; -} - - -#define IsUTF8Signature(p) \ - ((*(p+0) == '\xEF' && *(p+1) == '\xBB' && *(p+2) == '\xBF')) - - -#define UTF8StringStart(p) \ - (IsUTF8Signature(p)) ? (p+3) : (p) - - -/* byte length of UTF-8 sequence based on value of first byte. - for UTF-16 (21-bit space), max. code length is 4, so we only need to look - at 4 upper bits. - */ -static const INT utf8_lengths[16]= -{ - 1,1,1,1,1,1,1,1, /* 0000 to 0111 : 1 byte (plain ASCII) */ - 0,0,0,0, /* 1000 to 1011 : not valid */ - 2,2, /* 1100, 1101 : 2 bytes */ - 3, /* 1110 : 3 bytes */ - 4 /* 1111 :4 bytes */ -}; - -/*++ -Function : - UTF8_mbslen_bytes [INTERNAL] - - Calculates the byte size of a NULL-terminated UTF-8 string. - -Parameters : - char *utf8_string : string to examine - -Return value : - size (in bytes) of a NULL-terminated UTF-8 string. - -1 if invalid NULL-terminated UTF-8 string ---*/ -static INT UTF8_mbslen_bytes(LPCSTR utf8_string) -{ - INT length=0; - INT code_size; - BYTE byte; - - while(*utf8_string) - { - byte=(BYTE)*utf8_string; - - if( (byte <= 0xF7) && (0 != (code_size = utf8_lengths[ byte >> 4 ]))) - { - length+=code_size; - utf8_string+=code_size; - } - else - { - /* we got an invalid byte value but need to count it, - it will be later ignored during the string conversion */ - //WARN("invalid first byte value 0x%02X in UTF-8 sequence!\n",byte); - length++; - utf8_string++; - } - } - length++; /* include NULL terminator */ - return length; -} - -/*++ -Function : - UTF8_mbslen [INTERNAL] - - Calculates the character size of a NULL-terminated UTF-8 string. - -Parameters : - char *utf8_string : string to examine - int byte_length : byte size of string - -Return value : - size (in characters) of a UTF-8 string. - -1 if invalid UTF-8 string ---*/ -static INT UTF8_mbslen(LPCSTR source, INT byte_length) -{ - INT wchar_length=0; - INT code_size; - BYTE byte; - - while(byte_length > 0) - { - byte=(BYTE)*source; - - /* UTF-16 can't encode 5-byte and 6-byte sequences, so maximum value - for first byte is 11110111. Use lookup table to determine sequence - length based on upper 4 bits of first byte */ - if ((byte <= 0xF7) && (0 != (code_size=utf8_lengths[ byte >> 4]))) - { - /* 1 sequence == 1 character */ - wchar_length++; - - if(code_size==4) - wchar_length++; - - source+=code_size; /* increment pointer */ - byte_length-=code_size; /* decrement counter*/ - } - else - { - /* - unlike UTF8_mbslen_bytes, we ignore the invalid characters. - we only report the number of valid characters we have encountered - to match the Windows behavior. - */ - //WARN("invalid byte 0x%02X in UTF-8 sequence, skipping it!\n", - // byte); - source++; - byte_length--; - } - } - return wchar_length; -} - - //============================================================================= // // EditLoadFile() @@ -7581,7 +6684,7 @@ BOOL FileVars_IsValidEncoding(LPFILEVARS lpfv) { CPINFO cpi; if (lpfv->mask & FV_ENCODING && lpfv->iEncoding >= 0 && - lpfv->iEncoding < COUNTOF(mEncoding)) { + lpfv->iEncoding < Encoding_CountOf()) { if ((mEncoding[lpfv->iEncoding].uFlags & NCP_INTERNAL) || IsValidCodePage(mEncoding[lpfv->iEncoding].uCodePage) && GetCPInfo(mEncoding[lpfv->iEncoding].uCodePage,&cpi)) { diff --git a/src/Edit.h b/src/Edit.h index 1bab6cd88..18a1cf405 100644 --- a/src/Edit.h +++ b/src/Edit.h @@ -112,78 +112,10 @@ void EditMarkAll(HWND,int,BOOL,BOOL); void EditSetAccelWordNav(HWND,BOOL); void CompleteWord(HWND,BOOL); -extern int g_DOSEncoding; - -#define NCP_DEFAULT 1 -#define NCP_UTF8 2 -#define NCP_UTF8_SIGN 4 -#define NCP_UNICODE 8 -#define NCP_UNICODE_REVERSE 16 -#define NCP_UNICODE_BOM 32 -#define NCP_8BIT 64 -#define NCP_ANSI 128 -#define NCP_OEM 256 -#define NCP_MBCS 512 -#define NCP_INTERNAL (NCP_DEFAULT|NCP_UTF8|NCP_UTF8_SIGN|NCP_UNICODE|NCP_UNICODE_REVERSE|NCP_UNICODE_BOM|NCP_ANSI|NCP_OEM|NCP_MBCS) -#define NCP_RECODE 1024 - -#define CPI_GET -2 -#define CPI_NONE -1 -#define CPI_ANSI_DEFAULT 0 -#define CPI_OEM 1 -#define CPI_UNICODEBOM 2 -#define CPI_UNICODEBEBOM 3 -#define CPI_UNICODE 4 -#define CPI_UNICODEBE 5 -#define CPI_UTF8 6 -#define CPI_UTF8SIGN 7 -#define CPI_UTF7 8 - -#define IDS_ENCODINGNAME0 61000 -#define IDS_EOLMODENAME0 62000 - -typedef struct _np2encoding { - UINT uFlags; - UINT uCodePage; - char* pszParseNames; - int idsName; - WCHAR wchLabel[64]; -} NP2ENCODING; - -int Encoding_Current(int); // getter/setter -int Encoding_Source(int); // getter/setter -int Encoding_SrcWeak(int); // getter/setter -BOOL Encoding_HasChanged(int); // query/setter - -void Encoding_InitDefaults(); -int Encoding_MapIniSetting(BOOL,int); -void Encoding_GetLabel(int); -int Encoding_MatchW(LPCWSTR); -int Encoding_MatchA(char*); -BOOL Encoding_IsValid(int); -int Encoding_GetByCodePage(UINT); -void Encoding_AddToListView(HWND,int,BOOL); -BOOL Encoding_GetFromListView(HWND,int *); -void Encoding_AddToComboboxEx(HWND,int,BOOL); -BOOL Encoding_GetFromComboboxEx(HWND,int *); -BOOL Encoding_IsDefault(int); -BOOL Encoding_IsANSI(int); -BOOL Encoding_IsOEM(int); - -UINT Encoding_SciGetCodePage(HWND); -int Encoding_SciMappedCodePage(int); -void Encoding_SciSetCodePage(HWND,int); - - -BOOL IsUnicode(const char*,int,LPBOOL,LPBOOL); -BOOL IsUTF8(const char*,int); -BOOL IsUTF7(const char*,int); - //void SciInitThemes(HWND); //LRESULT CALLBACK SciThemedWndProc(HWND,UINT,WPARAM,LPARAM); - #define FV_TABWIDTH 1 #define FV_INDENTWIDTH 2 #define FV_TABSASSPACES 4 diff --git a/src/Helpers.c b/src/Helpers.c index fb207d2f8..e3eb15398 100644 --- a/src/Helpers.c +++ b/src/Helpers.c @@ -34,11 +34,13 @@ #include #include //#include +#include "scintilla.h" #include "resource.h" #include "helpers.h" -extern HINSTANCE g_hInstance; +extern HINSTANCE g_hInstance; +extern BOOL bSkipUnicodeDetection; //============================================================================= @@ -1916,7 +1918,8 @@ BOOL MRU_Load(LPMRULIST pmru) { pmru->pszItems[n] = StrDup(tchItem); StringCchPrintf(tchName,COUNTOF(tchName),L"ENC%.2i",i + 1); - pmru->iEncoding[n] = IniSectionGetInt(pIniSection,tchName,0); + int iCP = IniSectionGetInt(pIniSection,tchName,0); + pmru->iEncoding[n] = Encoding_MapIniSetting(TRUE,iCP); StringCchPrintf(tchName,COUNTOF(tchName),L"POS%.2i",i + 1); pmru->iCaretPos[n] = IniSectionGetInt(pIniSection,tchName,0); ++n; @@ -1949,7 +1952,8 @@ BOOL MRU_Save(LPMRULIST pmru) { if (pmru->iEncoding[i] > 0) { StringCchPrintf(tchName,COUNTOF(tchName),L"ENC%.2i",i + 1); - IniSectionSetInt(pIniSection,tchName,pmru->iEncoding[i]); + int iCP = Encoding_MapIniSetting(FALSE,pmru->iEncoding[i]); + IniSectionSetInt(pIniSection,tchName,iCP); } if (pmru->iCaretPos[i] > 0) { StringCchPrintf(tchName,COUNTOF(tchName),L"POS%.2i",i + 1); @@ -1993,26 +1997,6 @@ BOOL MRU_MergeSave(LPMRULIST pmru,BOOL bAddFiles,BOOL bRelativePath,BOOL bUnexpa return TRUE; } - -BOOL MRU_SetEnc(LPMRULIST pmru,int iIndex,int iEnc) { - - if (iIndex < 0 || iIndex > pmru->iSize - 1) - return FALSE; - - pmru->iEncoding[iIndex] = iEnc; - return TRUE; -} - - -BOOL MRU_SetPos(LPMRULIST pmru,int iIndex,int iPos) { - - if (iIndex < 0 || iIndex > pmru->iSize - 1) - return FALSE; - - pmru->iCaretPos[iIndex] = iPos; - return TRUE; -} - /* Themed Dialogs @@ -2557,5 +2541,884 @@ VOID RestoreWndFromTray(HWND hWnd) } +//============================================================================= +// +// Encoding Helper Functions +// + +int g_DOSEncoding; + +// Supported Encodings +WCHAR wchANSI[16] = { L'\0' }; +WCHAR wchOEM[16] = { L'\0' }; + +NP2ENCODING mEncoding[] = { + { NCP_ANSI | NCP_RECODE, CP_ACP, "ansi,system,ascii,", 61000, L"" }, + { NCP_OEM | NCP_RECODE, CP_OEMCP, "oem,oem,", 61001, L"" }, + { NCP_UNICODE | NCP_UNICODE_BOM, CP_UTF8, "", 61002, L"" }, + { NCP_UNICODE | NCP_UNICODE_REVERSE | NCP_UNICODE_BOM, CP_UTF8, "", 61003, L"" }, + { NCP_UNICODE | NCP_RECODE, CP_UTF8, "utf-16,utf16,unicode,", 61004, L"" }, + { NCP_UNICODE | NCP_UNICODE_REVERSE | NCP_RECODE, CP_UTF8, "utf-16be,utf16be,unicodebe,", 61005, L"" }, + { NCP_UTF8 | NCP_RECODE, CP_UTF8, "utf-8,utf8,", 61006, L"" }, + { NCP_UTF8 | NCP_UTF8_SIGN, CP_UTF8, "utf-8,utf8,", 61007, L"" }, + { NCP_8BIT | NCP_RECODE, CP_UTF7, "utf-7,utf7,", 61008, L"" }, + { NCP_8BIT | NCP_RECODE, 720, "DOS-720,dos720,", 61009, L"" }, + { NCP_8BIT | NCP_RECODE, 28596, "iso-8859-6,iso88596,arabic,csisolatinarabic,ecma114,isoir127,", 61010, L"" }, + { NCP_8BIT | NCP_RECODE, 10004, "x-mac-arabic,xmacarabic,", 61011, L"" }, + { NCP_8BIT | NCP_RECODE, 1256, "windows-1256,windows1256,cp1256", 61012, L"" }, + { NCP_8BIT | NCP_RECODE, 775, "ibm775,ibm775,cp500,", 61013, L"" }, + { NCP_8BIT | NCP_RECODE, 28594, "iso-8859-4,iso88594,csisolatin4,isoir110,l4,latin4,", 61014, L"" }, + { NCP_8BIT | NCP_RECODE, 1257, "windows-1257,windows1257,", 61015, L"" }, + { NCP_8BIT | NCP_RECODE, 852, "ibm852,ibm852,cp852,", 61016, L"" }, + { NCP_8BIT | NCP_RECODE, 28592, "iso-8859-2,iso88592,csisolatin2,isoir101,latin2,l2,", 61017, L"" }, + { NCP_8BIT | NCP_RECODE, 10029, "x-mac-ce,xmacce,", 61018, L"" }, + { NCP_8BIT | NCP_RECODE, 1250, "windows-1250,windows1250,xcp1250,", 61019, L"" }, + { NCP_8BIT | NCP_RECODE, 936, "gb2312,gb2312,chinese,cngb,csgb2312,csgb231280,gb231280,gbk,", 61020, L"" }, + { NCP_8BIT | NCP_RECODE, 10008, "x-mac-chinesesimp,xmacchinesesimp,", 61021, L"" }, + { NCP_8BIT | NCP_RECODE, 950, "big5,big5,cnbig5,csbig5,xxbig5,", 61022, L"" }, + { NCP_8BIT | NCP_RECODE, 10002, "x-mac-chinesetrad,xmacchinesetrad,", 61023, L"" }, + { NCP_8BIT | NCP_RECODE, 10082, "x-mac-croatian,xmaccroatian,", 61024, L"" }, + { NCP_8BIT | NCP_RECODE, 866, "cp866,cp866,ibm866,", 61025, L"" }, + { NCP_8BIT | NCP_RECODE, 28595, "iso-8859-5,iso88595,csisolatin5,csisolatincyrillic,cyrillic,isoir144,", 61026, L"" }, + { NCP_8BIT | NCP_RECODE, 20866, "koi8-r,koi8r,cskoi8r,koi,koi8,", 61027, L"" }, + { NCP_8BIT | NCP_RECODE, 21866, "koi8-u,koi8u,koi8ru,", 61028, L"" }, + { NCP_8BIT | NCP_RECODE, 10007, "x-mac-cyrillic,xmaccyrillic,", 61029, L"" }, + { NCP_8BIT | NCP_RECODE, 1251, "windows-1251,windows1251,xcp1251,", 61030, L"" }, + { NCP_8BIT | NCP_RECODE, 28603, "iso-8859-13,iso885913,", 61031, L"" }, + { NCP_8BIT | NCP_RECODE, 863, "ibm863,ibm863,", 61032, L"" }, + { NCP_8BIT | NCP_RECODE, 737, "ibm737,ibm737,", 61033, L"" }, + { NCP_8BIT | NCP_RECODE, 28597, "iso-8859-7,iso88597,csisolatingreek,ecma118,elot928,greek,greek8,isoir126,", 61034, L"" }, + { NCP_8BIT | NCP_RECODE, 10006, "x-mac-greek,xmacgreek,", 61035, L"" }, + { NCP_8BIT | NCP_RECODE, 1253, "windows-1253,windows1253,", 61036, L"" }, + { NCP_8BIT | NCP_RECODE, 869, "ibm869,ibm869,", 61037, L"" }, + { NCP_8BIT | NCP_RECODE, 862, "DOS-862,dos862,", 61038, L"" }, + { NCP_8BIT | NCP_RECODE, 38598, "iso-8859-8-i,iso88598i,logical,", 61039, L"" }, + { NCP_8BIT | NCP_RECODE, 28598, "iso-8859-8,iso88598,csisolatinhebrew,hebrew,isoir138,visual,", 61040, L"" }, + { NCP_8BIT | NCP_RECODE, 10005, "x-mac-hebrew,xmachebrew,", 61041, L"" }, + { NCP_8BIT | NCP_RECODE, 1255, "windows-1255,windows1255,", 61042, L"" }, + { NCP_8BIT | NCP_RECODE, 861, "ibm861,ibm861,", 61043, L"" }, + { NCP_8BIT | NCP_RECODE, 10079, "x-mac-icelandic,xmacicelandic,", 61044, L"" }, + { NCP_8BIT | NCP_RECODE, 10001, "x-mac-japanese,xmacjapanese,", 61045, L"" }, + { NCP_8BIT | NCP_RECODE, 932, "shift_jis,shiftjis,shiftjs,csshiftjis,cswindows31j,mskanji,xmscp932,xsjis,", 61046, L"" }, + { NCP_8BIT | NCP_RECODE, 10003, "x-mac-korean,xmackorean,", 61047, L"" }, + { NCP_8BIT | NCP_RECODE, 949, "windows-949,windows949,ksc56011987,csksc5601,euckr,isoir149,korean,ksc56011989", 61048, L"" }, + { NCP_8BIT | NCP_RECODE, 28593, "iso-8859-3,iso88593,latin3,isoir109,l3,", 61049, L"" }, + { NCP_8BIT | NCP_RECODE, 28605, "iso-8859-15,iso885915,latin9,l9,", 61050, L"" }, + { NCP_8BIT | NCP_RECODE, 865, "ibm865,ibm865,", 61051, L"" }, + { NCP_8BIT | NCP_RECODE, 437, "ibm437,ibm437,437,cp437,cspc8,codepage437,", 61052, L"" }, + { NCP_8BIT | NCP_RECODE, 858, "ibm858,ibm858,ibm00858,", 61053, L"" }, + { NCP_8BIT | NCP_RECODE, 860, "ibm860,ibm860,", 61054, L"" }, + { NCP_8BIT | NCP_RECODE, 10010, "x-mac-romanian,xmacromanian,", 61055, L"" }, + { NCP_8BIT | NCP_RECODE, 10021, "x-mac-thai,xmacthai,", 61056, L"" }, + { NCP_8BIT | NCP_RECODE, 874, "windows-874,windows874,dos874,iso885911,tis620,", 61057, L"" }, + { NCP_8BIT | NCP_RECODE, 857, "ibm857,ibm857,", 61058, L"" }, + { NCP_8BIT | NCP_RECODE, 28599, "iso-8859-9,iso88599,latin5,isoir148,l5,", 61059, L"" }, + { NCP_8BIT | NCP_RECODE, 10081, "x-mac-turkish,xmacturkish,", 61060, L"" }, + { NCP_8BIT | NCP_RECODE, 1254, "windows-1254,windows1254,", 61061, L"" }, + { NCP_8BIT | NCP_RECODE, 10017, "x-mac-ukrainian,xmacukrainian,", 61062, L"" }, + { NCP_8BIT | NCP_RECODE, 1258, "windows-1258,windows-258,", 61063, L"" }, + { NCP_8BIT | NCP_RECODE, 850, "ibm850,ibm850,", 61064, L"" }, + { NCP_8BIT | NCP_RECODE, 28591, "iso-8859-1,iso88591,cp819,latin1,ibm819,isoir100,latin1,l1,", 61065, L"" }, + { NCP_8BIT | NCP_RECODE, 10000, "macintosh,macintosh,", 61066, L"" }, + { NCP_8BIT | NCP_RECODE, 1252, "windows-1252,windows1252,cp367,cp819,ibm367,us,xansi,", 61067, L"" }, + { NCP_8BIT | NCP_RECODE, 37, "ebcdic-cp-us,ebcdiccpus,ebcdiccpca,ebcdiccpwt,ebcdiccpnl,ibm037,cp037,", 61068, L"" }, + { NCP_8BIT | NCP_RECODE, 500, "x-ebcdic-international,xebcdicinternational,", 61069, L"" }, + { NCP_8BIT | NCP_RECODE, 875, "x-EBCDIC-GreekModern,xebcdicgreekmodern,", 61070, L"" }, + { NCP_8BIT | NCP_RECODE, 1026, "CP1026,cp1026,csibm1026,ibm1026,", 61071, L"" }, + //{ NCP_8BIT|NCP_RECODE, 870, "CP870,cp870,ebcdiccproece,ebcdiccpyu,csibm870,ibm870,", 00000, L"" }, // IBM EBCDIC (Multilingual Latin-2) + //{ NCP_8BIT|NCP_RECODE, 1047, "IBM01047,ibm01047,", 00000, L"" }, // IBM EBCDIC (Open System Latin-1) + //{ NCP_8BIT|NCP_RECODE, 1140, "x-ebcdic-cp-us-euro,xebcdiccpuseuro,", 00000, L"" }, // IBM EBCDIC (US-Canada-Euro) + //{ NCP_8BIT|NCP_RECODE, 1141, "x-ebcdic-germany-euro,xebcdicgermanyeuro,", 00000, L"" }, // IBM EBCDIC (Germany-Euro) + //{ NCP_8BIT|NCP_RECODE, 1142, "x-ebcdic-denmarknorway-euro,xebcdicdenmarknorwayeuro,", 00000, L"" }, // IBM EBCDIC (Denmark-Norway-Euro) + //{ NCP_8BIT|NCP_RECODE, 1143, "x-ebcdic-finlandsweden-euro,xebcdicfinlandswedeneuro,", 00000, L"" }, // IBM EBCDIC (Finland-Sweden-Euro) + //{ NCP_8BIT|NCP_RECODE, 1144, "x-ebcdic-italy-euro,xebcdicitalyeuro,", 00000, L"" }, // IBM EBCDIC (Italy-Euro) + //{ NCP_8BIT|NCP_RECODE, 1145, "x-ebcdic-spain-euro,xebcdicspaineuro,", 00000, L"" }, // IBM EBCDIC (Spain-Latin America-Euro) + //{ NCP_8BIT|NCP_RECODE, 1146, "x-ebcdic-uk-euro,xebcdicukeuro,", 00000, L"" }, // IBM EBCDIC (UK-Euro) + //{ NCP_8BIT|NCP_RECODE, 1147, "x-ebcdic-france-euro,xebcdicfranceeuro,", 00000, L"" }, // IBM EBCDIC (France-Euro) + //{ NCP_8BIT|NCP_RECODE, 1148, "x-ebcdic-international-euro,xebcdicinternationaleuro,", 00000, L"" }, // IBM EBCDIC (International-Euro) + //{ NCP_8BIT|NCP_RECODE, 1149, "x-ebcdic-icelandic-euro,xebcdicicelandiceuro,", 00000, L"" }, // IBM EBCDIC (Icelandic-Euro) + //{ NCP_8BIT|NCP_RECODE, 1361, "johab,johab,", 00000, L"" }, // Korean (Johab) + //{ NCP_8BIT|NCP_RECODE, 20273, "x-EBCDIC-Germany,xebcdicgermany,", 00000, L"" }, // IBM EBCDIC (Germany) + //{ NCP_8BIT|NCP_RECODE, 20277, "x-EBCDIC-DenmarkNorway,xebcdicdenmarknorway,ebcdiccpdk,ebcdiccpno,", 00000, L"" }, // IBM EBCDIC (Denmark-Norway) + //{ NCP_8BIT|NCP_RECODE, 20278, "x-EBCDIC-FinlandSweden,xebcdicfinlandsweden,ebcdicpfi,ebcdiccpse,", 00000, L"" }, // IBM EBCDIC (Finland-Sweden) + //{ NCP_8BIT|NCP_RECODE, 20280, "x-EBCDIC-Italy,xebcdicitaly,", 00000, L"" }, // IBM EBCDIC (Italy) + //{ NCP_8BIT|NCP_RECODE, 20284, "x-EBCDIC-Spain,xebcdicspain,ebcdiccpes,", 00000, L"" }, // IBM EBCDIC (Spain-Latin America) + //{ NCP_8BIT|NCP_RECODE, 20285, "x-EBCDIC-UK,xebcdicuk,ebcdiccpgb,", 00000, L"" }, // IBM EBCDIC (UK) + //{ NCP_8BIT|NCP_RECODE, 20290, "x-EBCDIC-JapaneseKatakana,xebcdicjapanesekatakana,", 00000, L"" }, // IBM EBCDIC (Japanese Katakana) + //{ NCP_8BIT|NCP_RECODE, 20297, "x-EBCDIC-France,xebcdicfrance,ebcdiccpfr,", 00000, L"" }, // IBM EBCDIC (France) + //{ NCP_8BIT|NCP_RECODE, 20420, "x-EBCDIC-Arabic,xebcdicarabic,ebcdiccpar1,", 00000, L"" }, // IBM EBCDIC (Arabic) + //{ NCP_8BIT|NCP_RECODE, 20423, "x-EBCDIC-Greek,xebcdicgreek,ebcdiccpgr,", 00000, L"" }, // IBM EBCDIC (Greek) + //{ NCP_8BIT|NCP_RECODE, 20424, "x-EBCDIC-Hebrew,xebcdichebrew,ebcdiccphe,", 00000, L"" }, // IBM EBCDIC (Hebrew) + //{ NCP_8BIT|NCP_RECODE, 20833, "x-EBCDIC-KoreanExtended,xebcdickoreanextended,", 00000, L"" }, // IBM EBCDIC (Korean Extended) + //{ NCP_8BIT|NCP_RECODE, 20838, "x-EBCDIC-Thai,xebcdicthai,ibmthai,csibmthai,", 00000, L"" }, // IBM EBCDIC (Thai) + //{ NCP_8BIT|NCP_RECODE, 20871, "x-EBCDIC-Icelandic,xebcdicicelandic,ebcdiccpis,", 00000, L"" }, // IBM EBCDIC (Icelandic) + //{ NCP_8BIT|NCP_RECODE, 20880, "x-EBCDIC-CyrillicRussian,xebcdiccyrillicrussian,ebcdiccyrillic,", 00000, L"" }, // IBM EBCDIC (Cyrillic Russian) + //{ NCP_8BIT|NCP_RECODE, 20905, "x-EBCDIC-Turkish,xebcdicturkish,ebcdiccptr,", 00000, L"" }, // IBM EBCDIC (Turkish) + //{ NCP_8BIT|NCP_RECODE, 20924, "IBM00924,ibm00924,ebcdiclatin9euro,", 00000, L"" }, // IBM EBCDIC (Open System-Euro Latin-1) + //{ NCP_8BIT|NCP_RECODE, 21025, "x-EBCDIC-CyrillicSerbianBulgarian,xebcdiccyrillicserbianbulgarian,", 00000, L"" }, // IBM EBCDIC (Cyrillic Serbian-Bulgarian) + //{ NCP_8BIT|NCP_RECODE, 50930, "x-EBCDIC-JapaneseAndKana,xebcdicjapaneseandkana,", 00000, L"" }, // IBM EBCDIC (Japanese and Japanese Katakana) + //{ NCP_8BIT|NCP_RECODE, 50931, "x-EBCDIC-JapaneseAndUSCanada,xebcdicjapaneseanduscanada,", 00000, L"" }, // IBM EBCDIC (Japanese and US-Canada) + //{ NCP_8BIT|NCP_RECODE, 50933, "x-EBCDIC-KoreanAndKoreanExtended,xebcdickoreanandkoreanextended,", 00000, L"" }, // IBM EBCDIC (Korean and Korean Extended) + //{ NCP_8BIT|NCP_RECODE, 50935, "x-EBCDIC-SimplifiedChinese,xebcdicsimplifiedchinese,", 00000, L"" }, // IBM EBCDIC (Chinese Simplified) + //{ NCP_8BIT|NCP_RECODE, 50937, "x-EBCDIC-TraditionalChinese,xebcdictraditionalchinese,", 00000, L"" }, // IBM EBCDIC (Chinese Traditional) + //{ NCP_8BIT|NCP_RECODE, 50939, "x-EBCDIC-JapaneseAndJapaneseLatin,xebcdicjapaneseandjapaneselatin,", 00000, L"" }, // IBM EBCDIC (Japanese and Japanese-Latin) + //{ NCP_8BIT|NCP_RECODE, 20105, "x-IA5,xia5,", 00000, L"" }, // Western European (IA5) + //{ NCP_8BIT|NCP_RECODE, 20106, "x-IA5-German,xia5german,", 00000, L"" }, // German (IA5) + //{ NCP_8BIT|NCP_RECODE, 20107, "x-IA5-Swedish,xia5swedish,", 00000, L"" }, // Swedish (IA5) + //{ NCP_8BIT|NCP_RECODE, 20108, "x-IA5-Norwegian,xia5norwegian,", 00000, L"" }, // Norwegian (IA5) + //{ NCP_8BIT|NCP_RECODE, 20936, "x-cp20936,xcp20936,", 00000, L"" }, // Chinese Simplified (GB2312) + //{ NCP_8BIT|NCP_RECODE, 20932, "euc-jp,,", 00000, L"" }, // Japanese (JIS X 0208-1990 & 0212-1990) + //{ NCP_8BIT|NCP_RECODE, 50220, "iso-2022-jp,iso2022jp,", 00000, L"" }, // Japanese (JIS) + //{ NCP_8BIT|NCP_RECODE, 50221, "csISO2022JP,csiso2022jp,", 00000, L"" }, // Japanese (JIS-Allow 1 byte Kana) + //{ NCP_8BIT|NCP_RECODE, 50222, "_iso-2022-jp$SIO,iso2022jpSIO,", 00000, L"" }, // Japanese (JIS-Allow 1 byte Kana - SO/SI) + //{ NCP_8BIT|NCP_RECODE, 50225, "iso-2022-kr,iso2022kr,csiso2022kr,", 00000, L"" }, // Korean (ISO-2022-KR) + //{ NCP_8BIT|NCP_RECODE, 50227, "x-cp50227,xcp50227,", 00000, L"" }, // Chinese Simplified (ISO-2022) + //{ NCP_8BIT|NCP_RECODE, 50229, "iso-2022-cn,iso2022cn,", 00000, L"" }, // Chinese Traditional (ISO-2022) + //{ NCP_8BIT|NCP_RECODE, 20000, "x-Chinese-CNS,xchinesecns,", 00000, L"" }, // Chinese Traditional (CNS) + //{ NCP_8BIT|NCP_RECODE, 20002, "x-Chinese-Eten,xchineseeten,", 00000, L"" }, // Chinese Traditional (Eten) + //{ NCP_8BIT|NCP_RECODE, 51932, "euc-jp,eucjp,xeuc,xeucjp,", 00000, L"" }, // Japanese (EUC) + //{ NCP_8BIT|NCP_RECODE, 51936, "euc-cn,euccn,xeuccn,", 00000, L"" }, // Chinese Simplified (EUC) + //{ NCP_8BIT|NCP_RECODE, 51949, "euc-kr,euckr,cseuckr,", 00000, L"" }, // Korean (EUC) + //{ NCP_8BIT|NCP_RECODE, 52936, "hz-gb-2312,hzgb2312,hz,", 00000, L"" }, // Chinese Simplified (HZ-GB2312) + { NCP_8BIT | NCP_RECODE, 54936, "gb18030,gb18030,", 61072, L"" } // Chinese Simplified (GB18030) + //{ NCP_8BIT|NCP_RECODE, 57002, "x-iscii-de,xisciide,", 00000, L"" }, // ISCII Devanagari + //{ NCP_8BIT|NCP_RECODE, 57003, "x-iscii-be,xisciibe,", 00000, L"" }, // ISCII Bengali + //{ NCP_8BIT|NCP_RECODE, 57004, "x-iscii-ta,xisciita,", 00000, L"" }, // ISCII Tamil + //{ NCP_8BIT|NCP_RECODE, 57005, "x-iscii-te,xisciite,", 00000, L"" }, // ISCII Telugu + //{ NCP_8BIT|NCP_RECODE, 57006, "x-iscii-as,xisciias,", 00000, L"" }, // ISCII Assamese + //{ NCP_8BIT|NCP_RECODE, 57007, "x-iscii-or,xisciior,", 00000, L"" }, // ISCII Oriya + //{ NCP_8BIT|NCP_RECODE, 57008, "x-iscii-ka,xisciika,", 00000, L"" }, // ISCII Kannada + //{ NCP_8BIT|NCP_RECODE, 57009, "x-iscii-ma,xisciima,", 00000, L"" }, // ISCII Malayalam + //{ NCP_8BIT|NCP_RECODE, 57010, "x-iscii-gu,xisciigu,", 00000, L"" }, // ISCII Gujarathi + //{ NCP_8BIT|NCP_RECODE, 57011, "x-iscii-pa,xisciipa,", 00000, L"" }, // ISCII Panjabi +}; + + +int Encoding_CountOf() { + return COUNTOF(mEncoding); +} + +int Encoding_Current(int iEncoding) { + static int CurrentEncoding = CPI_NONE; + + if (iEncoding >= 0) { + if (Encoding_IsValid(iEncoding)) + CurrentEncoding = iEncoding; + else + CurrentEncoding = CPI_UTF8; + } + return CurrentEncoding; +} + + +int Encoding_Source(int iSrcEncoding) { + static int SourceEncoding = CPI_NONE; + + if (iSrcEncoding >= 0) { + if (Encoding_IsValid(iSrcEncoding)) + SourceEncoding = iSrcEncoding; + else + SourceEncoding = CPI_UTF8; + } + else if (iSrcEncoding == CPI_NONE) { + SourceEncoding = CPI_NONE; + } + return SourceEncoding; +} + + +int Encoding_SrcWeak(int iSrcWeakEnc) { + static int SourceWeakEncoding = CPI_NONE; + + if (iSrcWeakEnc >= 0) { + if (Encoding_IsValid(iSrcWeakEnc)) + SourceWeakEncoding = iSrcWeakEnc; + else + SourceWeakEncoding = CPI_UTF8; + } + else if (iSrcWeakEnc == CPI_NONE) { + SourceWeakEncoding = CPI_NONE; + } + return SourceWeakEncoding; +} + + +BOOL Encoding_HasChanged(int iOriginalEncoding) { + static int OriginalEncoding = CPI_NONE; + + if (iOriginalEncoding >= CPI_NONE) { + OriginalEncoding = iOriginalEncoding; + } + return (BOOL)(OriginalEncoding != Encoding_Current(CPI_GET)); +} + + +void Encoding_InitDefaults() { + const UINT uCodePageMBCS[20] = { + 42, // (Symbol) + 50220,50221,50222,50225,50227,50229, // (Chinese, Japanese, Korean) + 54936, // (GB18030) + 57002,57003,57004,57005,57006,57007,57008,57009,57010,57011, // (ISCII) + 65000, // (UTF-7) + 65001 // (UTF-8) + }; + + mEncoding[CPI_ANSI_DEFAULT].uCodePage = GetACP(); // set ANSI system CP + StringCchPrintf(wchANSI,COUNTOF(wchANSI),L" (CP-%u)",mEncoding[CPI_ANSI_DEFAULT].uCodePage); + + for (int i = CPI_UTF7 + 1; i < COUNTOF(mEncoding); ++i) { + if (Encoding_IsValid(i) && (mEncoding[i].uCodePage == mEncoding[CPI_ANSI_DEFAULT].uCodePage)) { + mEncoding[i].uFlags |= NCP_ANSI; + if (mEncoding[i].uFlags & NCP_8BIT) + mEncoding[CPI_ANSI_DEFAULT].uFlags |= NCP_8BIT; + break; + } + } + + mEncoding[CPI_OEM].uCodePage = GetOEMCP(); + StringCchPrintf(wchOEM,COUNTOF(wchOEM),L" (CP-%u)",mEncoding[CPI_OEM].uCodePage); + + for (int i = CPI_UTF7 + 1; i < COUNTOF(mEncoding); ++i) { + if (Encoding_IsValid(i) && (mEncoding[i].uCodePage == mEncoding[CPI_OEM].uCodePage)) { + mEncoding[i].uFlags |= NCP_OEM; + if (mEncoding[i].uFlags & NCP_8BIT) + mEncoding[CPI_OEM].uFlags |= NCP_8BIT; + break; + } + } + + // multi byte character sets + for (int i = 0; i < COUNTOF(mEncoding); ++i) { + for (int k = 0; k < COUNTOF(uCodePageMBCS); k++) { + if (mEncoding[i].uCodePage == uCodePageMBCS[k]) { + mEncoding[i].uFlags |= NCP_MBCS; + } + } + } + + g_DOSEncoding = CPI_OEM; + // Try to set the DOS encoding to DOS-437 if the default OEMCP is not DOS-437 + if (mEncoding[g_DOSEncoding].uCodePage != 437) { + for (int i = CPI_UTF7 + 1; i < COUNTOF(mEncoding); ++i) { + if (Encoding_IsValid(i) && (mEncoding[i].uCodePage == 437)) { + g_DOSEncoding = i; + break; + } + } + } +} + + +int Encoding_MapIniSetting(BOOL bLoad,int iSetting) { + if (bLoad) { + switch (iSetting) { + case -1: return CPI_NONE; + case 0: return CPI_ANSI_DEFAULT; + case 1: return CPI_UNICODEBOM; + case 2: return CPI_UNICODEBEBOM; + case 3: return CPI_UTF8; + case 4: return CPI_UTF8SIGN; + case 5: return CPI_OEM; + case 6: return CPI_UNICODE; + case 7: return CPI_UNICODEBE; + case 8: return CPI_UTF7; + default: { + for (int i = CPI_UTF7 + 1; i < COUNTOF(mEncoding); i++) { + if ((mEncoding[i].uCodePage == (UINT)iSetting) && Encoding_IsValid(i)) + return(i); + } + return CPI_ANSI_DEFAULT; + } + } + } + else { + switch (iSetting) { + case CPI_NONE: return -1; + case CPI_ANSI_DEFAULT: return 0; + case CPI_UNICODEBOM: return 1; + case CPI_UNICODEBEBOM: return 2; + case CPI_UTF8: return 3; + case CPI_UTF8SIGN: return 4; + case CPI_OEM: return 5; + case CPI_UNICODE: return 6; + case CPI_UNICODEBE: return 7; + case CPI_UTF7: return 8; + default: { + if (Encoding_IsValid(iSetting)) + return(mEncoding[iSetting].uCodePage); + else + return CPI_ANSI_DEFAULT; + } + } + } +} + +int Encoding_MapUnicode(int iUni) { + + if (iUni == CPI_UNICODEBOM) + return CPI_UNICODE; + else if (iUni == CPI_UNICODEBEBOM) + return CPI_UNICODEBE; + else if (iUni == CPI_UTF8SIGN) + return CPI_UTF8; + else + return iUni; +} + +void Encoding_GetLabel(int iEncoding) { + if (mEncoding[iEncoding].wchLabel[0] == L'\0') { + WCHAR wch1[128] = { L'\0' }; + WCHAR wch2[128] = { L'\0' }; + GetString(mEncoding[iEncoding].idsName,wch1,COUNTOF(wch1)); + WCHAR *pwsz = StrChr(wch1,L';'); + if (pwsz) { + pwsz = StrChr(CharNext(pwsz),L';'); + if (pwsz) { + pwsz = CharNext(pwsz); + } + } + if (!pwsz) + pwsz = wch1; + + StringCchCopyN(wch2,COUNTOF(wch2),pwsz,COUNTOF(wch1)); + + if (Encoding_IsANSI(iEncoding)) + StringCchCatN(wch2,COUNTOF(wch2),wchANSI,COUNTOF(wchANSI)); + else if (Encoding_IsOEM(iEncoding)) + StringCchCatN(wch2,COUNTOF(wch2),wchOEM,COUNTOF(wchOEM)); + + StringCchCopyN(mEncoding[iEncoding].wchLabel,COUNTOF(mEncoding[iEncoding].wchLabel), + wch2,COUNTOF(mEncoding[iEncoding].wchLabel)); + } +} + + +int Encoding_MatchW(LPCWSTR pwszTest) { + char tchTest[256] = { '\0' }; + WideCharToMultiByteStrg(CP_ACP,pwszTest,tchTest); + return(Encoding_MatchA(tchTest)); +} + + +int Encoding_MatchA(char *pchTest) { + char chTest[256] = { '\0' }; + char *pchSrc = pchTest; + char *pchDst = chTest; + *pchDst++ = ','; + while (*pchSrc) { + if (IsCharAlphaNumericA(*pchSrc)) + *pchDst++ = *CharLowerA(pchSrc); + pchSrc++; + } + *pchDst++ = ','; + *pchDst = 0; + for (int i = 0; i < COUNTOF(mEncoding); i++) { + if (StrStrIA(mEncoding[i].pszParseNames,chTest)) { + CPINFO cpi; + if ((mEncoding[i].uFlags & NCP_INTERNAL) || + IsValidCodePage(mEncoding[i].uCodePage) && + GetCPInfo(mEncoding[i].uCodePage,&cpi)) + return(i); + else + return(-1); + } + } + return(-1); +} + + +int Encoding_GetByCodePage(UINT cp) { + for (int i = 0; i < COUNTOF(mEncoding); i++) { + if (cp == mEncoding[i].uCodePage) { + return i; + } + } + return CPI_ANSI_DEFAULT; +} + + +BOOL Encoding_IsValid(int iTestEncoding) { + CPINFO cpi; + if ((iTestEncoding >= 0) && (iTestEncoding < COUNTOF(mEncoding))) { + if ((mEncoding[iTestEncoding].uFlags & NCP_INTERNAL) || + IsValidCodePage(mEncoding[iTestEncoding].uCodePage) && + GetCPInfo(mEncoding[iTestEncoding].uCodePage,&cpi)) { + return(TRUE); + } + } + return(FALSE); +} + + +typedef struct _ee { + int id; + WCHAR wch[256]; +} ENCODINGENTRY,*PENCODINGENTRY; + +int CmpEncoding(const void *s1,const void *s2) { + return StrCmp(((PENCODINGENTRY)s1)->wch,((PENCODINGENTRY)s2)->wch); +} + +void Encoding_AddToListView(HWND hwnd,int idSel,BOOL bRecodeOnly) { + int i; + int iSelItem = -1; + LVITEM lvi; + WCHAR wchBuf[256] = { L'\0' }; + + PENCODINGENTRY pEE = LocalAlloc(LPTR,COUNTOF(mEncoding) * sizeof(ENCODINGENTRY)); + for (i = 0; i < COUNTOF(mEncoding); i++) { + pEE[i].id = i; + GetString(mEncoding[i].idsName,pEE[i].wch,COUNTOF(pEE[i].wch)); + } + qsort(pEE,COUNTOF(mEncoding),sizeof(ENCODINGENTRY),CmpEncoding); + + ZeroMemory(&lvi,sizeof(LVITEM)); + lvi.mask = LVIF_PARAM | LVIF_TEXT | LVIF_IMAGE; + lvi.pszText = wchBuf; + + for (i = 0; i < COUNTOF(mEncoding); i++) { + + int id = pEE[i].id; + if (!bRecodeOnly || (mEncoding[id].uFlags & NCP_RECODE)) { + + lvi.iItem = ListView_GetItemCount(hwnd); + + WCHAR *pwsz = StrChr(pEE[i].wch,L';'); + if (pwsz) { + StringCchCopyN(wchBuf,COUNTOF(wchBuf),CharNext(pwsz),COUNTOF(wchBuf)); + pwsz = StrChr(wchBuf,L';'); + if (pwsz) + *pwsz = 0; + } + else + StringCchCopyN(wchBuf,COUNTOF(wchBuf),pEE[i].wch,COUNTOF(wchBuf)); + + if (Encoding_IsANSI(id)) + StringCchCatN(wchBuf,COUNTOF(wchBuf),wchANSI,COUNTOF(wchANSI)); + else if (Encoding_IsOEM(id)) + StringCchCatN(wchBuf,COUNTOF(wchBuf),wchOEM,COUNTOF(wchOEM)); + + if (Encoding_IsValid(id)) + lvi.iImage = 0; + else + lvi.iImage = 1; + + lvi.lParam = (LPARAM)id; + ListView_InsertItem(hwnd,&lvi); + + if (idSel == id) + iSelItem = lvi.iItem; + } + } + + LocalFree(pEE); + + if (iSelItem != -1) { + ListView_SetItemState(hwnd,iSelItem,LVIS_SELECTED | LVIS_FOCUSED,LVIS_SELECTED | LVIS_FOCUSED); + ListView_EnsureVisible(hwnd,iSelItem,FALSE); + } + else { + ListView_SetItemState(hwnd,0,LVIS_FOCUSED,LVIS_FOCUSED); + ListView_EnsureVisible(hwnd,0,FALSE); + } +} + + +BOOL Encoding_GetFromListView(HWND hwnd,int *pidEncoding) { + LVITEM lvi; + + lvi.iItem = ListView_GetNextItem(hwnd,-1,LVNI_ALL | LVNI_SELECTED); + lvi.iSubItem = 0; + lvi.mask = LVIF_PARAM; + + if (ListView_GetItem(hwnd,&lvi)) { + if (Encoding_IsValid((int)lvi.lParam)) + *pidEncoding = (int)lvi.lParam; + else + *pidEncoding = -1; + + return (TRUE); + } + return(FALSE); +} + + +void Encoding_AddToComboboxEx(HWND hwnd,int idSel,BOOL bRecodeOnly) { + int i; + int iSelItem = -1; + COMBOBOXEXITEM cbei; + WCHAR wchBuf[256] = { L'\0' }; + + PENCODINGENTRY pEE = LocalAlloc(LPTR,COUNTOF(mEncoding) * sizeof(ENCODINGENTRY)); + for (i = 0; i < COUNTOF(mEncoding); i++) { + pEE[i].id = i; + GetString(mEncoding[i].idsName,pEE[i].wch,COUNTOF(pEE[i].wch)); + } + qsort(pEE,COUNTOF(mEncoding),sizeof(ENCODINGENTRY),CmpEncoding); + + ZeroMemory(&cbei,sizeof(COMBOBOXEXITEM)); + cbei.mask = CBEIF_TEXT | CBEIF_IMAGE | CBEIF_SELECTEDIMAGE | CBEIF_LPARAM; + cbei.pszText = wchBuf; + cbei.cchTextMax = COUNTOF(wchBuf); + cbei.iImage = 0; + cbei.iSelectedImage = 0; + + for (i = 0; i < COUNTOF(mEncoding); i++) { + + int id = pEE[i].id; + if (!bRecodeOnly || (mEncoding[id].uFlags & NCP_RECODE)) { + + CPINFO cpi; + + cbei.iItem = SendMessage(hwnd,CB_GETCOUNT,0,0); + + WCHAR *pwsz = StrChr(pEE[i].wch,L';'); + if (pwsz) { + StringCchCopyN(wchBuf,COUNTOF(wchBuf),CharNext(pwsz),COUNTOF(wchBuf)); + pwsz = StrChr(wchBuf,L';'); + if (pwsz) + *pwsz = 0; + } + else + StringCchCopyN(wchBuf,COUNTOF(wchBuf),pEE[i].wch,COUNTOF(wchBuf)); + + if (Encoding_IsANSI(id)) + StringCchCatN(wchBuf,COUNTOF(wchBuf),wchANSI,COUNTOF(wchANSI)); + else if (id == CPI_OEM) + StringCchCatN(wchBuf,COUNTOF(wchBuf),wchOEM,COUNTOF(wchOEM)); + + if ((mEncoding[id].uFlags & NCP_INTERNAL) || + (IsValidCodePage(mEncoding[id].uCodePage) && + GetCPInfo(mEncoding[id].uCodePage,&cpi))) + cbei.iImage = 0; + else + cbei.iImage = 1; + + cbei.lParam = (LPARAM)id; + SendMessage(hwnd,CBEM_INSERTITEM,0,(LPARAM)&cbei); + + if (idSel == id) + iSelItem = (int)cbei.iItem; + } + } + + LocalFree(pEE); + + if (iSelItem != -1) + SendMessage(hwnd,CB_SETCURSEL,(WPARAM)iSelItem,0); +} + + +BOOL Encoding_GetFromComboboxEx(HWND hwnd,int *pidEncoding) { + COMBOBOXEXITEM cbei; + + cbei.iItem = SendMessage(hwnd,CB_GETCURSEL,0,0); + cbei.mask = CBEIF_LPARAM; + + if (SendMessage(hwnd,CBEM_GETITEM,0,(LPARAM)&cbei)) { + if (Encoding_IsValid((int)cbei.lParam)) + *pidEncoding = (int)cbei.lParam; + else + *pidEncoding = -1; + + return (TRUE); + } + return(FALSE); +} + + +BOOL Encoding_IsDefault(int iEncoding) { + return (mEncoding[iEncoding].uFlags & NCP_DEFAULT); +} + +BOOL Encoding_IsANSI(int iEncoding) { + return (mEncoding[iEncoding].uFlags & NCP_ANSI); +} + +BOOL Encoding_IsOEM(int iEncoding) { + return (mEncoding[iEncoding].uFlags & NCP_OEM); +} + +UINT Encoding_SciGetCodePage(HWND hwnd) { + // remove internal support for Chinese, Japan, Korean DBCS use UTF-8 instead + /* + int cp = (UINT)SendMessage(hwnd,SCI_GETCODEPAGE,0,0); + if (cp == 932 || cp == 936 || cp == 949 || cp == 950) { + return cp; + } + return (cp == 0) ? CP_ACP : CP_UTF8; + */ + UNUSED(hwnd); + return CP_UTF8; +} + + +int Encoding_SciMappedCodePage(int iEncoding) { + // remove internal support for Chinese, Japan, Korean DBCS use UTF-8 instead + /* + if (Encoding_IsValid(iEncoding)) { + // check for Chinese, Japan, Korean DBCS code pages and switch accordingly + int cp = (int)mEncoding[iEncoding].uCodePage; + if (cp == 932 || cp == 936 || cp == 949 || cp == 950) { + return cp; + } + } + */ + UNUSED(iEncoding); + return SC_CP_UTF8; +} + + +void Encoding_SciSetCodePage(HWND hwnd,int iEncoding) { + int cp = Encoding_SciMappedCodePage(iEncoding); + SendMessage(hwnd,SCI_SETCODEPAGE,(WPARAM)cp,0); + // charsets can be changed via styles schema + /* + int charset = SC_CHARSET_ANSI; + switch (cp) { + case 932: + charset = SC_CHARSET_SHIFTJIS; + break; + case 936: + charset = SC_CHARSET_GB2312; + break; + case 949: + charset = SC_CHARSET_HANGUL; + break; + case 950: + charset = SC_CHARSET_CHINESEBIG5; + break; + default: + charset = iDefaultCharSet; + break; + } + SendMessage(hwnd,SCI_STYLESETCHARACTERSET,(WPARAM)STYLE_DEFAULT,(LPARAM)charset); + */ +} + + +BOOL IsUnicode(const char* pBuffer,int cb,LPBOOL lpbBOM,LPBOOL lpbReverse) { + int i = 0xFFFF; + + BOOL bIsTextUnicode; + + BOOL bHasBOM; + BOOL bHasRBOM; + + if (!pBuffer || cb < 2) + return FALSE; + + if (!bSkipUnicodeDetection) + bIsTextUnicode = IsTextUnicode(pBuffer,cb,&i); + else + bIsTextUnicode = FALSE; + + bHasBOM = (*((UNALIGNED PWCHAR)pBuffer) == 0xFEFF); + bHasRBOM = (*((UNALIGNED PWCHAR)pBuffer) == 0xFFFE); + + if (i == 0xFFFF) // i doesn't seem to have been modified ... + i = 0; + + if (bIsTextUnicode || bHasBOM || bHasRBOM || + ((i & (IS_TEXT_UNICODE_UNICODE_MASK | IS_TEXT_UNICODE_REVERSE_MASK)) && + !((i & IS_TEXT_UNICODE_UNICODE_MASK) && (i & IS_TEXT_UNICODE_REVERSE_MASK)) && + !(i & IS_TEXT_UNICODE_ODD_LENGTH) && + !(i & IS_TEXT_UNICODE_ILLEGAL_CHARS && !(i & IS_TEXT_UNICODE_REVERSE_SIGNATURE)) && + !((i & IS_TEXT_UNICODE_REVERSE_MASK) == IS_TEXT_UNICODE_REVERSE_STATISTICS))) { + + if (lpbBOM) + *lpbBOM = (bHasBOM || bHasRBOM || + (i & (IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE))) + ? TRUE : FALSE; + + if (lpbReverse) + *lpbReverse = (bHasRBOM || (i & IS_TEXT_UNICODE_REVERSE_MASK)) ? TRUE : FALSE; + + return TRUE; + } + + else + + return FALSE; +} + + +BOOL IsUTF8(const char* pTest,int nLength) { + static int byte_class_table[256] = { + /* 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F */ + /* 00 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + /* 10 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + /* 20 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + /* 30 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + /* 40 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + /* 50 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + /* 60 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + /* 70 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + /* 80 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + /* 90 */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + /* A0 */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + /* B0 */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + /* C0 */ 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + /* D0 */ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + /* E0 */ 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 7, 7, + /* F0 */ 9,10,10,10,11, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 + /* 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F */ }; + + /* state table */ + typedef enum { + kSTART = 0,kA,kB,kC,kD,kE,kF,kG,kERROR,kNumOfStates + } utf8_state; + + static utf8_state state_table[] = { + /* kSTART, kA, kB, kC, kD, kE, kF, kG, kERROR */ + /* 0x00-0x7F: 0 */ kSTART, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, + /* 0x80-0x8F: 1 */ kERROR, kSTART, kA, kERROR, kA, kB, kERROR, kB, kERROR, + /* 0x90-0x9f: 2 */ kERROR, kSTART, kA, kERROR, kA, kB, kB, kERROR, kERROR, + /* 0xa0-0xbf: 3 */ kERROR, kSTART, kA, kA, kERROR, kB, kB, kERROR, kERROR, + /* 0xc0-0xc1, 0xf5-0xff: 4 */ kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, + /* 0xc2-0xdf: 5 */ kA, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, + /* 0xe0: 6 */ kC, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, + /* 0xe1-0xec, 0xee-0xef: 7 */ kB, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, + /* 0xed: 8 */ kD, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, + /* 0xf0: 9 */ kF, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, + /* 0xf1-0xf3: 10 */ kE, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, + /* 0xf4: 11 */ kG, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR, kERROR }; + +#define BYTE_CLASS(b) (byte_class_table[(unsigned char)b]) +#define NEXT_STATE(b,cur) (state_table[(BYTE_CLASS(b) * kNumOfStates) + (cur)]) + + utf8_state current = kSTART; + int i; + + const char* pt = pTest; + int len = nLength; + + for (i = 0; i < len; i++,pt++) { + + current = NEXT_STATE(*pt,current); + if (kERROR == current) + break; + } + + return (current == kSTART) ? TRUE : FALSE; +} + + +BOOL IsUTF7(const char* pTest,int nLength) { + int i; + const char *pt = pTest; + + for (i = 0; i < nLength; i++) { + if (*pt & 0x80 || !*pt) + return FALSE; + pt++; + } + + return TRUE; +} + + +/* byte length of UTF-8 sequence based on value of first byte. +for UTF-16 (21-bit space), max. code length is 4, so we only need to look +at 4 upper bits. +*/ +static const INT utf8_lengths[16] = +{ + 1,1,1,1,1,1,1,1, /* 0000 to 0111 : 1 byte (plain ASCII) */ + 0,0,0,0, /* 1000 to 1011 : not valid */ + 2,2, /* 1100, 1101 : 2 bytes */ + 3, /* 1110 : 3 bytes */ + 4 /* 1111 :4 bytes */ +}; + +/*++ +Function : +UTF8_mbslen_bytes [INTERNAL] + +Calculates the byte size of a NULL-terminated UTF-8 string. + +Parameters : +char *utf8_string : string to examine + +Return value : +size (in bytes) of a NULL-terminated UTF-8 string. +-1 if invalid NULL-terminated UTF-8 string +--*/ +INT UTF8_mbslen_bytes(LPCSTR utf8_string) +{ + INT length = 0; + INT code_size; + BYTE byte; + + while (*utf8_string) { + byte = (BYTE)*utf8_string; + + if ((byte <= 0xF7) && (0 != (code_size = utf8_lengths[byte >> 4]))) { + length += code_size; + utf8_string += code_size; + } + else { + /* we got an invalid byte value but need to count it, + it will be later ignored during the string conversion */ + //WARN("invalid first byte value 0x%02X in UTF-8 sequence!\n",byte); + length++; + utf8_string++; + } + } + length++; /* include NULL terminator */ + return length; +} + +/*++ +Function : +UTF8_mbslen [INTERNAL] + +Calculates the character size of a NULL-terminated UTF-8 string. + +Parameters : +char *utf8_string : string to examine +int byte_length : byte size of string + +Return value : +size (in characters) of a UTF-8 string. +-1 if invalid UTF-8 string +--*/ +INT UTF8_mbslen(LPCSTR source,INT byte_length) +{ + INT wchar_length = 0; + INT code_size; + BYTE byte; + + while (byte_length > 0) { + byte = (BYTE)*source; + + /* UTF-16 can't encode 5-byte and 6-byte sequences, so maximum value + for first byte is 11110111. Use lookup table to determine sequence + length based on upper 4 bits of first byte */ + if ((byte <= 0xF7) && (0 != (code_size = utf8_lengths[byte >> 4]))) { + /* 1 sequence == 1 character */ + wchar_length++; + + if (code_size == 4) + wchar_length++; + + source += code_size; /* increment pointer */ + byte_length -= code_size; /* decrement counter*/ + } + else { + /* + unlike UTF8_mbslen_bytes, we ignore the invalid characters. + we only report the number of valid characters we have encountered + to match the Windows behavior. + */ + //WARN("invalid byte 0x%02X in UTF-8 sequence, skipping it!\n", + // byte); + source++; + byte_length--; + } + } + return wchar_length; +} + /// End of Helpers.c \\\ diff --git a/src/Helpers.h b/src/Helpers.h index 95845b9dd..78cc53d8e 100644 --- a/src/Helpers.h +++ b/src/Helpers.h @@ -225,9 +225,6 @@ int MRU_Enum(LPMRULIST,int,LPWSTR,int); BOOL MRU_Load(LPMRULIST); BOOL MRU_Save(LPMRULIST); BOOL MRU_MergeSave(LPMRULIST,BOOL,BOOL,BOOL); -BOOL MRU_SetEnc(LPMRULIST,int,int); -BOOL MRU_SetPos(LPMRULIST,int,int); - //==== Themed Dialogs ========================================================= @@ -340,6 +337,88 @@ inline int _StringCchCmpINW(PCNZWCH s1,int l1,PCNZWCH s2,int l2) { #define StringCchCompareIX(s1,s2) StringCchCompareIXA((s1),(s2)) #endif +// ===== File Encoding ===== + +extern int g_DOSEncoding; + +#define NCP_DEFAULT 1 +#define NCP_UTF8 2 +#define NCP_UTF8_SIGN 4 +#define NCP_UNICODE 8 +#define NCP_UNICODE_REVERSE 16 +#define NCP_UNICODE_BOM 32 +#define NCP_8BIT 64 +#define NCP_ANSI 128 +#define NCP_OEM 256 +#define NCP_MBCS 512 +#define NCP_INTERNAL (NCP_DEFAULT|NCP_UTF8|NCP_UTF8_SIGN|NCP_UNICODE|NCP_UNICODE_REVERSE|NCP_UNICODE_BOM|NCP_ANSI|NCP_OEM|NCP_MBCS) +#define NCP_RECODE 1024 + +#define CPI_GET -2 +#define CPI_NONE -1 +#define CPI_ANSI_DEFAULT 0 +#define CPI_OEM 1 +#define CPI_UNICODEBOM 2 +#define CPI_UNICODEBEBOM 3 +#define CPI_UNICODE 4 +#define CPI_UNICODEBE 5 +#define CPI_UTF8 6 +#define CPI_UTF8SIGN 7 +#define CPI_UTF7 8 + +#define IDS_ENCODINGNAME0 61000 +#define IDS_EOLMODENAME0 62000 + +typedef struct _np2encoding { + UINT uFlags; + UINT uCodePage; + char* pszParseNames; + int idsName; + WCHAR wchLabel[64]; +} NP2ENCODING; + +int Encoding_CountOf(); +int Encoding_Current(int); // getter/setter +int Encoding_Source(int); // getter/setter +int Encoding_SrcWeak(int); // getter/setter +BOOL Encoding_HasChanged(int); // query/setter + +void Encoding_InitDefaults(); +int Encoding_MapIniSetting(BOOL,int); +int Encoding_MapUnicode(int); +void Encoding_GetLabel(int); +int Encoding_MatchW(LPCWSTR); +int Encoding_MatchA(char*); +BOOL Encoding_IsValid(int); +int Encoding_GetByCodePage(UINT); +void Encoding_AddToListView(HWND,int,BOOL); +BOOL Encoding_GetFromListView(HWND,int *); +void Encoding_AddToComboboxEx(HWND,int,BOOL); +BOOL Encoding_GetFromComboboxEx(HWND,int *); +BOOL Encoding_IsDefault(int); +BOOL Encoding_IsANSI(int); +BOOL Encoding_IsOEM(int); + +UINT Encoding_SciGetCodePage(HWND); +int Encoding_SciMappedCodePage(int); +void Encoding_SciSetCodePage(HWND,int); + + +BOOL IsUnicode(const char*,int,LPBOOL,LPBOOL); +BOOL IsUTF8(const char*,int); +BOOL IsUTF7(const char*,int); + +#define IsUTF8Signature(p) \ + ((*(p+0) == '\xEF' && *(p+1) == '\xBB' && *(p+2) == '\xBF')) + + +#define UTF8StringStart(p) \ + (IsUTF8Signature(p)) ? (p+3) : (p) + +INT UTF8_mbslen_bytes(LPCSTR utf8_string); +INT UTF8_mbslen(LPCSTR source,INT byte_length); + +// -------------------------------------------------------------------------------------------------------------------------------- // including and linking against pathcch.lib // api-ms-win-core-path-l1-1-0.dll library : Minimum supported client is Windows 8 :-/ diff --git a/src/Notepad3.c b/src/Notepad3.c index 7daec145d..dd3a2fe73 100644 --- a/src/Notepad3.c +++ b/src/Notepad3.c @@ -2119,6 +2119,11 @@ void MsgChangeNotify(HWND hwnd, WPARAM wParam, LPARAM lParam) Encoding_SrcWeak(Encoding_Current(CPI_GET)); + int idx,iCaretPos = 0; + if (MRU_FindFile(pFileMRU,szCurFile,&idx)) { + iCaretPos = pFileMRU->iCaretPos[idx]; + } + if (FileLoad(TRUE, FALSE, TRUE, FALSE, szCurFile)) { if (bIsTail && iFileWatchingMode == 2) { @@ -2137,6 +2142,10 @@ void MsgChangeNotify(HWND hwnd, WPARAM wParam, LPARAM lParam) SendMessage(hwndEdit, SCI_LINESCROLL, 0, (LPARAM)iVisTopLine - iNewTopLine); SendMessage(hwndEdit, SCI_SETXOFFSET, (WPARAM)iXOffset, 0); } + // set historic caret pos + else if (iCaretPos > 0) { + SendMessage(hwndEdit,SCI_GOTOPOS,(WPARAM)iCaretPos,0); + } } } } @@ -2549,6 +2558,11 @@ LRESULT MsgCommand(HWND hwnd,WPARAM wParam,LPARAM lParam) Encoding_SrcWeak(Encoding_Current(CPI_GET)); + int idx,iCaretPos = 0; + if (MRU_FindFile(pFileMRU,tchCurFile2,&idx)) { + iCaretPos = pFileMRU->iCaretPos[idx]; + } + if (FileLoad(TRUE,FALSE,TRUE,FALSE,tchCurFile2)) { if (SendMessage(hwndEdit,SCI_GETLENGTH,0,0) >= 4) { @@ -2562,6 +2576,10 @@ LRESULT MsgCommand(HWND hwnd,WPARAM wParam,LPARAM lParam) SendMessage(hwndEdit,SCI_LINESCROLL,0,(LPARAM)iVisTopLine - iNewTopLine); SendMessage(hwndEdit,SCI_SETXOFFSET,(WPARAM)iXOffset,0); } + // set historic caret pos + else if (iCaretPos > 0) { + SendMessage(hwndEdit,SCI_GOTOPOS,(WPARAM)iCaretPos,0); + } } } } @@ -2959,14 +2977,7 @@ LRESULT MsgCommand(HWND hwnd,WPARAM wParam,LPARAM lParam) WCHAR tchCurFile2[MAX_PATH] = { L'\0' }; - int iNewEncoding = Encoding_Current(CPI_GET); - - if (iNewEncoding == CPI_UTF8SIGN) - iNewEncoding = CPI_UTF8; - else if (iNewEncoding == CPI_UNICODEBOM) - iNewEncoding = CPI_UNICODE; - else if (iNewEncoding == CPI_UNICODEBEBOM) - iNewEncoding = CPI_UNICODEBE; + int iNewEncoding = Encoding_MapUnicode(Encoding_Current(CPI_GET)); if ((bModified || Encoding_HasChanged(CPI_GET)) && MsgBox(MBOKCANCEL,IDS_ASK_RECODE) != IDOK) return(0); @@ -4796,14 +4807,7 @@ LRESULT MsgCommand(HWND hwnd,WPARAM wParam,LPARAM lParam) { WCHAR tchCurFile2[MAX_PATH] = { L'\0' }; if (StringCchLen(szCurFile)) { - if (iDefaultEncoding == CPI_UNICODEBOM) - Encoding_Source(CPI_UNICODE); - else if (iDefaultEncoding == CPI_UNICODEBEBOM) - Encoding_Source(CPI_UNICODEBE); - else if (iDefaultEncoding == CPI_UTF8SIGN) - Encoding_Source(CPI_UTF8); - else - Encoding_Source(iDefaultEncoding); + Encoding_Source(Encoding_MapUnicode(iDefaultEncoding)); StringCchCopy(tchCurFile2,COUNTOF(tchCurFile2),szCurFile); FileLoad(FALSE,FALSE,TRUE,TRUE,tchCurFile2); } @@ -7433,7 +7437,7 @@ int UndoRedoSelectionMap(int token, UndoRedoSelection_t* selection) // FileIO() // // -BOOL FileIO(BOOL fLoad,LPCWSTR psz,BOOL bNoEncDetect,int *ienc,int *ieol, +BOOL FileIO(BOOL fLoad,LPCWSTR pszFileName,BOOL bNoEncDetect,int *ienc,int *ieol, BOOL *pbUnicodeErr,BOOL *pbFileTooBig, BOOL *pbCancelDataLoss,BOOL bSaveCopy) { @@ -7443,7 +7447,7 @@ BOOL FileIO(BOOL fLoad,LPCWSTR psz,BOOL bNoEncDetect,int *ienc,int *ieol, BeginWaitCursor(); - FormatString(tch,COUNTOF(tch),(fLoad) ? IDS_LOADFILE : IDS_SAVEFILE,PathFindFileName(psz)); + FormatString(tch,COUNTOF(tch),(fLoad) ? IDS_LOADFILE : IDS_SAVEFILE,PathFindFileName(pszFileName)); StatusSetText(hwndStatus,STATUS_HELP,tch); StatusSetSimple(hwndStatus,TRUE); @@ -7451,12 +7455,19 @@ BOOL FileIO(BOOL fLoad,LPCWSTR psz,BOOL bNoEncDetect,int *ienc,int *ieol, InvalidateRect(hwndStatus,NULL,TRUE); UpdateWindow(hwndStatus); - if (fLoad) - fSuccess = EditLoadFile(hwndEdit,psz,bNoEncDetect,ienc,ieol,pbUnicodeErr,pbFileTooBig); - else - fSuccess = EditSaveFile(hwndEdit,psz,*ienc,pbCancelDataLoss,bSaveCopy); + if (fLoad) { + fSuccess = EditLoadFile(hwndEdit,pszFileName,bNoEncDetect,ienc,ieol,pbUnicodeErr,pbFileTooBig); + } + else { + int idx; + if (MRU_FindFile(pFileMRU,pszFileName,&idx)) { + pFileMRU->iEncoding[idx] = *ienc; + pFileMRU->iCaretPos[idx] = (int)SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0); + } + fSuccess = EditSaveFile(hwndEdit,pszFileName,*ienc,pbCancelDataLoss,bSaveCopy); + } - dwFileAttributes = GetFileAttributes(psz); + dwFileAttributes = GetFileAttributes(pszFileName); bReadOnly = (dwFileAttributes != INVALID_FILE_ATTRIBUTES && dwFileAttributes & FILE_ATTRIBUTE_READONLY); StatusSetSimple(hwndStatus,FALSE); @@ -7479,6 +7490,7 @@ BOOL FileLoad(BOOL bDontSave,BOOL bNew,BOOL bReload,BOOL bNoEncDetect,LPCWSTR lp BOOL fSuccess; BOOL bUnicodeErr = FALSE; BOOL bFileTooBig = FALSE; + int fileEncoding = CPI_ANSI_DEFAULT; if (!bDontSave) { @@ -7568,8 +7580,9 @@ BOOL FileLoad(BOOL bDontSave,BOOL bNew,BOOL bReload,BOOL bNoEncDetect,LPCWSTR lp iEOLMode = iLineEndings[iDefaultEOLMode]; SendMessage(hwndEdit,SCI_SETEOLMODE,iLineEndings[iDefaultEOLMode],0); if (Encoding_Source(CPI_GET) != CPI_NONE) { - Encoding_Current(Encoding_Source(CPI_GET)); - Encoding_HasChanged(Encoding_Source(CPI_GET)); + fileEncoding = Encoding_Source(CPI_GET); + Encoding_Current(fileEncoding); + Encoding_HasChanged(fileEncoding); } else { Encoding_Current(iDefaultEncoding); @@ -7587,7 +7600,15 @@ BOOL FileLoad(BOOL bDontSave,BOOL bNew,BOOL bReload,BOOL bNoEncDetect,LPCWSTR lp return FALSE; } else { - int fileEncoding = Encoding_Current(CPI_GET); + int idx; + if (!bReload && MRU_FindFile(pFileMRU,szFileName,&idx)) { + fileEncoding = pFileMRU->iEncoding[idx]; + if (fileEncoding > 0) + Encoding_Source(Encoding_MapUnicode(fileEncoding)); + } + else + fileEncoding = Encoding_Current(CPI_GET); + fSuccess = FileIO(TRUE,szFileName,bNoEncDetect,&fileEncoding,&iEOLMode,&bUnicodeErr,&bFileTooBig,NULL,FALSE); if (fSuccess) Encoding_Current(fileEncoding); // load may change encoding @@ -7601,11 +7622,16 @@ BOOL FileLoad(BOOL bDontSave,BOOL bNew,BOOL bReload,BOOL bNoEncDetect,LPCWSTR lp if (!flagLexerSpecified) // flag will be cleared Style_SetLexerFromFile(hwndEdit,szCurFile); UpdateLineNumberWidth(); - Encoding_HasChanged(Encoding_Current(CPI_GET)); bModified = FALSE; //bReadOnly = FALSE; SendMessage(hwndEdit,SCI_SETEOLMODE,iEOLMode,0); - MRU_AddFile(pFileMRU,szFileName,flagRelativeFileMRU,flagPortableMyDocs,0,0); + fileEncoding = Encoding_Current(CPI_GET); + Encoding_HasChanged(fileEncoding); + int idx, iCaretPos = 0; + if (MRU_FindFile(pFileMRU,szFileName,&idx)) { + iCaretPos = pFileMRU->iCaretPos[idx]; + } + MRU_AddFile(pFileMRU,szFileName,flagRelativeFileMRU,flagPortableMyDocs,fileEncoding,iCaretPos); if (flagUseSystemMRU == 2) SHAddToRecentDocs(SHARD_PATHW,szFileName); @@ -7618,7 +7644,7 @@ BOOL FileLoad(BOOL bDontSave,BOOL bNew,BOOL bReload,BOOL bNoEncDetect,LPCWSTR lp // the .LOG feature ... if (SendMessage(hwndEdit,SCI_GETLENGTH,0,0) >= 4) { - char tchLog[5] = ""; + char tchLog[5] = { '\0' }; SendMessage(hwndEdit,SCI_GETTEXT,5,(LPARAM)tchLog); if (StringCchCompareXA(tchLog,".LOG") == 0) { EditJumpTo(hwndEdit,-1,0); @@ -7631,8 +7657,11 @@ BOOL FileLoad(BOOL bDontSave,BOOL bNew,BOOL bReload,BOOL bNoEncDetect,LPCWSTR lp EditJumpTo(hwndEdit,-1,0); EditEnsureSelectionVisible(hwndEdit); } + // set historic caret pos + else if (iCaretPos > 0) { + SendMessage(hwndEdit,SCI_GOTOPOS,(WPARAM)iCaretPos,0); + } } - // consistent settings file handling (if loaded in editor) bEnableSaveSettings = (StringCchCompareI(szCurFile, szIniFile) == 0) ? FALSE : TRUE; UpdateSettingsCmds(); @@ -7675,8 +7704,14 @@ BOOL FileSave(BOOL bSaveAlways,BOOL bAsk,BOOL bSaveAs,BOOL bSaveCopy) } } - if (!bSaveAlways && (!bModified && !Encoding_HasChanged(CPI_GET) || bIsEmptyNewFile) && !bSaveAs) + if (!bSaveAlways && (!bModified && !Encoding_HasChanged(CPI_GET) || bIsEmptyNewFile) && !bSaveAs) { + int idx; + if (MRU_FindFile(pFileMRU,szCurFile,&idx)) { + pFileMRU->iEncoding[idx] = Encoding_Current(CPI_GET); + pFileMRU->iCaretPos[idx] = (int)SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0); + } return TRUE; + } if (bAsk) { @@ -7749,7 +7784,6 @@ BOOL FileSave(BOOL bSaveAlways,BOOL bAsk,BOOL bSaveAs,BOOL bSaveCopy) else return FALSE; } - else { int fileEncoding = Encoding_Current(CPI_GET); fSuccess = FileIO(FALSE,szCurFile,FALSE,&fileEncoding,&iEOLMode,NULL,NULL,&bCancelDataLoss,FALSE); @@ -7763,8 +7797,8 @@ BOOL FileSave(BOOL bSaveAlways,BOOL bAsk,BOOL bSaveAs,BOOL bSaveCopy) bModified = FALSE; int iCurrEnc = Encoding_Current(CPI_GET); Encoding_HasChanged(iCurrEnc); - int mpEnc = Encoding_MapIniSetting(FALSE,iCurrEnc); - MRU_AddFile(pFileMRU,szCurFile,flagRelativeFileMRU,flagPortableMyDocs,mpEnc,0); + int iCaretPos = (int)SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0); + MRU_AddFile(pFileMRU,szCurFile,flagRelativeFileMRU,flagPortableMyDocs,iCurrEnc,iCaretPos); if (flagUseSystemMRU == 2) SHAddToRecentDocs(SHARD_PATHW,szCurFile); UpdateToolbar(); @@ -7789,14 +7823,8 @@ BOOL FileSave(BOOL bSaveAlways,BOOL bAsk,BOOL bSaveAs,BOOL bSaveCopy) if (GetTempPath(MAX_PATH,lpTempPathBuffer) && GetTempFileName(lpTempPathBuffer,TEXT("NP3"),0,szTempFileName)) { int fileEncoding = Encoding_Current(CPI_GET); - int idx; - if (MRU_FindFile(pFileMRU,tchFile,&idx)) { - MRU_SetEnc(pFileMRU,idx,Encoding_MapIniSetting(FALSE,fileEncoding)); - MRU_SetPos(pFileMRU,idx,(int)SendMessage(hwndEdit,SCI_GETCURRENTPOS,0,0)); - } if (FileIO(FALSE,szTempFileName,FALSE,&fileEncoding,&iEOLMode,NULL,NULL,&bCancelDataLoss,TRUE)) { //~Encoding_Current(fileEncoding); // save should not change encoding - WCHAR szArguments[2048] = { L'\0' }; LPWSTR lpCmdLine = GetCommandLine(); int wlen = lstrlen(lpCmdLine) + 2;