annotate lisp/games/cookie1.el @ 0:376386a54a3c r19-14

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