Mercurial > emacs
annotate lisp/play/cookie1.el @ 4112:ab7dee78e335
(comint-{next,prev}-prompt, comint-send-eof): New functions.
(comint-mode-map): Bind C-c C-n, C-c C-p, and C-c C-d to those.
Bind C-c C-y to comint-previous-input, for compatibility with v18 shell.el.
author | Roland McGrath <roland@gnu.org> |
---|---|
date | Fri, 16 Jul 1993 20:30:18 +0000 |
parents | afa2afad53c4 |
children | e6d5beaca907 |
rev | line source |
---|---|
3383 | 1 ;;; cookie1.el --- retrieve random phrases from fortune cookie files |
2321 | 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 ;; | |
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
|
36 ;; 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
|
37 ;; |
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 ;; * 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
|
39 ;; 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
|
40 ;; |
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 ;; * 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
|
42 ;; |
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 ;; 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
|
44 ;; 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
|
45 ;; `cookie-delimiter'. |
2321 | 46 ;; |
47 ;; 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
|
48 ;; 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
|
49 ;; 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
|
50 ;; from other packages such as yow.el and spook.el. |
2322 | 51 ;; |
52 ;; TO DO: teach cookie-snarf to auto-detect ITS PINS or UNIX fortune(6) | |
53 ;; format and do the right thing. | |
2321 | 54 |
55 ;;; Code: | |
56 | |
57 ; Randomize the seed in the random number generator. | |
58 (random t) | |
59 | |
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
|
60 (defconst cookie-delimiter "\n%%\n\\|\0" |
2321 | 61 "Delimiter used to separate cookie file entries.") |
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 |
2321 | 66 (defun cookie (phrase-file startmsg endmsg) |
67 "Return a random phrase from PHRASE-FILE. When the phrase file | |
68 is read in, display STARTMSG at beginning of load, ENDMSG at end." | |
69 (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg))) | |
70 (shuffle-vector cookie-vector) | |
71 (aref cookie-vector 1))) | |
72 | |
73 (defun cookie-insert (phrase-file &optional count startmsg endmsg) | |
74 "Insert random phrases from PHRASE-FILE; COUNT of them. When the phrase file | |
75 is read in, display STARTMSG at beginning of load, ENDMSG at end." | |
76 (let ((cookie-vector (cookie-snarf phrase-file startmsg endmsg))) | |
77 (shuffle-vector cookie-vector) | |
78 (let ((start (point))) | |
79 (insert ?\n) | |
80 (cookie1 (min (- (length cookie-vector) 1) (or count 1)) cookie-vector) | |
81 (insert ?\n) | |
82 (fill-region-as-paragraph start (point) nil)))) | |
83 | |
84 (defun cookie1 (arg cookie-vec) | |
85 "Inserts a cookie phrase ARG times." | |
86 (cond ((zerop arg) t) | |
87 (t (insert (aref cookie-vec arg)) | |
88 (insert " ") | |
89 (cookie1 (1- arg) cookie-vec)))) | |
90 | |
91 (defun cookie-snarf (phrase-file startmsg endmsg) | |
92 "Reads in the PHRASE-FILE, returns it as a vector of strings. Emit | |
93 STARTMSG and ENDMSG before and after. Caches the result; second and | |
94 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
|
95 (let ((sym (intern-soft phrase-file cookie-cache))) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
96 (and sym (not (equal (symbol-function sym) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
97 (nth 5 (file-attributes phrase-file)))) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
98 (yes-or-no-p (concat phrase-file |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
99 " has changed. Read new contents? ")) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
100 (setq sym nil)) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
101 (if sym |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
102 (symbol-value sym) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
103 (setq sym (intern phrase-file cookie-cache)) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
104 (message startmsg) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
105 (save-excursion |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
106 (let ((buf (generate-new-buffer "*cookie*")) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
107 (result nil)) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
108 (set-buffer buf) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
109 (fset sym (nth 5 (file-attributes phrase-file))) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
110 (insert-file-contents (expand-file-name phrase-file)) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
111 (re-search-forward cookie-delimiter) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
112 (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
|
113 (let ((beg (point))) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
114 (re-search-forward cookie-delimiter) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
115 (setq result (cons (buffer-substring beg (1- (point))) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
116 result)))) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
117 (kill-buffer buf) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
118 (message endmsg) |
afa2afad53c4
(cookie-cache): New defvar.
Roland McGrath <roland@gnu.org>
parents:
3432
diff
changeset
|
119 (set sym (apply 'vector result))))))) |
2321 | 120 |
121 (defun pick-random (n) | |
122 "Returns a random number from 0 to N-1 inclusive." | |
123 (% (logand 0777777 (random)) n)) | |
124 | |
125 ; Thanks to Ian G Batten <BattenIG@CS.BHAM.AC.UK> | |
126 ; [of the University of Birmingham Computer Science Department] | |
127 ; for the iterative version of this shuffle. | |
128 ; | |
129 (defun shuffle-vector (vector) | |
130 "Randomly permute the elements of VECTOR (all permutations equally likely)" | |
131 (let ((i 0) | |
132 j | |
133 temp | |
134 (len (length vector))) | |
135 (while (< i len) | |
136 (setq j (+ i (pick-random (- len i)))) | |
137 (setq temp (aref vector i)) | |
138 (aset vector i (aref vector j)) | |
139 (aset vector j temp) | |
140 (setq i (1+ i)))) | |
141 vector) | |
142 | |
3388 | 143 (provide 'cookie1) |
2321 | 144 |
3432 | 145 ;;; cookie1.el ends here |