Commit | Line | Data |
---|---|---|
8eb74eec | 1 | ;;; cookie1.el --- retrieve random phrases from fortune cookie files |
d5edbd11 | 2 | |
73b0cd50 | 3 | ;; Copyright (C) 1993, 2001-2011 Free Software Foundation, Inc. |
d5edbd11 ER |
4 | |
5 | ;; Author: Eric S. Raymond <esr@snark.thyrsus.com> | |
6 | ;; Maintainer: FSF | |
0419a4af | 7 | ;; Keywords: games, extensions |
d5edbd11 ER |
8 | ;; Created: Mon Mar 22 17:06:26 1993 |
9 | ||
10 | ;; This file is part of GNU Emacs. | |
11 | ||
b1fc2b50 | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
d5edbd11 | 13 | ;; it under the terms of the GNU General Public License as published by |
b1fc2b50 GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
d5edbd11 ER |
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 | |
b1fc2b50 | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
d5edbd11 ER |
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 | |
c5eeaf52 | 32 | ;; function `shuffle-vector' may be of interest to programmers. |
d5edbd11 | 33 | ;; |
45b44a5a ER |
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'. | |
d5edbd11 | 44 | ;; |
96ce5c4f GM |
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 | ;; | |
d5edbd11 | 49 | ;; This code derives from Steve Strassman's 1987 spook.el package, but |
45b44a5a ER |
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. | |
d5edbd11 ER |
53 | |
54 | ;;; Code: | |
55 | ||
56 | ; Randomize the seed in the random number generator. | |
57 | (random t) | |
58 | ||
bfaef6dc | 59 | (defconst cookie-delimiter "\n%%\n\\|\n%\n\\|\0" |
d5edbd11 ER |
60 | "Delimiter used to separate cookie file entries.") |
61 | ||
4054367c RM |
62 | (defvar cookie-cache (make-vector 511 0) |
63 | "Cache of cookie files that have already been snarfed.") | |
64 | ||
f58c6f70 | 65 | ;;;###autoload |
d5edbd11 | 66 | (defun cookie (phrase-file startmsg endmsg) |
132c0cc0 PJ |
67 | "Return a random phrase from PHRASE-FILE. |
68 | When the phrase file is read in, display STARTMSG at the beginning | |
69 | of load, ENDMSG at the end." | |
d5edbd11 ER |
70 | (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg))) |
71 | (shuffle-vector cookie-vector) | |
a2064b09 | 72 | (aref cookie-vector 0))) |
d5edbd11 | 73 | |
f58c6f70 | 74 | ;;;###autoload |
d5edbd11 | 75 | (defun cookie-insert (phrase-file &optional count startmsg endmsg) |
132c0cc0 PJ |
76 | "Insert random phrases from PHRASE-FILE; COUNT of them. |
77 | When the phrase file is read in, display STARTMSG at the beginning | |
78 | of load, ENDMSG at the end." | |
d5edbd11 ER |
79 | (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg))) |
80 | (shuffle-vector cookie-vector) | |
81 | (let ((start (point))) | |
82 | (insert ?\n) | |
83 | (cookie1 (min (- (length cookie-vector) 1) (or count 1)) cookie-vector) | |
84 | (insert ?\n) | |
85 | (fill-region-as-paragraph start (point) nil)))) | |
86 | ||
87 | (defun cookie1 (arg cookie-vec) | |
88 | "Inserts a cookie phrase ARG times." | |
89 | (cond ((zerop arg) t) | |
90 | (t (insert (aref cookie-vec arg)) | |
91 | (insert " ") | |
92 | (cookie1 (1- arg) cookie-vec)))) | |
93 | ||
f58c6f70 | 94 | ;;;###autoload |
d5edbd11 | 95 | (defun cookie-snarf (phrase-file startmsg endmsg) |
b81d991d RM |
96 | "Reads in the PHRASE-FILE, returns it as a vector of strings. |
97 | Emit STARTMSG and ENDMSG before and after. Caches the result; second | |
98 | and subsequent calls on the same file won't go to disk." | |
4054367c RM |
99 | (let ((sym (intern-soft phrase-file cookie-cache))) |
100 | (and sym (not (equal (symbol-function sym) | |
101 | (nth 5 (file-attributes phrase-file)))) | |
102 | (yes-or-no-p (concat phrase-file | |
103 | " has changed. Read new contents? ")) | |
104 | (setq sym nil)) | |
105 | (if sym | |
106 | (symbol-value sym) | |
107 | (setq sym (intern phrase-file cookie-cache)) | |
4120f5f5 | 108 | (message "%s" startmsg) |
4054367c RM |
109 | (save-excursion |
110 | (let ((buf (generate-new-buffer "*cookie*")) | |
111 | (result nil)) | |
112 | (set-buffer buf) | |
113 | (fset sym (nth 5 (file-attributes phrase-file))) | |
114 | (insert-file-contents (expand-file-name phrase-file)) | |
115 | (re-search-forward cookie-delimiter) | |
116 | (while (progn (skip-chars-forward " \t\n\r\f") (not (eobp))) | |
117 | (let ((beg (point))) | |
118 | (re-search-forward cookie-delimiter) | |
bfaef6dc | 119 | (setq result (cons (buffer-substring beg (match-beginning 0)) |
4054367c RM |
120 | result)))) |
121 | (kill-buffer buf) | |
4120f5f5 | 122 | (message "%s" endmsg) |
4054367c | 123 | (set sym (apply 'vector result))))))) |
d5edbd11 | 124 | |
b81d991d RM |
125 | (defun read-cookie (prompt phrase-file startmsg endmsg &optional require-match) |
126 | "Prompt with PROMPT and read with completion among cookies in PHRASE-FILE. | |
127 | STARTMSG and ENDMSG are passed along to `cookie-snarf'. | |
128 | Optional fifth arg REQUIRE-MATCH non-nil forces a matching cookie." | |
129 | ;; Make sure the cookies are in the cache. | |
130 | (or (intern-soft phrase-file cookie-cache) | |
131 | (cookie-snarf phrase-file startmsg endmsg)) | |
132 | (completing-read prompt | |
133 | (let ((sym (intern phrase-file cookie-cache))) | |
134 | ;; We cache the alist form of the cookie in a property. | |
135 | (or (get sym 'completion-alist) | |
136 | (let* ((alist nil) | |
137 | (vec (cookie-snarf phrase-file | |
138 | startmsg endmsg)) | |
139 | (i (length vec))) | |
\81ukasz Stelmach, 2010-08-26 17:33:52 +0200">d5720b4c | 140 | (while (>= (setq i (1- i)) 0) |
b81d991d RM |
141 | (setq alist (cons (list (aref vec i)) alist))) |
142 | (put sym 'completion-alist alist)))) | |
143 | nil require-match nil nil)) | |
144 | ||
d5edbd11 ER |
145 | ; Thanks to Ian G Batten <BattenIG@CS.BHAM.AC.UK> |
146 | ; [of the University of Birmingham Computer Science Department] | |
147 | ; for the iterative version of this shuffle. | |
148 | ; | |
f58c6f70 | 149 | ;;;###autoload |
d5edbd11 | 150 | (defun shuffle-vector (vector) |
132c0cc0 | 151 | "Randomly permute the elements of VECTOR (all permutations equally likely)." |
d5edbd11 ER |
152 | (let ((i 0) |
153 | j | |
154 | temp | |
155 | (len (length vector))) | |
156 | (while (< i len) | |
c5eeaf52 | 157 | (setq j (+ i (random (- len i)))) |
d5edbd11 ER |
158 | (setq temp (aref vector i)) |
159 | (aset vector i (aref vector j)) | |
160 | (aset vector j temp) | |
161 | (setq i (1+ i)))) | |
162 | vector) | |
163 | ||
3aa7cce7 | 164 | (provide 'cookie1) |
d5edbd11 | 165 | |
20a82895 | 166 | ;;; cookie1.el ends here |