home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / editors / 3329 < prev    next >
Encoding:
Text File  |  1993-01-28  |  2.4 KB  |  89 lines

  1. Path: sparky!uunet!ukma!bogus.sura.net!howland.reston.ans.net!zaphod.mps.ohio-state.edu!moe.ksu.ksu.edu!math.ksu.edu!deadend
  2. From: jxh@math.ksu.edu (James C. Hu)
  3. Newsgroups: comp.editors
  4. Subject: Re: Centering lines in vi
  5. Date: 28 Jan 1993 16:43:08 -0600
  6. Organization: Dept. of Mathematics, Kansas State University
  7. Lines: 76
  8. Distribution: inet
  9. Message-ID: <1k9nhsINN9ia@hilbert.math.ksu.edu>
  10. References: <1993Jan28.155257.4953@walter.bellcore.com>
  11. NNTP-Posting-Host: hilbert.math.ksu.edu
  12.  
  13. pietro@nova.bellcore.com (Pietro Manzoni) writes:
  14.  
  15. >Hi,
  16. >is there anybody who knows whether there is a :map command to center a
  17. >single line in VI?
  18.  
  19. There is probably already a package that does this, but I'd put this in
  20. as an example on how to build useful simple filters for vi.  At the end
  21. of this post is a simple program that centers text.  Compile it and
  22. install it in ~/bin/center, or whatever.
  23.  
  24. Then, make the following map:
  25. :map == !!center^M
  26. (the ^M represents the result of hitting CTRL-V followed by CTRL-M)
  27.  
  28. Presto, you have a pseudo center "operator".  Now you can do a == to
  29. center a single line, or a 5== to center the next 5 lines.
  30.  
  31. Enjoy.
  32.  
  33. /* File:    center.c
  34.  * Creator: James C. Hu (sirius@matt.ksu.ksu.edu)
  35.  *
  36.  * Description:
  37.  *     Centers lines of input.
  38.  * 
  39.  *     Caveats are that lines cannot be longer than the specified
  40.  *     centering line length, if they are, then they may be truncated,
  41.  *     and that the default centering line length is 72.
  42.  *
  43.  * Copyright:
  44.  *     This program is placed into the public doman.
  45.  *
  46.  * Date Started: Thu Jan 28 15:33:44 CST 1993
  47.  *
  48.  * Change Log:
  49.  */
  50.  
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53. #include <string.h>
  54.  
  55. static int length = 72;
  56. static char *buf;
  57. static char format[10];         /* should be enough */
  58.  
  59. int main(int argc, char *argv[])
  60. {
  61.   int i,n,buflen;
  62.   char *p;
  63.  
  64.   switch(argc) {
  65.   case 2:
  66.     length = atoi(argv[1]);
  67.     if (length == 0) length = 72;
  68.   case 1:
  69.     break;
  70.   default:
  71.     fprintf(stderr, "usage: center [width]");
  72.     exit(1);
  73.   }
  74.  
  75.   buf = malloc((length + 2) * sizeof(char));
  76.   while (fgets(buf, length+2, stdin) != NULL) {
  77.     if ((p = strrchr(buf, '\n')) != NULL) *p = '\0';
  78.     while (isspace(*buf)) buf++;
  79.     buflen = strlen(buf);
  80.     sprintf(format, "%%%ds\n", length/2 + (buflen+1)/2);
  81.     printf(format, buf);
  82.   }
  83.   return 0;
  84. }
  85.  
  86. -- 
  87. James C. Hu (jxh@math.ksu.edu), 1804 Denholm Dr., Manhattan, KS 66502
  88. I speak for me, the whole me, and nothing but for me.  So help me me.
  89.