mu4e/mu4e-actions: re-factor apply-patch/mbox

The old code directly hacked around with ido-read-directory to achieve
its smarts. However other completion methods are available so this
re-factors the code to use an appropriately predicated completing-read
with a new history variable which is just used for patch application.
This commit is contained in:
Alex Bennée 2018-05-21 18:53:42 +01:00
parent 67b9ef8384
commit 79774d4d1b
1 changed files with 30 additions and 22 deletions

View File

@ -217,36 +217,44 @@ store your org-contacts."
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar mu4e~patch-directory-history nil
"History of directories we have applied patches to.")
;; This essentially works around the fact that read-directory-name
;; can't have custom history.
(defun mu4e~read-patch-directory (&optional prompt)
"Read a `PROMPT'ed directory name via `completing-read' with history."
(unless prompt
(setq prompt "Target directory:"))
(file-truename
(completing-read prompt 'read-file-name-internal #'file-directory-p
nil nil 'mu4e~patch-directory-history)))
(defun mu4e-action-git-apply-patch (msg) (defun mu4e-action-git-apply-patch (msg)
"Apply the git [patch] message." "Apply `MSG' as a git patch."
(let ((path (ido-read-directory-name "Target directory: " (let ((path (mu4e~read-patch-directory "Target directory: ")))
(car ido-work-directory-list) (let ((default-directory path))
"~/" t))) (shell-command
(setf ido-work-directory-list (format "git apply %s"
(cons path (delete path ido-work-directory-list))) (shell-quote-argument (mu4e-message-field msg :path)))))))
(shell-command
(format "cd %s; git apply %s"
path
(mu4e-message-field msg :path)))))
(defun mu4e-action-git-apply-mbox (msg &optional signoff) (defun mu4e-action-git-apply-mbox (msg &optional signoff)
"Apply and commit the git [patch] MSG with optional SIGNOFF. "Apply `MSG' a git patch with optional `SIGNOFF'.
If the `default-directory' matches the most recent history entry don't If the `default-directory' matches the most recent history entry don't
bother asking for the git tree again (useful for bulk actions)." bother asking for the git tree again (useful for bulk actions)."
(let ((cwd (car ido-work-directory-list))) (let ((cwd (substring-no-properties
(or (car mu4e~patch-directory-history)
"not-a-dir"))))
(unless (and (stringp cwd) (string= default-directory cwd)) (unless (and (stringp cwd) (string= default-directory cwd))
(setq cwd (ido-read-directory-name "Target directory: " (setq cwd (mu4e~read-patch-directory "Target directory: ")))
cwd (let ((default-directory cwd))
"~/" t)) (shell-command
(setf ido-work-directory-list (format "git am %s %s"
(cons cwd (delete cwd ido-work-directory-list)))) (if signoff "--signoff" "")
(shell-command (shell-quote-argument (mu4e-message-field msg :path)))))))
(format "cd %s; git am %s %s"
(shell-quote-argument cwd)
(if signoff "--signoff" "")
(shell-quote-argument (mu4e-message-field msg :path))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;