(zone, zone-pgm-stress): Don't use window-system.
[bpt/emacs.git] / lisp / play / cookie1.el
CommitLineData
8eb74eec 1;;; cookie1.el --- retrieve random phrases from fortune cookie files
d5edbd11
ER
2
3;; Copyright (C) 1993 Free Software Foundation, Inc.
4
5;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
6;; Maintainer: FSF
7;; Keywords: games
8;; Created: Mon Mar 22 17:06:26 1993
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation; either version 2, or (at your option)
15;; any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
b578f267
EN
23;; along with GNU Emacs; see the file COPYING. If not, write to the
24;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25;; Boston, MA 02111-1307, USA.
d5edbd11
ER
26
27;;; Commentary:
28
29;; Support for random cookie fetches from phrase files, used for such
30;; critical applications as emulating Zippy the Pinhead and confounding
31;; the NSA Trunk Trawler.
32;;
33;; The two entry points are `cookie' and `cookie-insert'. The helper
c5eeaf52 34;; function `shuffle-vector' may be of interest to programmers.
d5edbd11 35;;
45b44a5a
ER
36;; The code expects phrase files to be in one of two formats:
37;;
38;; * ITS-style LINS format (strings terminated by ASCII 0 characters,
39;; leading whitespace ignored).
40;;
41;; * UNIX fortune file format (quotes terminated by %% on a line by itself).
42;;
43;; Everything up to the first delimiter is treated as a comment. Other
44;; formats could be supported by adding alternates to the regexp
45;; `cookie-delimiter'.
d5edbd11 46;;
96ce5c4f
GM
47;; strfile(1) is the program used to compile the files for fortune(6).
48;; In order to achieve total compatibility with strfile(1), cookie files
49;; should start with two consecutive delimiters (and no comment).
50;;
d5edbd11 51;; This code derives from Steve Strassman's 1987 spook.el package, but
45b44a5a
ER
52;; has been generalized so that it supports multiple simultaneous
53;; cookie databases and fortune files. It is intended to be called
54;; from other packages such as yow.el and spook.el.
cf597dc8
ER
55;;
56;; TO DO: teach cookie-snarf to auto-detect ITS PINS or UNIX fortune(6)
57;; format and do the right thing.
d5edbd11
ER
58
59;;; Code:
60
61; Randomize the seed in the random number generator.
62(random t)
63
bfaef6dc 64(defconst cookie-delimiter "\n%%\n\\|\n%\n\\|\0"
d5edbd11
ER
65 "Delimiter used to separate cookie file entries.")
66
4054367c
RM
67(defvar cookie-cache (make-vector 511 0)
68 "Cache of cookie files that have already been snarfed.")
69
f58c6f70 70;;;###autoload
d5edbd11
ER
71(defun cookie (phrase-file startmsg endmsg)
72 "Return a random phrase from PHRASE-FILE. When the phrase file
73is read in, display STARTMSG at beginning of load, ENDMSG at end."
74 (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg)))
75 (shuffle-vector cookie-vector)
76 (aref cookie-vector 1)))
77
f58c6f70 78;;;###autoload
d5edbd11
ER
79(defun cookie-insert (phrase-file &optional count startmsg endmsg)
80 "Insert random phrases from PHRASE-FILE; COUNT of them. When the phrase file
81is read in, display STARTMSG at beginning of load, ENDMSG at end."
82 (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg)))
83 (shuffle-vector cookie-vector)
84 (let ((start (point)))
85 (insert ?\n)
86 (cookie1 (min (- (length cookie-vector) 1) (or count 1)) cookie-vector)
87 (insert ?\n)
88 (fill-region-as-paragraph start (point) nil))))
89
90(defun cookie1 (arg cookie-vec)
91 "Inserts a cookie phrase ARG times."
92 (cond ((zerop arg) t)
93 (t (insert (aref cookie-vec arg))
94 (insert " ")
95 (cookie1 (1- arg) cookie-vec))))
96
f58c6f70 97;;;###autoload
d5edbd11 98(defun cookie-snarf (phrase-file startmsg endmsg)
b81d991d
RM
99 "Reads in the PHRASE-FILE, returns it as a vector of strings.
100Emit STARTMSG and ENDMSG before and after. Caches the result; second
101and subsequent calls on the same file won't go to disk."
4054367c
RM
102 (let ((sym (intern-soft phrase-file cookie-cache)))
103 (and sym (not (equal (symbol-function sym)
104 (nth 5 (file-attributes phrase-file))))
105 (yes-or-no-p (concat phrase-file
106 " has changed. Read new contents? "))
107 (setq sym nil))
108 (if sym
109 (symbol-value sym)
110 (setq sym (intern phrase-file cookie-cache))
4120f5f5 111 (message "%s" startmsg)
4054367c
RM
112 (save-excursion
113 (let ((buf (generate-new-buffer "*cookie*"))
114 (result nil))
115 (set-buffer buf)
116 (fset sym (nth 5 (file-attributes phrase-file)))
117 (insert-file-contents (expand-file-name phrase-file))
118 (re-search-forward cookie-delimiter)
119 (while (progn (skip-chars-forward " \t\n\r\f") (not (eobp)))
120 (let ((beg (point)))
121 (re-search-forward cookie-delimiter)
bfaef6dc 122 (setq result (cons (buffer-substring beg (match-beginning 0))
4054367c
RM
123 result))))
124 (kill-buffer buf)
4120f5f5 125 (message "%s" endmsg)
4054367c 126 (set sym (apply 'vector result)))))))
d5edbd11 127
b81d991d
RM
128(defun read-cookie (prompt phrase-file startmsg endmsg &optional require-match)
129 "Prompt with PROMPT and read with completion among cookies in PHRASE-FILE.
130STARTMSG and ENDMSG are passed along to `cookie-snarf'.
131Optional fifth arg REQUIRE-MATCH non-nil forces a matching cookie."
132 ;; Make sure the cookies are in the cache.
133 (or (intern-soft phrase-file cookie-cache)
134 (cookie-snarf phrase-file startmsg endmsg))
135 (completing-read prompt
136 (let ((sym (intern phrase-file cookie-cache)))
137 ;; We cache the alist form of the cookie in a property.
138 (or (get sym 'completion-alist)
139 (let* ((alist nil)
140 (vec (cookie-snarf phrase-file
141 startmsg endmsg))
142 (i (length vec)))
143 (while (> (setq i (1- i)) 0)
144 (setq alist (cons (list (aref vec i)) alist)))
145 (put sym 'completion-alist alist))))
146 nil require-match nil nil))
147
d5edbd11
ER
148; Thanks to Ian G Batten <BattenIG@CS.BHAM.AC.UK>
149; [of the University of Birmingham Computer Science Department]
150; for the iterative version of this shuffle.
151;
f58c6f70 152;;;###autoload
d5edbd11
ER
153(defun shuffle-vector (vector)
154 "Randomly permute the elements of VECTOR (all permutations equally likely)"
155 (let ((i 0)
156 j
157 temp
158 (len (length vector)))
159 (while (< i len)
c5eeaf52 160 (setq j (+ i (random (- len i))))
d5edbd11
ER
161 (setq temp (aref vector i))
162 (aset vector i (aref vector j))
163 (aset vector j temp)
164 (setq i (1+ i))))
165 vector)
166
3aa7cce7 167(provide 'cookie1)
d5edbd11 168
20a82895 169;;; cookie1.el ends here