unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* [PATCH 0/6] Use notmuch insert for fcc
@ 2016-06-10 23:02 Mark Walters
  2016-06-10 23:02 ` [PATCH 1/6] emacs: notmuch-check-exit-status bugfix Mark Walters
                   ` (6 more replies)
  0 siblings, 7 replies; 14+ messages in thread
From: Mark Walters @ 2016-06-10 23:02 UTC (permalink / raw)
  To: notmuch

This series allows the use of notmuch insert for fcc. It is rather
larger than I would like but I am fairly happy with the user
experience. A defcustom controls whether to use notmuch insert or file
fcc.

The syntax in the insert case is

Fcc: folder +tag1 -tag2

which inserts the message in folder, and +tag1 and -tag2 changes (if
present) are applied afterwards.

Since setting an fcc-header to "| notmuch insert --folder=sent"
essentially works, I will try to justify why the series ends this
big. The main reason is error handling: the pipe as above does no
error handling, so if the notmuch database is locked the fcc will be
silently lost. Unfortunately message-do-fcc which handles fcc is quite
inflexible and always passes full filename as fcc, which prevents the
syntax given above.

Thus to get this syntax we need to extract a local copy of
message-do-fcc which we simplify and rejig for our needs. The exact
rejigged form is in preparation for the postpone/resume/draft patches.

We could get rid of file fcc, but some people might still want it, and
it is used in the test suite, including by some non-emacs tests (via
emacs_fcc_message in test-lib.sh) so I am happier to keep it for now.

Best wishes

Mark



Mark Walters (6):
  emacs: notmuch-check-exit-status bugfix
  emacs: maildir-fcc: prepare for use of notmuch insert
  emacs: maildir import message-do-fcc
  emacs: simplify our local copy of message-do-fcc
  Modify our local copy of message-do-fcc
  emacs: maildir: add the actual insert code

 emacs/notmuch-lib.el         |  10 ++-
 emacs/notmuch-maildir-fcc.el | 192 +++++++++++++++++++++++++++++++++++++------
 emacs/notmuch-mua.el         |  10 +--
 test/test-lib.el             |   5 ++
 4 files changed, 185 insertions(+), 32 deletions(-)

-- 
2.1.4

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH 1/6] emacs: notmuch-check-exit-status bugfix
  2016-06-10 23:02 [PATCH 0/6] Use notmuch insert for fcc Mark Walters
@ 2016-06-10 23:02 ` Mark Walters
  2016-06-10 23:02 ` [PATCH 2/6] emacs: maildir-fcc: prepare for use of notmuch insert Mark Walters
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 14+ messages in thread
From: Mark Walters @ 2016-06-10 23:02 UTC (permalink / raw)
  To: notmuch

This function prints diagnostic information in the event of an
error. However, one of the callers has an optional :stdin-string
keyword argument. This causes the error printing routine to error
itself.

Rather than reach notmuch-check-exit-status about the possible keyword
arguments (currently only one but could be more in the future) this
commit just tells notmuch-check-exit-status how to print non-string arguments.
---
 emacs/notmuch-lib.el | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el
index 1781af4..2f015b0 100644
--- a/emacs/notmuch-lib.el
+++ b/emacs/notmuch-lib.el
@@ -790,9 +790,15 @@ You may need to restart Emacs or upgrade your notmuch package."))
 		    (insert-file-contents err-file)
 		    (unless (eobp)
 		      (buffer-string)))))
+	   (command-string
+	    (mapconcat (lambda (arg)
+			 (shell-quote-argument
+			  (cond ((stringp arg) arg)
+				((symbolp arg) (symbol-name arg))
+				(t "*UNKNOWN ARGUMENT*"))))
+		       command " "))
 	   (extra
-	    (concat
-	     "command: " (mapconcat #'shell-quote-argument command " ") "\n"
+	    (concat "command: " command-string "\n"
 	     (if (integerp exit-status)
 		 (format "exit status: %s\n" exit-status)
 	       (format "exit signal: %s\n" exit-status))
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 2/6] emacs: maildir-fcc: prepare for use of notmuch insert
  2016-06-10 23:02 [PATCH 0/6] Use notmuch insert for fcc Mark Walters
  2016-06-10 23:02 ` [PATCH 1/6] emacs: notmuch-check-exit-status bugfix Mark Walters
@ 2016-06-10 23:02 ` Mark Walters
  2016-06-10 23:02 ` [PATCH 3/6] emacs: maildir import message-do-fcc Mark Walters
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 14+ messages in thread
From: Mark Walters @ 2016-06-10 23:02 UTC (permalink / raw)
  To: notmuch

We move some code around in preparation for the use of notmuch
insert. In particular, we move the check for a valid maildir for the
fcc to when the message is sent rather than when the fcc header is
inserted. The main motivation is consistency with the insert version
(coming later) where we cannot check the validity until send.

We allow the user some chance to correct the header; the choice here
is intended to be consistent with the insert version to come.
---
 emacs/notmuch-maildir-fcc.el | 69 ++++++++++++++++++++++++++++----------------
 1 file changed, 44 insertions(+), 25 deletions(-)

diff --git a/emacs/notmuch-maildir-fcc.el b/emacs/notmuch-maildir-fcc.el
index bbf6132..835258f 100644
--- a/emacs/notmuch-maildir-fcc.el
+++ b/emacs/notmuch-maildir-fcc.el
@@ -65,11 +65,9 @@ yet when sending a mail."
  :require 'notmuch-fcc-initialization
  :group 'notmuch-send)
 
-(defun notmuch-fcc-handler (destdir)
-  "Write buffer to `destdir', marking it as sent
 
-Intended to be dynamically bound to `message-fcc-handler-function'"
-    (notmuch-maildir-fcc-write-buffer-to-maildir destdir t))
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; Functions which set up the fcc header in the message buffer.
 
 (defun notmuch-fcc-header-setup ()
   "Add an Fcc header to the current message buffer.
@@ -110,27 +108,25 @@ by notmuch-mua-mail"
 	   (error "Invalid `notmuch-fcc-dirs' setting (neither string nor list)")))))
 
     (when subdir
-      (message-add-header
-       (concat "Fcc: "
-	       (file-truename
-		;; If the resulting directory is not an absolute path,
-		;; prepend the standard notmuch database path.
-		(if (= (elt subdir 0) ?/)
-		    subdir
-		  (concat (notmuch-database-path) "/" subdir)))))
-      
-      ;; finally test if fcc points to a valid maildir
-      (let ((fcc-header (message-field-value "Fcc")))
-	(unless (notmuch-maildir-fcc-dir-is-maildir-p fcc-header)
-	  (cond ((not (file-writable-p fcc-header))
-		 (error (format "No permission to create %s, which does not exist"
-				fcc-header)))
-		((y-or-n-p (format "%s is not a maildir. Create it? "
-				   fcc-header))
-		 (notmuch-maildir-fcc-create-maildir fcc-header))
-		(t
-		 (error "Message not sent"))))))))
- 
+      (notmuch-maildir-add-file-style-fcc-header subdir))))
+
+(defun notmuch-maildir-add-file-style-fcc-header (subdir)
+  (message-add-header
+   (concat "Fcc: "
+	   (file-truename
+	    ;; If the resulting directory is not an absolute path,
+	    ;; prepend the standard notmuch database path.
+	    (if (= (elt subdir 0) ?/)
+		subdir
+	      (concat (notmuch-database-path) "/" subdir))))))
+
+(defun notmuch-fcc-handler (fcc-header)
+  "Store message with file fcc."
+  (notmuch-maildir-fcc-file-fcc fcc-header))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; Functions for saving a message using file fcc.
+
 (defun notmuch-maildir-fcc-host-fixer (hostname)
   (replace-regexp-in-string "/\\|:"
 			    (lambda (s)
@@ -192,6 +188,29 @@ if successful, nil if not."
    (concat destdir "/tmp/" msg-id)
    (concat destdir "/cur/" msg-id ":2," (when mark-seen "S"))))
 
+(defun notmuch-maildir-fcc-file-fcc (fcc-header)
+  "Write the message to the file specified by FCC-HEADER.
+
+It offers the user a chance to correct the header, or filesystem,
+if needed."
+  (if (notmuch-maildir-fcc-dir-is-maildir-p fcc-header)
+      (notmuch-maildir-fcc-write-buffer-to-maildir fcc-header 't)
+    ;; The fcc-header is not a valid maildir see if the user wants to
+    ;; fix it in some way.
+    (let* ((prompt (format "Fcc %s is not a maildir: (r)etry, (c)reate folder, (i)gnore, or  (e)dit the header? "
+			   fcc-header))
+	    (response (read-char-choice prompt '(?r ?c ?i ?e))))
+	 (case response
+	       (?r (notmuch-maildir-fcc-file-fcc fcc-header))
+	       (?c (if (file-writable-p fcc-header)
+		       (notmuch-maildir-fcc-create-maildir fcc-header)
+		     (message "No permission to create %s." fcc-header)
+		     (sit-for 2))
+		   (notmuch-maildir-fcc-file-fcc fcc-header))
+	       (?i 't)
+	       (?e (notmuch-maildir-fcc-file-fcc
+		    (read-from-minibuffer "Fcc header: " fcc-header)))))))
+
 (defun notmuch-maildir-fcc-write-buffer-to-maildir (destdir &optional mark-seen)
   "Writes the current buffer to maildir destdir. If mark-seen is
 non-nil, it will write it to cur/, and mark it as read. It should
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 3/6] emacs: maildir import message-do-fcc
  2016-06-10 23:02 [PATCH 0/6] Use notmuch insert for fcc Mark Walters
  2016-06-10 23:02 ` [PATCH 1/6] emacs: notmuch-check-exit-status bugfix Mark Walters
  2016-06-10 23:02 ` [PATCH 2/6] emacs: maildir-fcc: prepare for use of notmuch insert Mark Walters
@ 2016-06-10 23:02 ` Mark Walters
  2016-06-10 23:02 ` [PATCH 4/6] emacs: simplify our local copy of message-do-fcc Mark Walters
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 14+ messages in thread
From: Mark Walters @ 2016-06-10 23:02 UTC (permalink / raw)
  To: notmuch

We will need our own local copy of message-do-fcc so this commit just
copies the code straight from message.el so that it is easier to see
our local changes coming in the next commit.
---
 emacs/notmuch-maildir-fcc.el | 64 ++++++++++++++++++++++++++++++++++++++++++++
 emacs/notmuch-mua.el         |  7 +++--
 2 files changed, 69 insertions(+), 2 deletions(-)

diff --git a/emacs/notmuch-maildir-fcc.el b/emacs/notmuch-maildir-fcc.el
index 835258f..6fed11f 100644
--- a/emacs/notmuch-maildir-fcc.el
+++ b/emacs/notmuch-maildir-fcc.el
@@ -120,6 +120,70 @@ by notmuch-mua-mail"
 		subdir
 	      (concat (notmuch-database-path) "/" subdir))))))
 
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; Functions for saving a message either using notmuch insert or file
+;; fcc. First functions common to the two cases.
+
+(defun notmuch-maildir-message-do-fcc ()
+  "Process Fcc headers in the current buffer.
+
+This is a direct copy from message-mode's message-do-fcc."
+  (let ((case-fold-search t)
+	(buf (current-buffer))
+	list file
+	(mml-externalize-attachments message-fcc-externalize-attachments))
+    (save-excursion
+      (save-restriction
+	(message-narrow-to-headers)
+	(setq file (message-fetch-field "fcc" t)))
+      (when file
+	(set-buffer (get-buffer-create " *message temp*"))
+	(erase-buffer)
+	(insert-buffer-substring buf)
+	(message-encode-message-body)
+	(save-restriction
+	  (message-narrow-to-headers)
+	  (while (setq file (message-fetch-field "fcc" t))
+	    (push file list)
+	    (message-remove-header "fcc" nil t))
+	  (let ((mail-parse-charset message-default-charset)
+		(rfc2047-header-encoding-alist
+		 (cons '("Newsgroups" . default)
+		       rfc2047-header-encoding-alist)))
+	    (mail-encode-encoded-word-buffer)))
+	(goto-char (point-min))
+	(when (re-search-forward
+	       (concat "^" (regexp-quote mail-header-separator) "$")
+	       nil t)
+	  (replace-match "" t t ))
+	;; Process FCC operations.
+	(while list
+	  (setq file (pop list))
+	  (if (string-match "^[ \t]*|[ \t]*\\(.*\\)[ \t]*$" file)
+	      ;; Pipe the article to the program in question.
+	      (call-process-region (point-min) (point-max) shell-file-name
+				   nil nil nil shell-command-switch
+				   (match-string 1 file))
+	    ;; Save the article.
+	    (setq file (expand-file-name file))
+	    (unless (file-exists-p (file-name-directory file))
+	      (make-directory (file-name-directory file) t))
+	    (if (and message-fcc-handler-function
+		     (not (eq message-fcc-handler-function 'rmail-output)))
+		(funcall message-fcc-handler-function file)
+	      ;; FIXME this option, rmail-output (also used if
+	      ;; message-fcc-handler-function is nil) is not
+	      ;; documented anywhere AFAICS.  It should work in Emacs
+	      ;; 23; I suspect it does not work in Emacs 22.
+	      ;; FIXME I don't see the need for the two different cases here.
+	      ;; mail-use-rfc822 makes no difference (in Emacs 23),and
+	      ;; the third argument just controls \"Wrote file\" message.
+	      (if (and (file-readable-p file) (mail-file-babyl-p file))
+		  (rmail-output file 1 nil t)
+		(let ((mail-use-rfc822 t))
+		  (rmail-output file 1 t t))))))
+	(kill-buffer (current-buffer))))))
+
 (defun notmuch-fcc-handler (fcc-header)
   "Store message with file fcc."
   (notmuch-maildir-fcc-file-fcc fcc-header))
diff --git a/emacs/notmuch-mua.el b/emacs/notmuch-mua.el
index 1ca8056..3c1c046 100644
--- a/emacs/notmuch-mua.el
+++ b/emacs/notmuch-mua.el
@@ -33,6 +33,7 @@
 (declare-function notmuch-show-insert-body "notmuch-show" (msg body depth))
 (declare-function notmuch-fcc-header-setup "notmuch-maildir-fcc" ())
 (declare-function notmuch-fcc-handler "notmuch-maildir-fcc" (destdir))
+(declare-function notmuch-maildir-message-do-fcc "notmuch-maildir-fcc" ())
 
 ;;
 
@@ -491,12 +492,14 @@ will be addressed to all recipients of the source message."
 (defun notmuch-mua-send-and-exit (&optional arg)
   (interactive "P")
   (let ((message-fcc-handler-function #'notmuch-fcc-handler))
-    (message-send-and-exit arg)))
+    (letf (((symbol-function 'message-do-fcc) #'notmuch-maildir-message-do-fcc))
+	  (message-send-and-exit arg))))
 
 (defun notmuch-mua-send (&optional arg)
   (interactive "P")
   (let ((message-fcc-handler-function #'notmuch-fcc-handler))
-    (message-send arg)))
+    (letf (((symbol-function 'message-do-fcc) #'notmuch-maildir-message-do-fcc))
+	  (message-send arg))))
 
 (defun notmuch-mua-kill-buffer ()
   (interactive)
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 4/6] emacs: simplify our local copy of message-do-fcc
  2016-06-10 23:02 [PATCH 0/6] Use notmuch insert for fcc Mark Walters
                   ` (2 preceding siblings ...)
  2016-06-10 23:02 ` [PATCH 3/6] emacs: maildir import message-do-fcc Mark Walters
@ 2016-06-10 23:02 ` Mark Walters
  2016-09-03 20:20   ` David Bremner
  2016-06-10 23:02 ` [PATCH 5/6] Modify " Mark Walters
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 14+ messages in thread
From: Mark Walters @ 2016-06-10 23:02 UTC (permalink / raw)
  To: notmuch

message-do-fcc has lots of functionality we don't need, so remove it.
---
 emacs/notmuch-maildir-fcc.el | 24 +-----------------------
 emacs/notmuch-mua.el         | 11 ++++-------
 2 files changed, 5 insertions(+), 30 deletions(-)

diff --git a/emacs/notmuch-maildir-fcc.el b/emacs/notmuch-maildir-fcc.el
index 6fed11f..7b3f232 100644
--- a/emacs/notmuch-maildir-fcc.el
+++ b/emacs/notmuch-maildir-fcc.el
@@ -159,29 +159,7 @@ This is a direct copy from message-mode's message-do-fcc."
 	;; Process FCC operations.
 	(while list
 	  (setq file (pop list))
-	  (if (string-match "^[ \t]*|[ \t]*\\(.*\\)[ \t]*$" file)
-	      ;; Pipe the article to the program in question.
-	      (call-process-region (point-min) (point-max) shell-file-name
-				   nil nil nil shell-command-switch
-				   (match-string 1 file))
-	    ;; Save the article.
-	    (setq file (expand-file-name file))
-	    (unless (file-exists-p (file-name-directory file))
-	      (make-directory (file-name-directory file) t))
-	    (if (and message-fcc-handler-function
-		     (not (eq message-fcc-handler-function 'rmail-output)))
-		(funcall message-fcc-handler-function file)
-	      ;; FIXME this option, rmail-output (also used if
-	      ;; message-fcc-handler-function is nil) is not
-	      ;; documented anywhere AFAICS.  It should work in Emacs
-	      ;; 23; I suspect it does not work in Emacs 22.
-	      ;; FIXME I don't see the need for the two different cases here.
-	      ;; mail-use-rfc822 makes no difference (in Emacs 23),and
-	      ;; the third argument just controls \"Wrote file\" message.
-	      (if (and (file-readable-p file) (mail-file-babyl-p file))
-		  (rmail-output file 1 nil t)
-		(let ((mail-use-rfc822 t))
-		  (rmail-output file 1 t t))))))
+	  (notmuch-fcc-handler file))
 	(kill-buffer (current-buffer))))))
 
 (defun notmuch-fcc-handler (fcc-header)
diff --git a/emacs/notmuch-mua.el b/emacs/notmuch-mua.el
index 3c1c046..60ad6c3 100644
--- a/emacs/notmuch-mua.el
+++ b/emacs/notmuch-mua.el
@@ -32,7 +32,6 @@
 
 (declare-function notmuch-show-insert-body "notmuch-show" (msg body depth))
 (declare-function notmuch-fcc-header-setup "notmuch-maildir-fcc" ())
-(declare-function notmuch-fcc-handler "notmuch-maildir-fcc" (destdir))
 (declare-function notmuch-maildir-message-do-fcc "notmuch-maildir-fcc" ())
 
 ;;
@@ -491,15 +490,13 @@ will be addressed to all recipients of the source message."
 
 (defun notmuch-mua-send-and-exit (&optional arg)
   (interactive "P")
-  (let ((message-fcc-handler-function #'notmuch-fcc-handler))
-    (letf (((symbol-function 'message-do-fcc) #'notmuch-maildir-message-do-fcc))
-	  (message-send-and-exit arg))))
+  (letf (((symbol-function 'message-do-fcc) #'notmuch-maildir-message-do-fcc))
+	(message-send-and-exit arg)))
 
 (defun notmuch-mua-send (&optional arg)
   (interactive "P")
-  (let ((message-fcc-handler-function #'notmuch-fcc-handler))
-    (letf (((symbol-function 'message-do-fcc) #'notmuch-maildir-message-do-fcc))
-	  (message-send arg))))
+  (letf (((symbol-function 'message-do-fcc) #'notmuch-maildir-message-do-fcc))
+	(message-send arg)))
 
 (defun notmuch-mua-kill-buffer ()
   (interactive)
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 5/6] Modify our local copy of message-do-fcc
  2016-06-10 23:02 [PATCH 0/6] Use notmuch insert for fcc Mark Walters
                   ` (3 preceding siblings ...)
  2016-06-10 23:02 ` [PATCH 4/6] emacs: simplify our local copy of message-do-fcc Mark Walters
@ 2016-06-10 23:02 ` Mark Walters
  2016-09-03 20:33   ` David Bremner
  2016-06-10 23:02 ` [PATCH 6/6] emacs: maildir: add the actual insert code Mark Walters
  2016-09-03 20:43 ` [PATCH 0/6] Use notmuch insert for fcc David Bremner
  6 siblings, 1 reply; 14+ messages in thread
From: Mark Walters @ 2016-06-10 23:02 UTC (permalink / raw)
  To: notmuch

Since we also need to use this code for the draft handling we split
message-do-fcc into convenient sub-chunks (functions or macros as
appropriate).
---
 emacs/notmuch-maildir-fcc.el | 71 ++++++++++++++++++++++++++------------------
 1 file changed, 42 insertions(+), 29 deletions(-)

diff --git a/emacs/notmuch-maildir-fcc.el b/emacs/notmuch-maildir-fcc.el
index 7b3f232..fc645c5 100644
--- a/emacs/notmuch-maildir-fcc.el
+++ b/emacs/notmuch-maildir-fcc.el
@@ -124,43 +124,56 @@ by notmuch-mua-mail"
 ;; Functions for saving a message either using notmuch insert or file
 ;; fcc. First functions common to the two cases.
 
+(defmacro with-temporary-notmuch-message-buffer (&rest body)
+  "Set-up a temporary copy of the current message-mode buffer."
+  `(let ((case-fold-search t)
+	 (buf (current-buffer))
+	 (mml-externalize-attachments message-fcc-externalize-attachments))
+     (with-current-buffer (get-buffer-create " *message temp*")
+       (erase-buffer)
+       (insert-buffer-substring buf)
+       ,@body)))
+
+(defun notmuch-maildir-setup-message-for-saving ()
+  "Setup message for saving. Should be called on a temporary copy.
+
+This is taken from the function message-do-fcc."
+  (message-encode-message-body)
+  (save-restriction
+    (message-narrow-to-headers)
+    (let ((mail-parse-charset message-default-charset)
+	  (rfc2047-header-encoding-alist
+	   (cons '("Newsgroups" . default)
+		 rfc2047-header-encoding-alist)))
+      (mail-encode-encoded-word-buffer)))
+  (goto-char (point-min))
+  (when (re-search-forward
+	 (concat "^" (regexp-quote mail-header-separator) "$")
+	 nil t)
+    (replace-match "" t t )))
+
 (defun notmuch-maildir-message-do-fcc ()
   "Process Fcc headers in the current buffer.
 
-This is a direct copy from message-mode's message-do-fcc."
-  (let ((case-fold-search t)
-	(buf (current-buffer))
-	list file
-	(mml-externalize-attachments message-fcc-externalize-attachments))
+This is a rearranged version of message mode's message-do-fcc."
+  (let (list file)
     (save-excursion
       (save-restriction
 	(message-narrow-to-headers)
 	(setq file (message-fetch-field "fcc" t)))
       (when file
-	(set-buffer (get-buffer-create " *message temp*"))
-	(erase-buffer)
-	(insert-buffer-substring buf)
-	(message-encode-message-body)
-	(save-restriction
-	  (message-narrow-to-headers)
-	  (while (setq file (message-fetch-field "fcc" t))
-	    (push file list)
-	    (message-remove-header "fcc" nil t))
-	  (let ((mail-parse-charset message-default-charset)
-		(rfc2047-header-encoding-alist
-		 (cons '("Newsgroups" . default)
-		       rfc2047-header-encoding-alist)))
-	    (mail-encode-encoded-word-buffer)))
-	(goto-char (point-min))
-	(when (re-search-forward
-	       (concat "^" (regexp-quote mail-header-separator) "$")
-	       nil t)
-	  (replace-match "" t t ))
-	;; Process FCC operations.
-	(while list
-	  (setq file (pop list))
-	  (notmuch-fcc-handler file))
-	(kill-buffer (current-buffer))))))
+	(with-temporary-notmuch-message-buffer
+	 (save-restriction
+	   (message-narrow-to-headers)
+	   (while (setq file (message-fetch-field "fcc" t))
+	     (push file list)
+	     (message-remove-header "fcc" nil t)))
+	 (notmuch-maildir-setup-message-for-saving)
+	 ;; Process FCC operations.
+	 (while list
+	   (setq file (pop list))
+	   (notmuch-fcc-handler file))
+	 (kill-buffer (current-buffer)))))))
 
 (defun notmuch-fcc-handler (fcc-header)
   "Store message with file fcc."
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 6/6] emacs: maildir: add the actual insert code
  2016-06-10 23:02 [PATCH 0/6] Use notmuch insert for fcc Mark Walters
                   ` (4 preceding siblings ...)
  2016-06-10 23:02 ` [PATCH 5/6] Modify " Mark Walters
@ 2016-06-10 23:02 ` Mark Walters
  2016-09-03 20:43 ` [PATCH 0/6] Use notmuch insert for fcc David Bremner
  6 siblings, 0 replies; 14+ messages in thread
From: Mark Walters @ 2016-06-10 23:02 UTC (permalink / raw)
  To: notmuch

With all the preparation it is now simple to add the actual insert
code. Since insert can fail for many reasons we let the user decide
interactively deal with it.

We modify test-lib.el to set file fcc, so that all the old tests and
emacs_fcc_message from test-lib.sh still work
---
 emacs/notmuch-maildir-fcc.el | 74 ++++++++++++++++++++++++++++++++++++++++++--
 test/test-lib.el             |  5 +++
 2 files changed, 76 insertions(+), 3 deletions(-)

diff --git a/emacs/notmuch-maildir-fcc.el b/emacs/notmuch-maildir-fcc.el
index fc645c5..db75d0d 100644
--- a/emacs/notmuch-maildir-fcc.el
+++ b/emacs/notmuch-maildir-fcc.el
@@ -65,6 +65,12 @@ yet when sending a mail."
  :require 'notmuch-fcc-initialization
  :group 'notmuch-send)
 
+(defcustom notmuch-maildir-use-notmuch-insert 't
+  "Should fcc use notmuch insert instead of simple fcc"
+  :type '(choice :tag "Fcc Method"
+		 (const :tag "Use notmuch insert" t)
+		 (const :tag "Use simple fcc" nil))
+  :group 'notmuch-send)
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Functions which set up the fcc header in the message buffer.
@@ -108,7 +114,18 @@ by notmuch-mua-mail"
 	   (error "Invalid `notmuch-fcc-dirs' setting (neither string nor list)")))))
 
     (when subdir
-      (notmuch-maildir-add-file-style-fcc-header subdir))))
+      (if notmuch-maildir-use-notmuch-insert
+	  (notmuch-maildir-add-notmuch-insert-style-fcc-header subdir)
+	(notmuch-maildir-add-file-style-fcc-header subdir)))))
+
+(defun notmuch-maildir-add-notmuch-insert-style-fcc-header (subdir)
+  ;; Notmuch insert does not accept absolute paths, so check the user
+  ;; really want this header inserted.
+
+  (when (or (not (= (elt subdir 0) ?/))
+	    (y-or-n-p (format "Fcc header %s is an absolute path and notmuch insert is requested.\nInsert header anyway? "
+			      subdir)))
+    (message-add-header (concat "Fcc: " subdir))))
 
 (defun notmuch-maildir-add-file-style-fcc-header (subdir)
   (message-add-header
@@ -176,8 +193,59 @@ This is a rearranged version of message mode's message-do-fcc."
 	 (kill-buffer (current-buffer)))))))
 
 (defun notmuch-fcc-handler (fcc-header)
-  "Store message with file fcc."
-  (notmuch-maildir-fcc-file-fcc fcc-header))
+  "Store message with notmuch insert or normal (file) fcc.
+
+If `notmuch-maildir-use-notmuch-insert` is set then store the
+message using notmuch insert. Otherwise store the message using
+normal fcc."
+  (if notmuch-maildir-use-notmuch-insert
+      (notmuch-maildir-fcc-with-notmuch-insert fcc-header)
+    (notmuch-maildir-fcc-file-fcc fcc-header)))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; Functions for saving a message using notmuch insert.
+
+(defun notmuch-maildir-notmuch-insert-current-buffer (folder &optional create tags)
+  "Use notmuch insert to put the current buffer in the database.
+
+This inserts the current buffer as a message into the notmuch
+database in folder FOLDER. If CREATE is non-nil it will supply
+the --create-folder flag to create the folder if necessary. TAGS
+should be a list of tag changes to apply to the inserted message."
+  (let* ((args (append (when create (list "--create-folder"))
+		       (list (concat "--folder=" folder))
+		       tags)))
+    (apply 'notmuch-call-notmuch-process
+	   :stdin-string (buffer-string) "insert" args)))
+
+(defun notmuch-maildir-fcc-with-notmuch-insert (fcc-header &optional create)
+  "Store message with notmuch insert.
+
+The fcc-header should be of the form \"folder +tag1 -tag2\" where
+folder is the folder (relative to the notmuch mailstore) to store
+the message in, and tag1 and tag2 are tag changes to apply to the
+stored message. If CREATE is non-nil then create the folder if
+necessary."
+  (let* ((args (split-string-and-unquote fcc-header))
+	 (folder (car args))
+	 (tags (cdr args)))
+    (condition-case nil
+	(notmuch-maildir-notmuch-insert-current-buffer folder create tags)
+      ;; Since there are many reasons notmuch insert could fail, e.g.,
+      ;; locked database, non-existent folder (which could be due to a
+      ;; typo, or just the user want a new folder, let the user decide
+      ;; how to deal with it.
+      (error
+       (let ((response (read-char-choice
+			"Insert failed: (r)etry, (c)reate folder, (i)gnore, or  (e)dit the header? "
+			'(?r ?c ?i ?e))))
+	 (case response
+	       (?r (notmuch-maildir-fcc-with-notmuch-insert fcc-header))
+	       (?c (notmuch-maildir-fcc-with-notmuch-insert fcc-header 't))
+	       (?i 't)
+	       (?e (notmuch-maildir-fcc-with-notmuch-insert
+		    (read-from-minibuffer "Fcc header: " fcc-header)))))))))
+
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Functions for saving a message using file fcc.
diff --git a/test/test-lib.el b/test/test-lib.el
index 496626f..9946010 100644
--- a/test/test-lib.el
+++ b/test/test-lib.el
@@ -184,6 +184,11 @@ nothing."
 (setq notmuch-tag-deleted-formats
       '((".*" nil)))
 
+;; Also for historical reasons, we set the fcc handler to file not
+;; insert.
+
+(setq notmuch-maildir-use-notmuch-insert nil)
+
 ;; force a common html renderer, to avoid test variations between
 ;; environments
 
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCH 4/6] emacs: simplify our local copy of message-do-fcc
  2016-06-10 23:02 ` [PATCH 4/6] emacs: simplify our local copy of message-do-fcc Mark Walters
@ 2016-09-03 20:20   ` David Bremner
  2016-09-03 21:42     ` Mark Walters
  0 siblings, 1 reply; 14+ messages in thread
From: David Bremner @ 2016-09-03 20:20 UTC (permalink / raw)
  To: Mark Walters, notmuch

Mark Walters <markwalters1009@gmail.com> writes:

> message-do-fcc has lots of functionality we don't need, so remove it.
> ---

It seems like the docstring should be updated?

d

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 5/6] Modify our local copy of message-do-fcc
  2016-06-10 23:02 ` [PATCH 5/6] Modify " Mark Walters
@ 2016-09-03 20:33   ` David Bremner
  2016-09-03 21:39     ` Mark Walters
  0 siblings, 1 reply; 14+ messages in thread
From: David Bremner @ 2016-09-03 20:33 UTC (permalink / raw)
  To: Mark Walters, notmuch

Mark Walters <markwalters1009@gmail.com> writes:

> +	   (cons '("Newsgroups" . default)
> +		 rfc2047-header-encoding-alist)))
  this seems like more message-mode functionality we don't need? I guess
  it's harmless, but could omit one dynamically bound variable.
  
> +	 (while list
> +	   (setq file (pop list))
> +	   (notmuch-fcc-handler file))
> +	 (kill-buffer (current-buffer)))))))

Is it sensible for us to support a list of output destinations? It seems
like an odd corner case to me. I guess the amount of extra code is not
too high. In any case I suppose it could be a followup (as could my
comment above).

d

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 0/6] Use notmuch insert for fcc
  2016-06-10 23:02 [PATCH 0/6] Use notmuch insert for fcc Mark Walters
                   ` (5 preceding siblings ...)
  2016-06-10 23:02 ` [PATCH 6/6] emacs: maildir: add the actual insert code Mark Walters
@ 2016-09-03 20:43 ` David Bremner
  6 siblings, 0 replies; 14+ messages in thread
From: David Bremner @ 2016-09-03 20:43 UTC (permalink / raw)
  To: Mark Walters, notmuch

Mark Walters <markwalters1009@gmail.com> writes:

> This series allows the use of notmuch insert for fcc. It is rather
> larger than I would like but I am fairly happy with the user
> experience. A defcustom controls whether to use notmuch insert or file
> fcc.

Overall this series looks good. I made a few minor comments, but I think
it's all optional, so let me know if you want to polish it, or I should
just merge.

d

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 5/6] Modify our local copy of message-do-fcc
  2016-09-03 20:33   ` David Bremner
@ 2016-09-03 21:39     ` Mark Walters
  2016-09-03 22:37       ` David Bremner
  0 siblings, 1 reply; 14+ messages in thread
From: Mark Walters @ 2016-09-03 21:39 UTC (permalink / raw)
  To: David Bremner, notmuch


Many thanks for the review.

On Sat, 03 Sep 2016, David Bremner <david@tethera.net> wrote:
> Mark Walters <markwalters1009@gmail.com> writes:
>
>> +	   (cons '("Newsgroups" . default)
>> +		 rfc2047-header-encoding-alist)))
>   this seems like more message-mode functionality we don't need? I guess
>   it's harmless, but could omit one dynamically bound variable.

I agree this might as well be removed.

>> +	 (while list
>> +	   (setq file (pop list))
>> +	   (notmuch-fcc-handler file))
>> +	 (kill-buffer (current-buffer)))))))
>
> Is it sensible for us to support a list of output destinations? It seems
> like an odd corner case to me. I guess the amount of extra code is not
> too high. In any case I suppose it could be a followup (as could my
> comment above).

We get a list here if someone has more than one fcc header. I don't know
why they would want to, but gnus supported it so I kept it. I am happy
either way here.

Best wishes

Mark

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 4/6] emacs: simplify our local copy of message-do-fcc
  2016-09-03 20:20   ` David Bremner
@ 2016-09-03 21:42     ` Mark Walters
  2016-09-03 22:36       ` David Bremner
  0 siblings, 1 reply; 14+ messages in thread
From: Mark Walters @ 2016-09-03 21:42 UTC (permalink / raw)
  To: David Bremner, notmuch


On Sat, 03 Sep 2016, David Bremner <david@tethera.net> wrote:
> Mark Walters <markwalters1009@gmail.com> writes:
>
>> message-do-fcc has lots of functionality we don't need, so remove it.
>> ---
>
> It seems like the docstring should be updated?


Yes, you are quite right. It does get updated in the next patch; I
should move that change here. Is the wording of the later docstring
suitable? 

Best wishes

Mark

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 4/6] emacs: simplify our local copy of message-do-fcc
  2016-09-03 21:42     ` Mark Walters
@ 2016-09-03 22:36       ` David Bremner
  0 siblings, 0 replies; 14+ messages in thread
From: David Bremner @ 2016-09-03 22:36 UTC (permalink / raw)
  To: Mark Walters, notmuch

Mark Walters <markwalters1009@gmail.com> writes:

> On Sat, 03 Sep 2016, David Bremner <david@tethera.net> wrote:
>> Mark Walters <markwalters1009@gmail.com> writes:
>>
>>> message-do-fcc has lots of functionality we don't need, so remove it.
>>> ---
>>
>> It seems like the docstring should be updated?
>
>
> Yes, you are quite right. It does get updated in the next patch; I
> should move that change here. Is the wording of the later docstring
> suitable? 

Sure.

d

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 5/6] Modify our local copy of message-do-fcc
  2016-09-03 21:39     ` Mark Walters
@ 2016-09-03 22:37       ` David Bremner
  0 siblings, 0 replies; 14+ messages in thread
From: David Bremner @ 2016-09-03 22:37 UTC (permalink / raw)
  To: Mark Walters, notmuch

Mark Walters <markwalters1009@gmail.com> writes:

> Many thanks for the review.
>
> On Sat, 03 Sep 2016, David Bremner <david@tethera.net> wrote:
>> Mark Walters <markwalters1009@gmail.com> writes:
>>
>>> +	   (cons '("Newsgroups" . default)
>>> +		 rfc2047-header-encoding-alist)))
>>   this seems like more message-mode functionality we don't need? I guess
>>   it's harmless, but could omit one dynamically bound variable.
>
> I agree this might as well be removed.
>
>>> +	 (while list
>>> +	   (setq file (pop list))
>>> +	   (notmuch-fcc-handler file))
>>> +	 (kill-buffer (current-buffer)))))))
>>
>> Is it sensible for us to support a list of output destinations? It seems
>> like an odd corner case to me. I guess the amount of extra code is not
>> too high. In any case I suppose it could be a followup (as could my
>> comment above).
>
> We get a list here if someone has more than one fcc header. I don't know
> why they would want to, but gnus supported it so I kept it. I am happy
> either way here.

Hmm. OK. let's leave it as long as it doesn't cause any problems

d

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2016-09-03 22:37 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-06-10 23:02 [PATCH 0/6] Use notmuch insert for fcc Mark Walters
2016-06-10 23:02 ` [PATCH 1/6] emacs: notmuch-check-exit-status bugfix Mark Walters
2016-06-10 23:02 ` [PATCH 2/6] emacs: maildir-fcc: prepare for use of notmuch insert Mark Walters
2016-06-10 23:02 ` [PATCH 3/6] emacs: maildir import message-do-fcc Mark Walters
2016-06-10 23:02 ` [PATCH 4/6] emacs: simplify our local copy of message-do-fcc Mark Walters
2016-09-03 20:20   ` David Bremner
2016-09-03 21:42     ` Mark Walters
2016-09-03 22:36       ` David Bremner
2016-06-10 23:02 ` [PATCH 5/6] Modify " Mark Walters
2016-09-03 20:33   ` David Bremner
2016-09-03 21:39     ` Mark Walters
2016-09-03 22:37       ` David Bremner
2016-06-10 23:02 ` [PATCH 6/6] emacs: maildir: add the actual insert code Mark Walters
2016-09-03 20:43 ` [PATCH 0/6] Use notmuch insert for fcc David Bremner

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).