Update copyright notices for 2013.
[bpt/emacs.git] / lisp / play / cookie1.el
1 ;;; cookie1.el --- retrieve random phrases from fortune cookie files
2
3 ;; Copyright (C) 1993, 2001-2013 Free Software Foundation, Inc.
4
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
6 ;; Maintainer: FSF
7 ;; Keywords: games, extensions
8 ;; Created: Mon Mar 22 17:06:26 1993
9
10 ;; This file is part of GNU Emacs.
11
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 3 of the License, or
15 ;; (at your option) any later version.
16
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.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; Support for random cookie fetches from phrase files, used for such
28 ;; critical applications as emulating Zippy the Pinhead and confounding
29 ;; the NSA Trunk Trawler.
30 ;;
31 ;; The two entry points are `cookie' and `cookie-insert'. The helper
32 ;; function `shuffle-vector' may be of interest to programmers.
33 ;;
34 ;; The code expects phrase files to be in one of two formats:
35 ;;
36 ;; * ITS-style LINS format (strings terminated by ASCII 0 characters,
37 ;; leading whitespace ignored).
38 ;;
39 ;; * UNIX fortune file format (quotes terminated by %% on a line by itself).
40 ;;
41 ;; Everything up to the first delimiter is treated as a comment. Other
42 ;; formats could be supported by adding alternates to the regexp
43 ;; `cookie-delimiter'.
44 ;;
45 ;; strfile(1) is the program used to compile the files for fortune(6).
46 ;; In order to achieve total compatibility with strfile(1), cookie files
47 ;; should start with two consecutive delimiters (and no comment).
48 ;;
49 ;; This code derives from Steve Strassmann's 1987 spook.el package, but
50 ;; has been generalized so that it supports multiple simultaneous
51 ;; cookie databases and fortune files. It is intended to be called
52 ;; from other packages such as yow.el and spook.el.
53
54 ;;; Code:
55
56 (defconst cookie-delimiter "\n%%\n\\|\n%\n\\|\0"
57 "Delimiter used to separate cookie file entries.")
58
59 (defvar cookie-cache (make-vector 511 0)
60 "Cache of cookie files that have already been snarfed.")
61
62 ;;;###autoload
63 (defun cookie (phrase-file startmsg endmsg)
64 "Return a random phrase from PHRASE-FILE.
65 When the phrase file is read in, display STARTMSG at the beginning
66 of load, ENDMSG at the end."
67 (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg)))
68 (shuffle-vector cookie-vector)
69 (aref cookie-vector 0)))
70
71 ;;;###autoload
72 (defun cookie-insert (phrase-file &optional count startmsg endmsg)
73 "Insert random phrases from PHRASE-FILE; COUNT of them.
74 When the phrase file is read in, display STARTMSG at the beginning
75 of load, ENDMSG at the end."
76 (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg)))
77 (shuffle-vector cookie-vector)
78 (let ((start (point)))
79 (insert ?\n)
80 (cookie1 (min (- (length cookie-vector) 1) (or count 1)) cookie-vector)
81 (insert ?\n)
82 (fill-region-as-paragraph start (point) nil))))
83
84 (defun cookie1 (arg cookie-vec)
85 "Inserts a cookie phrase ARG times."
86 (cond ((zerop arg) t)
87 (t (insert (aref cookie-vec arg))
88 (insert " ")
89 (cookie1 (1- arg) cookie-vec))))
90
91 ;;;###autoload
92 (defun cookie-snarf (phrase-file startmsg endmsg)
93 "Reads in the PHRASE-FILE, returns it as a vector of strings.
94 Emit STARTMSG and ENDMSG before and after. Caches the result; second
95 and subsequent calls on the same file won't go to disk."
96 (or (file-readable-p phrase-file)
97 (error "Cannot read file `%s'" phrase-file))
98 (let ((sym (intern-soft phrase-file cookie-cache)))
99 (and sym (not (equal (symbol-function sym)
100 (nth 5 (file-attributes phrase-file))))
101 (yes-or-no-p (concat phrase-file
102 " has changed. Read new contents? "))
103 (setq sym nil))
104 (if sym
105 (symbol-value sym)
106 (setq sym (intern phrase-file cookie-cache))
107 (message "%s" startmsg)
108 (save-excursion
109 (let ((buf (generate-new-buffer "*cookie*"))
110 (result nil))
111 (set-buffer buf)
112 (fset sym (nth 5 (file-attributes phrase-file)))
113 (insert-file-contents (expand-file-name phrase-file))
114 (re-search-forward cookie-delimiter)
115 (while (progn (skip-chars-forward " \t\n\r\f") (not (eobp)))
116 (let ((beg (point)))
117 (re-search-forward cookie-delimiter)
118 (setq result (cons (buffer-substring beg (match-beginning 0))
119 result))))
120 (kill-buffer buf)
121 (message "%s" endmsg)
122 (set sym (apply 'vector result)))))))
123
124 (defun read-cookie (prompt phrase-file startmsg endmsg &optional require-match)
125 "Prompt with PROMPT and read with completion among cookies in PHRASE-FILE.
126 STARTMSG and ENDMSG are passed along to `cookie-snarf'.
127 Optional fifth arg REQUIRE-MATCH non-nil forces a matching cookie."
128 ;; Make sure the cookies are in the cache.
129 (or (intern-soft phrase-file cookie-cache)
130 (cookie-snarf phrase-file startmsg endmsg))
131 (completing-read prompt
132 (let ((sym (intern phrase-file cookie-cache)))
133 ;; We cache the alist form of the cookie in a property.
134 (or (get sym 'completion-alist)
135 (let* ((alist nil)
136 (vec (cookie-snarf phrase-file
137 startmsg endmsg))
138 (i (length vec)))
139 (while (>= (setq i (1- i)) 0)
140 (setq alist (cons (list (aref vec i)) alist)))
141 (put sym 'completion-alist alist))))
142 nil require-match nil nil))
143
144 ; Thanks to Ian G Batten <BattenIG@CS.BHAM.AC.UK>
145 ; [of the University of Birmingham Computer Science Department]
146 ; for the iterative version of this shuffle.
147 ;
148 ;;;###autoload
149 (defun shuffle-vector (vector)
150 "Randomly permute the elements of VECTOR (all permutations equally likely)."
151 (let ((i 0)
152 j
153 temp
154 (len (length vector)))
155 (while (< i len)
156 (setq j (+ i (random (- len i))))
157 (setq temp (aref vector i))
158 (aset vector i (aref vector j))
159 (aset vector j temp)
160 (setq i (1+ i))))
161 vector)
162
163 (provide 'cookie1)
164
165 ;;; cookie1.el ends here