1 ;;; cookie1.el --- retrieve random phrases from fortune cookie files
3 ;; Copyright (C) 1993 Free Software Foundation, Inc.
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
7 ;; Keywords: games, extensions
8 ;; Created: Mon Mar 22 17:06:26 1993
10 ;; This file is part of GNU Emacs.
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)
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.
22 ;; You should have received a copy of the GNU General Public License
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.
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.
33 ;; The two entry points are `cookie' and `cookie-insert'. The helper
34 ;; function `shuffle-vector' may be of interest to programmers.
36 ;; The code expects phrase files to be in one of two formats:
38 ;; * ITS-style LINS format (strings terminated by ASCII 0 characters,
39 ;; leading whitespace ignored).
41 ;; * UNIX fortune file format (quotes terminated by %% on a line by itself).
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'.
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).
51 ;; This code derives from Steve Strassman's 1987 spook.el package, but
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.
56 ;; TO DO: teach cookie-snarf to auto-detect ITS PINS or UNIX fortune(6)
57 ;; format and do the right thing.
61 ; Randomize the seed in the random number generator.
64 (defconst cookie-delimiter
"\n%%\n\\|\n%\n\\|\0"
65 "Delimiter used to separate cookie file entries.")
67 (defvar cookie-cache
(make-vector 511 0)
68 "Cache of cookie files that have already been snarfed.")
71 (defun cookie (phrase-file startmsg endmsg
)
72 "Return a random phrase from PHRASE-FILE.
73 When the phrase file is read in, display STARTMSG at the beginning
74 of load, ENDMSG at the end."
75 (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg
)))
76 (shuffle-vector cookie-vector
)
77 (aref cookie-vector
1)))
80 (defun cookie-insert (phrase-file &optional count startmsg endmsg
)
81 "Insert random phrases from PHRASE-FILE; COUNT of them.
82 When the phrase file is read in, display STARTMSG at the beginning
83 of load, ENDMSG at the end."
84 (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg
)))
85 (shuffle-vector cookie-vector
)
86 (let ((start (point)))
88 (cookie1 (min (- (length cookie-vector
) 1) (or count
1)) cookie-vector
)
90 (fill-region-as-paragraph start
(point) nil
))))
92 (defun cookie1 (arg cookie-vec
)
93 "Inserts a cookie phrase ARG times."
95 (t (insert (aref cookie-vec arg
))
97 (cookie1 (1- arg
) cookie-vec
))))
100 (defun cookie-snarf (phrase-file startmsg endmsg
)
101 "Reads in the PHRASE-FILE, returns it as a vector of strings.
102 Emit STARTMSG and ENDMSG before and after. Caches the result; second
103 and subsequent calls on the same file won't go to disk."
104 (let ((sym (intern-soft phrase-file cookie-cache
)))
105 (and sym
(not (equal (symbol-function sym
)
106 (nth 5 (file-attributes phrase-file
))))
107 (yes-or-no-p (concat phrase-file
108 " has changed. Read new contents? "))
112 (setq sym
(intern phrase-file cookie-cache
))
113 (message "%s" startmsg
)
115 (let ((buf (generate-new-buffer "*cookie*"))
118 (fset sym
(nth 5 (file-attributes phrase-file
)))
119 (insert-file-contents (expand-file-name phrase-file
))
120 (re-search-forward cookie-delimiter
)
121 (while (progn (skip-chars-forward " \t\n\r\f") (not (eobp)))
123 (re-search-forward cookie-delimiter
)
124 (setq result
(cons (buffer-substring beg
(match-beginning 0))
127 (message "%s" endmsg
)
128 (set sym
(apply 'vector result
)))))))
130 (defun read-cookie (prompt phrase-file startmsg endmsg
&optional require-match
)
131 "Prompt with PROMPT and read with completion among cookies in PHRASE-FILE.
132 STARTMSG and ENDMSG are passed along to `cookie-snarf'.
133 Optional fifth arg REQUIRE-MATCH non-nil forces a matching cookie."
134 ;; Make sure the cookies are in the cache.
135 (or (intern-soft phrase-file cookie-cache
)
136 (cookie-snarf phrase-file startmsg endmsg
))
137 (completing-read prompt
138 (let ((sym (intern phrase-file cookie-cache
)))
139 ;; We cache the alist form of the cookie in a property.
140 (or (get sym
'completion-alist
)
142 (vec (cookie-snarf phrase-file
145 (while (> (setq i
(1- i
)) 0)
146 (setq alist
(cons (list (aref vec i
)) alist
)))
147 (put sym
'completion-alist alist
))))
148 nil require-match nil nil
))
150 ; Thanks to Ian G Batten <BattenIG@CS.BHAM.AC.UK>
151 ; [of the University of Birmingham Computer Science Department]
152 ; for the iterative version of this shuffle.
155 (defun shuffle-vector (vector)
156 "Randomly permute the elements of VECTOR (all permutations equally likely)."
160 (len (length vector
)))
162 (setq j
(+ i
(random (- len i
))))
163 (setq temp
(aref vector i
))
164 (aset vector i
(aref vector j
))
171 ;;; arch-tag: 4a8a8712-df6a-4f34-b030-108a1b47f9f2
172 ;;; cookie1.el ends here