summaryrefslogtreecommitdiff
path: root/lisp/play/fortune.el
blob: 3c057f4149773a83309961b149c1c453471bdfbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
;;; fortune.el --- use fortune to create signatures

;; Copyright (C) 1999, 2001-2019 Free Software Foundation, Inc.

;; Author: Holger Schauer <Holger.Schauer@gmx.de>
;; Keywords: games utils mail

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:
;; This utility allows you to automatically cut regions to a fortune
;; file.  In case that the region stems from an article buffer (mail or
;; news), it will try to automatically determine the author of the
;; fortune.  It will also allow you to compile your fortune-database
;; as well as providing a function to extract a fortune for use as your
;; signature.
;; Of course, it can simply display a fortune, too.
;; Use prefix arguments to specify different fortune databases.

;;; Installation:

;; Please check the customize settings -- you will at least have to
;; modify the values of `fortune-dir' and `fortune-file'.

;; I then use this in my .gnus:
;;(message "Making new signature: %s" (fortune-to-signature "~/fortunes/"))
;; This automagically creates a new signature when starting up Gnus.
;; Note that the call to fortune-to-signature specifies a directory in which
;; several fortune-files and their databases are stored.

;; If you like to get a new signature for every message, you can also hook
;; it into message-mode:
;; (add-hook 'message-setup-hook 'fortune-to-signature)
;; This time no fortune-file is specified, so fortune-to-signature would use
;; the default-file as specified by fortune-file.

;; I have also this in my .gnus:
;;(add-hook 'gnus-article-mode-hook
;;	  (lambda ()
;;	     (define-key gnus-article-mode-map "i" 'fortune-from-region)))
;; which allows marking a region and then pressing "i" so that the marked
;; region will be automatically added to my favorite fortune-file.

;;; Code:

;;; **************
;;; Customizable Settings
(defgroup fortune nil
  "Settings for fortune."
  :link '(emacs-commentary-link "fortune.el")
  :version "21.1"
  :group 'games)
(defgroup fortune-signature nil
  "Settings for use of fortune for signatures."
  :group 'fortune
  :group 'mail)

(defcustom fortune-dir "~/docs/ascii/misc/fortunes/"
  "The directory to look in for local fortune cookies files."
  :type 'directory
  :group 'fortune)
(defcustom fortune-file
  (expand-file-name "usenet" fortune-dir)
  "The file in which local fortune cookies will be stored."
  :type 'file
  :group 'fortune)
(defcustom fortune-database-extension  ".dat"
  "The extension of the corresponding fortune database.
Normally you won't have a reason to change it."
  :type 'string
  :group 'fortune)
(defcustom fortune-program "fortune"
  "Program to select a fortune cookie."
  :type 'string
  :group 'fortune)
(defcustom fortune-program-options ()
  "List of options to pass to the fortune program."
  :type '(choice (repeat (string :tag "Option"))
                 (string :tag "Obsolete string of options"))
  :version "23.1"
  :group 'fortune)
(defcustom fortune-strfile "strfile"
  "Program to compute a new fortune database."
  :type 'string
  :group 'fortune)
(defcustom fortune-strfile-options ""
  "Options to pass to the strfile program (a string)."
  :type 'string
  :group 'fortune)
(defcustom fortune-quiet-strfile-options "> /dev/null"
  "Text added to the command for running `strfile'.
By default it discards the output produced by `strfile'.
Set this to \"\" if you would like to see the output."
  :type 'string
  :group 'fortune)

(defcustom fortune-always-compile t
  "Non-nil means automatically compile fortune files.
If nil, you must invoke `fortune-compile' manually to do that."
  :type 'boolean
  :group 'fortune)
(defcustom fortune-author-line-prefix "                  -- "
  "Prefix to put before the author name of a fortunate."
  :type 'string
  :group 'fortune-signature)
(defcustom fortune-fill-column fill-column
  "Fill column for fortune files."
  :type 'integer
  :group 'fortune-signature)
(defcustom fortune-from-mail "private e-mail"
  "String to use to characterize that the fortune comes from an e-mail.
No need to add an `in'."
  :type 'string
  :group 'fortune-signature)
(defcustom fortune-sigstart ""
  "Some text to insert before the fortune cookie, in a mail signature."
  :type 'string
  :group 'fortune-signature)
(defcustom fortune-sigend ""
  "Some text to insert after the fortune cookie, in a mail signature."
  :type 'string
  :group 'fortune-signature)


;; not customizable settings
(defvar fortune-buffer-name "*fortune*")
(defconst fortune-end-sep "\n%\n")


;;; **************
;;; Inserting a new fortune
(defun fortune-append (string &optional interactive file)
  "Appends STRING to the fortune FILE.

If INTERACTIVE is non-nil, don't compile the fortune file afterwards."
  (setq file (expand-file-name
	      (substitute-in-file-name (or file fortune-file))))
  (if (file-directory-p file)
      (error "Cannot append fortune to directory %s" file))
  (if interactive ; switch to file and return buffer
      (find-file-other-frame file)
    (find-file-noselect file))
  (let ((fortune-buffer (get-file-buffer file)))

    (set-buffer fortune-buffer)
    (goto-char (point-max))
    (setq fill-column fortune-fill-column)
    (setq auto-fill-inhibit-regexp "^%")
    (turn-on-auto-fill)
    (insert string fortune-end-sep)
    (unless interactive
      (save-buffer)
      (if fortune-always-compile
	  (fortune-compile file)))))

(defun fortune-ask-file ()
  "Asks the user for a file-name."
  (expand-file-name
   (read-file-name
    "Fortune file to use: "
    fortune-dir nil nil "")))

;;;###autoload
(defun fortune-add-fortune (string file)
  "Add STRING to a fortune file FILE.

Interactively, if called with a prefix argument,
read the file name to use.  Otherwise use the value of `fortune-file'."
  (interactive
   (list (read-string "Fortune: ")
	 (if current-prefix-arg (fortune-ask-file))))
  (fortune-append string t file))

;;;###autoload
(defun fortune-from-region (beg end file)
  "Append the current region to a local fortune-like data file.

Interactively, if called with a prefix argument,
read the file name to use.  Otherwise use the value of `fortune-file'."
  (interactive
   (list (region-beginning) (region-end)
	 (if current-prefix-arg (fortune-ask-file))))
  (let ((string (buffer-substring beg end))
	author newsgroup help-point)
    ;; try to determine author ...
    (save-excursion
      (goto-char (point-min))
      (setq help-point
	    (search-forward-regexp
	     "^From: \\(.*\\)$"
	     (point-max) t))
      (if help-point
	  (setq author (buffer-substring (match-beginning 1) help-point))
	(setq author "An unknown author")))
    ;; ... and newsgroup
    (save-excursion
      (goto-char (point-min))
      (setq help-point
	    (search-forward-regexp
	     "^Newsgroups: \\(.*\\)$"
	     (point-max) t))
      (if help-point
	  (setq newsgroup (buffer-substring (match-beginning 1) help-point))
	(setq newsgroup (if (or (eq major-mode 'gnus-article-mode)
				(eq major-mode 'vm-mode)
				(eq major-mode 'rmail-mode))
			    fortune-from-mail
			  "unknown"))))

    ;; append entry to end of fortune file, and display result
    (setq string (concat "\"" string "\""
			 "\n"
			 fortune-author-line-prefix
			 author " in " newsgroup))
    (fortune-append string t file)))


;;; **************
;;; Compile new database with strfile
;;;###autoload
(defun fortune-compile (&optional file)
  "Compile fortune file.

If called with a prefix asks for the FILE to compile, otherwise uses
the value of `fortune-file'.  This currently cannot handle directories."
  (interactive
    (list
     (if current-prefix-arg
	 (fortune-ask-file)
       fortune-file)))
  (let* ((fortune-file (expand-file-name (substitute-in-file-name file)))
	 (fortune-dat (expand-file-name
		       (substitute-in-file-name
			(concat fortune-file fortune-database-extension)))))
  (cond ((file-exists-p fortune-file)
         (cond ((file-newer-than-file-p fortune-file fortune-dat)
                (message "Compiling new fortune database %s" fortune-dat)
                (shell-command
                 (concat fortune-strfile fortune-strfile-options
                         " " fortune-file fortune-quiet-strfile-options)))))
	(t (error "Can't compile fortune file %s" fortune-file)))))


;;; **************
;;; Use fortune for signature
;;;###autoload
(defun fortune-to-signature (&optional file)
  "Create signature from output of the fortune program.

If called with a prefix asks for the FILE to choose the fortune from,
otherwise uses the value of `fortune-file'.  If you want to have fortune
choose from a set of files in a directory, call interactively with prefix
and choose the directory as the fortune-file."
  (interactive
    (list
     (if current-prefix-arg
	 (fortune-ask-file)
       fortune-file)))
   (save-excursion
    (fortune-in-buffer t file)
    (set-buffer fortune-buffer-name)
    (let* ((fortune (buffer-string))
	   (signature (concat fortune-sigstart fortune fortune-sigend)))
      (setq mail-signature signature)
      (if (boundp 'message-signature)
	  (setq message-signature signature)))))


;;; **************
;;; Display fortune
(defun fortune-in-buffer (_interactive &optional file)
  "Put a fortune cookie in the *fortune* buffer.
INTERACTIVE is ignored.  Optional argument FILE, when supplied,
specifies the file to choose the fortune from."
  (let ((fortune-buffer (or (get-buffer fortune-buffer-name)
			    (generate-new-buffer fortune-buffer-name)))
	(fort-file (expand-file-name
		    (substitute-in-file-name
		     (or file fortune-file)))))
    (with-current-buffer fortune-buffer
      (let ((inhibit-read-only t))
        (erase-buffer)
        (if fortune-always-compile
            (fortune-compile fort-file))
        (apply 'call-process
               fortune-program            ; program to call
               nil fortune-buffer nil     ; INFILE BUFFER DISPLAY
               (append (if (stringp fortune-program-options)
                           (split-string fortune-program-options)
                         fortune-program-options) (list fort-file)))))))

;;;###autoload
(defun fortune-message (&optional file)
  "Display a fortune cookie to the mini-buffer.
If called with a prefix, it has the same behavior as `fortune'.
Optional FILE is a fortune file from which a cookie will be selected."
  (interactive (list (if current-prefix-arg
                         (fortune-ask-file)
                       fortune-file)))
  (with-temp-buffer
    (let ((fortune-buffer-name (current-buffer)))
      (fortune-in-buffer t file)
      ;; Avoid trailing newline.
      (if (bolp) (delete-char -1))
      (message "%s" (buffer-string)))))

;;;###autoload
(defun fortune (&optional file)
  "Display a fortune cookie.
If called with a prefix asks for the FILE to choose the fortune from,
otherwise uses the value of `fortune-file'.  If you want to have fortune
choose from a set of files in a directory, call interactively with prefix
and choose the directory as the fortune-file."
  (interactive (list (if current-prefix-arg
                         (fortune-ask-file)
                       fortune-file)))
  (fortune-in-buffer t file)
  (switch-to-buffer (get-buffer fortune-buffer-name))
  (setq buffer-read-only t))


;;; Provide ourselves.
(provide 'fortune)

;;; fortune.el ends here