1 ;;; fortune.el --- use fortune to create signatures
3 ;; Copyright (C) 1999, 2001 Free Software Foundation, Inc.
5 ;; Author: Holger Schauer <Holger.Schauer@gmx.de>
6 ;; Keywords: games utils mail
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
26 ;; This utility allows you to automatically cut regions to a fortune
27 ;; file. In case that the region stems from an article buffer (mail or
28 ;; news), it will try to automatically determine the author of the
29 ;; fortune. It will also allow you to compile your fortune-database
30 ;; as well as providing a function to extract a fortune for use as your
32 ;; Of course, it can simply display a fortune, too.
33 ;; Use prefix arguments to specify different fortune databases.
37 ;; Please check the customize settings -- you will at least have to
38 ;; modify the values of `fortune-dir' and `fortune-file'.
40 ;; I then use this in my .gnus:
41 ;;(message "Making new signature: %s" (fortune-to-signature "~/fortunes/"))
42 ;; This automagically creates a new signature when starting up Gnus.
43 ;; Note that the call to fortune-to-signature specifies a directory in which
44 ;; several fortune-files and their databases are stored.
46 ;; If you like to get a new signature for every message, you can also hook
47 ;; it into message-mode:
48 ;; (add-hook 'message-setup-hook 'fortune-to-signature)
49 ;; This time no fortune-file is specified, so fortune-to-signature would use
50 ;; the default-file as specified by fortune-file.
52 ;; I have also this in my .gnus:
53 ;;(add-hook 'gnus-article-mode-hook
55 ;; (define-key gnus-article-mode-map "i" 'fortune-from-region)))
56 ;; which allows marking a region and then pressing "i" so that the marked
57 ;; region will be automatically added to my favourite fortune-file.
62 ;;; Customizable Settings
64 "Settings for fortune."
65 :link '(emacs-commentary-link "fortune.el")
68 (defgroup fortune-signature nil
69 "Settings for use of fortune for signatures."
73 (defcustom fortune-dir "~/docs/ascii/misc/fortunes/"
74 "*The directory to look in for local fortune cookies files."
77 (defcustom fortune-file
78 (expand-file-name "usenet" fortune-dir)
79 "*The file in which local fortune cookies will be stored."
82 (defcustom fortune-database-extension ".dat"
83 "The extension of the corresponding fortune database.
84 Normally you won't have a reason to change it."
87 (defcustom fortune-program "fortune"
88 "Program to select a fortune cookie."
91 (defcustom fortune-program-options ""
92 "Options to pass to the fortune program (a string)."
95 (defcustom fortune-strfile "strfile"
96 "Program to compute a new fortune database."
99 (defcustom fortune-strfile-options ""
100 "Options to pass to the strfile program (a string)."
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."
110 (defcustom fortune-always-compile t
111 "*Non-nil means automatically compile fortune files.
112 If nil, you must invoke `fortune-compile' manually to do that."
115 (defcustom fortune-author-line-prefix " -- "
116 "Prefix to put before the author name of a fortunate."
118 :group 'fortune-signature)
119 (defcustom fortune-fill-column fill-column
120 "Fill column for fortune files."
122 :group 'fortune-signature)
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'."
127 :group 'fortune-signature)
128 (defcustom fortune-sigstart ""
129 "*Some text to insert before the fortune cookie, in a mail signature."
131 :group 'fortune-signature)
132 (defcustom fortune-sigend ""
133 "*Some text to insert after the fortune cookie, in a mail signature."
135 :group 'fortune-signature)
138 ;; not customizable settings
139 (defvar fortune-buffer-name "*fortune*")
140 (defconst fortune-end-sep "\n%\n")
144 ;;; Inserting a new fortune
145 (defun fortune-append (string &optional interactive file)
146 "Appends STRING to the fortune FILE.
148 If INTERACTIVE is non-nil, don't compile the fortune file afterwards."
149 (setq file (expand-file-name
150 (substitute-in-file-name (or file fortune-file))))
151 (if (file-directory-p file)
152 (error "Cannot append fortune to directory %s" file))
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)))
158 (set-buffer fortune-buffer)
159 (goto-char (point-max))
160 (setq fill-column fortune-fill-column)
161 (setq auto-fill-inhibit-regexp "^%")
163 (insert string fortune-end-sep)
166 (if fortune-always-compile
167 (fortune-compile file)))))
169 (defun fortune-ask-file ()
170 "Asks the user for a file-name."
173 "Fortune file to use: "
174 fortune-dir nil nil "")))
177 (defun fortune-add-fortune (string file)
178 "Add STRING to a fortune file FILE.
180 Interactively, if called with a prefix argument,
181 read the file name to use. Otherwise use the value of `fortune-file'."
183 (list (read-string "Fortune: ")
184 (if current-prefix-arg (fortune-ask-file))))
185 (fortune-append string t file))
188 (defun fortune-from-region (beg end file)
189 "Append the current region to a local fortune-like data file.
191 Interactively, if called with a prefix argument,
192 read the file name to use. Otherwise use the value of `fortune-file'."
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 ...
200 (goto-char (point-min))
202 (search-forward-regexp
206 (setq author (buffer-substring (match-beginning 1) help-point))
207 (setq author "An unknown author")))
210 (goto-char (point-min))
212 (search-forward-regexp
213 "^Newsgroups: \\(.*\\)$"
216 (setq newsgroup (buffer-substring (match-beginning 1) help-point))
217 (setq newsgroup (if (or (eq major-mode 'gnus-article-mode)
218 (eq major-mode 'vm-mode)
219 (eq major-mode 'rmail-mode))
223 ;; append entry to end of fortune file, and display result
224 (setq string (concat "\"" string "\""
226 fortune-author-line-prefix
227 author " in " newsgroup))
228 (fortune-append string t file)))
232 ;;; Compile new database with strfile
234 (defun fortune-compile (&optional file)
235 "Compile fortune file.
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."
241 (if current-prefix-arg
244 (let* ((fortune-file (expand-file-name (substitute-in-file-name file)))
245 (fortune-dat (expand-file-name
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)
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)))))
259 ;;; Use fortune for signature
261 (defun fortune-to-signature (&optional file)
262 "Create signature from output of the fortune program.
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."
270 (if current-prefix-arg
274 (fortune-in-buffer (interactive-p) file)
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)))))
285 (defun fortune-in-buffer (interactive &optional file)
286 "Put a fortune cookie in the *fortune* buffer.
288 When INTERACTIVE is nil, don't display it. Optional argument FILE,
289 when supplied, specifies the file to choose the fortune from."
290 (let ((fortune-buffer (or (get-buffer fortune-buffer-name)
291 (generate-new-buffer fortune-buffer-name)))
292 (fort-file (expand-file-name
293 (substitute-in-file-name
294 (or file fortune-file)))))
296 (set-buffer fortune-buffer)
300 (if fortune-always-compile
301 (fortune-compile fort-file))
304 fortune-program ;; programm to call
305 nil fortune-buffer nil ;; INFILE BUFFER DISPLAYP
306 (concat fortune-program-options fort-file)))))
310 (defun fortune (&optional file)
311 "Display a fortune cookie.
313 If called with a prefix asks for the FILE to choose the fortune from,
314 otherwise uses the value of `fortune-file'. If you want to have fortune
315 choose from a set of files in a directory, call interactively with prefix
316 and choose the directory as the fortune-file."
319 (if current-prefix-arg
322 (fortune-in-buffer t file)
323 (switch-to-buffer (get-buffer fortune-buffer-name))
324 (toggle-read-only 1))
327 ;;; Provide ourselves.
330 ;;; fortune.el ends here