aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric S. Raymond1993-03-22 22:58:27 +0000
committerEric S. Raymond1993-03-22 22:58:27 +0000
commitd5edbd116f76fe8ad9ded062352dd5eb4e2d4686 (patch)
treec4f52ce9d14fad8baeb8220f07ea05046181e4c3
parented73b2568d16197d327c7d00d3116ae72caffa67 (diff)
downloademacs-d5edbd116f76fe8ad9ded062352dd5eb4e2d4686.tar.gz
emacs-d5edbd116f76fe8ad9ded062352dd5eb4e2d4686.zip
Initial revision
-rw-r--r--lisp/play/cookie1.el126
1 files changed, 126 insertions, 0 deletions
diff --git a/lisp/play/cookie1.el b/lisp/play/cookie1.el
new file mode 100644
index 00000000000..b9babdfeec2
--- /dev/null
+++ b/lisp/play/cookie1.el
@@ -0,0 +1,126 @@
1;;; cookie.el --- retrieve random phrases from fortune cookie files
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
23;; along with GNU Emacs; see the file COPYING. If not, write to
24;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
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;; functions `pick-random' and `shuffle-vector' may be of interest to
34;; programmers.
35;;
36;; The code expects phrase files to be in ITS-style LINS format
37;; (strings terminated by ASCII 0 characters, leading whitespace
38;; ignored). Everything up to the first delimiter is treated as a
39;; comment. Other formats (notably UNIX fortune-file format) could
40;; easily be supported by changing the `cookie-delimiter' constant.
41;;
42;; This code derives from Steve Strassman's 1987 spook.el package, but
43;; has been generalized so that it supports multiple simultaneous cookie
44;; databases. It is intended to be called from other packages such as
45;; yow.el and spook.el.
46
47;;; Code:
48
49; Randomize the seed in the random number generator.
50(random t)
51
52(defconst cookie-delimiter "\0"
53 "Delimiter used to separate cookie file entries.")
54
55(defun cookie (phrase-file startmsg endmsg)
56 "Return a random phrase from PHRASE-FILE. When the phrase file
57is read in, display STARTMSG at beginning of load, ENDMSG at end."
58 (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg)))
59 (shuffle-vector cookie-vector)
60 (aref cookie-vector 1)))
61
62(defun cookie-insert (phrase-file &optional count startmsg endmsg)
63 "Insert random phrases from PHRASE-FILE; COUNT of them. When the phrase file
64is read in, display STARTMSG at beginning of load, ENDMSG at end."
65 (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg)))
66 (shuffle-vector cookie-vector)
67 (let ((start (point)))
68 (insert ?\n)
69 (cookie1 (min (- (length cookie-vector) 1) (or count 1)) cookie-vector)
70 (insert ?\n)
71 (fill-region-as-paragraph start (point) nil))))
72
73(defun cookie1 (arg cookie-vec)
74 "Inserts a cookie phrase ARG times."
75 (cond ((zerop arg) t)
76 (t (insert (aref cookie-vec arg))
77 (insert " ")
78 (cookie1 (1- arg) cookie-vec))))
79
80(defun cookie-snarf (phrase-file startmsg endmsg)
81 "Reads in the PHRASE-FILE, returns it as a vector of strings. Emit
82STARTMSG and ENDMSG before and after. Caches the result; second and
83subsequent calls on the same file won't go to disk."
84 (if (boundp (intern phrase-file))
85 (eval (intern phrase-file))
86 (message startmsg)
87 (save-excursion
88 (let ((buf (generate-new-buffer "*cookie*"))
89 (result nil))
90 (set-buffer buf)
91 (insert-file-contents (expand-file-name phrase-file))
92 (search-forward cookie-delimiter)
93 (while (progn (skip-chars-forward " \t\n\r\f") (not (eobp)))
94 (let ((beg (point)))
95 (search-forward "\0")
96 (setq result (cons (buffer-substring beg (1- (point)))
97 result))))
98 (kill-buffer buf)
99 (message endmsg)
100 (set (intern phrase-file) (apply 'vector result))))))
101
102(defun pick-random (n)
103 "Returns a random number from 0 to N-1 inclusive."
104 (% (logand 0777777 (random)) n))
105
106; Thanks to Ian G Batten <BattenIG@CS.BHAM.AC.UK>
107; [of the University of Birmingham Computer Science Department]
108; for the iterative version of this shuffle.
109;
110(defun shuffle-vector (vector)
111 "Randomly permute the elements of VECTOR (all permutations equally likely)"
112 (let ((i 0)
113 j
114 temp
115 (len (length vector)))
116 (while (< i len)
117 (setq j (+ i (pick-random (- len i))))
118 (setq temp (aref vector i))
119 (aset vector i (aref vector j))
120 (aset vector j temp)
121 (setq i (1+ i))))
122 vector)
123
124(provide 'cookie)
125
126;;; cookie.el ends here