Mercurial > emacs
annotate lisp/play/fortune.el @ 38425:c6e12c6b1498
Some fixes to follow coding conventions.
author | Pavel Janík <Pavel@Janik.cz> |
---|---|
date | Mon, 16 Jul 2001 09:10:19 +0000 |
parents | 6cc3d3a4025c |
children | 2d351e49dc96 |
rev | line source |
---|---|
38425
c6e12c6b1498
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
38397
diff
changeset
|
1 ;;; fortune.el --- use fortune to create signatures |
37426 | 2 ;; Copyright (C) 1999, 2001 Free Software Foundation, Inc. |
25480 | 3 |
4 ;; Author: Holger Schauer <Holger.Schauer@gmx.de> | |
5 ;; Keywords: games utils mail | |
6 | |
38425
c6e12c6b1498
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
38397
diff
changeset
|
7 ;; This file is part of GNU Emacs. |
25480 | 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) |
38397 | 68 |
25480 | 69 (defgroup fortune-signature nil |
70 "Settings for use of fortune for signatures." | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
71 :group 'fortune |
25480 | 72 :group 'mail) |
73 | |
74 (defcustom fortune-dir "~/docs/ascii/misc/fortunes/" | |
75 "*The directory to look in for local fortune cookies files." | |
38397 | 76 :group 'fortune |
77 :type 'directory) | |
78 | |
79 (defcustom fortune-file (expand-file-name "usenet" fortune-dir) | |
25480 | 80 "*The file in which local fortune cookies will be stored." |
38397 | 81 :group 'fortune |
82 :type 'file) | |
83 | |
25480 | 84 (defcustom fortune-database-extension ".dat" |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
85 "The extension of the corresponding fortune database. |
25480 | 86 Normally you won't have a reason to change it." |
38397 | 87 :group 'fortune |
88 :type 'string) | |
89 | |
25480 | 90 (defcustom fortune-program "fortune" |
91 "Program to select a fortune cookie." | |
38397 | 92 :group 'fortune |
93 :type 'file) | |
94 | |
25480 | 95 (defcustom fortune-program-options "" |
96 "Options to pass to the fortune program." | |
38397 | 97 :group 'fortune |
98 :type 'string) | |
99 | |
25480 | 100 (defcustom fortune-strfile "strfile" |
101 "Program to compute a new fortune database." | |
38397 | 102 :group 'fortune |
103 :type 'file) | |
104 | |
25480 | 105 (defcustom fortune-strfile-options "" |
106 "Options to pass to the strfile program." | |
38397 | 107 :group 'fortune |
108 :type 'string) | |
109 | |
25480 | 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." | |
38397 | 114 :group 'fortune |
115 :type 'string) | |
25480 | 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." | |
38397 | 120 :group 'fortune |
121 :type 'boolean) | |
122 | |
25480 | 123 (defcustom fortune-author-line-prefix " -- " |
124 "Prefix to put before the author name of a fortunate." | |
38397 | 125 :group 'fortune-signature |
126 :type 'string) | |
127 | |
25480 | 128 (defcustom fortune-fill-column fill-column |
129 "Fill column for fortune files." | |
38397 | 130 :group 'fortune-signature |
131 :type 'integer) | |
132 | |
25480 | 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) | |
38397 | 138 |
25480 | 139 (defcustom fortune-sigstart "" |
140 "*Some text to insert before the fortune cookie, in a mail signature." | |
38397 | 141 :group 'fortune-signature |
142 :type 'string) | |
143 | |
25480 | 144 (defcustom fortune-sigend "" |
145 "*Some text to insert after the fortune cookie, in a mail signature." | |
38397 | 146 :group 'fortune-signature |
147 :type 'string) | |
25480 | 148 |
149 | |
150 ;; not customizable settings | |
151 (defvar fortune-buffer-name "*fortune*") | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
152 (defconst fortune-end-sep "\n%\n") |
25480 | 153 |
154 | |
155 ;;; ************** | |
156 ;;; Inserting a new fortune | |
157 (defun fortune-append (string &optional interactive file) | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
158 "Appends STRING to the fortune FILE. |
25480 | 159 |
160 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
|
161 (setq file (expand-file-name |
25480 | 162 (substitute-in-file-name (or file fortune-file)))) |
163 (if (file-directory-p file) | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
164 (error "Cannot append fortune to directory %s" file)) |
25480 | 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." | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
183 (expand-file-name |
25480 | 184 (read-file-name |
185 "Fortune file to use: " | |
186 fortune-dir nil nil ""))) | |
187 | |
37426 | 188 ;;;###autoload |
25480 | 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 | |
37426 | 199 ;;;###autoload |
25480 | 200 (defun fortune-from-region (beg end file) |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
201 "Append the current region to a local fortune-like data file. |
25480 | 202 |
203 Interactively, if called with a prefix argument, | |
204 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
|
205 (interactive |
25480 | 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)) | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
213 (setq help-point |
25480 | 214 (search-forward-regexp |
215 "^From: \\(.*\\)$" | |
216 (point-max) t)) | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
217 (if help-point |
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
218 (setq author (buffer-substring (match-beginning 1) help-point)) |
25480 | 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)) | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
227 (if help-point |
25480 | 228 (setq newsgroup (buffer-substring (match-beginning 1) help-point)) |
37428
6586c75e5eda
(fortune-from-region): Use `eq' instead of `eql'.
Gerd Moellmann <gerd@gnu.org>
parents:
37426
diff
changeset
|
229 (setq newsgroup (if (or (eq major-mode 'gnus-article-mode) |
6586c75e5eda
(fortune-from-region): Use `eq' instead of `eql'.
Gerd Moellmann <gerd@gnu.org>
parents:
37426
diff
changeset
|
230 (eq major-mode 'vm-mode) |
6586c75e5eda
(fortune-from-region): Use `eq' instead of `eql'.
Gerd Moellmann <gerd@gnu.org>
parents:
37426
diff
changeset
|
231 (eq major-mode 'rmail-mode)) |
25480 | 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 | |
37426 | 245 ;;;###autoload |
25480 | 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." | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
251 (interactive |
25480 | 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))) | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
257 (fortune-dat (expand-file-name |
25480 | 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) | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
264 (shell-command |
25480 | 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 | |
37426 | 272 ;;;###autoload |
25480 | 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." | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
280 (interactive |
25480 | 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 | |
37426 | 321 ;;;###autoload |
25480 | 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." | |
30860
d0e5a99cbda1
(fortune) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
25480
diff
changeset
|
329 (interactive |
25480 | 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 |