home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / functions / printify-buffer.el < prev    next >
Encoding:
Text File  |  1991-03-20  |  1.9 KB  |  45 lines

  1. ; Path: utkcs2!emory!swrinde!ucsd!helios.ee.lbl.gov!pasteur!tully.Berkeley.EDU!mcgrath
  2. ; >From: mcgrath@tully.Berkeley.EDU (Roland McGrath)
  3. ; Newsgroups: comp.emacs
  4. ; Subject: Re: searching for "non-printables"
  5. ; Date: 23 Jul 90 19:43:31 GMT
  6. ; References: <929@meaddata.mead.UUCP> <MCGRATH.90Jul20171843@homer.Berkeley.EDU>
  7. ;     <Jul.22.09.46.19.1990.21055@athos.rutgers.edu>
  8. ; Organization: Hackers Anonymous International, Ltd., Inc. (Applications
  9. ;     welcome)
  10. ; X-Local-Date: 23 Jul 90 12:43:31 PDT
  11. ; In-reply-to: shapiro@athos.rutgers.edu's message of 22 Jul 90 13:46:21 GMT
  12. ;    I don't think mcgrath answered the question, at least not my question,
  13. ;    which is
  14. ;       How can you pass through a file checking for the existence of any
  15. ;    control characters that might screw up printing? 
  16. ;       and    does anyone have an .el file which will convert all such
  17. ;    characters to some printable representation (for example, so they will
  18. ;    print the way they appear on the screen)
  19. ;            Joel Shapiro
  20. ; I answered the question asked in the posting I was responding to.
  21. ; Since I don't know what characters will screw up printing for you, I cannot
  22. ; necessarily answer your question to your satisfaction.  However, if it is
  23. ; assumed that all non-printing characters other than TAB, LF, SPC, RET, and FF are problematical, I can offer this:
  24. ; A regular expression for searching for such characters is (in a Lisp string):
  25. ; "[\^@-\^h\^k\^n-\^_\177-\377]".  The following function will turn such
  26. ; characters into their printing representations:
  27. (defun printify-buffer ()
  28.   "Turn nonprinting characters in the current buffer 
  29. into their printable representations."
  30.   (interactive)
  31.   (save-excursion
  32.     (goto-char (point-min))
  33.     (let (c)
  34.       (while (re-search-forward "[\^@-\^h\^k\^n-\^_\177-\377]" nil t)
  35.     (setq c (char-after (point)))
  36.     (delete-char 1)
  37.     (insert (if (and (>= c ?\^@) (<= c ?\^_))
  38.             (format "^%c" (- c ?@)))
  39.         (format "\\%03o" c))))))
  40.