home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / functions / randomize.el < prev    next >
Encoding:
Text File  |  1992-04-07  |  2.4 KB  |  68 lines

  1. ; Path: hal.com!decwrl!mips!pacbell.com!iggy.GW.Vitalink.COM!widener!dsinc!bagate!cbmvax!devon!dastari!dastari!hermit
  2. ; From: hermit@dastari.uucp (Mark Buda)
  3. ; Newsgroups: gnu.emacs.sources
  4. ; Subject: randomizing text
  5. ; Date: 26 Mar 92 19:34:26 GMT
  6. ; Reply-To: hermit%dastari.uucp@devon.lns.pa.us
  7. ; Organization: Competitive Computer Systems, Inc.
  8. ;; Here are a couple functions I whipped up to take a region or
  9. ;; rectangle and randomize it. (It was the first thing I wrote in
  10. ;; Elisp.) The original purpose was to be able to include sample
  11. ;; reports from an accounting system in the documentation without
  12. ;; having to make up test data or reveal somebody's actual data.
  13.  
  14. ;; randomize-rectangle randomizes a rectangle; randomize-text does the
  15. ;; region.
  16.  
  17. ;; LCD Archive Entry:
  18. ;; randomize|Mark Buda|hermit@dastari.uucp|
  19. ;; Randomize a rectangle or region by replacing letters and numbers randomly.|
  20. ;; 92-03-26||~/functions/randomize.el.Z|
  21.  
  22. ;; Capital letters are turned into other capital letters, lowercase
  23. ;; into other lowercase, and numbers into other numbers. Everything
  24. ;; else is left alone.
  25.  
  26. ;; Copyright 1991 Mark R. Buda (hermit%dastari.uucp@devon.lns.pa.us).
  27. ;; You can do whatever you want with this except distribute it without
  28. ;; source code or misrepresent its authorship. (Meaning, you must
  29. ;; preserve this copyright notice, and if you distribute a modified
  30. ;; version, you must note that in the source.)
  31.  
  32. ;; randomize some text
  33.  
  34. (load-library "rect")
  35.  
  36. (defun rnd (number)
  37.   "Generate a random integer x, 0 <= x < NUMBER"
  38.   (interactive "nRandom from 0 to: ")
  39.   (setq number (% (random) number))
  40.   (if (< number 0) (- number) number))
  41.  
  42. (defun randomize-text (start end)
  43.   "Replace text and numbers in the region with randomly generated text."
  44.   (interactive "r")
  45.   (let ((case-fold-search nil))
  46.     (save-excursion
  47.       (goto-char start)
  48.       (while (/= (point) end)
  49.     (cond ((looking-at "[0-9]")
  50.            (delete-char 1)
  51.            (insert-char (+ ?0 (rnd 10)) 1))
  52.           ((looking-at "[a-z]")
  53.            (delete-char 1)
  54.            (insert-char (+ ?a (rnd 26)) 1))
  55.           ((looking-at "[A-Z]")
  56.            (delete-char 1)
  57.            (insert-char (+ ?A (rnd 26)) 1))
  58.           (t (forward-char 1)))))))
  59.  
  60. (defun randomize-rectangle (start end)
  61.   "Replace text and numbers in a rectangle with randomly generated text."
  62.   (interactive "r")
  63.   (operate-on-rectangle
  64.    (function (lambda (start before end)
  65.            (randomize-text start (point))))
  66.    start end nil))
  67.