Commit | Line | Data |
---|---|---|
6e44da43 | 1 | ;;; fortune.el --- use fortune to create signatures |
23b809c2 | 2 | ;; Copyright (C) 1999, 2001 Free Software Foundation, Inc. |
74d93533 RS |
3 | |
4 | ;; Author: Holger Schauer <Holger.Schauer@gmx.de> | |
5 | ;; Keywords: games utils mail | |
6 | ||
6e44da43 | 7 | ;; This file is part of GNU Emacs. |
74d93533 RS |
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 () | |
329d7667 | 49 | ;; (fortune-to-signature))) |
74d93533 RS |
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))) | |
329d7667 | 57 | ;; which allows marking a region and then pressing "i" so that the marked |
74d93533 RS |
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." | |
329d7667 | 66 | :version "21.1" |
74d93533 | 67 | :group 'games) |
a6cfdbe4 | 68 | |
74d93533 RS |
69 | (defgroup fortune-signature nil |
70 | "Settings for use of fortune for signatures." | |
329d7667 | 71 | :group 'fortune |
74d93533 RS |
72 | :group 'mail) |
73 | ||
74 | (defcustom fortune-dir "~/docs/ascii/misc/fortunes/" | |
75 | "*The directory to look in for local fortune cookies files." | |
a6cfdbe4 GM |
76 | :group 'fortune |
77 | :type 'directory) | |
78 | ||
79 | (defcustom fortune-file (expand-file-name "usenet" fortune-dir) | |
74d93533 | 80 | "*The file in which local fortune cookies will be stored." |
a6cfdbe4 GM |
81 | :group 'fortune |
82 | :type 'file) | |
83 | ||
74d93533 | 84 | (defcustom fortune-database-extension ".dat" |
329d7667 | 85 | "The extension of the corresponding fortune database. |
74d93533 | 86 | Normally you won't have a reason to change it." |
a6cfdbe4 GM |
87 | :group 'fortune |
88 | :type 'string) | |
89 | ||
74d93533 RS |
90 | (defcustom fortune-program "fortune" |
91 | "Program to select a fortune cookie." | |
a6cfdbe4 GM |
92 | :group 'fortune |
93 | :type 'file) | |
94 | ||
74d93533 RS |
95 | (defcustom fortune-program-options "" |
96 | "Options to pass to the fortune program." | |
a6cfdbe4 GM |
97 | :group 'fortune |
98 | :type 'string) | |
99 | ||
74d93533 RS |
100 | (defcustom fortune-strfile "strfile" |
101 | "Program to compute a new fortune database." | |
a6cfdbe4 GM |
102 | :group 'fortune |
103 | :type 'file) | |
104 | ||
74d93533 RS |
105 | (defcustom fortune-strfile-options "" |
106 | "Options to pass to the strfile program." | |
a6cfdbe4 GM |
107 | :group 'fortune |
108 | :type 'string) | |
109 | ||
74d93533 RS |
110 | (defcustom fortune-quiet-strfile-options "> /dev/null" |
111 | "Text added to the command for running `strfile'. | |
112 | By default it discards the output produced by `strfile'. | |
113 | Set this to \"\" if you would like to see the output." | |
a6cfdbe4 GM |
114 | :group 'fortune |
115 | :type 'string) | |
74d93533 RS |
116 | |
117 | (defcustom fortune-always-compile t | |
118 | "*Non-nil means automatically compile fortune files. | |
119 | If nil, you must invoke `fortune-compile' manually to do that." | |
a6cfdbe4 GM |
120 | :group 'fortune |
121 | :type 'boolean) | |
122 | ||
74d93533 RS |
123 | (defcustom fortune-author-line-prefix " -- " |
124 | "Prefix to put before the author name of a fortunate." | |
a6cfdbe4 GM |
125 | :group 'fortune-signature |
126 | :type 'string) | |
127 | ||
74d93533 RS |
128 | (defcustom fortune-fill-column fill-column |
129 | "Fill column for fortune files." | |
a6cfdbe4 GM |
130 | :group 'fortune-signature |
131 | :type 'integer) | |
132 | ||
74d93533 RS |
133 | (defcustom fortune-from-mail "private e-mail" |
134 | "String to use to characterize that the fortune comes from an e-mail. | |
135 | No need to add an `in'." | |
136 | :type 'string | |
137 | :group 'fortune-signature) | |
a6cfdbe4 | 138 | |
74d93533 RS |
139 | (defcustom fortune-sigstart "" |
140 | "*Some text to insert before the fortune cookie, in a mail signature." | |
a6cfdbe4 GM |
141 | :group 'fortune-signature |
142 | :type 'string) | |
143 | ||
74d93533 RS |
144 | (defcustom fortune-sigend "" |
145 | "*Some text to insert after the fortune cookie, in a mail signature." | |
a6cfdbe4 GM |
146 | :group 'fortune-signature |
147 | :type 'string) | |
74d93533 RS |
148 | |
149 | ||
150 | ;; not customizable settings | |
151 | (defvar fortune-buffer-name "*fortune*") | |
329d7667 | 152 | (defconst fortune-end-sep "\n%\n") |
74d93533 RS |
153 | |
154 | ||
155 | ;;; ************** | |
156 | ;;; Inserting a new fortune | |
157 | (defun fortune-append (string &optional interactive file) | |
329d7667 | 158 | "Appends STRING to the fortune FILE. |
74d93533 RS |
159 | |
160 | If INTERACTIVE is non-nil, don't compile the fortune file afterwards." | |
329d7667 | 161 | (setq file (expand-file-name |
74d93533 RS |
162 | (substitute-in-file-name (or file fortune-file)))) |
163 | (if (file-directory-p file) | |
329d7667 | 164 | (error "Cannot append fortune to directory %s" file)) |
74d93533 RS |
165 | (if interactive ; switch to file and return buffer |
166 | (find-file-other-frame file) | |
167 | (find-file-noselect file)) | |
168 | (let ((fortune-buffer (get-file-buffer file))) | |
169 | ||
170 | (set-buffer fortune-buffer) | |
171 | (goto-char (point-max)) | |
172 | (setq fill-column fortune-fill-column) | |
173 | (setq auto-fill-inhibit-regexp "^%") | |
174 | (turn-on-auto-fill) | |
175 | (insert string fortune-end-sep) | |
176 | (unless interactive | |
177 | (save-buffer) | |
178 | (if fortune-always-compile | |
179 | (fortune-compile file))))) | |
180 | ||
181 | (defun fortune-ask-file () | |
182 | "Asks the user for a file-name." | |
329d7667 | 183 | (expand-file-name |
74d93533 RS |
184 | (read-file-name |
185 | "Fortune file to use: " | |
186 | fortune-dir nil nil ""))) | |
187 | ||
23b809c2 | 188 | ;;;###autoload |
74d93533 RS |
189 | (defun fortune-add-fortune (string file) |
190 | "Add STRING to a fortune file FILE. | |
191 | ||
192 | Interactively, if called with a prefix argument, | |
193 | read the file name to use. Otherwise use the value of `fortune-file'." | |
194 | (interactive | |
195 | (list (read-string "Fortune: ") | |
196 | (if current-prefix-arg (fortune-ask-file)))) | |
197 | (fortune-append string t file)) | |
198 | ||
23b809c2 | 199 | ;;;###autoload |
74d93533 | 200 | (defun fortune-from-region (beg end file) |
329d7667 | 201 | "Append the current region to a local fortune-like data file. |
74d93533 RS |
202 | |
203 | Interactively, if called with a prefix argument, | |
204 | read the file name to use. Otherwise use the value of `fortune-file'." | |
329d7667 | 205 | (interactive |
74d93533 RS |
206 | (list (region-beginning) (region-end) |
207 | (if current-prefix-arg (fortune-ask-file)))) | |
208 | (let ((string (buffer-substring beg end)) | |
209 | author newsgroup help-point) | |
210 | ;; try to determine author ... | |
211 | (save-excursion | |
212 | (goto-char (point-min)) | |
329d7667 | 213 | (setq help-point |
74d93533 RS |
214 | (search-forward-regexp |
215 | "^From: \\(.*\\)$" | |
216 | (point-max) t)) | |
329d7667 DL |
217 | (if help-point |
218 | (setq author (buffer-substring (match-beginning 1) help-point)) | |
74d93533 RS |
219 | (setq author "An unknown author"))) |
220 | ;; ... and newsgroup | |
221 | (save-excursion | |
222 | (goto-char (point-min)) | |
223 | (setq help-point | |
224 | (search-forward-regexp | |
225 | "^Newsgroups: \\(.*\\)$" | |
226 | (point-max) t)) | |
329d7667 | 227 | (if help-point |
74d93533 | 228 | (setq newsgroup (buffer-substring (match-beginning 1) help-point)) |
1d14e80c GM |
229 | (setq newsgroup (if (or (eq major-mode 'gnus-article-mode) |
230 | (eq major-mode 'vm-mode) | |
231 | (eq major-mode 'rmail-mode)) | |
74d93533 RS |
232 | fortune-from-mail |
233 | "unknown")))) | |
234 | ||
235 | ;; append entry to end of fortune file, and display result | |
236 | (setq string (concat "\"" string "\"" | |
237 | "\n" | |
238 | fortune-author-line-prefix | |
239 | author " in " newsgroup)) | |
240 | (fortune-append string t file))) | |
241 | ||
242 | ||
243 | ;;; ************** | |
244 | ;;; Compile new database with strfile | |
23b809c2 | 245 | ;;;###autoload |
74d93533 RS |
246 | (defun fortune-compile (&optional file) |
247 | "Compile fortune file. | |
248 | ||
249 | If called with a prefix asks for the FILE to compile, otherwise uses | |
250 | the value of `fortune-file'. This currently cannot handle directories." | |
329d7667 | 251 | (interactive |
74d93533 RS |
252 | (list |
253 | (if current-prefix-arg | |
254 | (fortune-ask-file) | |
255 | fortune-file))) | |
256 | (let* ((fortune-file (expand-file-name (substitute-in-file-name file))) | |
329d7667 | 257 | (fortune-dat (expand-file-name |
74d93533 RS |
258 | (substitute-in-file-name |
259 | (concat fortune-file fortune-database-extension))))) | |
260 | (cond ((file-exists-p fortune-file) | |
261 | (if (file-exists-p fortune-dat) | |
262 | (cond ((file-newer-than-file-p fortune-file fortune-dat) | |
263 | (message "Compiling new fortune database %s" fortune-dat) | |
329d7667 | 264 | (shell-command |
74d93533 RS |
265 | (concat fortune-strfile fortune-strfile-options |
266 | " " fortune-file fortune-quiet-strfile-options)))))) | |
267 | (t (error "Can't compile fortune file %s" fortune-file))))) | |
268 | ||
269 | ||
270 | ;;; ************** | |
271 | ;;; Use fortune for signature | |
23b809c2 | 272 | ;;;###autoload |
74d93533 RS |
273 | (defun fortune-to-signature (&optional file) |
274 | "Create signature from output of the fortune program. | |
275 | ||
276 | If called with a prefix asks for the FILE to choose the fortune from, | |
277 | otherwise uses the value of `fortune-file'. If you want to have fortune | |
278 | choose from a set of files in a directory, call interactively with prefix | |
279 | and choose the directory as the fortune-file." | |
329d7667 | 280 | (interactive |
74d93533 RS |
281 | (list |
282 | (if current-prefix-arg | |
283 | (fortune-ask-file) | |
284 | fortune-file))) | |
285 | (save-excursion | |
286 | (fortune-in-buffer (interactive-p) file) | |
287 | (set-buffer fortune-buffer-name) | |
288 | (let* ((fortune (buffer-string)) | |
289 | (signature (concat fortune-sigstart fortune fortune-sigend))) | |
290 | (setq mail-signature signature) | |
291 | (if (boundp 'message-signature) | |
292 | (setq message-signature signature))))) | |
293 | ||
294 | ||
295 | ;;; ************** | |
296 | ;;; Display fortune | |
297 | (defun fortune-in-buffer (interactive &optional file) | |
298 | "Put a fortune cookie in the *fortune* buffer. | |
299 | ||
300 | When INTERACTIVE is nil, don't display it. Optional argument FILE, | |
301 | when supplied, specifies the file to choose the fortune from." | |
302 | (let ((fortune-buffer (or (get-buffer fortune-buffer-name) | |
303 | (generate-new-buffer fortune-buffer-name))) | |
304 | (fort-file (expand-file-name | |
305 | (substitute-in-file-name | |
306 | (or file fortune-file))))) | |
307 | (save-excursion | |
308 | (set-buffer fortune-buffer) | |
309 | (toggle-read-only 0) | |
310 | (erase-buffer) | |
311 | ||
312 | (if fortune-always-compile | |
313 | (fortune-compile fort-file)) | |
314 | ||
315 | (call-process | |
316 | fortune-program ;; programm to call | |
317 | nil fortune-buffer nil ;; INFILE BUFFER DISPLAYP | |
318 | (concat fortune-program-options fort-file))))) | |
319 | ||
320 | ||
23b809c2 | 321 | ;;;###autoload |
74d93533 RS |
322 | (defun fortune (&optional file) |
323 | "Display a fortune cookie. | |
324 | ||
325 | If called with a prefix asks for the FILE to choose the fortune from, | |
326 | otherwise uses the value of `fortune-file'. If you want to have fortune | |
327 | choose from a set of files in a directory, call interactively with prefix | |
328 | and choose the directory as the fortune-file." | |
329d7667 | 329 | (interactive |
74d93533 RS |
330 | (list |
331 | (if current-prefix-arg | |
332 | (fortune-ask-file) | |
333 | fortune-file))) | |
334 | (fortune-in-buffer t file) | |
335 | (switch-to-buffer (get-buffer fortune-buffer-name)) | |
336 | (toggle-read-only 1)) | |
337 | ||
338 | ||
339 | ;;; Provide ourselves. | |
340 | (provide 'fortune) | |
341 | ||
342 | ;;; fortune.el ends here |