comparison src/charset.c @ 20782:c4bce986c31a v8.2.0943

patch 8.2.0943: displaying ^M or ^J depends on current buffer Commit: https://github.com/vim/vim/commit/32ee627750e8b7b3fa6516b893e72f6e6af54710 Author: Bram Moolenaar <Bram@vim.org> Date: Wed Jun 10 14:16:49 2020 +0200 patch 8.2.0943: displaying ^M or ^J depends on current buffer Problem: Displaying ^M or ^J depends on current buffer. Solution: Pass the displayed buffer to transchar(). (closes https://github.com/vim/vim/issues/6225)
author Bram Moolenaar <Bram@vim.org>
date Wed, 10 Jun 2020 14:30:04 +0200
parents 6ff992bf4c82
children ee43d943c3bb
comparison
equal deleted inserted replaced
20781:93e66f657101 20782:c4bce986c31a
497 * When chartab_initialized == FALSE don't use g_chartab[]. 497 * When chartab_initialized == FALSE don't use g_chartab[].
498 * Does NOT work for multi-byte characters, c must be <= 255. 498 * Does NOT work for multi-byte characters, c must be <= 255.
499 * Also doesn't work for the first byte of a multi-byte, "c" must be a 499 * Also doesn't work for the first byte of a multi-byte, "c" must be a
500 * character! 500 * character!
501 */ 501 */
502 static char_u transchar_buf[7]; 502 static char_u transchar_charbuf[7];
503 503
504 char_u * 504 char_u *
505 transchar(int c) 505 transchar(int c)
506 { 506 {
507 return transchar_buf(curbuf, c);
508 }
509
510 char_u *
511 transchar_buf(buf_T *buf, int c)
512 {
507 int i; 513 int i;
508 514
509 i = 0; 515 i = 0;
510 if (IS_SPECIAL(c)) // special key code, display as ~@ char 516 if (IS_SPECIAL(c)) // special key code, display as ~@ char
511 { 517 {
512 transchar_buf[0] = '~'; 518 transchar_charbuf[0] = '~';
513 transchar_buf[1] = '@'; 519 transchar_charbuf[1] = '@';
514 i = 2; 520 i = 2;
515 c = K_SECOND(c); 521 c = K_SECOND(c);
516 } 522 }
517 523
518 if ((!chartab_initialized && ( 524 if ((!chartab_initialized && (
522 (c >= ' ' && c <= '~') 528 (c >= ' ' && c <= '~')
523 #endif 529 #endif
524 )) || (c < 256 && vim_isprintc_strict(c))) 530 )) || (c < 256 && vim_isprintc_strict(c)))
525 { 531 {
526 // printable character 532 // printable character
527 transchar_buf[i] = c; 533 transchar_charbuf[i] = c;
528 transchar_buf[i + 1] = NUL; 534 transchar_charbuf[i + 1] = NUL;
529 } 535 }
530 else 536 else
531 transchar_nonprint(transchar_buf + i, c); 537 transchar_nonprint(buf, transchar_charbuf + i, c);
532 return transchar_buf; 538 return transchar_charbuf;
533 } 539 }
534 540
535 /* 541 /*
536 * Like transchar(), but called with a byte instead of a character. Checks 542 * Like transchar(), but called with a byte instead of a character. Checks
537 * for an illegal UTF-8 byte. 543 * for an illegal UTF-8 byte.
539 char_u * 545 char_u *
540 transchar_byte(int c) 546 transchar_byte(int c)
541 { 547 {
542 if (enc_utf8 && c >= 0x80) 548 if (enc_utf8 && c >= 0x80)
543 { 549 {
544 transchar_nonprint(transchar_buf, c); 550 transchar_nonprint(curbuf, transchar_charbuf, c);
545 return transchar_buf; 551 return transchar_charbuf;
546 } 552 }
547 return transchar(c); 553 return transchar(c);
548 } 554 }
549 555
550 /* 556 /*
551 * Convert non-printable character to two or more printable characters in 557 * Convert non-printable character to two or more printable characters in
552 * "buf[]". "buf" needs to be able to hold five bytes. 558 * "buf[]". "charbuf" needs to be able to hold five bytes.
553 * Does NOT work for multi-byte characters, c must be <= 255. 559 * Does NOT work for multi-byte characters, c must be <= 255.
554 */ 560 */
555 void 561 void
556 transchar_nonprint(char_u *buf, int c) 562 transchar_nonprint(buf_T *buf, char_u *charbuf, int c)
557 { 563 {
558 if (c == NL) 564 if (c == NL)
559 c = NUL; // we use newline in place of a NUL 565 c = NUL; // we use newline in place of a NUL
560 else if (c == CAR && get_fileformat(curbuf) == EOL_MAC) 566 else if (c == CAR && get_fileformat(buf) == EOL_MAC)
561 c = NL; // we use CR in place of NL in this case 567 c = NL; // we use CR in place of NL in this case
562 568
563 if (dy_flags & DY_UHEX) // 'display' has "uhex" 569 if (dy_flags & DY_UHEX) // 'display' has "uhex"
564 transchar_hex(buf, c); 570 transchar_hex(charbuf, c);
565 571
566 #ifdef EBCDIC 572 #ifdef EBCDIC
567 // For EBCDIC only the characters 0-63 and 255 are not printable 573 // For EBCDIC only the characters 0-63 and 255 are not printable
568 else if (CtrlChar(c) != 0 || c == DEL) 574 else if (CtrlChar(c) != 0 || c == DEL)
569 #else 575 #else
570 else if (c <= 0x7f) // 0x00 - 0x1f and 0x7f 576 else if (c <= 0x7f) // 0x00 - 0x1f and 0x7f
571 #endif 577 #endif
572 { 578 {
573 buf[0] = '^'; 579 charbuf[0] = '^';
574 #ifdef EBCDIC 580 #ifdef EBCDIC
575 if (c == DEL) 581 if (c == DEL)
576 buf[1] = '?'; // DEL displayed as ^? 582 charbuf[1] = '?'; // DEL displayed as ^?
577 else 583 else
578 buf[1] = CtrlChar(c); 584 charbuf[1] = CtrlChar(c);
579 #else 585 #else
580 buf[1] = c ^ 0x40; // DEL displayed as ^? 586 charbuf[1] = c ^ 0x40; // DEL displayed as ^?
581 #endif 587 #endif
582 588
583 buf[2] = NUL; 589 charbuf[2] = NUL;
584 } 590 }
585 else if (enc_utf8 && c >= 0x80) 591 else if (enc_utf8 && c >= 0x80)
586 { 592 {
587 transchar_hex(buf, c); 593 transchar_hex(charbuf, c);
588 } 594 }
589 #ifndef EBCDIC 595 #ifndef EBCDIC
590 else if (c >= ' ' + 0x80 && c <= '~' + 0x80) // 0xa0 - 0xfe 596 else if (c >= ' ' + 0x80 && c <= '~' + 0x80) // 0xa0 - 0xfe
591 { 597 {
592 buf[0] = '|'; 598 charbuf[0] = '|';
593 buf[1] = c - 0x80; 599 charbuf[1] = c - 0x80;
594 buf[2] = NUL; 600 charbuf[2] = NUL;
595 } 601 }
596 #else 602 #else
597 else if (c < 64) 603 else if (c < 64)
598 { 604 {
599 buf[0] = '~'; 605 charbuf[0] = '~';
600 buf[1] = MetaChar(c); 606 charbuf[1] = MetaChar(c);
601 buf[2] = NUL; 607 charbuf[2] = NUL;
602 } 608 }
603 #endif 609 #endif
604 else // 0x80 - 0x9f and 0xff 610 else // 0x80 - 0x9f and 0xff
605 { 611 {
606 /* 612 /*
607 * TODO: EBCDIC I don't know what to do with this chars, so I display 613 * TODO: EBCDIC I don't know what to do with this chars, so I display
608 * them as '~?' for now 614 * them as '~?' for now
609 */ 615 */
610 buf[0] = '~'; 616 charbuf[0] = '~';
611 #ifdef EBCDIC 617 #ifdef EBCDIC
612 buf[1] = '?'; // 0xff displayed as ~? 618 charbuf[1] = '?'; // 0xff displayed as ~?
613 #else 619 #else
614 buf[1] = (c - 0x80) ^ 0x40; // 0xff displayed as ~? 620 charbuf[1] = (c - 0x80) ^ 0x40; // 0xff displayed as ~?
615 #endif 621 #endif
616 buf[2] = NUL; 622 charbuf[2] = NUL;
617 } 623 }
618 } 624 }
619 625
620 void 626 void
621 transchar_hex(char_u *buf, int c) 627 transchar_hex(char_u *buf, int c)