home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / v / vim_src.zip / CHARSET.C < prev    next >
C/C++ Source or Header  |  1993-01-12  |  2KB  |  121 lines

  1. /* vi:ts=4:sw=4
  2.  *
  3.  * VIM - Vi IMitation
  4.  *
  5.  * Code Contributions By:    Bram Moolenaar            mool@oce.nl
  6.  *                            Tim Thompson            twitch!tjt
  7.  *                            Tony Andrews            onecom!wldrdg!tony 
  8.  *                            G. R. (Fred) Walter        watmath!watcgl!grwalter 
  9.  */
  10.  
  11. #include "vim.h"
  12. #include "globals.h"
  13. #include "proto.h"
  14. #include "param.h"
  15.  
  16.  
  17.     char *
  18. transchar(c)
  19.     unsigned c;
  20. {
  21.         static char buf[3];
  22.  
  23.         if (c < ' ')
  24.         {
  25.                 if (c == NL)
  26.                         c = NUL;        /* we use newline in place of a NUL */
  27.                 buf[0] = '^';
  28.                 buf[1] = '@' + c;
  29.                 buf[2] = NUL;
  30.         }
  31.         else if (c <= '~' || c > 0xa0 || p_gr)
  32.         {
  33.                 buf[0] = c;
  34.                 buf[1] = NUL;
  35.         }
  36.         else
  37.         {
  38.                 buf[0] = '~';
  39.                 buf[1] = c - 0x80 + '@';
  40.                 buf[2] = NUL;
  41.         }
  42.         return buf;
  43. }
  44.  
  45. /*
  46.  * output 'len' characters in 'str' (including NULs) with translation
  47.  * if 'len' is -1, output upto a NUL character
  48.  * return the number of characters it takes on the screen
  49.  */
  50.     int
  51. outtrans(str, len)
  52.     register char *str;
  53.     register int   len;
  54. {
  55.     int retval = 0;
  56.  
  57.     if (len == -1)
  58.         len = strlen(str);
  59.     while (--len >= 0)
  60.     {
  61.         outstrn(transchar(*(u_char *)str));
  62.         retval += charsize(*(u_char *)str);
  63.         ++str;
  64.     }
  65.     return retval;
  66. }
  67.  
  68. /*
  69.  * return the number of characters 'c' will take on the screen
  70.  */
  71.     int
  72. charsize(c)
  73.     int c;
  74. {
  75.     return ((c >= ' ' && (p_gr || c <= '~')) || c > 0xa0 ? 1 : 2);
  76. }
  77.  
  78. /*
  79.  * return the number of characters string 's' will take on the screen
  80.  */
  81.     int
  82. strsize(s)
  83.     char *s;
  84. {
  85.     int    len = 0;
  86.  
  87.     while (*s)
  88.         len += charsize(*s++);
  89.     return len;
  90. }
  91.  
  92. /*
  93.  * return the number of characters 'c' will take on the screen, taking
  94.  * into account the size of a tab
  95.  */
  96.     int
  97. chartabsize(c, col)
  98.     int c, col;
  99. {
  100.     if ((c >= ' ' && (c <= '~' || p_gr)) || c > 0xa0)
  101.            return 1;
  102.        else if (c == TAB && !p_list)
  103.            return (int)(p_ts - (col % p_ts));
  104.        else
  105.         return 2;
  106. }
  107.  
  108. /*
  109.  * return TRUE if 'c' is an identifier character
  110.  */
  111.     int
  112. isidchar(c)
  113.     int c;
  114. {
  115. #ifdef __STDC__
  116.         return (isalnum(c) || c == '_');
  117. #else
  118.         return (isalpha(c) || isdigit(c) || c == '_');
  119. #endif
  120. }
  121.