Skip to content

Commit c492a27

Browse files
committed
Convert mail popup
Extract and rewrite the cover letter searching functionality to be more idiomatic.
1 parent b51b658 commit c492a27

File tree

1 file changed

+62
-58
lines changed

1 file changed

+62
-58
lines changed

magit-stgit.el

Lines changed: 62 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ one from the minibuffer, and move to the next line."
272272
[("e" "Edit" magit-stgit-edit)
273273
("n" "Rename" magit-stgit-rename)
274274
("k" "Delete" magit-stgit-delete)]
275-
[("m" "Mail patches" magit-stgit-mail-popup)]])
275+
[("m" "Mail patches" magit-stgit-mail)]])
276276

277277
;;;###autoload
278278
(defun magit-stgit-init ()
@@ -595,66 +595,70 @@ ask for confirmation before deleting."
595595

596596
;;;; magit-stgit-mail
597597

598-
(magit-define-popup magit-stgit-mail-popup
599-
"Popup console for StGit mail."
600-
'magit-stgit-commands
598+
(transient-define-prefix magit-stgit-mail ()
599+
"Send a set of patches by e-mail."
600+
:value '("--git" "--auto-recipients")
601601
:man-page "stg-mail"
602-
:switches '((?m "Generate an mbox file instead of sending" "--mbox")
603-
(?g "Use git send-email" "--git" t)
604-
(?e "Edit cover letter before send" "--edit-cover")
605-
(?a "Auto-detect recipients for each patch" "--auto")
606-
(?A "Auto-detect To, Cc and Bcc for all patches from cover"
607-
"--auto-recipients" t))
608-
:options '((?o "Set file as cover message" "--cover="
609-
(lambda (prompt default) (read-file-name "Find file: " default)))
610-
(?v "Add version to [PATCH ...]" "--version=")
611-
(?p "Add prefix to [... PATCH ...]" "--prefix=")
612-
(?t "Mail To" "--to=")
613-
(?c "Mail Cc" "--cc=")
614-
(?b "Mail Bcc:" "--bcc="))
615-
:actions '((?m "Send" magit-stgit-mail)))
602+
["Arguments"
603+
("-m" "Generate an mbox file instead of sending" "--mbox")
604+
("-g" "Use git send-email" "--git")
605+
("-e" "Edit cover letter before sending" "--edit-cover")
606+
("-a" "Automatically Cc the patch signers" "--auto")
607+
("-A" "Auto-detect To, Cc and Bcc for all patches from cover"
608+
"--auto-recipients")
609+
""
610+
("-o" "Set file as cover message" "--cover="
611+
:reader (lambda (_prompt initial-input _history)
612+
(read-file-name "Find file: " nil nil nil initial-input)))
613+
("-v" "Add version to [PATCH ...]" "--version=")
614+
("-p" "Add prefix to [... PATCH ...]" "--prefix=")
615+
("-t" "Mail To" "--to=")
616+
("-c" "Mail Cc" "--cc=")
617+
("-b" "Mail Bcc" "--bcc=")]
618+
["Actions"
619+
("m" "Send" magit-stgit--mail)])
620+
621+
(defun magit-stgit--mail-recipients (&optional cover)
622+
"Return a list of zero or one of each of `--to', `--cc' and
623+
`--bcc' arguments for `stg mail', with the values determined from
624+
the cover letter file COVER, or the current buffer if COVER is
625+
nil."
626+
(let ((buffer (current-buffer))
627+
(fields '()))
628+
(with-temp-buffer
629+
(if cover
630+
(insert-file-contents cover)
631+
(insert-buffer-substring buffer))
632+
(goto-char (point-min))
633+
(while (re-search-forward (rx (group (or "To" "Cc" "Bcc")) ":" (+ blank)
634+
(group (* nonl)) (* blank) eol)
635+
nil t)
636+
(let ((field (match-string 1))
637+
(recipient (match-string 2)))
638+
(when (string-match "<" recipient)
639+
(setf (alist-get field fields nil nil #'equal) recipient)))))
640+
(cl-loop for (field . recipient) in fields
641+
collect (format "--%s=\"%s\"" (downcase field) recipient))))
616642

617643
;;;###autoload
618-
(defun magit-stgit-mail (patches &rest args)
619-
"Send PATCHES with \"stg mail\".
620-
621-
If a cover is specified, it will be searched to automatically set
622-
the To, Cc, and Bcc fields for all patches."
623-
(interactive (list (magit-stgit-read-patches t t t t "Send patch")
624-
(magit-stgit-mail-arguments)))
625-
(setq args (-flatten args)) ; nested list when called from popup
626-
(let* ((auto "--auto-recipients")
627-
(have-auto (member auto args))
628-
(cover (car (delq nil (mapcar (lambda (arg)
629-
(if (string-prefix-p "--cover=" arg)
630-
arg nil))
631-
args))))
632-
(cover-pos -1))
633-
(when have-auto
634-
(setq args (delete auto args)))
635-
(when (and have-auto cover)
636-
(setq cover (substring cover 8))
637-
(setq cover (with-temp-buffer (insert-file-contents cover)
638-
(buffer-string)))
639-
(while (setq cover-pos
640-
(string-match
641-
"^\\(To\\|Cc\\|Bcc\\):[[:space:]]+\\(.*\\)[[:space:]]*$"
642-
cover (1+ cover-pos)))
643-
(let ((field (match-string 1 cover))
644-
(recipient (match-string 2 cover)))
645-
(setq field (match-string 1 cover))
646-
(when (string-match "<" recipient)
647-
(setq recipient (format "\"%s\"" recipient)))
648-
(cond ((equal field "To")
649-
(setq args (cons (format "--to=%s" recipient)
650-
args)))
651-
((equal field "Cc")
652-
(setq args (cons (format "--cc=%s" recipient)
653-
args)))
654-
((equal field "Bcc")
655-
(setq args (cons (format "--bcc=%s" recipient)
656-
args)))))))
657-
(magit-run-stgit-async "mail" args patches)))
644+
(defun magit-stgit--mail (patches &rest args)
645+
"Invoke `stg mail ARGS... -- PATCHES...'.
646+
647+
ARGS can contain the fake argument `--auto-recipients' which is
648+
not passed to `stg mail'. If the argument is specified and
649+
`--cover' is present in ARGS, the cover letter will be searched
650+
to automatically set the values of `--to', `--cc' and `--bcc'.
651+
652+
If called interactively, mail the patches around point or read
653+
one from the minibuffer."
654+
(interactive (cons (magit-stgit-read-patches t t t t "Send patch")
655+
(transient-args 'magit-stgit-mail)))
656+
(let* ((autop (member "--auto-recipients" args))
657+
(args (remove "--auto-recipients" args))
658+
(cover (transient-arg-value "--cover=" args)))
659+
(magit-run-stgit-async
660+
"mail" args (and autop cover (magit-stgit--mail-recipients cover))
661+
"--" patches)))
658662

659663
;;; Mode
660664

0 commit comments

Comments
 (0)