From a996e8d46f822aa97d392cefac304f06cd91fd20 Mon Sep 17 00:00:00 2001 From: Leo Date: Tue, 21 Dec 2021 15:02:12 +0100 Subject: Add ability to edit patches before applying them Sometimes it is necessary to edit patches before applying them. These changes allows you to do that by providing a new command `piem-edit` that shows the buffer that is prepared by `piem-am-ready-mbox` to the user. [km] Fixed let-binding in piem-edit-patch-am. Message-Id: <20211221140212.30248-1-sourcehut@relevant-information.com> --- piem.el | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/piem.el b/piem.el index afe6b7a..b06719b 100644 --- a/piem.el +++ b/piem.el @@ -597,14 +597,18 @@ public-inbox's configuration), return the value of ;; blank line. Assume we're in a header. (insert (format "Message-Id: <%s>\n" mid)))))))) -(defun piem-am-ready-mbox () +(defun piem-am-ready-mbox (&optional buffer-name) "Generate a buffer containing an am-ready mbox. The return value is (BUFFER . FORMAT), where FORMAT is either \"mbox\" or \"mboxrd\". Callers are responsible for killing the -buffer." +buffer. + +By default the buffer name is hidden, but when BUFFER-NAME is +non-nil, use that name instead." (when-let ((res (run-hook-with-args-until-success 'piem-am-ready-mbox-functions))) - (pcase-let ((buffer (generate-new-buffer " *piem am-ready mbox*")) + (pcase-let ((buffer (generate-new-buffer + (or buffer-name " *piem am-ready mbox*"))) (`(,fn . ,format) (if (member (cdr-safe res) '("mbox" "mboxrd")) res @@ -981,6 +985,43 @@ this triggers the creation of a new worktree." (magit-status-setup-buffer am-directory) (dired am-directory)))) +(defvar-local piem-edit-patch--coderepo nil) +(defvar-local piem-edit-patch--format nil) + +(defun piem-edit () + "Edit an am-ready mbox before feeding it to `git am'." + (interactive) + (pcase-let ((`(,mbox . ,format) + (or (piem-am-ready-mbox "*piem-edit-patch*") + (user-error + "Could not find am-ready mbox for current buffer"))) + (coderepo (piem-inbox-coderepo))) + (pop-to-buffer mbox) + (piem-edit-patch-mode) + (setq piem-edit-patch--coderepo coderepo) + (setq piem-edit-patch--format format))) + +(defun piem-edit-patch-am () + "Apply the patch that is currently edited." + (interactive) + (let ((buf (current-buffer))) + (piem-am buf + "mbox" + (piem-extract-mbox-info (current-buffer)) + piem-edit-patch--coderepo) + (kill-buffer buf))) + +(defvar piem-edit-patch-mode-map + (let ((map (make-sparse-keymap))) + (define-key map (kbd "C-c C-c") #'piem-edit-patch-am) + map) + "Keymap for editing patches with piem.") + +(define-derived-mode piem-edit-patch-mode text-mode "piem-edit-patch" + "Major mode for editing patches with piem." + :group 'piem + (buffer-enable-undo)) + ;;;; Dispatch -- cgit v1.2.3