From 742879f1b3d51a405971b845bba64a7f71979b8c Mon Sep 17 00:00:00 2001
From: Lars Ingebrigtsen <larsi@gnus.org>
Date: Thu, 4 Nov 2021 06:09:08 +0100
Subject: [PATCH] Add new user option mml-attach-file-at-the-end

* lisp/gnus/mml.el (mml-attach-file-at-the-end): New user option.
(mml-attach-file): Use it.
---
 etc/NEWS         |  7 +++++++
 lisp/gnus/mml.el | 24 ++++++++++++++++++------
 2 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 48ce2e4fcff..8161c5b4c5c 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -151,7 +151,14 @@ change the terminal used on a remote host.
 
 * Changes in Specialized Modes and Packages in Emacs 29.1
 
+** Message
+
+---
+*** New user option 'mml-attach-file-at-the-end'.
+If non-nil, 'C-c C-a' will put attached files at the end of the message.
+
 ** EIEIO
+
 +++
 *** 'slot-value' can now be used to read slots of 'cl-defstruct' objects
 
diff --git a/lisp/gnus/mml.el b/lisp/gnus/mml.el
index 5f35e73cd7c..079c1b51225 100644
--- a/lisp/gnus/mml.el
+++ b/lisp/gnus/mml.el
@@ -1409,6 +1409,13 @@ to specify options."
   :version "22.1" ;; Gnus 5.10.9
   :group 'message)
 
+(defcustom mml-attach-file-at-the-end nil
+  "If non-nil, \\[mml-attach-file] attaches files at the end of the message.
+If nil, files are attached at point."
+  :type 'boolean
+  :version "29.1"
+  :group 'message)
+
 ;;;###autoload
 (defun mml-attach-file (file &optional type description disposition)
   "Attach a file to the outgoing MIME message.
@@ -1423,6 +1430,8 @@ specifies how the attachment is intended to be displayed.  It can
 be either \"inline\" (displayed automatically within the message
 body) or \"attachment\" (separate from the body).
 
+Also see the `mml-attach-file-at-the-end' variable.
+
 If given a prefix interactively, no prompting will be done for
 the TYPE, DESCRIPTION or DISPOSITION values.  Instead defaults
 will be computed and used."
@@ -1440,8 +1449,11 @@ will be computed and used."
 			 (mml-minibuffer-read-disposition type nil file))))
      (list file type description disposition)))
   ;; If in the message header, attach at the end and leave point unchanged.
-  (let ((head (unless (message-in-body-p) (point))))
-    (if head (goto-char (point-max)))
+  (let ((at-end (and (or (not (message-in-body-p))
+                         mml-attach-file-at-the-end)
+                     (point))))
+    (when at-end
+      (goto-char (point-max)))
     (mml-insert-empty-tag 'part
 			  'type type
 			  ;; icicles redefines read-file-name and returns a
@@ -1451,13 +1463,13 @@ will be computed and used."
 			  'description description)
     ;; When using Mail mode, make sure it does the mime encoding
     ;; when you send the message.
-    (or (eq mail-user-agent 'message-user-agent)
-	(setq mail-encode-mml t))
-    (when head
+    (unless (eq mail-user-agent 'message-user-agent)
+      (setq mail-encode-mml t))
+    (when at-end
       (unless (pos-visible-in-window-p)
 	(message "The file \"%s\" has been attached at the end of the message"
 		 (file-name-nondirectory file)))
-      (goto-char head))))
+      (goto-char at-end))))
 
 (defun mml-dnd-attach-file (uri _action)
   "Attach a drag and drop file.
-- 
2.39.5