From 79774d4d1be5db1044b58532ed95d24538ff1579 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Benn=C3=A9e?= Date: Mon, 21 May 2018 18:53:42 +0100 Subject: [PATCH] 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. --- mu4e/mu4e-actions.el | 52 +++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/mu4e/mu4e-actions.el b/mu4e/mu4e-actions.el index c59a16ec..c4d7a155 100644 --- a/mu4e/mu4e-actions.el +++ b/mu4e/mu4e-actions.el @@ -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) - "Apply the git [patch] message." - (let ((path (ido-read-directory-name "Target directory: " - (car ido-work-directory-list) - "~/" t))) - (setf ido-work-directory-list - (cons path (delete path ido-work-directory-list))) - (shell-command - (format "cd %s; git apply %s" - path - (mu4e-message-field msg :path))))) + "Apply `MSG' as a git patch." + (let ((path (mu4e~read-patch-directory "Target directory: "))) + (let ((default-directory path)) + (shell-command + (format "git apply %s" + (shell-quote-argument (mu4e-message-field msg :path))))))) (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 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)) - (setq cwd (ido-read-directory-name "Target directory: " - cwd - "~/" t)) - (setf ido-work-directory-list - (cons cwd (delete cwd ido-work-directory-list)))) - (shell-command - (format "cd %s; git am %s %s" - (shell-quote-argument cwd) - (if signoff "--signoff" "") - (shell-quote-argument (mu4e-message-field msg :path)))))) + (setq cwd (mu4e~read-patch-directory "Target directory: "))) + (let ((default-directory cwd)) + (shell-command + (format "git am %s %s" + (if signoff "--signoff" "") + (shell-quote-argument (mu4e-message-field msg :path))))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;