annotate lisp/play/cookie1.el @ 107777:13c077500eb3

2010-04-04 John Wiegley <jwiegley@gmail.com> * ido.el (ido-use-virtual-buffers): New variable to indicate whether "virtual buffer" support is enabled for IDO. Essentially it works as follows: Say you are visiting a file and the buffer gets cleaned up by mignight.el. Later, you want to switch to that buffer, but find it's no longer open. With virtual buffers enabled, the buffer name stays in the buffer list (using the ido-virtual face, and always at the end), and if you select it, it opens the file back up again. This allows you to think less about whether recently opened files are still open or not. Most of the time you can quit Emacs, restart, and then switch to a file buffer that was previously open as if it still were. NOTE: This feature has been present in iswitchb for several years now, and I'm porting the same logic to IDO. (ido-virtual): Face used to indicate virtual buffers in the list. (ido-buffer-internal): If a buffer is chosen, and no such buffer exists, but a virtual buffer of that name does (which would be why it was in the list), recreate the buffer by reopening the file. (ido-make-buffer-list): If virtual buffers are being used, call `ido-add-virtual-buffers-to-list' before the make list hook. (ido-virtual-buffers): New variable which contains a copy of the current contents of the `recentf-list', albeit pared down for the sake of speed, and with proper faces applied. (ido-add-virtual-buffers-to-list): Using the `recentf-list', create a list of "virtual buffers" to present to the user in addition to the currently open set. Note that this logic could get rather slow if that list is too large. With the default `recentf-max-saved-items' of 200, there is little speed penalty.
author jwiegley@gmail.com
date Sun, 04 Apr 2010 02:55:19 -0400
parents 1d1d5d9bd884
children 3d32b7120396
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
75347
e3694f1cb928 Add 2007 to copyright years.
Glenn Morris <rgm@gnu.org>
parents: 74509
diff changeset
3 ;; Copyright (C) 1993, 2001, 2002, 2003, 2004, 2005,
106815
1d1d5d9bd884 Add 2010 to copyright years.
Glenn Morris <rgm@gnu.org>
parents: 100908
diff changeset
4 ;; 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
5
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
6 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
7 ;; Maintainer: FSF
30858
b9f0d1526fea Add `extensions' to keywords, since it's internal.
Dave Love <fx@gnu.org>
parents: 30514
diff changeset
8 ;; Keywords: games, extensions
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
9 ;; Created: Mon Mar 22 17:06:26 1993
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
10
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
11 ;; This file is part of GNU Emacs.
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
12
94675
949bd6ad1ba4 Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 93975
diff changeset
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
14 ;; it under the terms of the GNU General Public License as published by
94675
949bd6ad1ba4 Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 93975
diff changeset
15 ;; the Free Software Foundation, either version 3 of the License, or
949bd6ad1ba4 Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 93975
diff changeset
16 ;; (at your option) any later version.
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
17
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
18 ;; 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
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
21 ;; GNU General Public License for more details.
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
22
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
23 ;; You should have received a copy of the GNU General Public License
94675
949bd6ad1ba4 Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 93975
diff changeset
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
2321
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 ;;
30514
42bf9adb59a3 Add explanation of how to make cookie.el
Gerd Moellmann <gerd@gnu.org>
parents: 20887
diff changeset
46 ;; strfile(1) is the program used to compile the files for fortune(6).
42bf9adb59a3 Add explanation of how to make cookie.el
Gerd Moellmann <gerd@gnu.org>
parents: 20887
diff changeset
47 ;; In order to achieve total compatibility with strfile(1), cookie files
42bf9adb59a3 Add explanation of how to make cookie.el
Gerd Moellmann <gerd@gnu.org>
parents: 20887
diff changeset
48 ;; should start with two consecutive delimiters (and no comment).
42bf9adb59a3 Add explanation of how to make cookie.el
Gerd Moellmann <gerd@gnu.org>
parents: 20887
diff changeset
49 ;;
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
50 ;; 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
51 ;; 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
52 ;; 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
53 ;; from other packages such as yow.el and spook.el.
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
54
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
55 ;;; Code:
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
56
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
57 ; Randomize the seed in the random number generator.
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
58 (random t)
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
59
20887
bb0f5fde5973 (cookie-snarf): Use match-beginning on the delimiter.
Richard M. Stallman <rms@gnu.org>
parents: 14307
diff changeset
60 (defconst cookie-delimiter "\n%%\n\\|\n%\n\\|\0"
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
61 "Delimiter used to separate cookie file entries.")
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
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
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
67 (defun cookie (phrase-file startmsg endmsg)
41666
5cd64b3e28c3 (cookie, cookie-insert, shuffle-vector): Doc fixes.
Pavel Janík <Pavel@Janik.cz>
parents: 30858
diff changeset
68 "Return a random phrase from PHRASE-FILE.
5cd64b3e28c3 (cookie, cookie-insert, shuffle-vector): Doc fixes.
Pavel Janík <Pavel@Janik.cz>
parents: 30858
diff changeset
69 When the phrase file is read in, display STARTMSG at the beginning
5cd64b3e28c3 (cookie, cookie-insert, shuffle-vector): Doc fixes.
Pavel Janík <Pavel@Janik.cz>
parents: 30858
diff changeset
70 of load, ENDMSG at the end."
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
71 (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg)))
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
72 (shuffle-vector cookie-vector)
71413
e04e474d258e lisp/play/cookie1.el (cookie): Work properly when there's only one entry
Miles Bader <miles@gnu.org>
parents: 68634
diff changeset
73 (aref cookie-vector 0)))
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
74
4151
e6d5beaca907 (cookie, cookie-insert, cookie-snarf, shuffle-vector): Autoload these.
Roland McGrath <roland@gnu.org>
parents: 4090
diff changeset
75 ;;;###autoload
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
76 (defun cookie-insert (phrase-file &optional count startmsg endmsg)
41666
5cd64b3e28c3 (cookie, cookie-insert, shuffle-vector): Doc fixes.
Pavel Janík <Pavel@Janik.cz>
parents: 30858
diff changeset
77 "Insert random phrases from PHRASE-FILE; COUNT of them.
5cd64b3e28c3 (cookie, cookie-insert, shuffle-vector): Doc fixes.
Pavel Janík <Pavel@Janik.cz>
parents: 30858
diff changeset
78 When the phrase file is read in, display STARTMSG at the beginning
5cd64b3e28c3 (cookie, cookie-insert, shuffle-vector): Doc fixes.
Pavel Janík <Pavel@Janik.cz>
parents: 30858
diff changeset
79 of load, ENDMSG at the end."
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
80 (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg)))
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
81 (shuffle-vector cookie-vector)
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
82 (let ((start (point)))
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
83 (insert ?\n)
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
84 (cookie1 (min (- (length cookie-vector) 1) (or count 1)) cookie-vector)
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
85 (insert ?\n)
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
86 (fill-region-as-paragraph start (point) nil))))
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
87
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
88 (defun cookie1 (arg cookie-vec)
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
89 "Inserts a cookie phrase ARG times."
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
90 (cond ((zerop arg) t)
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
91 (t (insert (aref cookie-vec arg))
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
92 (insert " ")
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
93 (cookie1 (1- arg) cookie-vec))))
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
94
4151
e6d5beaca907 (cookie, cookie-insert, cookie-snarf, shuffle-vector): Autoload these.
Roland McGrath <roland@gnu.org>
parents: 4090
diff changeset
95 ;;;###autoload
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
96 (defun cookie-snarf (phrase-file startmsg endmsg)
4736
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
97 "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
98 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
99 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
100 (let ((sym (intern-soft phrase-file cookie-cache)))
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
101 (and sym (not (equal (symbol-function sym)
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
102 (nth 5 (file-attributes phrase-file))))
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
103 (yes-or-no-p (concat phrase-file
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
104 " has changed. Read new contents? "))
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
105 (setq sym nil))
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
106 (if sym
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
107 (symbol-value sym)
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
108 (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
109 (message "%s" startmsg)
4090
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
110 (save-excursion
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
111 (let ((buf (generate-new-buffer "*cookie*"))
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
112 (result nil))
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
113 (set-buffer buf)
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
114 (fset sym (nth 5 (file-attributes phrase-file)))
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
115 (insert-file-contents (expand-file-name phrase-file))
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 (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
118 (let ((beg (point)))
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
119 (re-search-forward cookie-delimiter)
20887
bb0f5fde5973 (cookie-snarf): Use match-beginning on the delimiter.
Richard M. Stallman <rms@gnu.org>
parents: 14307
diff changeset
120 (setq result (cons (buffer-substring beg (match-beginning 0))
4090
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
121 result))))
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
122 (kill-buffer buf)
14307
8f846c3889c8 (cookie-snarf): Pass proper format string to message.
Karl Heuer <kwzh@gnu.org>
parents: 14169
diff changeset
123 (message "%s" endmsg)
4090
afa2afad53c4 (cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents: 3432
diff changeset
124 (set sym (apply 'vector result)))))))
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
125
4736
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
126 (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
127 "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
128 STARTMSG and ENDMSG are passed along to `cookie-snarf'.
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
129 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
130 ;; Make sure the cookies are in the cache.
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
131 (or (intern-soft phrase-file cookie-cache)
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
132 (cookie-snarf phrase-file startmsg endmsg))
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
133 (completing-read prompt
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
134 (let ((sym (intern phrase-file cookie-cache)))
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
135 ;; 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
136 (or (get sym 'completion-alist)
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
137 (let* ((alist nil)
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
138 (vec (cookie-snarf phrase-file
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
139 startmsg endmsg))
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
140 (i (length vec)))
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
141 (while (> (setq i (1- i)) 0)
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
142 (setq alist (cons (list (aref vec i)) alist)))
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
143 (put sym 'completion-alist alist))))
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
144 nil require-match nil nil))
6aeb2227ed30 (read-cookie): New function.
Roland McGrath <roland@gnu.org>
parents: 4405
diff changeset
145
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
146 ; Thanks to Ian G Batten <BattenIG@CS.BHAM.AC.UK>
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
147 ; [of the University of Birmingham Computer Science Department]
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
148 ; for the iterative version of this shuffle.
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
149 ;
4151
e6d5beaca907 (cookie, cookie-insert, cookie-snarf, shuffle-vector): Autoload these.
Roland McGrath <roland@gnu.org>
parents: 4090
diff changeset
150 ;;;###autoload
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
151 (defun shuffle-vector (vector)
41666
5cd64b3e28c3 (cookie, cookie-insert, shuffle-vector): Doc fixes.
Pavel Janík <Pavel@Janik.cz>
parents: 30858
diff changeset
152 "Randomly permute the elements of VECTOR (all permutations equally likely)."
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
153 (let ((i 0)
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
154 j
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
155 temp
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
156 (len (length vector)))
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
157 (while (< i len)
4405
935cc145f0b5 (pick-random): Remove.
Paul Eggert <eggert@twinsun.com>
parents: 4151
diff changeset
158 (setq j (+ i (random (- len i))))
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
159 (setq temp (aref vector i))
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
160 (aset vector i (aref vector j))
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
161 (aset vector j temp)
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
162 (setq i (1+ i))))
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
163 vector)
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
164
3388
afd441c3e936 Provide cookie1.
Richard M. Stallman <rms@gnu.org>
parents: 3383
diff changeset
165 (provide 'cookie1)
2321
63128ec90cfe Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
166
93975
1e3a407766b9 Fix up comment convention on the arch-tag lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 79716
diff changeset
167 ;; arch-tag: 4a8a8712-df6a-4f34-b030-108a1b47f9f2
3432
972428ca06b6 Comment change.
Richard M. Stallman <rms@gnu.org>
parents: 3388
diff changeset
168 ;;; cookie1.el ends here