1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-12-14 09:39:42 +00:00
emacs/lisp/play/fortune.el

324 lines
11 KiB
EmacsLisp
Raw Normal View History

;;; fortune.el --- use fortune to create signatures
;; Copyright (C) 1999, 2001-2015 Free Software Foundation, Inc.
1999-09-01 23:31:57 +00:00
;; Author: Holger Schauer <Holger.Schauer@gmx.de>
;; Keywords: games utils mail
;; This file is part of GNU Emacs.
1999-09-01 23:31:57 +00:00
;; GNU Emacs is free software: you can redistribute it and/or modify
1999-09-01 23:31:57 +00:00
;; 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.
1999-09-01 23:31:57 +00:00
;; 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 <http://www.gnu.org/licenses/>.
1999-09-01 23:31:57 +00:00
;;; 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'.
1999-09-01 23:31:57 +00:00
;; 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)
1999-09-01 23:31:57 +00:00
;; 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 ()
1999-09-01 23:31:57 +00:00
;; (define-key gnus-article-mode-map "i" 'fortune-from-region)))
;; which allows marking a region and then pressing "i" so that the marked
2011-11-17 17:40:48 +00:00
;; region will be automatically added to my favorite fortune-file.
1999-09-01 23:31:57 +00:00
;;; Code:
;;; **************
;;; Customizable Settings
(defgroup fortune nil
"Settings for fortune."
:link '(emacs-commentary-link "fortune.el")
:version "21.1"
1999-09-01 23:31:57 +00:00
:group 'games)
(defgroup fortune-signature nil
"Settings for use of fortune for signatures."
:group 'fortune
1999-09-01 23:31:57 +00:00
: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)
1999-09-01 23:31:57 +00:00
(defcustom fortune-database-extension ".dat"
"The extension of the corresponding fortune database.
1999-09-01 23:31:57 +00:00
Normally you won't have a reason to change it."
:type 'string
:group 'fortune)
1999-09-01 23:31:57 +00:00
(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)
1999-09-01 23:31:57 +00:00
(defcustom fortune-strfile "strfile"
"Program to compute a new fortune database."
:type 'string
:group 'fortune)
1999-09-01 23:31:57 +00:00
(defcustom fortune-strfile-options ""
"Options to pass to the strfile program (a string)."
:type 'string
:group 'fortune)
1999-09-01 23:31:57 +00:00
(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)
1999-09-01 23:31:57 +00:00
(defcustom fortune-always-compile t
"Non-nil means automatically compile fortune files.
1999-09-01 23:31:57 +00:00
If nil, you must invoke `fortune-compile' manually to do that."
:type 'boolean
:group 'fortune)
1999-09-01 23:31:57 +00:00
(defcustom fortune-author-line-prefix " -- "
"Prefix to put before the author name of a fortunate."
:type 'string
:group 'fortune-signature)
1999-09-01 23:31:57 +00:00
(defcustom fortune-fill-column fill-column
"Fill column for fortune files."
:type 'integer
:group 'fortune-signature)
1999-09-01 23:31:57 +00:00
(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)
1999-09-01 23:31:57 +00:00
(defcustom fortune-sigend ""
"Some text to insert after the fortune cookie, in a mail signature."
:type 'string
:group 'fortune-signature)
1999-09-01 23:31:57 +00:00
;; not customizable settings
(defvar fortune-buffer-name "*fortune*")
(defconst fortune-end-sep "\n%\n")
1999-09-01 23:31:57 +00:00
;;; **************
;;; Inserting a new fortune
(defun fortune-append (string &optional interactive file)
"Appends STRING to the fortune FILE.
1999-09-01 23:31:57 +00:00
If INTERACTIVE is non-nil, don't compile the fortune file afterwards."
(setq file (expand-file-name
1999-09-01 23:31:57 +00:00
(substitute-in-file-name (or file fortune-file))))
(if (file-directory-p file)
(error "Cannot append fortune to directory %s" file))
1999-09-01 23:31:57 +00:00
(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
1999-09-01 23:31:57 +00:00
(read-file-name
"Fortune file to use: "
fortune-dir nil nil "")))
2001-04-23 11:50:36 +00:00
;;;###autoload
1999-09-01 23:31:57 +00:00
(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))
2001-04-23 11:50:36 +00:00
;;;###autoload
1999-09-01 23:31:57 +00:00
(defun fortune-from-region (beg end file)
"Append the current region to a local fortune-like data file.
1999-09-01 23:31:57 +00:00
Interactively, if called with a prefix argument,
read the file name to use. Otherwise use the value of `fortune-file'."
(interactive
1999-09-01 23:31:57 +00:00
(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
1999-09-01 23:31:57 +00:00
(search-forward-regexp
"^From: \\(.*\\)$"
(point-max) t))
(if help-point
(setq author (buffer-substring (match-beginning 1) help-point))
1999-09-01 23:31:57 +00:00
(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
1999-09-01 23:31:57 +00:00
(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))
1999-09-01 23:31:57 +00:00
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
2001-04-23 11:50:36 +00:00
;;;###autoload
1999-09-01 23:31:57 +00:00
(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
1999-09-01 23:31:57 +00:00
(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
1999-09-01 23:31:57 +00:00
(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)))))
2003-02-04 13:24:35 +00:00
1999-09-01 23:31:57 +00:00
;;; **************
;;; Use fortune for signature
2001-04-23 11:50:36 +00:00
;;;###autoload
1999-09-01 23:31:57 +00:00
(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
1999-09-01 23:31:57 +00:00
(list
(if current-prefix-arg
(fortune-ask-file)
fortune-file)))
(save-excursion
(fortune-in-buffer t file)
1999-09-01 23:31:57 +00:00
(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)
1999-09-01 23:31:57 +00:00
"Put a fortune cookie in the *fortune* buffer.
INTERACTIVE is ignored. Optional argument FILE, when supplied,
specifies the file to choose the fortune from."
1999-09-01 23:31:57 +00:00
(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)))))
* url-util.el (url-insert-entities-in-string): * url-nfs.el (url-nfs-unescape): * url-ldap.el (url-ldap): * url-imap.el (url-imap): * url-cid.el (url-cid-gnus, url-cid): Use with-current-buffer. * erc.el (erc-display-line-1, erc-process-away): * erc-truncate.el (erc-truncate-buffer-to-size): Use with-current-buffer. * term/ns-win.el (ns-scroll-bar-move, ns-face-at-pos): * play/mpuz.el (mpuz-create-buffer): * play/landmark.el (lm-prompt-for-move, lm-print-wts, lm-print-smell) (lm-print-y,s,noise, lm-print-w0, lm-init): * play/gomoku.el (gomoku-prompt-for-move): * play/fortune.el (fortune-in-buffer): * play/dissociate.el (dissociated-press): * play/decipher.el (decipher-adjacency-list, decipher-display-regexp) (decipher-analyze-buffer, decipher-stats-buffer,decipher-stats-buffer): * mail/supercite.el (sc-eref-show): * mail/smtpmail.el (smtpmail-send-it): * mail/rmailsum.el (rmail-summary-next-labeled-message) (rmail-summary-previous-labeled-message, rmail-summary-wipe) (rmail-summary-undelete-many, rmail-summary-rmail-update) (rmail-summary-goto-msg, rmail-summary-expunge) (rmail-summary-get-new-mail, rmail-summary-search-backward) (rmail-summary-add-label, rmail-summary-output-menu) (rmail-summary-output-body): * mail/rfc822.el (rfc822-addresses): * mail/reporter.el (reporter-dump-variable, reporter-dump-state): * mail/mailpost.el (post-mail-send-it): * mail/hashcash.el (hashcash-generate-payment): * mail/feedmail.el (feedmail-run-the-queue) (feedmail-queue-send-edit-prompt-help-first) (feedmail-send-it-immediately, feedmail-give-it-to-buffer-eater) (feedmail-deduce-address-list): * eshell/esh-ext.el (eshell-remote-command): * eshell/em-unix.el (eshell-occur-mode-mouse-goto): * emulation/viper-util.el (viper-glob-unix-files, viper-save-setting) (viper-wildcard-to-regexp, viper-glob-mswindows-files) (viper-save-string-in-file, viper-valid-marker): * emulation/viper-keym.el (viper-toggle-key): * emulation/viper-ex.el (ex-expand-filsyms, viper-get-ex-file) (ex-edit, ex-global, ex-mark, ex-next-related-buffer, ex-quit) (ex-get-inline-cmd-args, ex-tag, ex-command, ex-compile): * emulation/viper-cmd.el (viper-exec-form-in-vi) (viper-exec-form-in-emacs, viper-brac-function): * emulation/viper.el (viper-delocalize-var): * emulation/vip.el (vip-mode, vip-get-ex-token, vip-ex, vip-get-ex-pat) (vip-get-ex-command, vip-get-ex-opt-gc, vip-get-ex-buffer) (vip-get-ex-count, vip-get-ex-file, ex-edit, ex-global, ex-mark) (ex-map, ex-unmap, ex-quit, ex-read, ex-tag, ex-command): * emulation/vi.el (vi-switch-mode, vi-ex-cmd): * emulation/edt.el (edt-electric-helpify): * emulation/cua-rect.el (cua--rectangle-aux-replace): * emulation/cua-gmrk.el (cua--insert-at-global-mark) (cua--delete-at-global-mark, cua--copy-rectangle-to-global-mark) (cua-indent-to-global-mark-column): * calendar/diary-lib.el (calendar-mark-1): * calendar/cal-hebrew.el (calendar-hebrew-mark-date-pattern): Use with-current-buffer. * emulation/viper.el (viper-delocalize-var): Use dolist.
2009-11-03 02:04:29 +00:00
(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)))))))
1999-09-01 23:31:57 +00:00
2001-04-23 11:50:36 +00:00
;;;###autoload
1999-09-01 23:31:57 +00:00
(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)))
1999-09-01 23:31:57 +00:00
(fortune-in-buffer t file)
(switch-to-buffer (get-buffer fortune-buffer-name))
(setq buffer-read-only t))
1999-09-01 23:31:57 +00:00
;;; Provide ourselves.
(provide 'fortune)
;;; fortune.el ends here