(edebug-form-spec prop): use dolist.
[emacs.git] / lisp / play / fortune.el
blobc3e94eaaa37478df742a85189bd90e4cde5c1059
1 ;;; fortune.el --- Use fortune to create signatures
2 ;; Copyright (C) 1999 Free Software Foundation, Inc.
4 ;; Author: Holger Schauer <Holger.Schauer@gmx.de>
5 ;; Keywords: games utils mail
7 ;; This file is part of Emacs.
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.
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.
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.
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.
34 ;;; Installation:
36 ;; Please check the customize settings - you will at least have to modify the
37 ;; values of `fortune-dir' and `fortune-file'.
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.
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 ()
49 ;; (fortune-to-signature)))
50 ;; This time no fortune-file is specified, so fortune-to-signature would use
51 ;; the default-file as specified by fortune-file.
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)))
57 ;; which allows marking a region and then pressing "i" so that the marked
58 ;; region will be automatically added to my favourite fortune-file.
60 ;;; Code:
62 ;;; **************
63 ;;; Customizable Settings
64 (defgroup fortune nil
65 "Settings for fortune."
66 :version "21.1"
67 :group 'games)
68 (defgroup fortune-signature nil
69 "Settings for use of fortune for signatures."
70 :group 'fortune
71 :group 'mail)
73 (defcustom fortune-dir "~/docs/ascii/misc/fortunes/"
74 "*The directory to look in for local fortune cookies files."
75 :group 'fortune)
76 (defcustom fortune-file
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"
81 "The extension of the corresponding fortune database.
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)
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)
125 ;; not customizable settings
126 (defvar fortune-buffer-name "*fortune*")
127 (defconst fortune-end-sep "\n%\n")
130 ;;; **************
131 ;;; Inserting a new fortune
132 (defun fortune-append (string &optional interactive file)
133 "Appends STRING to the fortune FILE.
135 If INTERACTIVE is non-nil, don't compile the fortune file afterwards."
136 (setq file (expand-file-name
137 (substitute-in-file-name (or file fortune-file))))
138 (if (file-directory-p file)
139 (error "Cannot append fortune to directory %s" file))
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)))
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)))))
156 (defun fortune-ask-file ()
157 "Asks the user for a file-name."
158 (expand-file-name
159 (read-file-name
160 "Fortune file to use: "
161 fortune-dir nil nil "")))
163 ;;; ###autoload
164 (defun fortune-add-fortune (string file)
165 "Add STRING to a fortune file FILE.
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))
174 ;;; ###autoload
175 (defun fortune-from-region (beg end file)
176 "Append the current region to a local fortune-like data file.
178 Interactively, if called with a prefix argument,
179 read the file name to use. Otherwise use the value of `fortune-file'."
180 (interactive
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))
188 (setq help-point
189 (search-forward-regexp
190 "^From: \\(.*\\)$"
191 (point-max) t))
192 (if help-point
193 (setq author (buffer-substring (match-beginning 1) help-point))
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))
202 (if help-point
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"))))
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)))
218 ;;; **************
219 ;;; Compile new database with strfile
220 ;;; ###autoload
221 (defun fortune-compile (&optional file)
222 "Compile fortune file.
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."
226 (interactive
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)))
232 (fortune-dat (expand-file-name
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)
239 (shell-command
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)))))
245 ;;; **************
246 ;;; Use fortune for signature
247 ;;; ###autoload
248 (defun fortune-to-signature (&optional file)
249 "Create signature from output of the fortune program.
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."
255 (interactive
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)))))
270 ;;; **************
271 ;;; Display fortune
272 (defun fortune-in-buffer (interactive &optional file)
273 "Put a fortune cookie in the *fortune* buffer.
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)
287 (if fortune-always-compile
288 (fortune-compile fort-file))
290 (call-process
291 fortune-program ;; programm to call
292 nil fortune-buffer nil ;; INFILE BUFFER DISPLAYP
293 (concat fortune-program-options fort-file)))))
296 ;;; ###autoload
297 (defun fortune (&optional file)
298 "Display a fortune cookie.
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."
304 (interactive
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))
314 ;;; Provide ourselves.
315 (provide 'fortune)
317 ;;; fortune.el ends here