]> code.delx.au - gnu-emacs/blob - lisp/play/cookie1.el
Switch to recommended form of GPLv3 permissions notice.
[gnu-emacs] / lisp / play / cookie1.el
1 ;;; cookie1.el --- retrieve random phrases from fortune cookie files
2
3 ;; Copyright (C) 1993, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
7 ;; Maintainer: FSF
8 ;; Keywords: games, extensions
9 ;; Created: Mon Mar 22 17:06:26 1993
10
11 ;; This file is part of GNU Emacs.
12
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.
17
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.
22
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/>.
25
26 ;;; Commentary:
27
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.
31 ;;
32 ;; The two entry points are `cookie' and `cookie-insert'. The helper
33 ;; function `shuffle-vector' may be of interest to programmers.
34 ;;
35 ;; The code expects phrase files to be in one of two formats:
36 ;;
37 ;; * ITS-style LINS format (strings terminated by ASCII 0 characters,
38 ;; leading whitespace ignored).
39 ;;
40 ;; * UNIX fortune file format (quotes terminated by %% on a line by itself).
41 ;;
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'.
45 ;;
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).
49 ;;
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.
54 ;;
55 ;; TO DO: teach cookie-snarf to auto-detect ITS PINS or UNIX fortune(6)
56 ;; format and do the right thing.
57
58 ;;; Code:
59
60 ; Randomize the seed in the random number generator.
61 (random t)
62
63 (defconst cookie-delimiter "\n%%\n\\|\n%\n\\|\0"
64 "Delimiter used to separate cookie file entries.")
65
66 (defvar cookie-cache (make-vector 511 0)
67 "Cache of cookie files that have already been snarfed.")
68
69 ;;;###autoload
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)))
77
78 ;;;###autoload
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)))
86 (insert ?\n)
87 (cookie1 (min (- (length cookie-vector) 1) (or count 1)) cookie-vector)
88 (insert ?\n)
89 (fill-region-as-paragraph start (point) nil))))
90
91 (defun cookie1 (arg cookie-vec)
92 "Inserts a cookie phrase ARG times."
93 (cond ((zerop arg) t)
94 (t (insert (aref cookie-vec arg))
95 (insert " ")
96 (cookie1 (1- arg) cookie-vec))))
97
98 ;;;###autoload
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? "))
108 (setq sym nil))
109 (if sym
110 (symbol-value sym)
111 (setq sym (intern phrase-file cookie-cache))
112 (message "%s" startmsg)
113 (save-excursion
114 (let ((buf (generate-new-buffer "*cookie*"))
115 (result nil))
116 (set-buffer buf)
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)))
121 (let ((beg (point)))
122 (re-search-forward cookie-delimiter)
123 (setq result (cons (buffer-substring beg (match-beginning 0))
124 result))))
125 (kill-buffer buf)
126 (message "%s" endmsg)
127 (set sym (apply 'vector result)))))))
128
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)
140 (let* ((alist nil)
141 (vec (cookie-snarf phrase-file
142 startmsg endmsg))
143 (i (length vec)))
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))
148
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.
152 ;
153 ;;;###autoload
154 (defun shuffle-vector (vector)
155 "Randomly permute the elements of VECTOR (all permutations equally likely)."
156 (let ((i 0)
157 j
158 temp
159 (len (length vector)))
160 (while (< i len)
161 (setq j (+ i (random (- len i))))
162 (setq temp (aref vector i))
163 (aset vector i (aref vector j))
164 (aset vector j temp)
165 (setq i (1+ i))))
166 vector)
167
168 (provide 'cookie1)
169
170 ;; arch-tag: 4a8a8712-df6a-4f34-b030-108a1b47f9f2
171 ;;; cookie1.el ends here