1 ;;; cookie1.el --- retrieve random phrases from fortune cookie files
3 ;; Copyright (C) 1993, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
8 ;; Keywords: games, extensions
9 ;; Created: Mon Mar 22 17:06:26 1993
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; Support for random cookie fetches from phrase files, used for such
29 ;; critical applications as emulating Zippy the Pinhead and confounding
30 ;; the NSA Trunk Trawler.
32 ;; The two entry points are `cookie' and `cookie-insert'. The helper
33 ;; function `shuffle-vector' may be of interest to programmers.
35 ;; The code expects phrase files to be in one of two formats:
37 ;; * ITS-style LINS format (strings terminated by ASCII 0 characters,
38 ;; leading whitespace ignored).
40 ;; * UNIX fortune file format (quotes terminated by %% on a line by itself).
42 ;; Everything up to the first delimiter is treated as a comment. Other
43 ;; formats could be supported by adding alternates to the regexp
44 ;; `cookie-delimiter'.
46 ;; strfile(1) is the program used to compile the files for fortune(6).
47 ;; In order to achieve total compatibility with strfile(1), cookie files
48 ;; should start with two consecutive delimiters (and no comment).
50 ;; This code derives from Steve Strassman's 1987 spook.el package, but
51 ;; has been generalized so that it supports multiple simultaneous
52 ;; cookie databases and fortune files. It is intended to be called
53 ;; from other packages such as yow.el and spook.el.
55 ;; TO DO: teach cookie-snarf to auto-detect ITS PINS or UNIX fortune(6)
56 ;; format and do the right thing.
60 ; Randomize the seed in the random number generator.
63 (defconst cookie-delimiter
"\n%%\n\\|\n%\n\\|\0"
64 "Delimiter used to separate cookie file entries.")
66 (defvar cookie-cache
(make-vector 511 0)
67 "Cache of cookie files that have already been snarfed.")
70 (defun cookie (phrase-file startmsg endmsg
)
71 "Return a random phrase from PHRASE-FILE.
72 When the phrase file is read in, display STARTMSG at the beginning
73 of load, ENDMSG at the end."
74 (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg
)))
75 (shuffle-vector cookie-vector
)
76 (aref cookie-vector
0)))
79 (defun cookie-insert (phrase-file &optional count startmsg endmsg
)
80 "Insert random phrases from PHRASE-FILE; COUNT of them.
81 When the phrase file is read in, display STARTMSG at the beginning
82 of load, ENDMSG at the end."
83 (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg
)))
84 (shuffle-vector cookie-vector
)
85 (let ((start (point)))
87 (cookie1 (min (- (length cookie-vector
) 1) (or count
1)) cookie-vector
)
89 (fill-region-as-paragraph start
(point) nil
))))
91 (defun cookie1 (arg cookie-vec
)
92 "Inserts a cookie phrase ARG times."
94 (t (insert (aref cookie-vec arg
))
96 (cookie1 (1- arg
) cookie-vec
))))
99 (defun cookie-snarf (phrase-file startmsg endmsg
)
100 "Reads in the PHRASE-FILE, returns it as a vector of strings.
101 Emit STARTMSG and ENDMSG before and after. Caches the result; second
102 and subsequent calls on the same file won't go to disk."
103 (let ((sym (intern-soft phrase-file cookie-cache
)))
104 (and sym
(not (equal (symbol-function sym
)
105 (nth 5 (file-attributes phrase-file
))))
106 (yes-or-no-p (concat phrase-file
107 " has changed. Read new contents? "))
111 (setq sym
(intern phrase-file cookie-cache
))
112 (message "%s" startmsg
)
114 (let ((buf (generate-new-buffer "*cookie*"))
117 (fset sym
(nth 5 (file-attributes phrase-file
)))
118 (insert-file-contents (expand-file-name phrase-file
))
119 (re-search-forward cookie-delimiter
)
120 (while (progn (skip-chars-forward " \t\n\r\f") (not (eobp)))
122 (re-search-forward cookie-delimiter
)
123 (setq result
(cons (buffer-substring beg
(match-beginning 0))
126 (message "%s" endmsg
)
127 (set sym
(apply 'vector result
)))))))
129 (defun read-cookie (prompt phrase-file startmsg endmsg
&optional require-match
)
130 "Prompt with PROMPT and read with completion among cookies in PHRASE-FILE.
131 STARTMSG and ENDMSG are passed along to `cookie-snarf'.
132 Optional fifth arg REQUIRE-MATCH non-nil forces a matching cookie."
133 ;; Make sure the cookies are in the cache.
134 (or (intern-soft phrase-file cookie-cache
)
135 (cookie-snarf phrase-file startmsg endmsg
))
136 (completing-read prompt
137 (let ((sym (intern phrase-file cookie-cache
)))
138 ;; We cache the alist form of the cookie in a property.
139 (or (get sym
'completion-alist
)
141 (vec (cookie-snarf phrase-file
144 (while (> (setq i
(1- i
)) 0)
145 (setq alist
(cons (list (aref vec i
)) alist
)))
146 (put sym
'completion-alist alist
))))
147 nil require-match nil nil
))
149 ; Thanks to Ian G Batten <BattenIG@CS.BHAM.AC.UK>
150 ; [of the University of Birmingham Computer Science Department]
151 ; for the iterative version of this shuffle.
154 (defun shuffle-vector (vector)
155 "Randomly permute the elements of VECTOR (all permutations equally likely)."
159 (len (length vector
)))
161 (setq j
(+ i
(random (- len i
))))
162 (setq temp
(aref vector i
))
163 (aset vector i
(aref vector j
))
170 ;; arch-tag: 4a8a8712-df6a-4f34-b030-108a1b47f9f2
171 ;;; cookie1.el ends here