写了一个脚本,在EMACS里写信,再直接调用MUTT发信

Vim、Emacs配置和使用
回复
ahnuzfm
帖子: 38
注册时间: 2008-12-26 17:33
送出感谢: 0
接收感谢: 0

写了一个脚本,在EMACS里写信,再直接调用MUTT发信

#1

帖子 ahnuzfm » 2010-08-22 14:52

不知道有没有人已经实现了这个功能?有的话请大侠给个链接。

脚本很烂,N多BUG,但勉强可用。

用法: 用C-c m 写信,
用C-c C-c 发信,
地址栏中[To: BCC: CC:]用<TAB>,将用一个buffer读取.mutt/alias文件,将光标移到某行,用回车选取该行地址,

函数mutt-add-attachment 添加附件[绑定到H-a],函数mutt-add-BCC添加暗送[绑定到H-d],
mutt-add-CC添加抄送[绑定到H-b],
Mode 函数还有以下内容:
(define-key map (kbd "H-t") 'mutt-goto-to-address) 将光标移到地址栏
(define-key map (kbd "H-a") 'mutt-add-attachment) 将光标移到
(define-key map (kbd "H-s") 'mutt-goto-subject)将光标移到主题栏
(define-key map (kbd "H-b") 'mutt-goto-body)将光标移到邮件内容
(define-key map (kbd "H-c") 'mutt-add-CC)
(define-key map (kbd "H-d") 'mutt-add-BCC)
(define-key map (kbd "<tab>") 'mutt-indent)
(define-key map (kbd "s-c") 'mutt-send-mail)
(define-key map (kbd "C-c C-c") 'mutt-send-mail)
(define-key map (kbd "H-i") 'mutt-insert-signature)添加签名




代码: 全选

;;; zfmmuttsendmail.el
;; Created at 2010/08/17, and finished at 2010/08/20,14:35:52 Friday
;; Send mail by mutt without getting out of emacs.


;; TODO: 
;;      (1) auto insert address by consulting the addressbook.
;;      (2) there are still some bug in it:
;;              when inserting the address the point sometimes is wrong.

(global-set-key (kbd "A-m") 'mutt-write-letter)
(global-set-key (kbd "C-c m") 'mutt-write-letter)

(defvar zfm-default-to-address "ahnuzfm@163.com"
  "This address is the default mail address.")

(defvar zfm-letter-body-start "------------ BODY START -----------------------")
(defvar zfm-letter-body-end   "------------ BODY END -------------------------")


(defvar zfmmail-mode-font-lock-keywords    ;keyword for buffer display
  '(
    ("^To: .*" .  font-lock-type-face)
    ("^CC: .*" .  font-lock-type-face)
    ("^BCC: .*" .  font-lock-type-face)
    ("^Subject: .*" .  font-lock-function-name-face)
    ("^Attachment: .*" .  font-lock-function-name-face)
    ("^---+.*" . font-lock-comment-face)
    )
  "Expressions to highlight in `zfmmail-mode'.")
 
(defun mutt-to-address ()
  (save-excursion 
    (goto-char (point-min))
    (buffer-substring-no-properties 
     (progn (re-search-forward "^To: " nil t 1) (point))
     (progn (end-of-line) (point)))))
 
(defun mutt-attachment ()
  (save-excursion 
    (goto-char (point-min))
    (condition-case nil
      (format "-a %s"
	      (buffer-substring-no-properties 
	       (progn (re-search-forward "^Attachment: " nil nil 1) (point))
	       (progn (end-of-line) (point))))
      (error ""))))

(defun mutt-add-attachment ()
  (interactive)
  (goto-char (point-min))
  (condition-case nil
      (re-search-forward "^Attachment: " nil nil 1)
    (error 
     (progn
       (re-search-forward "^Subject: " nil t 1)
       (forward-line)
       (insert "Attachment: \n")
       (forward-line -1)
       (end-of-line)))))

(defun mutt-BCC ()
  (save-excursion 
    (goto-char (point-min))
    (condition-case nil
	(format "-b %s"
		(buffer-substring-no-properties 
		 (progn (re-search-forward "^BCC: " nil nil 1) (point)) 
		 (progn (end-of-line) (point))))
      (error ""))))

(defun mutt-add-BCC ()
  (interactive)
  (goto-char (point-min))
  (condition-case nil
      (re-search-forward "^BCC: " nil nil 1)
    (error 
     (progn
       (re-search-forward "^To: " nil t 1)
       (forward-line)
       (insert "BCC: \n")
       (forward-line -1)
       (end-of-line)))))

(defun mutt-add-CC ()
  (interactive)
  (goto-char (point-min))
  (condition-case nil
      (re-search-forward "^CC: " nil nil 1)
    (error 
     (progn
       (re-search-forward "^To: " nil t 1)
       (forward-line)
       (insert "CC: \n")
       (forward-line -1)
       (end-of-line)))))

(defun mutt-CC ()
  (save-excursion 
    (goto-char (point-min))
    (condition-case nil
	(format "-c %s"
		(buffer-substring-no-properties 
		 (progn	(re-search-forward "^CC: " nil nil 1) (point))
		 (progn (end-of-line) (point))))
      (error ""))))

(defun mutt-subject ()
  (save-excursion 
    (goto-char (point-min))
    (buffer-substring-no-properties 
     (progn (re-search-forward "^Subject: " nil t 1) (point))
     (progn (end-of-line) (point)))))
 
(defun mutt-letter-body ()
  (save-excursion 
    (goto-char (point-min))
    (buffer-substring-no-properties
     (progn (search-forward zfm-letter-body-start nil t 1) 
	    (forward-line 1) (beginning-of-line) (point))
     (progn (search-forward zfm-letter-body-end nil t 1) 
	    (forward-line 0) (beginning-of-line) (point)))))
 
(defun mutt-goto-to-address ()
  "Go to the to address of the letter"
  (interactive)
    (goto-char (point-min))
    (re-search-forward "^To: " nil t 1))
 
(defun mutt-goto-attachment ()
  "Go to the to attachment of the letter"
  (interactive)
    (goto-char (point-min))
    (re-search-forward "^Attachment: " nil t 1))

(defun mutt-goto-subject ()
  "Go to the to subject of the letter"
  (interactive)
    (goto-char (point-min))
    (re-search-forward "^Subject: " nil t 1))

(defun mutt-goto-body ()
  "Go to the to body of the letter"
  (interactive)
    (goto-char (point-min))
    (search-forward zfm-letter-body-start nil t 1)
    (forward-line))
 
(defun mutt-insert-signature ()
  "Insert the signature file ~/.signature"
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (search-forward zfm-letter-body-end nil t 1)
    (beginning-of-line)
    (insert "\n--")
    (insert-file "~/.signature")
    (insert "\n")))

(defun mutt-send-mail ()
  "Invoking mutt to send the mail in current buffer."
  (interactive)
  (condition-case nil
      (progn
	(shell-command (format "echo \"%s\" | mutt -s \"%s\" %s %s %s -- %s"
			     (mutt-letter-body)
			     (mutt-subject)
			     (mutt-attachment)
			     (mutt-BCC)
			     (mutt-CC)
			     (mutt-to-address)))
	(message (format "Message sent to %s successfully."
			 (mutt-to-address))))
    (error (message "Fail to sending message."))))

(defun mutt-write-letter ()
  (interactive)
  (let ((buffer (get-buffer-create "*ZfmMail*")))
    (with-current-buffer buffer
      (insert (format "To: %s\n" zfm-default-to-address))
      (insert (format "Subject: \n"))
      ;(insert "Attachment: ~/mutt.sh")
      (insert (format "%s\n" zfm-letter-body-start))
      ;(insert (format " (mutt-send-mail) \n"))
      (insert (format "\n%s\n" zfm-letter-body-end))
      (insert "All the lines from here are ignored.\n")
      (insert "The line containning CC and BCC without actual address should be erased,\n")
      (insert "since this code is not fully developed.\n")
      (goto-char (point-min))
      (search-forward "Subject: ")
      ;(forward-line)
      (unless (eq major-mode 'zfmmail-mode) (zfmmail-mode))
      (setq mutt-compose-buffer-name (buffer-file-name))
      (write-file (format-time-string "/tmp/mutt-mail-%Y%m%d-%H%M%S.zfmmail"))
      ;(write-file "/tmp/mutt-mail.zfmmail")
      )
    buffer
    (switch-to-buffer buffer)))

 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun mutt-read-address ()
  (interactive)
  (beginning-of-line)
  (search-forward "@" nil t)
  (setq mutt-current-address
	(buffer-substring-no-properties
	 (progn (search-forward " " nil t -1) (forward-char)  (point))
	 (progn (search-forward " " nil t 1) (forward-char -1) (point))))
  (message (format "Current address: %s." mutt-current-address))
  (with-current-buffer (other-buffer)
    (insert mutt-current-address))
  (kill-buffer "*mutt-adress*")
;  (switch-to-buffer mutt-compose-buffer-name)
)

(defvar mutt-adress-mode-map 
  (let ((map (make-sparse-keymap)))
    (define-key map (kbd "<return>") 'mutt-read-address)
    map))

(define-derived-mode mutt-adress nil "mutt-adress"
"Major mode to edit mail and send it by mutt without getting out of emacs.
\\{zfmmail-mode-map}
Turning on Text mode runs the normal hook `zfmmail-mode-hook'."
  ;(setq font-lock-defaults '(zfmmail-mode-font-lock-keywords))
  (setq buffer-read-only nil)
  (use-local-map mutt-adress-mode-map)
  (set (make-local-variable 'outline-regexp) "^-->.*\n-->"))
 
(defun mutt-address-buffer ()
  (interactive)
  (let ((buffer (get-buffer-create "*mutt-adress*")))
    (with-current-buffer buffer
      (insert-file "~/.mutt/alias")
      (goto-char (point-min))
      (unless (eq major-mode 'mutt-adress) (mutt-adress)))
    buffer
    (switch-to-buffer buffer)))

(defun mutt-indent ()
  "If in the to address line, redefine TAB key to read .mutt/alias file for
the address, otherwise do the normal TAB function."
  (interactive)
  (setq m1 (point-marker))
  (save-excursion
    (let ((string
	   (buffer-substring-no-properties
	    (progn (beginning-of-line) (point))
	    (progn (forward-char 4) (point)))))
      (if  (or (string= string "To: ") (string= string "CC: "))
	  (progn (beginning-of-line)
		 (forward-char 4)
		 (kill-line) 
		 (mutt-address-buffer))
	(progn (goto-char (marker-position m1))
	       (indent-for-tab-command)))
      (if  (string= string "BCC:")
	  (progn (beginning-of-line)
		 (forward-char 5)
		 (kill-line) 
		 (mutt-address-buffer))
	(progn (goto-char (marker-position m1))
	       (indent-for-tab-command)))))
  (set-marker m1 nil))

(defvar zfmmail-mode-map 
  (let ((map (make-sparse-keymap)))
    (define-key map (kbd "H-t") 'mutt-goto-to-address)
    (define-key map (kbd "H-a") 'mutt-add-attachment)
    (define-key map (kbd "H-s") 'mutt-goto-subject)
    (define-key map (kbd "H-b") 'mutt-goto-body)
    (define-key map (kbd "H-c") 'mutt-add-CC)
    (define-key map (kbd "H-d") 'mutt-add-BCC)
    (define-key map (kbd "<tab>") 'mutt-indent)
    (define-key map (kbd "s-c") 'mutt-send-mail)
    (define-key map (kbd "C-c C-c") 'mutt-send-mail)
    (define-key map (kbd "H-i") 'mutt-insert-signature)
    map)
  "Keymap for `zfmmail-mode'.")

(define-derived-mode zfmmail-mode nil "zfmmail"
"Major mode to edit mail and send it by mutt without getting out of emacs.
\\{zfmmail-mode-map}
Turning on Text mode runs the normal hook `zfmmail-mode-hook'."
  (setq font-lock-defaults '(zfmmail-mode-font-lock-keywords))
  (setq buffer-read-only nil)
  (use-local-map zfmmail-mode-map)
  (set (make-local-variable 'outline-regexp) "^-->.*\n-->"))

上次由 ahnuzfm 在 2010-08-22 15:11,总共编辑 1 次。
ahnuzfm
帖子: 38
注册时间: 2008-12-26 17:33
送出感谢: 0
接收感谢: 0

Re: 写了一个脚本,在EMACS里写信,再直接调用MUTT发信

#2

帖子 ahnuzfm » 2010-08-22 15:01

附一个截图:图片
附件
a.jpg
附一个截图
头像
croner
帖子: 737
注册时间: 2009-05-31 17:40
来自: NO
送出感谢: 2 次
接收感谢: 0

Re: 写了一个脚本,在EMACS里写信,再直接调用MUTT发信

#3

帖子 croner » 2010-08-22 15:13

用gnus发不是很好么?
No.
ahnuzfm
帖子: 38
注册时间: 2008-12-26 17:33
送出感谢: 0
接收感谢: 0

Re: 写了一个脚本,在EMACS里写信,再直接调用MUTT发信

#4

帖子 ahnuzfm » 2010-08-25 14:12

试了一下用_GNUS果然更好,只是打开时有点慢。
回复

回到 “Vim和Emacs”