--- /dev/null
+Return-Path: <markwalters1009@gmail.com>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by arlo.cworth.org (Postfix) with ESMTP id 598816DE0317\r
+ for <notmuch@notmuchmail.org>; Fri, 10 Jun 2016 16:04:28 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.338\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.338 tagged_above=-999 required=5 tests=[AWL=0.232,\r
+ DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
+ FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7,\r
+ RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_PASS=-0.001]\r
+ autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id Qxp3W1hToNeb for <notmuch@notmuchmail.org>;\r
+ Fri, 10 Jun 2016 16:04:20 -0700 (PDT)\r
+Received: from mail-wm0-f66.google.com (mail-wm0-f66.google.com\r
+ [74.125.82.66]) by arlo.cworth.org (Postfix) with ESMTPS id 3345B6DE0318 for\r
+ <notmuch@notmuchmail.org>; Fri, 10 Jun 2016 16:03:13 -0700 (PDT)\r
+Received: by mail-wm0-f66.google.com with SMTP id k184so1702398wme.2\r
+ for <notmuch@notmuchmail.org>; Fri, 10 Jun 2016 16:03:13 -0700 (PDT)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
+ h=from:to:cc:subject:date:message-id:in-reply-to:references;\r
+ bh=0t6RLAXXMYxS1mMewpk/jgqFS1PUEdeWY4LIYuwdcBo=;\r
+ b=bZhbzPtU9pvvfbEAyI5uakjJCQGDTTtc4To2kC26Cj/N07MzFBGZXqVvmxpd8VIBKR\r
+ NRijd6jQGSQ0pctUm2VKSpHyq35oHiodbgqar0BZkn4cycP4YEMRC8pUF+vBUyDCu4tF\r
+ mv9YZKiLQOELBSdRSGDu1wFTrdDbncskg6ObqMBj/O2cag/dAkHjaNveaNzBqKOvzP+T\r
+ D3t3SOqfQQZUYoZRl2C3AmrhJKzBd0hlVE7hIBf+KNejiQNtH4NyqnaGkaQyPHh2Kt+G\r
+ 3qH4nCKY/GKpznng4ddsslmOOe/3gCV6GohMDIZr1zrIbwqf0Lko1uiFN/QzyF6Doj5X\r
+ LF6w==\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+ d=1e100.net; s=20130820;\r
+ h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\r
+ :references;\r
+ bh=0t6RLAXXMYxS1mMewpk/jgqFS1PUEdeWY4LIYuwdcBo=;\r
+ b=K+v1ovbOxziKreE00fhzI0Zhqwkg3rAHMcPIhcpmY53ACid/9ZbV0qOyHQW1zkDRYd\r
+ BWZxcvm5aD85AJLh8mDj4JsqDtzP+AREE5AMF+qDhIQ/Rbx+6xCZirdAF08Ns4idCeUC\r
+ eSqWPELSvJdeGsGZr0TxSx59vI9pdG1smQk8AZun1z2p4Pc9WILaUgEqoD+3wF3/vN3X\r
+ 2hS/V3oH2Wt2Nx574rCFcOUnrVCQuwr/uTUEjarus2oeD1vMYPpEfuQP5n6Gmr7do0dJ\r
+ OiHetjKPF32U/MoKoK4VKE2Ao0ftQ3nFA5EnYptPI6R3T06cmfZ4PaxappdMNUhb6rKl\r
+ mEgQ==\r
+X-Gm-Message-State:\r
+ ALyK8tL4DLlblpr/N70w4RE3HmoJd6GwINzvoexFOkEz6ufYVwisfex3wGJ2sb2NVOhx4g==\r
+X-Received: by 10.194.162.168 with SMTP id yb8mr4623834wjb.32.1465599791798;\r
+ Fri, 10 Jun 2016 16:03:11 -0700 (PDT)\r
+Received: from localhost (5751dfa2.skybroadband.com. [87.81.223.162])\r
+ by smtp.gmail.com with ESMTPSA id j2sm6952404wjz.15.2016.06.10.16.03.11\r
+ (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);\r
+ Fri, 10 Jun 2016 16:03:11 -0700 (PDT)\r
+From: Mark Walters <markwalters1009@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 6/6] emacs: maildir: add the actual insert code\r
+Date: Sat, 11 Jun 2016 00:02:52 +0100\r
+Message-Id: <1465599772-10297-7-git-send-email-markwalters1009@gmail.com>\r
+X-Mailer: git-send-email 2.1.4\r
+In-Reply-To: <1465599772-10297-1-git-send-email-markwalters1009@gmail.com>\r
+References: <1465599772-10297-1-git-send-email-markwalters1009@gmail.com>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.20\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Fri, 10 Jun 2016 23:04:28 -0000\r
+\r
+With all the preparation it is now simple to add the actual insert\r
+code. Since insert can fail for many reasons we let the user decide\r
+interactively deal with it.\r
+\r
+We modify test-lib.el to set file fcc, so that all the old tests and\r
+emacs_fcc_message from test-lib.sh still work\r
+---\r
+ emacs/notmuch-maildir-fcc.el | 74 ++++++++++++++++++++++++++++++++++++++++++--\r
+ test/test-lib.el | 5 +++\r
+ 2 files changed, 76 insertions(+), 3 deletions(-)\r
+\r
+diff --git a/emacs/notmuch-maildir-fcc.el b/emacs/notmuch-maildir-fcc.el\r
+index fc645c5..db75d0d 100644\r
+--- a/emacs/notmuch-maildir-fcc.el\r
++++ b/emacs/notmuch-maildir-fcc.el\r
+@@ -65,6 +65,12 @@ yet when sending a mail."\r
+ :require 'notmuch-fcc-initialization\r
+ :group 'notmuch-send)\r
+ \r
++(defcustom notmuch-maildir-use-notmuch-insert 't\r
++ "Should fcc use notmuch insert instead of simple fcc"\r
++ :type '(choice :tag "Fcc Method"\r
++ (const :tag "Use notmuch insert" t)\r
++ (const :tag "Use simple fcc" nil))\r
++ :group 'notmuch-send)\r
+ \r
+ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\r
+ ;; Functions which set up the fcc header in the message buffer.\r
+@@ -108,7 +114,18 @@ by notmuch-mua-mail"\r
+ (error "Invalid `notmuch-fcc-dirs' setting (neither string nor list)")))))\r
+ \r
+ (when subdir\r
+- (notmuch-maildir-add-file-style-fcc-header subdir))))\r
++ (if notmuch-maildir-use-notmuch-insert\r
++ (notmuch-maildir-add-notmuch-insert-style-fcc-header subdir)\r
++ (notmuch-maildir-add-file-style-fcc-header subdir)))))\r
++\r
++(defun notmuch-maildir-add-notmuch-insert-style-fcc-header (subdir)\r
++ ;; Notmuch insert does not accept absolute paths, so check the user\r
++ ;; really want this header inserted.\r
++\r
++ (when (or (not (= (elt subdir 0) ?/))\r
++ (y-or-n-p (format "Fcc header %s is an absolute path and notmuch insert is requested.\nInsert header anyway? "\r
++ subdir)))\r
++ (message-add-header (concat "Fcc: " subdir))))\r
+ \r
+ (defun notmuch-maildir-add-file-style-fcc-header (subdir)\r
+ (message-add-header\r
+@@ -176,8 +193,59 @@ This is a rearranged version of message mode's message-do-fcc."\r
+ (kill-buffer (current-buffer)))))))\r
+ \r
+ (defun notmuch-fcc-handler (fcc-header)\r
+- "Store message with file fcc."\r
+- (notmuch-maildir-fcc-file-fcc fcc-header))\r
++ "Store message with notmuch insert or normal (file) fcc.\r
++\r
++If `notmuch-maildir-use-notmuch-insert` is set then store the\r
++message using notmuch insert. Otherwise store the message using\r
++normal fcc."\r
++ (if notmuch-maildir-use-notmuch-insert\r
++ (notmuch-maildir-fcc-with-notmuch-insert fcc-header)\r
++ (notmuch-maildir-fcc-file-fcc fcc-header)))\r
++\r
++;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\r
++;; Functions for saving a message using notmuch insert.\r
++\r
++(defun notmuch-maildir-notmuch-insert-current-buffer (folder &optional create tags)\r
++ "Use notmuch insert to put the current buffer in the database.\r
++\r
++This inserts the current buffer as a message into the notmuch\r
++database in folder FOLDER. If CREATE is non-nil it will supply\r
++the --create-folder flag to create the folder if necessary. TAGS\r
++should be a list of tag changes to apply to the inserted message."\r
++ (let* ((args (append (when create (list "--create-folder"))\r
++ (list (concat "--folder=" folder))\r
++ tags)))\r
++ (apply 'notmuch-call-notmuch-process\r
++ :stdin-string (buffer-string) "insert" args)))\r
++\r
++(defun notmuch-maildir-fcc-with-notmuch-insert (fcc-header &optional create)\r
++ "Store message with notmuch insert.\r
++\r
++The fcc-header should be of the form \"folder +tag1 -tag2\" where\r
++folder is the folder (relative to the notmuch mailstore) to store\r
++the message in, and tag1 and tag2 are tag changes to apply to the\r
++stored message. If CREATE is non-nil then create the folder if\r
++necessary."\r
++ (let* ((args (split-string-and-unquote fcc-header))\r
++ (folder (car args))\r
++ (tags (cdr args)))\r
++ (condition-case nil\r
++ (notmuch-maildir-notmuch-insert-current-buffer folder create tags)\r
++ ;; Since there are many reasons notmuch insert could fail, e.g.,\r
++ ;; locked database, non-existent folder (which could be due to a\r
++ ;; typo, or just the user want a new folder, let the user decide\r
++ ;; how to deal with it.\r
++ (error\r
++ (let ((response (read-char-choice\r
++ "Insert failed: (r)etry, (c)reate folder, (i)gnore, or (e)dit the header? "\r
++ '(?r ?c ?i ?e))))\r
++ (case response\r
++ (?r (notmuch-maildir-fcc-with-notmuch-insert fcc-header))\r
++ (?c (notmuch-maildir-fcc-with-notmuch-insert fcc-header 't))\r
++ (?i 't)\r
++ (?e (notmuch-maildir-fcc-with-notmuch-insert\r
++ (read-from-minibuffer "Fcc header: " fcc-header)))))))))\r
++\r
+ \r
+ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\r
+ ;; Functions for saving a message using file fcc.\r
+diff --git a/test/test-lib.el b/test/test-lib.el\r
+index 496626f..9946010 100644\r
+--- a/test/test-lib.el\r
++++ b/test/test-lib.el\r
+@@ -184,6 +184,11 @@ nothing."\r
+ (setq notmuch-tag-deleted-formats\r
+ '((".*" nil)))\r
+ \r
++;; Also for historical reasons, we set the fcc handler to file not\r
++;; insert.\r
++\r
++(setq notmuch-maildir-use-notmuch-insert nil)\r
++\r
+ ;; force a common html renderer, to avoid test variations between\r
+ ;; environments\r
+ \r
+-- \r
+2.1.4\r
+\r