[Mew-dist 09929] mew-sig.el

sen_ml at example.com sen_ml at example.com
1999年 8月 2日 (月) 19:19:39 JST


(pseudo) random に signature を選んで挿入するコードを書いてみました。

ほとんど、`mew-draft-insert-signature' を「ちょこっ」といじっただけのものです。

インストール方法:

~/.signatures というディレクトリを作成し、その下に signature file を
複数入れて下さい。

# 入れるファイルは一つだけでもいいですけど、全然楽しくないと思います。

.emacs に:

  (add-hook 'mew-init-hook (lambda () (require 'mew-sig)))

を入れて、keybinding が欲しい人は:

  (add-hook 'mew-draft-mode-hook
            (lambda ()
              (define-key mew-draft-mode-map
                "\C-c\t" 'mew-draft-insert-random-sig)))

等と入れて下さい。

# C-c TAB は既に使用されているので、mew-draft-insert-random-sig には他の
key sequence がいいかもしれません。候補受付中 (^^)

よかったら試してみて下さい。

# こんな感じのものがもうあったりして・・・
-------------- next part --------------
;;; mew-sig.el -- insert a signature chosen randomly from files in a directory

;;; Author: "Sen Nagata" <sen at example.com>
;;; Version: 0.2
;;; Important Note: `mew-draft-insert-random-sig' is a modified version of
;;;                 `mew-draft-insert-signature' from mew-draft.el

;;; Commentary:
;;
;; required elisp packages:
;;
;;   -mew (tested for 1.93)
;;
;; installation:
;;
;;   -put this file in an appropriate directory (so emacs can find it)
;;
;;   -put:
;;
;;     (add-hook 'mew-init-hook (lambda () (require 'mew-sig)))
;;
;;   in your .emacs files.
;;
;;   -create a directory called .signatures in your home directory
;;    and place files containing a signature each within.
;;
;; usage:
;;
;;   -invoke `mew-draft-insert-random-sig' when in draft-mode to
;;    insert a signature.
;;
;;   -if you'd like to bind a key sequence to `mew-draft-insert-random-sig',
;;    (e.g. C-c TAB), put:
;;
;;      (add-hook 'mew-draft-mode-hook
;;                (lambda ()
;;                  (define-key mew-draft-mode-map
;;                    "\C-c\t" 'mew-draft-insert-random-sig)))
;;
;;    in your .emacs.  note that C-c TAB is the key sequence for a command
;;    which already exists (mew-draft-insert-signature), so if you use that
;;    command, you might want to choose another key sequence.

;;; History:
;;
;; 0.2
;;
;; 1999-08-02:
;;
;;   modified `mew-sig-choose-file-randomly-from-dir' to return a list of
;;   absolute filenames -- also fixed the bug found on 1999-08-01.
;;   modified `mew-draft-insert-signature' to reflect the change in
;;   `mew-sig-choose-file-randomly-from-dir'.
;;
;; 1999-08-01:
;;
;;   found bug in `mew-sig-choose-file-randomly-from-dir' -- it should not
;;   return directory names.
;;
;; 0.1
;;
;; 1999-07-31:
;;
;;   initial implementation

;;; Code:

;; is this really necessary?
(eval-when-compile
  (require 'mew))

(defconst mew-sig-version "mew-sig.el 0.2")

(defvar mew-sig-dir (expand-file-name "~/.signatures/"))

;; this function is probably useful in other contexts -- i'd rather name it
;; 'filter', but there might be namespace problems, so i didn't.
(defun mew-sig-filter (function sequence)
  "Apply FUNCTION to each element of SEQUENCE, and make a list of the non-nil results.  The result is a list which might have a length which is shorter than SEQUENCE.  SEQUENCE may be a list."
  (delq nil (mapcar function sequence)))

(defun mew-sig-choose-file-randomly-from-dir (directory)
  (let* ((file-list 
	  (mew-sig-filter 
	   (lambda (x)
	     (if (file-regular-p x)
		 ;; decided to return a list of absolute filenames
		 ;(file-name-nondirectory x)
		 x))
	   (directory-files directory t)))
	 (index (truncate (random (length file-list)))))
    (nth index file-list)))

(defun mew-draft-insert-random-sig ()
  "Insert a random signature file contained in directory mew-sig-dir.
If attachments exist and mew-signature-as-lastpart is *non-nil*,
the file is attached to the last part. Otherwise, the file is 
inserted into the body. If mew-signature-insert-last is *non-nil*,
the file is inserted to the end of the body. Otherwise, inserted
the cursor position."
  (interactive)
  (let ((sigfile 
	 (mew-sig-choose-file-randomly-from-dir mew-sig-dir)))
    (if (not (file-exists-p sigfile))
	(message "No signature file %s" sigfile)
      (if (and (mew-attach-p) mew-signature-as-lastpart)
	  (progn
	    (goto-char (point-max))
	    (forward-line -2)
	    (mew-attach-forward)
	    (mew-attach-copy sigfile "Signature")
	    (mew-attach-disposition "") ;; nil is NG.
	    (mew-attach-description mew-signature-description)
	    )
	(if mew-signature-insert-last 
	    (if (null (mew-attach-p))
		(goto-char (point-max))
	      (goto-char (mew-attach-begin))
	      (forward-line -1)
	      (end-of-line)
	      (insert "\n")))
	(insert-file-contents sigfile)
	))
    ))

;; initialize random number seed -- should i do this here?
(random t)

(provide 'mew-sig)

;; mew-sig.el ends here


Mew-dist メーリングリストの案内