Mercurial > emacs
annotate lisp/play/fortune.el @ 36381:4993e5072bcb
2001-02-24 Michael Kifer <kifer@cs.sunysb.edu>
* ediff.texi: Fixed some typos
author | Michael Kifer <kifer@cs.stonybrook.edu> |
---|---|
date | Sun, 25 Feb 2001 02:02:08 +0000 |
parents | d0e5a99cbda1 |
children | 71d360920aad |
rev | line source |
---|---|
25480 | 1 ;;; fortune.el --- Use fortune to create signatures |
2 ;; Copyright (C) 1999 Free Software Foundation, Inc. | |
3 | |
4 ;; Author: Holger Schauer <Holger.Schauer@gmx.de> | |
5 ;; Keywords: games utils mail | |
6 | |
7 ;; This file is part of Emacs. | |
8 | |
9 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
10 ;; it under the terms of the GNU General Public License as published by | |
11 ;; the Free Software Foundation; either version 2, or (at your option) | |
12 ;; any later version. | |
13 | |
14 ;; GNU Emacs is distributed in the hope that it will be useful, | |
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 ;; GNU General Public License for more details. | |
18 | |
19 ;; You should have received a copy of the GNU General Public License | |
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
22 ;; Boston, MA 02111-1307, USA. | |
23 | |
24 ;;; Commentary: | |
25 ;; This utility allows you to automatically cut regions to a fortune | |
26 ;; file. In case that the region stems from an article buffer (mail or | |
27 ;; news), it will try to automatically determine the author of the | |
28 ;; fortune. It will also allow you to compile your fortune-database | |
29 ;; as well as providing a function to extract a fortune for use as your | |
30 ;; signature. | |
31 ;; Of course, it can simply display a fortune, too. | |
32 ;; Use prefix arguments to specify different fortune databases. | |
33 | |
34 ;;; Installation: | |
35 | |
36 ;; Please check the customize settings - you will at least have to modify the | |
37 ;; values of `fortune-dir' and `fortune-file'. | |
38 | |
39 ;; I then use this in my .gnus: | |
40 ;;(message "Making new signature: %s" (fortune-to-signature "~/fortunes/")) | |
41 ;; This automagically creates a new signature when starting up Gnus. | |
42 ;; Note that the call to fortune-to-signature specifies a directory in which | |
43 ;; several fortune-files and their databases are stored. | |
44 | |
45 ;; If you like to get a new signature for every message, you can also hook | |
46 ;; it into message-mode: | |
47 ;; (add-hook 'message-setup-hook | |
48 ;; '(lambda () | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
49 ;; (fortune-to-signature))) |
25480 | 50 ;; This time no fortune-file is specified, so fortune-to-signature would use |
51 ;; the default-file as specified by fortune-file. | |
52 | |
53 ;; I have also this in my .gnus: | |
54 ;;(add-hook 'gnus-article-mode-hook | |
55 ;; '(lambda () | |
56 ;; (define-key gnus-article-mode-map "i" 'fortune-from-region))) | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
57 ;; which allows marking a region and then pressing "i" so that the marked |
25480 | 58 ;; region will be automatically added to my favourite fortune-file. |
59 | |
60 ;;; Code: | |
61 | |
62 ;;; ************** | |
63 ;;; Customizable Settings | |
64 (defgroup fortune nil | |
65 "Settings for fortune." | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
66 :version "21.1" |
25480 | 67 :group 'games) |
68 (defgroup fortune-signature nil | |
69 "Settings for use of fortune for signatures." | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
70 :group 'fortune |
25480 | 71 :group 'mail) |
72 | |
73 (defcustom fortune-dir "~/docs/ascii/misc/fortunes/" | |
74 "*The directory to look in for local fortune cookies files." | |
75 :group 'fortune) | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
76 (defcustom fortune-file |
25480 | 77 (expand-file-name "usenet" fortune-dir) |
78 "*The file in which local fortune cookies will be stored." | |
79 :group 'fortune) | |
80 (defcustom fortune-database-extension ".dat" | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
81 "The extension of the corresponding fortune database. |
25480 | 82 Normally you won't have a reason to change it." |
83 :group 'fortune) | |
84 (defcustom fortune-program "fortune" | |
85 "Program to select a fortune cookie." | |
86 :group 'fortune) | |
87 (defcustom fortune-program-options "" | |
88 "Options to pass to the fortune program." | |
89 :group 'fortune) | |
90 (defcustom fortune-strfile "strfile" | |
91 "Program to compute a new fortune database." | |
92 :group 'fortune) | |
93 (defcustom fortune-strfile-options "" | |
94 "Options to pass to the strfile program." | |
95 :group 'fortune) | |
96 (defcustom fortune-quiet-strfile-options "> /dev/null" | |
97 "Text added to the command for running `strfile'. | |
98 By default it discards the output produced by `strfile'. | |
99 Set this to \"\" if you would like to see the output." | |
100 :group 'fortune) | |
101 | |
102 (defcustom fortune-always-compile t | |
103 "*Non-nil means automatically compile fortune files. | |
104 If nil, you must invoke `fortune-compile' manually to do that." | |
105 :group 'fortune) | |
106 (defcustom fortune-author-line-prefix " -- " | |
107 "Prefix to put before the author name of a fortunate." | |
108 :group 'fortune-signature) | |
109 (defcustom fortune-fill-column fill-column | |
110 "Fill column for fortune files." | |
111 :group 'fortune-signature) | |
112 (defcustom fortune-from-mail "private e-mail" | |
113 "String to use to characterize that the fortune comes from an e-mail. | |
114 No need to add an `in'." | |
115 :type 'string | |
116 :group 'fortune-signature) | |
117 (defcustom fortune-sigstart "" | |
118 "*Some text to insert before the fortune cookie, in a mail signature." | |
119 :group 'fortune-signature) | |
120 (defcustom fortune-sigend "" | |
121 "*Some text to insert after the fortune cookie, in a mail signature." | |
122 :group 'fortune-signature) | |
123 | |
124 | |
125 ;; not customizable settings | |
126 (defvar fortune-buffer-name "*fortune*") | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
127 (defconst fortune-end-sep "\n%\n") |
25480 | 128 |
129 | |
130 ;;; ************** | |
131 ;;; Inserting a new fortune | |
132 (defun fortune-append (string &optional interactive file) | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
133 "Appends STRING to the fortune FILE. |
25480 | 134 |
135 If INTERACTIVE is non-nil, don't compile the fortune file afterwards." | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
136 (setq file (expand-file-name |
25480 | 137 (substitute-in-file-name (or file fortune-file)))) |
138 (if (file-directory-p file) | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
139 (error "Cannot append fortune to directory %s" file)) |
25480 | 140 (if interactive ; switch to file and return buffer |
141 (find-file-other-frame file) | |
142 (find-file-noselect file)) | |
143 (let ((fortune-buffer (get-file-buffer file))) | |
144 | |
145 (set-buffer fortune-buffer) | |
146 (goto-char (point-max)) | |
147 (setq fill-column fortune-fill-column) | |
148 (setq auto-fill-inhibit-regexp "^%") | |
149 (turn-on-auto-fill) | |
150 (insert string fortune-end-sep) | |
151 (unless interactive | |
152 (save-buffer) | |
153 (if fortune-always-compile | |
154 (fortune-compile file))))) | |
155 | |
156 (defun fortune-ask-file () | |
157 "Asks the user for a file-name." | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
158 (expand-file-name |
25480 | 159 (read-file-name |
160 "Fortune file to use: " | |
161 fortune-dir nil nil ""))) | |
162 | |
163 ;;; ###autoload | |
164 (defun fortune-add-fortune (string file) | |
165 "Add STRING to a fortune file FILE. | |
166 | |
167 Interactively, if called with a prefix argument, | |
168 read the file name to use. Otherwise use the value of `fortune-file'." | |
169 (interactive | |
170 (list (read-string "Fortune: ") | |
171 (if current-prefix-arg (fortune-ask-file)))) | |
172 (fortune-append string t file)) | |
173 | |
174 ;;; ###autoload | |
175 (defun fortune-from-region (beg end file) | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
176 "Append the current region to a local fortune-like data file. |
25480 | 177 |
178 Interactively, if called with a prefix argument, | |
179 read the file name to use. Otherwise use the value of `fortune-file'." | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
180 (interactive |
25480 | 181 (list (region-beginning) (region-end) |
182 (if current-prefix-arg (fortune-ask-file)))) | |
183 (let ((string (buffer-substring beg end)) | |
184 author newsgroup help-point) | |
185 ;; try to determine author ... | |
186 (save-excursion | |
187 (goto-char (point-min)) | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
188 (setq help-point |
25480 | 189 (search-forward-regexp |
190 "^From: \\(.*\\)$" | |
191 (point-max) t)) | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
192 (if help-point |
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
193 (setq author (buffer-substring (match-beginning 1) help-point)) |
25480 | 194 (setq author "An unknown author"))) |
195 ;; ... and newsgroup | |
196 (save-excursion | |
197 (goto-char (point-min)) | |
198 (setq help-point | |
199 (search-forward-regexp | |
200 "^Newsgroups: \\(.*\\)$" | |
201 (point-max) t)) | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
202 (if help-point |
25480 | 203 (setq newsgroup (buffer-substring (match-beginning 1) help-point)) |
204 (setq newsgroup (if (or (eql major-mode 'gnus-article-mode) | |
205 (eql major-mode 'vm-mode) | |
206 (eql major-mode 'rmail-mode)) | |
207 fortune-from-mail | |
208 "unknown")))) | |
209 | |
210 ;; append entry to end of fortune file, and display result | |
211 (setq string (concat "\"" string "\"" | |
212 "\n" | |
213 fortune-author-line-prefix | |
214 author " in " newsgroup)) | |
215 (fortune-append string t file))) | |
216 | |
217 | |
218 ;;; ************** | |
219 ;;; Compile new database with strfile | |
220 ;;; ###autoload | |
221 (defun fortune-compile (&optional file) | |
222 "Compile fortune file. | |
223 | |
224 If called with a prefix asks for the FILE to compile, otherwise uses | |
225 the value of `fortune-file'. This currently cannot handle directories." | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
226 (interactive |
25480 | 227 (list |
228 (if current-prefix-arg | |
229 (fortune-ask-file) | |
230 fortune-file))) | |
231 (let* ((fortune-file (expand-file-name (substitute-in-file-name file))) | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
232 (fortune-dat (expand-file-name |
25480 | 233 (substitute-in-file-name |
234 (concat fortune-file fortune-database-extension))))) | |
235 (cond ((file-exists-p fortune-file) | |
236 (if (file-exists-p fortune-dat) | |
237 (cond ((file-newer-than-file-p fortune-file fortune-dat) | |
238 (message "Compiling new fortune database %s" fortune-dat) | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
239 (shell-command |
25480 | 240 (concat fortune-strfile fortune-strfile-options |
241 " " fortune-file fortune-quiet-strfile-options)))))) | |
242 (t (error "Can't compile fortune file %s" fortune-file))))) | |
243 | |
244 | |
245 ;;; ************** | |
246 ;;; Use fortune for signature | |
247 ;;; ###autoload | |
248 (defun fortune-to-signature (&optional file) | |
249 "Create signature from output of the fortune program. | |
250 | |
251 If called with a prefix asks for the FILE to choose the fortune from, | |
252 otherwise uses the value of `fortune-file'. If you want to have fortune | |
253 choose from a set of files in a directory, call interactively with prefix | |
254 and choose the directory as the fortune-file." | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
255 (interactive |
25480 | 256 (list |
257 (if current-prefix-arg | |
258 (fortune-ask-file) | |
259 fortune-file))) | |
260 (save-excursion | |
261 (fortune-in-buffer (interactive-p) file) | |
262 (set-buffer fortune-buffer-name) | |
263 (let* ((fortune (buffer-string)) | |
264 (signature (concat fortune-sigstart fortune fortune-sigend))) | |
265 (setq mail-signature signature) | |
266 (if (boundp 'message-signature) | |
267 (setq message-signature signature))))) | |
268 | |
269 | |
270 ;;; ************** | |
271 ;;; Display fortune | |
272 (defun fortune-in-buffer (interactive &optional file) | |
273 "Put a fortune cookie in the *fortune* buffer. | |
274 | |
275 When INTERACTIVE is nil, don't display it. Optional argument FILE, | |
276 when supplied, specifies the file to choose the fortune from." | |
277 (let ((fortune-buffer (or (get-buffer fortune-buffer-name) | |
278 (generate-new-buffer fortune-buffer-name))) | |
279 (fort-file (expand-file-name | |
280 (substitute-in-file-name | |
281 (or file fortune-file))))) | |
282 (save-excursion | |
283 (set-buffer fortune-buffer) | |
284 (toggle-read-only 0) | |
285 (erase-buffer) | |
286 | |
287 (if fortune-always-compile | |
288 (fortune-compile fort-file)) | |
289 | |
290 (call-process | |
291 fortune-program ;; programm to call | |
292 nil fortune-buffer nil ;; INFILE BUFFER DISPLAYP | |
293 (concat fortune-program-options fort-file))))) | |
294 | |
295 | |
296 ;;; ###autoload | |
297 (defun fortune (&optional file) | |
298 "Display a fortune cookie. | |
299 | |
300 If called with a prefix asks for the FILE to choose the fortune from, | |
301 otherwise uses the value of `fortune-file'. If you want to have fortune | |
302 choose from a set of files in a directory, call interactively with prefix | |
303 and choose the directory as the fortune-file." | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
304 (interactive |
25480 | 305 (list |
306 (if current-prefix-arg | |
307 (fortune-ask-file) | |
308 fortune-file))) | |
309 (fortune-in-buffer t file) | |
310 (switch-to-buffer (get-buffer fortune-buffer-name)) | |
311 (toggle-read-only 1)) | |
312 | |
313 | |
314 ;;; Provide ourselves. | |
315 (provide 'fortune) | |
316 | |
317 ;;; fortune.el ends here | |
318 |