*** empty log message ***
[bpt/emacs.git] / lisp / play / spook.el
1 ;;; spook.el --- spook phrase utility for overloading the NSA line eater
2
3 ;; Maintainer: FSF
4 ;; Last-Modified: 05 Dec 1991
5 ;; Keywords: games
6
7 ;; Copyright (C) 1988 Free Software Foundation, Inc.
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 ;;; Commentary:
26
27 ; Steve Strassmann (straz@media-lab.media.mit.edu) didn't write
28 ; this, and even if he did, he really didn't mean for you to use it
29 ; in an anarchistic way.
30 ; May 1987
31
32 ; To use this:
33 ; Make sure you have the variable SPOOK-PHRASES-FILE pointing to
34 ; a valid phrase file. Phrase files are in the same format as
35 ; zippy's yow.lines (ITS-style LINS format).
36 ; Strings are terminated by ascii 0 characters. Leading whitespace ignored.
37 ; Everything up to the first \000 is a comment.
38 ;
39 ; Just before sending mail, do M-x spook.
40 ; A number of phrases will be inserted into your buffer, to help
41 ; give your message that extra bit of attractiveness for automated
42 ; keyword scanners.
43
44 ;;; Code:
45
46 ; Variables
47 (defvar spook-phrases-file (concat data-directory "spook.lines")
48 "Keep your favorite phrases here.")
49
50 (defvar spook-phrase-default-count 15
51 "Default number of phrases to insert")
52
53 (defvar spook-vector nil
54 "Important phrases for NSA mail-watchers")
55
56 ; Randomize the seed in the random number generator.
57 (random t)
58
59 ; Call this with M-x spook.
60 (defun spook ()
61 "Adds that special touch of class to your outgoing mail."
62 (interactive)
63 (if (null spook-vector)
64 (setq spook-vector (snarf-spooks)))
65 (shuffle-vector spook-vector)
66 (let ((start (point)))
67 (insert ?\n)
68 (spook1 (min (- (length spook-vector) 1) spook-phrase-default-count))
69 (insert ?\n)
70 (fill-region-as-paragraph start (point) nil)))
71
72 (defun spook1 (arg)
73 "Inserts a spook phrase ARG times."
74 (cond ((zerop arg) t)
75 (t (insert (aref spook-vector arg))
76 (insert " ")
77 (spook1 (1- arg)))))
78
79 (defun snarf-spooks ()
80 "Reads in the phrase file"
81 (message "Checking authorization...")
82 (save-excursion
83 (let ((buf (generate-new-buffer "*spook*"))
84 (result '()))
85 (set-buffer buf)
86 (insert-file-contents (expand-file-name spook-phrases-file))
87 (search-forward "\0")
88 (while (progn (skip-chars-forward " \t\n\r\f") (not (eobp)))
89 (let ((beg (point)))
90 (search-forward "\0")
91 (setq result (cons (buffer-substring beg (1- (point)))
92 result))))
93 (kill-buffer buf)
94 (message "Checking authorization... Approved.")
95 (setq spook-vector (apply 'vector result)))))
96
97 (defun pick-random (n)
98 "Returns a random number from 0 to N-1 inclusive."
99 (% (logand 0777777 (random)) n))
100
101 ; Thanks to Ian G Batten <BattenIG@CS.BHAM.AC.UK>
102 ; [of the University of Birmingham Computer Science Department]
103 ; for the iterative version of this shuffle.
104 ;
105 (defun shuffle-vector (vector)
106 "Randomly permute the elements of VECTOR (all permutations equally likely)"
107 (let ((i 0)
108 j
109 temp
110 (len (length vector)))
111 (while (< i len)
112 (setq j (+ i (pick-random (- len i))))
113 (setq temp (aref vector i))
114 (aset vector i (aref vector j))
115 (aset vector j temp)
116 (setq i (1+ i))))
117 vector)
118
119 ;;; spook.el ends here