Backport from trunk.
[emacs.git] / lisp / play / cookie1.el
blobe786c6cc5c1f6e58e8b84ff1e013a1f7ffba6ac0
1 ;;; cookie1.el --- retrieve random phrases from fortune cookie files
3 ;; Copyright (C) 1993, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
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
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/>.
26 ;;; Commentary:
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 ;;; Code:
57 ; Randomize the seed in the random number generator.
58 (random t)
60 (defconst cookie-delimiter "\n%%\n\\|\n%\n\\|\0"
61 "Delimiter used to separate cookie file entries.")
63 (defvar cookie-cache (make-vector 511 0)
64 "Cache of cookie files that have already been snarfed.")
66 ;;;###autoload
67 (defun cookie (phrase-file startmsg endmsg)
68 "Return a random phrase from PHRASE-FILE.
69 When the phrase file is read in, display STARTMSG at the beginning
70 of load, ENDMSG at the end."
71 (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg)))
72 (shuffle-vector cookie-vector)
73 (aref cookie-vector 0)))
75 ;;;###autoload
76 (defun cookie-insert (phrase-file &optional count startmsg endmsg)
77 "Insert random phrases from PHRASE-FILE; COUNT of them.
78 When the phrase file is read in, display STARTMSG at the beginning
79 of load, ENDMSG at the end."
80 (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg)))
81 (shuffle-vector cookie-vector)
82 (let ((start (point)))
83 (insert ?\n)
84 (cookie1 (min (- (length cookie-vector) 1) (or count 1)) cookie-vector)
85 (insert ?\n)
86 (fill-region-as-paragraph start (point) nil))))
88 (defun cookie1 (arg cookie-vec)
89 "Inserts a cookie phrase ARG times."
90 (cond ((zerop arg) t)
91 (t (insert (aref cookie-vec arg))
92 (insert " ")
93 (cookie1 (1- arg) cookie-vec))))
95 ;;;###autoload
96 (defun cookie-snarf (phrase-file startmsg endmsg)
97 "Reads in the PHRASE-FILE, returns it as a vector of strings.
98 Emit STARTMSG and ENDMSG before and after. Caches the result; second
99 and subsequent calls on the same file won't go to disk."
100 (let ((sym (intern-soft phrase-file cookie-cache)))
101 (and sym (not (equal (symbol-function sym)
102 (nth 5 (file-attributes phrase-file))))
103 (yes-or-no-p (concat phrase-file
104 " has changed. Read new contents? "))
105 (setq sym nil))
106 (if sym
107 (symbol-value sym)
108 (setq sym (intern phrase-file cookie-cache))
109 (message "%s" startmsg)
110 (save-excursion
111 (let ((buf (generate-new-buffer "*cookie*"))
112 (result nil))
113 (set-buffer buf)
114 (fset sym (nth 5 (file-attributes phrase-file)))
115 (insert-file-contents (expand-file-name phrase-file))
116 (re-search-forward cookie-delimiter)
117 (while (progn (skip-chars-forward " \t\n\r\f") (not (eobp)))
118 (let ((beg (point)))
119 (re-search-forward cookie-delimiter)
120 (setq result (cons (buffer-substring beg (match-beginning 0))
121 result))))
122 (kill-buffer buf)
123 (message "%s" endmsg)
124 (set sym (apply 'vector result)))))))
126 (defun read-cookie (prompt phrase-file startmsg endmsg &optional require-match)
127 "Prompt with PROMPT and read with completion among cookies in PHRASE-FILE.
128 STARTMSG and ENDMSG are passed along to `cookie-snarf'.
129 Optional fifth arg REQUIRE-MATCH non-nil forces a matching cookie."
130 ;; Make sure the cookies are in the cache.
131 (or (intern-soft phrase-file cookie-cache)
132 (cookie-snarf phrase-file startmsg endmsg))
133 (completing-read prompt
134 (let ((sym (intern phrase-file cookie-cache)))
135 ;; We cache the alist form of the cookie in a property.
136 (or (get sym 'completion-alist)
137 (let* ((alist nil)
138 (vec (cookie-snarf phrase-file
139 startmsg endmsg))
140 (i (length vec)))
141 (while (>= (setq i (1- i)) 0)
142 (setq alist (cons (list (aref vec i)) alist)))
143 (put sym 'completion-alist alist))))
144 nil require-match nil nil))
146 ; Thanks to Ian G Batten <BattenIG@CS.BHAM.AC.UK>
147 ; [of the University of Birmingham Computer Science Department]
148 ; for the iterative version of this shuffle.
150 ;;;###autoload
151 (defun shuffle-vector (vector)
152 "Randomly permute the elements of VECTOR (all permutations equally likely)."
153 (let ((i 0)
155 temp
156 (len (length vector)))
157 (while (< i len)
158 (setq j (+ i (random (- len i))))
159 (setq temp (aref vector i))
160 (aset vector i (aref vector j))
161 (aset vector j temp)
162 (setq i (1+ i))))
163 vector)
165 (provide 'cookie1)
167 ;; arch-tag: 4a8a8712-df6a-4f34-b030-108a1b47f9f2
168 ;;; cookie1.el ends here