ex_cmds: refactor utf8 variables to TriState

- utf8 in helptags_one()
- this_utf in fix_help_buffer()
This commit is contained in:
Jan Edmund Lazo 2018-07-16 03:11:37 -04:00
parent f35df8d697
commit deae2e8434

View File

@ -5055,9 +5055,9 @@ void fix_help_buffer(void)
if (IObuff[0] == '*' if (IObuff[0] == '*'
&& (s = vim_strchr(IObuff + 1, '*')) && (s = vim_strchr(IObuff + 1, '*'))
!= NULL) { != NULL) {
int this_utf = MAYBE; TriState this_utf = kNone;
/* Change tag definition to a // Change tag definition to a
* reference and remove <CR>/<NL>. */ // reference and remove <CR>/<NL>.
IObuff[0] = '|'; IObuff[0] = '|';
*s = '|'; *s = '|';
while (*s != NUL) { while (*s != NUL) {
@ -5067,13 +5067,12 @@ void fix_help_buffer(void)
* above 127 is found and no * above 127 is found and no
* illegal byte sequence is found. * illegal byte sequence is found.
*/ */
if (*s >= 0x80 && this_utf != FALSE) { if (*s >= 0x80 && this_utf != kFalse) {
int l; this_utf = kTrue;
const int l = utf_ptr2len(s);
this_utf = TRUE; if (l == 1) {
l = utf_ptr2len(s); this_utf = kFalse;
if (l == 1) }
this_utf = FALSE;
s += l - 1; s += l - 1;
} }
++s; ++s;
@ -5082,18 +5081,20 @@ void fix_help_buffer(void)
* conversion to the current * conversion to the current
* 'encoding' may be required. */ * 'encoding' may be required. */
vc.vc_type = CONV_NONE; vc.vc_type = CONV_NONE;
convert_setup(&vc, (char_u *)( convert_setup(
this_utf == TRUE ? "utf-8" &vc,
: "latin1"), p_enc); (char_u *)(this_utf == kTrue ? "utf-8" : "latin1"),
if (vc.vc_type == CONV_NONE) p_enc);
/* No conversion needed. */ if (vc.vc_type == CONV_NONE) {
// No conversion needed.
cp = IObuff; cp = IObuff;
else { } else {
/* Do the conversion. If it fails // Do the conversion. If it fails
* use the unconverted text. */ // use the unconverted text.
cp = string_convert(&vc, IObuff, NULL); cp = string_convert(&vc, IObuff, NULL);
if (cp == NULL) if (cp == NULL) {
cp = IObuff; cp = IObuff;
}
} }
convert_setup(&vc, NULL, NULL); convert_setup(&vc, NULL, NULL);
@ -5150,8 +5151,7 @@ static void helptags_one(char_u *dir, char_u *ext, char_u *tagfname,
int fi; int fi;
char_u *s; char_u *s;
char_u *fname; char_u *fname;
int utf8 = MAYBE; TriState utf8 = kNone;
int this_utf8;
int firstline; int firstline;
int mix = FALSE; /* detected mixed encodings */ int mix = FALSE; /* detected mixed encodings */
@ -5220,26 +5220,26 @@ static void helptags_one(char_u *dir, char_u *ext, char_u *tagfname,
firstline = TRUE; firstline = TRUE;
while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int) { while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int) {
if (firstline) { if (firstline) {
/* Detect utf-8 file by a non-ASCII char in the first line. */ // Detect utf-8 file by a non-ASCII char in the first line.
this_utf8 = MAYBE; TriState this_utf8 = kNone;
for (s = IObuff; *s != NUL; ++s) for (s = IObuff; *s != NUL; s++) {
if (*s >= 0x80) { if (*s >= 0x80) {
int l; this_utf8 = kTrue;
const int l = utf_ptr2len(s);
this_utf8 = TRUE;
l = utf_ptr2len(s);
if (l == 1) { if (l == 1) {
/* Illegal UTF-8 byte sequence. */ // Illegal UTF-8 byte sequence.
this_utf8 = FALSE; this_utf8 = kFalse;
break; break;
} }
s += l - 1; s += l - 1;
} }
if (this_utf8 == MAYBE) /* only ASCII characters found */ }
this_utf8 = FALSE; if (this_utf8 == kNone) { // only ASCII characters found
if (utf8 == MAYBE) /* first file */ this_utf8 = kFalse;
}
if (utf8 == kNone) { // first file
utf8 = this_utf8; utf8 = this_utf8;
else if (utf8 != this_utf8) { } else if (utf8 != this_utf8) {
EMSG2(_( EMSG2(_(
"E670: Mix of help file encodings within a language: %s"), "E670: Mix of help file encodings within a language: %s"),
files[fi]); files[fi]);
@ -5316,8 +5316,9 @@ static void helptags_one(char_u *dir, char_u *ext, char_u *tagfname,
} }
} }
if (utf8 == TRUE) if (utf8 == kTrue) {
fprintf(fd_tags, "!_TAG_FILE_ENCODING\tutf-8\t//\n"); fprintf(fd_tags, "!_TAG_FILE_ENCODING\tutf-8\t//\n");
}
/* /*
* Write the tags into the file. * Write the tags into the file.