vi: code shrink print_literal()

Simplify the function print_literal() which is used to format a
string that may contain unprintable characters or control
characters.

- Unprintable characters were being displayed in normal text rather
  than the bold used for the rest of the message.  This doesn't seem
  particularly helpful and it upsets the calculation of the width
  of the message in show_status_line().  Use '?' rather than '.' for
  unprintable characters.

- Newlines in the string were displayed as both '^J' and '$', which
  is somewhat redundant.

function                                             old     new   delta
not_implemented                                      199     108     -91
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-91)             Total: -91 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Ron Yorston 2021-08-21 14:03:55 +01:00 committed by Denys Vlasenko
parent 08ad934ac4
commit 74c4f356ae

View File

@ -1377,21 +1377,14 @@ static void print_literal(char *buf, const char *s)
char *d;
unsigned char c;
buf[0] = '\0';
if (!s[0])
s = "(NULL)";
d = buf;
for (; *s; s++) {
int c_is_no_print;
c = *s;
c_is_no_print = (c & 0x80) && !Isprint(c);
if (c_is_no_print) {
strcpy(d, ESC_NORM_TEXT);
d += sizeof(ESC_NORM_TEXT)-1;
c = '.';
}
if ((c & 0x80) && !Isprint(c))
c = '?';
if (c < ' ' || c == 0x7f) {
*d++ = '^';
c |= '@'; // 0x40
@ -1400,14 +1393,6 @@ static void print_literal(char *buf, const char *s)
}
*d++ = c;
*d = '\0';
if (c_is_no_print) {
strcpy(d, ESC_BOLD_TEXT);
d += sizeof(ESC_BOLD_TEXT)-1;
}
if (*s == '\n') {
*d++ = '$';
*d = '\0';
}
if (d - buf > MAX_INPUT_LEN - 10) // paranoia
break;
}