Commit | Line | Data |
---|---|---|
8eb74eec | 1 | ;;; cookie1.el --- retrieve random phrases from fortune cookie files |
d5edbd11 | 2 | |
d7a0267c | 3 | ;; Copyright (C) 1993, 2001, 2002, 2003, 2004, 2005, |
49f70d46 | 4 | ;; 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc. |
d5edbd11 ER |
5 | |
6 | ;; Author: Eric S. Raymond <esr@snark.thyrsus.com> | |
7 | ;; Maintainer: FSF | |
0419a4af | 8 | ;; Keywords: games, extensions |
d5edbd11 ER |
9 | ;; Created: Mon Mar 22 17:06:26 1993 |
10 | ||
11 | ;; This file is part of GNU Emacs. | |
12 | ||
b1fc2b50 | 13 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
d5edbd11 | 14 | ;; it under the terms of the GNU General Public License as published by |
b1fc2b50 GM |
15 | ;; the Free Software Foundation, either version 3 of the License, or |
16 | ;; (at your option) any later version. | |
d5edbd11 ER |
17 | |
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. | |
22 | ||
23 | ;; You should have received a copy of the GNU General Public License | |
b1fc2b50 | 24 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
d5edbd11 ER |
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 | |
c5eeaf52 | 33 | ;; function `shuffle-vector' may be of interest to programmers. |
d5edbd11 | 34 | ;; |
45b44a5a ER |
35 | ;; The code expects phrase files to be in one of two formats: |
36 | ;; | |
37 | ;; * ITS-style LINS format (strings terminated by ASCII 0 characters, | |
38 | ;; leading whitespace ignored). | |
39 | ;; | |
40 | ;; * UNIX fortune file format (quotes terminated by %% on a line by itself). | |
41 | ;; | |
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'. | |
d5edbd11 | 45 | ;; |
96ce5c4f GM |
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). | |
49 | ;; | |
d5edbd11 | 50 | ;; This code derives from Steve Strassman's 1987 spook.el package, but |
45b44a5a ER |
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. | |
d5edbd11 ER |
54 | |
55 | ;;; Code: | |
56 | ||
57 | ; Randomize the seed in the random number generator. | |
58 | (random t) | |
59 | ||
bfaef6dc | 60 | (defconst cookie-delimiter "\n%%\n\\|\n%\n\\|\0" |
d5edbd11 ER |
61 | "Delimiter used to separate cookie file entries.") |
62 | ||
4054367c RM |
63 | (defvar cookie-cache (make-vector 511 0) |
64 | "Cache of cookie files that have already been snarfed.") | |
65 | ||
f58c6f70 | 66 | ;;;###autoload |
d5edbd11 | 67 | (defun cookie (phrase-file startmsg endmsg) |
132c0cc0 PJ |
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." | |
d5edbd11 ER |
71 | (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg))) |
72 | (shuffle-vector cookie-vector) | |
a2064b09 | 73 | (aref cookie-vector 0))) |
d5edbd11 | 74 | |
f58c6f70 | 75 | ;;;###autoload |
d5edbd11 | 76 | (defun cookie-insert (phrase-file &optional count startmsg endmsg) |
132c0cc0 PJ |
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." | |
d5edbd11 ER |
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)))) | |
87 | ||
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)))) | |
94 | ||
f58c6f70 | 95 | ;;;###autoload |
d5edbd11 | 96 | (defun cookie-snarf (phrase-file startmsg endmsg) |
b81d991d RM |
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." | |
4054367c RM |
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)) | |
4120f5f5 | 109 | (message "%s" startmsg) |
4054367c RM |
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) | |
bfaef6dc | 120 | (setq result (cons (buffer-substring beg (match-beginning 0)) |
4054367c RM |
121 | result)))) |
122 | (kill-buffer buf) | |
4120f5f5 | 123 | (message "%s" endmsg) |
4054367c | 124 | (set sym (apply 'vector result))))))) |
d5edbd11 | 125 | |
b81d991d RM |
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))) | |
\81ukasz Stelmach, 2010-08-26 17:33:52 +0200">d5720b4c | 141 | (while (>= (setq i (1- i)) 0) |
b81d991d RM |
142 | (setq alist (cons (list (aref vec i)) alist))) |
143 | (put sym 'completion-alist alist)))) | |
144 | nil require-match nil nil)) | |
145 | ||
d5edbd11 ER |
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. | |
149 | ; | |
f58c6f70 | 150 | ;;;###autoload |
d5edbd11 | 151 | (defun shuffle-vector (vector) |
132c0cc0 | 152 | "Randomly permute the elements of VECTOR (all permutations equally likely)." |
d5edbd11 ER |
153 | (let ((i 0) |
154 | j | |
155 | temp | |
156 | (len (length vector))) | |
157 | (while (< i len) | |
c5eeaf52 | 158 | (setq j (+ i (random (- len i)))) |
d5edbd11 ER |
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) | |
164 | ||
3aa7cce7 | 165 | (provide 'cookie1) |
d5edbd11 | 166 | |
cbee283d | 167 | ;; arch-tag: 4a8a8712-df6a-4f34-b030-108a1b47f9f2 |
20a82895 | 168 | ;;; cookie1.el ends here |