Mercurial > emacs
annotate lisp/play/cookie1.el @ 19161:248ec4d8a5e6
(mail-mode): Fix previous change.
Treat a supercite prefix not followed by text
as a paragraph separator.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Tue, 05 Aug 1997 06:41:51 +0000 |
parents | 8f846c3889c8 |
children | bb0f5fde5973 |
rev | line source |
---|---|
3383 | 1 ;;; cookie1.el --- retrieve random phrases from fortune cookie files |
2321 | 2 |
3 ;; Copyright (C) 1993 Free Software Foundation, Inc. | |
4 | |
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com> | |
6 ;; Maintainer: FSF | |
7 ;; Keywords: games | |
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 2, or (at your option) | |
15 ;; 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 | |
14169 | 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. | |
2321 | 26 |
27 ;;; Commentary: | |
28 | |
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. | |
32 ;; | |
33 ;; The two entry points are `cookie' and `cookie-insert'. The helper | |
4405 | 34 ;; function `shuffle-vector' may be of interest to programmers. |
2321 | 35 ;; |
2381
f8ae5fc2c196
(cookie) Enhanced it to handle both LINS files and UNIX fortune files.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2322
diff
changeset
|
36 ;; The code expects phrase files to be in one of two formats: |
f8ae5fc2c196
(cookie) Enhanced it to handle both LINS files and UNIX fortune files.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2322
diff
changeset
|
37 ;; |
f8ae5fc2c196
(cookie) Enhanced it to handle both LINS files and UNIX fortune files.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2322
diff
changeset
|
38 ;; * ITS-style LINS format (strings terminated by ASCII 0 characters, |
f8ae5fc2c196
(cookie) Enhanced it to handle both LINS files and UNIX fortune files.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2322
diff
changeset
|
39 ;; leading whitespace ignored). |
f8ae5fc2c196
(cookie) Enhanced it to handle both LINS files and UNIX fortune files.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2322
diff
changeset
|
40 ;; |
f8ae5fc2c196
(cookie) Enhanced it to handle both LINS files and UNIX fortune files.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2322
diff
changeset
|
41 ;; * UNIX fortune file format (quotes terminated by %% on a line by itself). |
f8ae5fc2c196
(cookie) Enhanced it to handle both LINS files and UNIX fortune files.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2322
diff
changeset
|
42 ;; |
f8ae5fc2c196
(cookie) Enhanced it to handle both LINS files and UNIX fortune files.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2322
diff
changeset
|
43 ;; Everything up to the first delimiter is treated as a comment. Other |
f8ae5fc2c196
(cookie) Enhanced it to handle both LINS files and UNIX fortune files.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2322
diff
changeset
|
44 ;; formats could be supported by adding alternates to the regexp |
f8ae5fc2c196
(cookie) Enhanced it to handle both LINS files and UNIX fortune files.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2322
diff
changeset
|
45 ;; `cookie-delimiter'. |
2321 | 46 ;; |
47 ;; This code derives from Steve Strassman's 1987 spook.el package, but | |
2381
f8ae5fc2c196
(cookie) Enhanced it to handle both LINS files and UNIX fortune files.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2322
diff
changeset
|
48 ;; has been generalized so that it supports multiple simultaneous |
f8ae5fc2c196
(cookie) Enhanced it to handle both LINS files and UNIX fortune files.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2322
diff
changeset
|
49 ;; cookie databases and fortune files. It is intended to be called |
f8ae5fc2c196
(cookie) Enhanced it to handle both LINS files and UNIX fortune files.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2322
diff
changeset
|
50 ;; from other packages such as yow.el and spook.el. |
2322 | 51 ;; |
52 ;; TO DO: teach cookie-snarf to auto-detect ITS PINS or UNIX fortune(6) | |
53 ;; format and do the right thing. | |
2321 | 54 |
55 ;;; Code: | |
56 | |
57 ; Randomize the seed in the random number generator. | |
58 (random t) | |
59 | |
2381
f8ae5fc2c196
(cookie) Enhanced it to handle both LINS files and UNIX fortune files.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2322
diff
changeset
|
60 (defconst cookie-delimiter "\n%%\n\\|\0" |
2321 | 61 "Delimiter used to separate cookie file entries.") |
62 | |
4090
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
63 (defvar cookie-cache (make-vector 511 0) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
64 "Cache of cookie files that have already been snarfed.") |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
65 |
4151
e6d5beaca907
(cookie, cookie-insert, cookie-snarf, shuffle-vector): Autoload these.
Roland McGrath <roland@gnu.org>
parents:
4090
diff
changeset
|
66 ;;;###autoload |
2321 | 67 (defun cookie (phrase-file startmsg endmsg) |
68 "Return a random phrase from PHRASE-FILE. When the phrase file | |
69 is read in, display STARTMSG at beginning of load, ENDMSG at end." | |
70 (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg))) | |
71 (shuffle-vector cookie-vector) | |
72 (aref cookie-vector 1))) | |
73 | |
4151
e6d5beaca907
(cookie, cookie-insert, cookie-snarf, shuffle-vector): Autoload these.
Roland McGrath <roland@gnu.org>
parents:
4090
diff
changeset
|
74 ;;;###autoload |
2321 | 75 (defun cookie-insert (phrase-file &optional count startmsg endmsg) |
76 "Insert random phrases from PHRASE-FILE; COUNT of them. When the phrase file | |
77 is read in, display STARTMSG at beginning of load, ENDMSG at end." | |
78 (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg))) | |
79 (shuffle-vector cookie-vector) | |
80 (let ((start (point))) | |
81 (insert ?\n) | |
82 (cookie1 (min (- (length cookie-vector) 1) (or count 1)) cookie-vector) | |
83 (insert ?\n) | |
84 (fill-region-as-paragraph start (point) nil)))) | |
85 | |
86 (defun cookie1 (arg cookie-vec) | |
87 "Inserts a cookie phrase ARG times." | |
88 (cond ((zerop arg) t) | |
89 (t (insert (aref cookie-vec arg)) | |
90 (insert " ") | |
91 (cookie1 (1- arg) cookie-vec)))) | |
92 | |
4151
e6d5beaca907
(cookie, cookie-insert, cookie-snarf, shuffle-vector): Autoload these.
Roland McGrath <roland@gnu.org>
parents:
4090
diff
changeset
|
93 ;;;###autoload |
2321 | 94 (defun cookie-snarf (phrase-file startmsg endmsg) |
4736
6aeb2227ed30
(read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents:
4405
diff
changeset
|
95 "Reads in the PHRASE-FILE, returns it as a vector of strings. |
6aeb2227ed30
(read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents:
4405
diff
changeset
|
96 Emit STARTMSG and ENDMSG before and after. Caches the result; second |
6aeb2227ed30
(read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents:
4405
diff
changeset
|
97 and subsequent calls on the same file won't go to disk." |
4090
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
98 (let ((sym (intern-soft phrase-file cookie-cache))) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
99 (and sym (not (equal (symbol-function sym) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
100 (nth 5 (file-attributes phrase-file)))) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
101 (yes-or-no-p (concat phrase-file |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
102 " has changed. Read new contents? ")) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
103 (setq sym nil)) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
104 (if sym |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
105 (symbol-value sym) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
106 (setq sym (intern phrase-file cookie-cache)) |
14307
8f846c3889c8
(cookie-snarf): Pass proper format string to message.
Karl Heuer <kwzh@gnu.org>
parents:
14169
diff
changeset
|
107 (message "%s" startmsg) |
4090
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
108 (save-excursion |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
109 (let ((buf (generate-new-buffer "*cookie*")) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
110 (result nil)) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
111 (set-buffer buf) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
112 (fset sym (nth 5 (file-attributes phrase-file))) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
113 (insert-file-contents (expand-file-name phrase-file)) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
114 (re-search-forward cookie-delimiter) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
115 (while (progn (skip-chars-forward " \t\n\r\f") (not (eobp))) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
116 (let ((beg (point))) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
117 (re-search-forward cookie-delimiter) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
118 (setq result (cons (buffer-substring beg (1- (point))) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
119 result)))) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
120 (kill-buffer buf) |
14307
8f846c3889c8
(cookie-snarf): Pass proper format string to message.
Karl Heuer <kwzh@gnu.org>
parents:
14169
diff
changeset
|
121 (message "%s" endmsg) |
4090
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
122 (set sym (apply 'vector result))))))) |
2321 | 123 |
4736
6aeb2227ed30
(read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents:
4405
diff
changeset
|
124 (defun read-cookie (prompt phrase-file startmsg endmsg &optional require-match) |
6aeb2227ed30
(read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents:
4405
diff
changeset
|
125 "Prompt with PROMPT and read with completion among cookies in PHRASE-FILE. |
6aeb2227ed30
(read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents:
4405
diff
changeset
|
126 STARTMSG and ENDMSG are passed along to `cookie-snarf'. |
6aeb2227ed30
(read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents:
4405
diff
changeset
|
127 Optional fifth arg REQUIRE-MATCH non-nil forces a matching cookie." |
6aeb2227ed30
(read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents:
4405
diff
changeset
|
128 ;; Make sure the cookies are in the cache. |
6aeb2227ed30
(read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents:
4405
diff
changeset
|
129 (or (intern-soft phrase-file cookie-cache) |
6aeb2227ed30
(read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents:
4405
diff
changeset
|
130 (cookie-snarf phrase-file startmsg endmsg)) |
6aeb2227ed30
(read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents:
4405
diff
changeset
|
131 (completing-read prompt |
6aeb2227ed30
(read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents:
4405
diff
changeset
|
132 (let ((sym (intern phrase-file cookie-cache))) |
6aeb2227ed30
(read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents:
4405
diff
changeset
|
133 ;; We cache the alist form of the cookie in a property. |
6aeb2227ed30
(read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents:
4405
diff
changeset
|
134 (or (get sym 'completion-alist) |
6aeb2227ed30
(read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents:
4405
diff
changeset
|
135 (let* ((alist nil) |
6aeb2227ed30
(read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents:
4405
diff
changeset
|
136 (vec (cookie-snarf phrase-file |
6aeb2227ed30
(read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents:
4405
diff
changeset
|
137 startmsg endmsg)) |
6aeb2227ed30
(read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents:
4405
diff
changeset
|
138 (i (length vec))) |
6aeb2227ed30
(read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents:
4405
diff
changeset
|
139 (while (> (setq i (1- i)) 0) |
6aeb2227ed30
(read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents:
4405
diff
changeset
|
140 (setq alist (cons (list (aref vec i)) alist))) |
6aeb2227ed30
(read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents:
4405
diff
changeset
|
141 (put sym 'completion-alist alist)))) |
6aeb2227ed30
(read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents:
4405
diff
changeset
|
142 nil require-match nil nil)) |
6aeb2227ed30
(read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents:
4405
diff
changeset
|
143 |
2321 | 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 ; | |
4151
e6d5beaca907
(cookie, cookie-insert, cookie-snarf, shuffle-vector): Autoload these.
Roland McGrath <roland@gnu.org>
parents:
4090
diff
changeset
|
148 ;;;###autoload |
2321 | 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) | |
4405 | 156 (setq j (+ i (random (- len i)))) |
2321 | 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 | |
3388 | 163 (provide 'cookie1) |
2321 | 164 |
3432 | 165 ;;; cookie1.el ends here |