* improve message-composition / draft handling

This commit is contained in:
djcb 2013-05-21 08:05:06 +03:00
parent 65ae41c37a
commit 222d8c3627
4 changed files with 61 additions and 43 deletions

View File

@ -188,7 +188,7 @@ If needed, set the Fcc header, and register the handler function."
(lambda (file) (lambda (file)
(setq message-fcc-handler-function old-handler) ;; reset the fcc handler (setq message-fcc-handler-function old-handler) ;; reset the fcc handler
(write-file file) ;; writing maildirs files is easy (write-file file) ;; writing maildirs files is easy
(mu4e~proc-add file maildir))))))) ;; update the database (mu4e~proc-add file (or maildir "/")))))))) ;; update the database
(defun mu4e~compose-register-message-save-hooks () (defun mu4e~compose-register-message-save-hooks ()
@ -365,8 +365,7 @@ tempfile)."
;; bind to `mu4e-compose-parent-message' of compose buffer ;; bind to `mu4e-compose-parent-message' of compose buffer
(set (make-local-variable 'mu4e-compose-parent-message) original-msg) (set (make-local-variable 'mu4e-compose-parent-message) original-msg)
(put 'mu4e-compose-parent-message 'permanent-local t) (put 'mu4e-compose-parent-message 'permanent-local t)
;; hide some headers
;; hide some headers
(let ((message-hidden-headers mu4e~compose-hidden-headers)) (let ((message-hidden-headers mu4e~compose-hidden-headers))
(message-hide-headers)) (message-hide-headers))
;; switch on the mode ;; switch on the mode

View File

@ -208,21 +208,21 @@ separator is never written to the message file. Also see
(purecopy "--text follows this line--")) (purecopy "--text follows this line--"))
(put 'mail-header-separator 'permanent-local t) (put 'mail-header-separator 'permanent-local t)
(save-excursion (save-excursion
;; make sure there's not one already
(mu4e~draft-remove-mail-header-separator)
(let ((sepa (propertize mail-header-separator (let ((sepa (propertize mail-header-separator
'intangible t 'intangible t
'read-only "Can't touch this" 'read-only "Can't touch this"
'rear-nonsticky t 'rear-nonsticky t
'font-lock-face 'mu4e-system-face))) 'font-lock-face 'mu4e-system-face)))
(widen) (widen)
(goto-char (point-min))
;; search for the first empty line ;; search for the first empty line
(unless (search-forward sepa nil t) (goto-char (point-min))
(goto-char (point-min)) (if (search-forward-regexp "^$" nil t)
(if (search-forward-regexp "^$" nil t)
(replace-match (concat sepa)) (replace-match (concat sepa))
(progn ;; no empty line? then prepend one (progn ;; no empty line? then prepend one
(goto-char (point-max)) (goto-char (point-max))
(insert "\n" sepa))))))) (insert "\n" sepa))))))
(defun mu4e~draft-remove-mail-header-separator () (defun mu4e~draft-remove-mail-header-separator ()
"Remove `mail-header-separator; we do this before saving a "Remove `mail-header-separator; we do this before saving a
@ -232,7 +232,7 @@ never hits the disk. Also see `mu4e~draft-insert-mail-header-separator."
(widen) (widen)
(goto-char (point-min)) (goto-char (point-min))
;; remove the --text follows this line-- separator ;; remove the --text follows this line-- separator
(when (search-forward-regexp (concat "^" mail-header-separator)) (when (search-forward-regexp (concat "^" mail-header-separator) nil t)
(let ((inhibit-read-only t)) (let ((inhibit-read-only t))
(replace-match ""))))) (replace-match "")))))
@ -339,7 +339,7 @@ You can append flags."
(defvar mu4e~draft-drafts-folder nil (defvar mu4e~draft-drafts-folder nil
"The drafts-folder for this compose buffer, based on "The drafts-folder for this compose buffer, based on
mu4e-drafts-folder', which will be evaluated once.") `mu4e-drafts-folder', which is evaluated once.")
(defun mu4e-draft-open (compose-type &optional msg) (defun mu4e-draft-open (compose-type &optional msg)
"Open a draft file for a new message (when COMPOSE-TYPE is reply, forward or new), "Open a draft file for a new message (when COMPOSE-TYPE is reply, forward or new),
@ -350,35 +350,37 @@ of `mu4e-maildir' and `mu4e-drafts-folder' (the latter will be
evaluated). The message file name is a unique name determined by evaluated). The message file name is a unique name determined by
`mu4e-send-draft-file-name'. The initial contents will be created `mu4e-send-draft-file-name'. The initial contents will be created
from either `mu4e~draft-reply-construct', or from either `mu4e~draft-reply-construct', or
`mu4e~draft-forward-construct' or `mu4e~draft-forward-construct' or `mu4e~draft-newmsg-construct'."
`mu4e~draft-newmsg-construct'."
;; evaluate mu4e-drafts-folder once, here, and use that value throughout.
(set (make-local-variable 'mu4e~draft-drafts-folder)
(mu4e-get-drafts-folder msg))
(put 'mu4e~draft-drafts-folder 'permanent-local t)
(unless mu4e-maildir (mu4e-error "mu4e-maildir not set")) (unless mu4e-maildir (mu4e-error "mu4e-maildir not set"))
(if (eq compose-type 'edit) (let ((draft-dir))
;; case-1: re-editing a draft messages. in this case, we do know the full (if (eq compose-type 'edit)
;; path, but we cannot really know 'drafts folder' ;; case-1: re-editing a draft messages. in this case, we do know the full
(find-file (mu4e-message-field msg :path)) ;; path, but we cannot really know 'drafts folder'... we make a guess
;; case-2: creating a new message; in this case, we can determing (progn
;; mu4e-get-drafts-folder (setq draft-dir (mu4e~guess-maildir (mu4e-message-field msg :path)))
(let* ((draftsfolder (mu4e-get-drafts-folder msg)) (find-file (mu4e-message-field msg :path)))
(draftpath ;; case-2: creating a new message; in this case, we can determing
(format "%s/%s/cur/%s" ;; mu4e-get-drafts-folder
mu4e-maildir (progn
draftsfolder (setq draft-dir (mu4e-get-drafts-folder msg))
(mu4e~draft-message-filename-construct "DS")))) (let ((draft-path
(find-file draftpath) (format "%s/%s/cur/%s"
(insert mu4e-maildir
(case compose-type draft-dir
(reply (mu4e~draft-reply-construct msg)) (mu4e~draft-message-filename-construct "DS"))))
(forward (mu4e~draft-forward-construct msg)) (find-file draft-path))
(new (mu4e~draft-newmsg-construct)) (insert
(t (mu4e-error "unsupported compose-type %S" compose-type)))) (case compose-type
;; save the drafts folder 'permanently' for this buffer (reply (mu4e~draft-reply-construct msg))
(set (make-local-variable 'mu4e~draft-drafts-folder) draftsfolder) (forward (mu4e~draft-forward-construct msg))
(put 'mu4e~draft-drafts-folder 'permanent-local t)))) (new (mu4e~draft-newmsg-construct))
(t (mu4e-error "unsupported compose-type %S" compose-type))))))
;; evaluate mu4e~drafts-drafts-folder once, here, and use that value throughout.
(set (make-local-variable 'mu4e~draft-drafts-folder) draft-dir)
(put 'mu4e~draft-drafts-folder 'permanent-local t)
(unless mu4e~draft-drafts-folder
(mu4e-error "failed to determine drafts folder"))))
;; (defun mu4e-draft-setup-fcc () ;; (defun mu4e-draft-setup-fcc ()

View File

@ -409,18 +409,21 @@ of 'my' email addresses (see `mu4e-user-mail-address-list')."
With MAILDIR set to the maildir this message resides in, With MAILDIR set to the maildir this message resides in,
e.g. '/drafts'; if this works, we will receive (:info add :path e.g. '/drafts'; if this works, we will receive (:info add :path
<path> :docid <docid>) as well as (:update <msg-sexp>)." <path> :docid <docid>) as well as (:update <msg-sexp>)."
(mu4e~proc-send-command "cmd:add path:%s maildir:%s" (mu4e~proc-send-command "cmd:add path:%s %s"
(mu4e~proc-escape path) (mu4e~proc-escape maildir))) (mu4e~proc-escape path)
(if maildir
(format "maildir:%s" (mu4e~proc-escape maildir))
"")))
(defun mu4e~proc-sent (path maildir) (defun mu4e~proc-sent (path maildir)
"Add the message at PATH to the database. "Add the message at PATH to the database.
With MAILDIR set to the maildir this message resides in, With MAILDIR set to the maildir this message resides in,
e.g. '/drafts'. e.g. '/drafts'.
if this works, we will receive (:info add :path <path> :docid if this works, we will receive (:info add :path <path> :docid
<docid> :fcc <path>)." <docid> :fcc <path>)."
(mu4e~proc-send-command "cmd:sent path:%s maildir:%s" (mu4e~proc-send-command "cmd:sent path:%s maildir:%s"
(mu4e~proc-escape path) (mu4e~proc-escape maildir))) (mu4e~proc-escape path) (mu4e~proc-escape maildir)))
(defun mu4e~proc-compose (type &optional docid) (defun mu4e~proc-compose (type &optional docid)

View File

@ -138,6 +138,20 @@ see its docstring)."
(defun mu4e~guess-maildir (path)
"Guess the maildir for some path, or nil if cannot find it."
(when (zerop (string-match mu4e-maildir path))
(replace-regexp-in-string
mu4e-maildir
""
(expand-file-name
(concat path "/../..")))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun mu4e-create-maildir-maybe (dir) (defun mu4e-create-maildir-maybe (dir)
"Offer to create maildir DIR if it does not exist yet. "Offer to create maildir DIR if it does not exist yet.