annotate lisp/play/cookie1.el @ 9445:712af9626dc0

(read_key_sequence): The output of function-key-map goes to key-translation-map, but not vice versa. Don't exit if either of them is a possibility.
author Richard M. Stallman <rms@gnu.org>
date Tue, 11 Oct 1994 09:22:44 +0000
parents 6aeb2227ed30
children 83f275dcd93a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3383
2b0cf11652b9 Renamed to cookie1.el.
Richard M. Stallman <rms@gnu.org>
parents: 2381
diff changeset
1 ;;; cookie1.el --- retrieve random phrases from fortune cookie files
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
2
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
3 ;; Copyright (C) 1993 Free Software Foundation, Inc.
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
4
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
6 ;; Maintainer: FSF
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
7 ;; Keywords: games
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
8 ;; Created: Mon Mar 22 17:06:26 1993
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
9
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
10 ;; This file is part of GNU Emacs.
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
11
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
13 ;; it under the terms of the GNU General Public License as published by
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
14 ;; the Free Software Foundation; either version 2, or (at your option)
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
15 ;; any later version.
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
16
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
17 ;; GNU Emacs is distributed in the hope that it will be useful,
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
20 ;; GNU General Public License for more details.
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
21
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
22 ;; You should have received a copy of the GNU General Public License
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
23 ;; along with GNU Emacs; see the file COPYING. If not, write to
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
25
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
26 ;;; Commentary:
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
27
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
28 ;; Support for random cookie fetches from phrase files, used for such
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
29 ;; critical applications as emulating Zippy the Pinhead and confounding
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
30 ;; the NSA Trunk Trawler.
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
31 ;;
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
32 ;; The two entry points are `cookie' and `cookie-insert'. The helper
4405
935cc145f0b5 (pick-random): Remove.
Paul Eggert <eggert@twinsun.com>
parents: 4151
diff changeset
33 ;; function `shuffle-vector' may be of interest to programmers.
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
34 ;;
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
35 ;; 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
36 ;;
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 ;; * 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
38 ;; 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
39 ;;
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 ;; * 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
41 ;;
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 ;; 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
43 ;; 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
44 ;; `cookie-delimiter'.
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
45 ;;
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
46 ;; 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
47 ;; 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
48 ;; 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
49 ;; from other packages such as yow.el and spook.el.
2322
a7ea7887481f Add TO-DO list.
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2321
diff changeset
50 ;;
a7ea7887481f Add TO-DO list.
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2321
diff changeset
51 ;; TO DO: teach cookie-snarf to auto-detect ITS PINS or UNIX fortune(6)
a7ea7887481f Add TO-DO list.
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2321
diff changeset
52 ;; format and do the right thing.
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
53
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
54 ;;; Code:
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
55
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
56 ; Randomize the seed in the random number generator.
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
57 (random t)
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
58
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
59 (defconst cookie-delimiter "\n%%\n\\|\0"
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
60 "Delimiter used to separate cookie file entries.")
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
61
4090
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
62 (defvar cookie-cache (make-vector 511 0)
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
63 "Cache of cookie files that have already been snarfed.")
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
64
4151
e6d5beaca907 (cookie, cookie-insert, cookie-snarf, shuffle-vector): Autoload these.
Roland McGrath <roland@gnu.org>
parents: 4090
diff changeset
65 ;;;###autoload
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
66 (defun cookie (phrase-file startmsg endmsg)
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
67 "Return a random phrase from PHRASE-FILE. When the phrase file
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
68 is read in, display STARTMSG at beginning of load, ENDMSG at end."
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
69 (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg)))
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
70 (shuffle-vector cookie-vector)
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
71 (aref cookie-vector 1)))
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
72
4151
e6d5beaca907 (cookie, cookie-insert, cookie-snarf, shuffle-vector): Autoload these.
Roland McGrath <roland@gnu.org>
parents: 4090
diff changeset
73 ;;;###autoload
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
74 (defun cookie-insert (phrase-file &optional count startmsg endmsg)
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
75 "Insert random phrases from PHRASE-FILE; COUNT of them. When the phrase file
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
76 is read in, display STARTMSG at beginning of load, ENDMSG at end."
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
77 (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg)))
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
78 (shuffle-vector cookie-vector)
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
79 (let ((start (point)))
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
80 (insert ?\n)
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
81 (cookie1 (min (- (length cookie-vector) 1) (or count 1)) cookie-vector)
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
82 (insert ?\n)
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
83 (fill-region-as-paragraph start (point) nil))))
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
84
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
85 (defun cookie1 (arg cookie-vec)
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
86 "Inserts a cookie phrase ARG times."
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
87 (cond ((zerop arg) t)
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
88 (t (insert (aref cookie-vec arg))
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
89 (insert " ")
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
90 (cookie1 (1- arg) cookie-vec))))
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
91
4151
e6d5beaca907 (cookie, cookie-insert, cookie-snarf, shuffle-vector): Autoload these.
Roland McGrath <roland@gnu.org>
parents: 4090
diff changeset
92 ;;;###autoload
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
93 (defun cookie-snarf (phrase-file startmsg endmsg)
4736
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
94 "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
95 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
96 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
97 (let ((sym (intern-soft phrase-file cookie-cache)))
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
98 (and sym (not (equal (symbol-function sym)
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
99 (nth 5 (file-attributes phrase-file))))
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
100 (yes-or-no-p (concat phrase-file
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
101 " has changed. Read new contents? "))
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
102 (setq sym nil))
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
103 (if sym
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
104 (symbol-value sym)
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
105 (setq sym (intern phrase-file cookie-cache))
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
106 (message startmsg)
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
107 (save-excursion
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
108 (let ((buf (generate-new-buffer "*cookie*"))
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
109 (result nil))
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
110 (set-buffer buf)
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
111 (fset sym (nth 5 (file-attributes phrase-file)))
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
112 (insert-file-contents (expand-file-name phrase-file))
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
113 (re-search-forward cookie-delimiter)
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
114 (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
115 (let ((beg (point)))
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
116 (re-search-forward cookie-delimiter)
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
117 (setq result (cons (buffer-substring beg (1- (point)))
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
118 result))))
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
119 (kill-buffer buf)
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
120 (message endmsg)
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
121 (set sym (apply 'vector result)))))))
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
122
4736
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
123 (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
124 "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
125 STARTMSG and ENDMSG are passed along to `cookie-snarf'.
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
126 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
127 ;; Make sure the cookies are in the cache.
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
128 (or (intern-soft phrase-file cookie-cache)
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
129 (cookie-snarf phrase-file startmsg endmsg))
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
130 (completing-read prompt
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
131 (let ((sym (intern phrase-file cookie-cache)))
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
132 ;; 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
133 (or (get sym 'completion-alist)
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
134 (let* ((alist nil)
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
135 (vec (cookie-snarf phrase-file
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
136 startmsg endmsg))
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
137 (i (length vec)))
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
138 (while (> (setq i (1- i)) 0)
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
139 (setq alist (cons (list (aref vec i)) alist)))
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
140 (put sym 'completion-alist alist))))
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
141 nil require-match nil nil))
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
142
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
143 ; Thanks to Ian G Batten <BattenIG@CS.BHAM.AC.UK>
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
144 ; [of the University of Birmingham Computer Science Department]
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
145 ; for the iterative version of this shuffle.
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
146 ;
4151
e6d5beaca907 (cookie, cookie-insert, cookie-snarf, shuffle-vector): Autoload these.
Roland McGrath <roland@gnu.org>
parents: 4090
diff changeset
147 ;;;###autoload
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
148 (defun shuffle-vector (vector)
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
149 "Randomly permute the elements of VECTOR (all permutations equally likely)"
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
150 (let ((i 0)
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
151 j
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
152 temp
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
153 (len (length vector)))
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
154 (while (< i len)
4405
935cc145f0b5 (pick-random): Remove.
Paul Eggert <eggert@twinsun.com>
parents: 4151
diff changeset
155 (setq j (+ i (random (- len i))))
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
156 (setq temp (aref vector i))
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
157 (aset vector i (aref vector j))
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
158 (aset vector j temp)
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
159 (setq i (1+ i))))
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
160 vector)
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
161
3388
afd441c3e936 Provide cookie1.
Richard M. Stallman <rms@gnu.org>
parents: 3383
diff changeset
162 (provide 'cookie1)
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
163
3432
972428ca06b6 Comment change.
Richard M. Stallman <rms@gnu.org>
parents: 3388
diff changeset
164 ;;; cookie1.el ends here