annotate lisp/games/cookie1.el @ 7:c153ca296910

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