home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / util / vim-2.0.lha / Vim-2.0 / src / charset.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-15  |  2.0 KB  |  125 lines

  1. /* vi:ts=4:sw=4
  2.  *
  3.  * VIM - Vi IMproved
  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.                 if (c == 0x7f)        /* DEL displayed as ^?, not ~? */
  39.                     buf[0] = '^';
  40.                 else
  41.                     buf[0] = '~';
  42.                 buf[1] = c - 0x80 + '@';
  43.                 buf[2] = NUL;
  44.         }
  45.         return buf;
  46. }
  47.  
  48. /*
  49.  * output 'len' characters in 'str' (including NULs) with translation
  50.  * if 'len' is -1, output upto a NUL character
  51.  * return the number of characters it takes on the screen
  52.  */
  53.     int
  54. outtrans(str, len)
  55.     register char *str;
  56.     register int   len;
  57. {
  58.     int retval = 0;
  59.  
  60.     if (len == -1)
  61.         len = strlen(str);
  62.     while (--len >= 0)
  63.     {
  64.         outstrn(transchar(*(u_char *)str));
  65.         retval += charsize(*(u_char *)str);
  66.         ++str;
  67.     }
  68.     return retval;
  69. }
  70.  
  71. /*
  72.  * return the number of characters 'c' will take on the screen
  73.  */
  74.     int
  75. charsize(c)
  76.     int c;
  77. {
  78.     return ((c >= ' ' && (p_gr || c <= '~')) || c > 0xa0 ? 1 : 2);
  79. }
  80.  
  81. /*
  82.  * return the number of characters string 's' will take on the screen
  83.  */
  84.     int
  85. strsize(s)
  86.     char *s;
  87. {
  88.     int    len = 0;
  89.  
  90.     while (*s)
  91.         len += charsize(*s++);
  92.     return len;
  93. }
  94.  
  95. /*
  96.  * return the number of characters 'c' will take on the screen, taking
  97.  * into account the size of a tab
  98.  */
  99.     int
  100. chartabsize(c, col)
  101.     register int    c;
  102.     int                col;
  103. {
  104.     if ((c >= ' ' && (c <= '~' || p_gr)) || c > 0xa0)
  105.            return 1;
  106.        else if (c == TAB && !p_list)
  107.            return (int)(p_ts - (col % p_ts));
  108.        else
  109.         return 2;
  110. }
  111.  
  112. /*
  113.  * return TRUE if 'c' is an identifier character
  114.  */
  115.     int
  116. isidchar(c)
  117.     int c;
  118. {
  119. #ifdef __STDC__
  120.         return (isalnum(c) || c == '_');
  121. #else
  122.         return (isalpha(c) || isdigit(c) || c == '_');
  123. #endif
  124. }
  125.