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