unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* Fcc, Maildir, and Emacs message-mode -- a bit of code
@ 2010-01-21 18:36 Jesse Rosenthal
  2010-01-27 14:44 ` Jameson Rollins
  2010-04-22  9:06 ` Sebastian Spaeth
  0 siblings, 2 replies; 35+ messages in thread
From: Jesse Rosenthal @ 2010-01-21 18:36 UTC (permalink / raw)
  To: notmuch


Dear all,

First of all, many thanks to Carl and others for writing notmuch.

Some folks on IRC were bemoaning message-mode's annoying inability to
save sent-mail to a Maildir using Fcc. I mentioned that I had written a
bit of Maildir elisp code for that purpose a while back, and it was
suggested that I share it with the list.

Some caveats:

- I've tested this a good number of times, but not very robustly, since
  I don't actually use it (I found that I prefer blind-copying
  myself). Please test it out somewhere safe, on a maildir you can
  afford to lose.

- The hardlinking performed by `add-name-to-file' might be platform and
  filesystem specific. I'm using linux/ext3, and it works here. I don't
  know what will happen anywhere else.

- I imagine there's a much better implementation inside of Wanderlust,
  if you can dig through all the libraries.

- It's not really commented, but I hope the function names are pretty
  self-explanatory.

- It requires that the directory in the `Fcc:' header already exist and
  be a maildir (i.e. have cur/, new/, and tmp/). It should be pretty
  simple to add in a prompt for creating a directory if it points to a
  nonexistent place.

Anyway, here it is:

http://jkr.acm.jhu.edu/jkr-maildir.el

To use it, set one of the following:

If you want Fcc'd messages to be marked as read:

     (setq message-fcc-handler-function 
          '(lambda (destdir) 
	     (jkr/maildir-write-buffer-to-maildir destdir t)))

If you want Fcc'd messages to be marked as new:

     (setq message-fcc-handler-function 
          '(lambda (destdir) 
	     (jkr/maildir-write-buffer-to-maildir destdir nil)))


Best,
Jesse

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

* Re: Fcc, Maildir, and Emacs message-mode -- a bit of code
  2010-01-21 18:36 Fcc, Maildir, and Emacs message-mode -- a bit of code Jesse Rosenthal
@ 2010-01-27 14:44 ` Jameson Rollins
  2010-01-29 13:21   ` Sebastian Spaeth
  2010-04-07 20:04   ` Dirk Hohndel
  2010-04-22  9:06 ` Sebastian Spaeth
  1 sibling, 2 replies; 35+ messages in thread
From: Jameson Rollins @ 2010-01-27 14:44 UTC (permalink / raw)
  To: notmuch


[-- Attachment #1.1: Type: text/plain, Size: 929 bytes --]

Hey, folks.  Following up on this thread about better fcc handling,
Jesse passed on a simple python script he wrote that uses the python
"mailbox" module to deliver a message on stdin to a specified maildir
directory.  It's very a simple, elegant and general purpose script
(attached).

I then put the following in my notmuch .emacs to use the new script in
message-mode to fcc sent mail to my ~/.mail/sent directory:

;; fcc handler
(defun maildir-deliver-region(destdir)
  (shell-command-on-region
   (point-min) (point-max)
   (concat "maildir-deliver.py -c -s -d " destdir)))
(setq message-fcc-handler-function 'maildir-deliver-region)
(defun my-message-header-setup ()
  (message-add-header "Fcc: ~/.mail/sent"))
(add-hook 'message-send-hook 'my-message-header-setup)

Works like a charm.  Thanks Jesse!

I think we should look at packaging this in a set of notmuch helper
scripts, hopefully including notmuchsync.

jamie.


[-- Attachment #1.2: Type: application/pgp-signature, Size: 835 bytes --]

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: python maildir delivery script --]
[-- Type: text/x-python, Size: 1090 bytes --]

#!/usr/bin/env python

import mailbox
import sys
import optparse

def maildir_deliver(msg, maildir, mark_read=False, mark_cur=False):
    if mark_read:
        msg.set_flags("S")
    if mark_cur:
        msg.set_subdir('cur')
    md = mailbox.Maildir(maildir)
    key = md.add(msg)
    md.close

if __name__ == '__main__':
    parser = optparse.OptionParser()
    parser.add_option("-d", "--destdir",
                      dest="maildir", 
                      help="destination maildir")
    parser.add_option("-s", "--seen",
                      action="store_true", 
                      dest="mark_read",
                      default=False,
                      help="mark message as read")
    parser.add_option("-c", "--cur",
                      action="store_true", 
                      dest="mark_cur",
                      default=False,
                      help="deliver message to cur instead of new")
    (options, args) = parser.parse_args()
    msg = mailbox.MaildirMessage(sys.stdin)
    maildir_deliver(msg, options.maildir, options.mark_read, options.mark_cur)

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

* Re: Fcc, Maildir, and Emacs message-mode -- a bit of code
  2010-01-27 14:44 ` Jameson Rollins
@ 2010-01-29 13:21   ` Sebastian Spaeth
  2010-01-29 14:46     ` Jameson Rollins
  2010-01-29 14:54     ` Jesse Rosenthal
  2010-04-07 20:04   ` Dirk Hohndel
  1 sibling, 2 replies; 35+ messages in thread
From: Sebastian Spaeth @ 2010-01-29 13:21 UTC (permalink / raw)
  To: Jameson Rollins, notmuch

First, I think this FCC method is phantastic and it works like a
charm. I agree that we should include the snippets at least in some
"contrib" directory (or doc/examples) to make it easier to find.

Jameson Rollins <jrollins@finestructure.net> wrote:
> (defun my-message-header-setup ()
>   (message-add-header "Fcc: ~/.mail/sent"))
> (add-hook 'message-send-hook 'my-message-header-setup)

I tried to replace that snippet with:

(setq message-default-mail-headers "Fcc:  ~/.mail/sent\n")

because I like to see the Fcc header when composing, but this only works
for new messages (via ctrl-x m) and not when invoked with "r"(eply) on
an existing message in notmuch.

Is message-default-mail-headers only invoked for completely new mails?
The message mode manual doesn't state anything about that.

And I agree with JRollins patch to not add the BCC to myself from
notmuch.el by default. Either make it a notmuch option, or specify how to
set up a default BCC in emacs in the docs.

Sebastian

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

* Re: Fcc, Maildir, and Emacs message-mode -- a bit of code
  2010-01-29 13:21   ` Sebastian Spaeth
@ 2010-01-29 14:46     ` Jameson Rollins
  2010-01-29 14:54     ` Jesse Rosenthal
  1 sibling, 0 replies; 35+ messages in thread
From: Jameson Rollins @ 2010-01-29 14:46 UTC (permalink / raw)
  To: Sebastian Spaeth, notmuch

[-- Attachment #1: Type: text/plain, Size: 1794 bytes --]

On Fri, 29 Jan 2010 14:21:56 +0100, "Sebastian Spaeth" <Sebastian@SSpaeth.de> wrote:
> First, I think this FCC method is phantastic and it works like a
> charm. I agree that we should include the snippets at least in some
> "contrib" directory (or doc/examples) to make it easier to find.

I'm really glad this was useful.  Thanks of course to Jesse, who
initially figured all this out.

I'm pretty convinced that soon enough we'll have a new packages worth of
notmuch helper functions and scripts.

> Jameson Rollins <jrollins@finestructure.net> wrote:
> > (defun my-message-header-setup ()
> >   (message-add-header "Fcc: ~/.mail/sent"))
> > (add-hook 'message-send-hook 'my-message-header-setup)
> 
> I tried to replace that snippet with:
> 
> (setq message-default-mail-headers "Fcc:  ~/.mail/sent\n")
>
> because I like to see the Fcc header when composing, but this only works
> for new messages (via ctrl-x m) and not when invoked with "r"(eply) on
> an existing message in notmuch.
>
> Is message-default-mail-headers only invoked for completely new mails?
> The message mode manual doesn't state anything about that.

Yeah, I started out doing that as well, but I tried every permutation of
setup hooks I could think of and couldn't get consisten behavior when
doing replys or new messages.  I ended up just using the send-hook
because it was simple and consistent.

> And I agree with JRollins patch to not add the BCC to myself from
> notmuch.el by default. Either make it a notmuch option, or specify how to
> set up a default BCC in emacs in the docs.

I think this needs to just be done via a personal emacs config.  There's
really no need for the notmuch CLI to handle this, when it can easily
enough be handled by the reader UI.

jamie.

[-- Attachment #2: Type: application/pgp-signature, Size: 835 bytes --]

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

* Re: Fcc, Maildir, and Emacs message-mode -- a bit of code
  2010-01-29 13:21   ` Sebastian Spaeth
  2010-01-29 14:46     ` Jameson Rollins
@ 2010-01-29 14:54     ` Jesse Rosenthal
  1 sibling, 0 replies; 35+ messages in thread
From: Jesse Rosenthal @ 2010-01-29 14:54 UTC (permalink / raw)
  To: Sebastian Spaeth, Jameson Rollins, notmuch

On Fri, 29 Jan 2010 14:21:56 +0100, Sebastian Spaeth <Sebastian@SSpaeth.de> wrote:
> First, I think this FCC method is phantastic and it works like a
> charm. I agree that we should include the snippets at least in some
> "contrib" directory (or doc/examples) to make it easier to find.

I'm glad it's proved useful. However, I should say that I'd be wary of
distributing it, or even recommending it, without at least rudimentary
error checking -- even if it's just the delivery function returning
either 0 or -1, and the emacs wrapper reporting failure back to the user
in a sensible fashion. As it is now, a failed delivery will either go
silently by, or muck everything up in a not-so-useful way.

Returning some sort of success/fail would just take a couple of
try/except lines. I would have put that in myself, but I just whipped it
up for personal use (and then never used it).

Best,
Jesse

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

* Re: Fcc, Maildir, and Emacs message-mode -- a bit of code
  2010-01-27 14:44 ` Jameson Rollins
  2010-01-29 13:21   ` Sebastian Spaeth
@ 2010-04-07 20:04   ` Dirk Hohndel
  1 sibling, 0 replies; 35+ messages in thread
From: Dirk Hohndel @ 2010-04-07 20:04 UTC (permalink / raw)
  To: Jameson Rollins, notmuch

On Wed, 27 Jan 2010 09:44:41 -0500, Jameson Rollins <jrollins@finestructure.net> wrote:
> Hey, folks.  Following up on this thread about better fcc handling,
> Jesse passed on a simple python script he wrote that uses the python
> "mailbox" module to deliver a message on stdin to a specified maildir
> directory.  It's very a simple, elegant and general purpose script
> (attached).
> 
> I then put the following in my notmuch .emacs to use the new script in
> message-mode to fcc sent mail to my ~/.mail/sent directory:
> 
> ;; fcc handler
> (defun maildir-deliver-region(destdir)
>   (shell-command-on-region
>    (point-min) (point-max)
>    (concat "maildir-deliver.py -c -s -d " destdir)))
> (setq message-fcc-handler-function 'maildir-deliver-region)
> (defun my-message-header-setup ()
>   (message-add-header "Fcc: ~/.mail/sent"))
> (add-hook 'message-send-hook 'my-message-header-setup)

This is really nice - just what I needed. Well, almost.

Instead of passing in destdir it would be even better if the lisp code
figured out which MailDir to use. So we'd have a configuration variable
that matched From addresses to MailDirs
"dirk@hohndel.org" "~/MailDirHohndel"
"dirk.hohndel@intel.com" "~/MailDirIntel"
etc.
And just for safety used the passed in destdir as fallback / default.

Anyone willing / able to add this? I can't lisp to save my life...

/D

-- 
Dirk Hohndel
Intel Open Source Technology Center

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

* Re: Fcc, Maildir, and Emacs message-mode -- a bit of code
  2010-01-21 18:36 Fcc, Maildir, and Emacs message-mode -- a bit of code Jesse Rosenthal
  2010-01-27 14:44 ` Jameson Rollins
@ 2010-04-22  9:06 ` Sebastian Spaeth
  2010-04-22  9:07   ` [PATCH 1/6] Add elisp file for FCC to maildir solution Sebastian Spaeth
                     ` (8 more replies)
  1 sibling, 9 replies; 35+ messages in thread
From: Sebastian Spaeth @ 2010-04-22  9:06 UTC (permalink / raw)
  To: Jesse Rosenthal, notmuch

As Carl stated that he would probably merge this one if someone tested
it, here it comes: I tested it and it works great. When I define a
non-existing directory as maildir it aborts with an error message that
is visible to the user.

I put this in a branch based on cworth/master and with Jesse's
permission, I renamed the functions and did some further cleanup (It's
also integrated in the build system)

The git branch is here:
http://github.com/spaetz/notmuch-all-feature/tree/feature/elisp-fcc

It's not enabled by default. Enable it with:

(setq message-fcc-handler-function 
      '(lambda (destdir) 
         (notmuch-maildir-fcc-write-buffer-to-maildir destdir t)))

(add-hook 'message-send-hook 
          '(lambda ()
             (message-add-header "Fcc: ~/mail/INBOX.Sent")))

in your .emacs for now.

I'll send the patch series by mail in a second.

I have the slight problem that offlineimap takes the message from
INBOX.Sent/cur and stuffs it in INBOX.Sent/new (but that cannot really
be related to this as the mail ends up correctly in /cur after sending
it).

Sebastian

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

* [PATCH 1/6] Add elisp file for FCC to maildir solution
  2010-04-22  9:06 ` Sebastian Spaeth
@ 2010-04-22  9:07   ` Sebastian Spaeth
  2010-04-22  9:07   ` [PATCH] notmuch.el: Make notmuch-show buffer name first subject, instead of thread-id (supersedes V1--3) Sebastian Spaeth
                     ` (7 subsequent siblings)
  8 siblings, 0 replies; 35+ messages in thread
From: Sebastian Spaeth @ 2010-04-22  9:07 UTC (permalink / raw)
  To: Notmuch developer list

From: Jesse Rosenthal <jrosenthal@jhu.edu>

File grabbed from http://jkr.acm.jhu.edu/jkr-maildir.el
but not integrated yet.

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
---
 emacs/notmuch-maildir-fcc.el |  115 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 115 insertions(+), 0 deletions(-)
 create mode 100644 emacs/notmuch-maildir-fcc.el

diff --git a/emacs/notmuch-maildir-fcc.el b/emacs/notmuch-maildir-fcc.el
new file mode 100644
index 0000000..e7fddf1
--- /dev/null
+++ b/emacs/notmuch-maildir-fcc.el
@@ -0,0 +1,115 @@
+;; This file is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published
+;; by the Free Software Foundation; either version 2, or (at your
+;; option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
+
+;; Commentary: 
+;;
+;; This is the beginning of a solution for storing sent mail in a
+;; maildir in emacs message mode, presented because some people might
+;; find it useful. It is *not* fully tested, it *may* overwrite files,
+;; and any directories you point this at may no longer be there
+;; afterwards. Use at your own risk.
+;;
+;; To use this as the fcc handler for message-mode, put
+;; one of the following in your init file:
+;; 
+;; if you want Fcc'd messages to be marked as read:
+;;
+;;     (setq message-fcc-handler-function 
+;;          '(lambda (destdir) 
+;;	     (jkr/maildir-write-buffer-to-maildir destdir t)))
+;;
+;; if you want Fcc'd messages to be marked as new:
+;;
+;;     (setq message-fcc-handler-function 
+;;          '(lambda (destdir) 
+;;	     (jkr/maildir-write-buffer-to-maildir destdir nil)))
+
+
+(defvar jkr/maildir-count 0)
+
+(defun jkr/maildir-host-fixer (hostname)
+  (replace-regexp-in-string "/\\|:"
+			    '(lambda (s)
+			         (cond ((string-equal s "/") "\\057")
+				       ((string-equal s ":") "\\072")
+				       (t s)))
+			    hostname
+			    t
+			    t))
+
+(defun jkr/maildir-make-uniq-maildir-id ()
+   (let* ((ct (current-time))
+	  (timeid (+ (* (car ct) 65536) (cadr ct)))
+	  (microseconds (caddr ct))
+	  (hostname (jkr/maildir-host-fixer system-name)))
+     (setq jkr/maildir-count (+ jkr/maildir-count 1))
+     (format "%d.%d_%d_%d.%s"
+	     timeid
+	     (emacs-pid)
+	     microseconds
+	     jkr/maildir-count
+	     hostname)))
+
+(defun jkr/maildir-dir-is-maildir-p (dir)
+  (and (file-exists-p (concat dir "/cur/"))
+       (file-exists-p (concat dir "/new/"))
+       (file-exists-p (concat dir "/tmp/"))))
+
+(defun jkr/maildir-save-buffer-to-tmp (destdir)
+  "Returns the msg id of the message written to the temp directory
+if successful, nil if not."
+  (let ((msg-id (jkr/maildir-make-uniq-maildir-id)))
+    (while (file-exists-p (concat destdir "/tmp/" msg-id))
+      (setq msg-id (jkr/maildir-make-uniq-maildir-id)))
+    (cond ((jkr/maildir-dir-is-maildir-p destdir)
+	   (write-file (concat destdir "/tmp/" msg-id))
+	   msg-id)
+	  (t
+	   (message (format "Can't write to %s. Not a maildir."  
+		     destdir))
+	   nil))))
+
+(defun jkr/maildir-move-tmp-to-new (destdir msg-id)
+  (add-name-to-file 
+   (concat destdir "/tmp/" msg-id)
+   (concat destdir "/new/" msg-id ":2,")))
+
+(defun jkr/maildir-move-tmp-to-cur (destdir msg-id &optional mark-seen)
+  (add-name-to-file 
+   (concat destdir "/tmp/" msg-id)
+   (concat destdir "/cur/" msg-id ":2," (when mark-seen "S"))))
+
+(defun jkr/maildir-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
+return t if successful, and nil otherwise."
+  (let ((orig-buffer (buffer-name)))
+    (with-temp-buffer 
+      (insert-buffer orig-buffer)
+      (catch 'link-error
+	(let ((msg-id (jkr/maildir-save-buffer-to-tmp destdir)))
+	  (when msg-id
+	    (cond (mark-seen
+		   (condition-case err
+		       (jkr/maildir-move-tmp-to-cur destdir msg-id t)
+		     (file-already-exists
+		      (throw 'link-error nil))))
+		  (t 
+		   (condition-case err
+		       (jkr/maildir-move-tmp-to-new destdir msg-id)
+		     (file-already-exists
+		      (throw 'link-error nil))))))
+	  (delete-file (concat destdir "/tmp/" msg-id))))
+      t)))
\ No newline at end of file
-- 
1.7.0.4

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

* [PATCH] notmuch.el: Make notmuch-show buffer name first subject, instead of thread-id (supersedes V1--3)
  2010-04-22  9:06 ` Sebastian Spaeth
  2010-04-22  9:07   ` [PATCH 1/6] Add elisp file for FCC to maildir solution Sebastian Spaeth
@ 2010-04-22  9:07   ` Sebastian Spaeth
  2010-04-22 13:34     ` Sebastian Spaeth
  2010-04-22  9:07   ` [PATCH 2/6] Integrate notmuch-maildir-fcc into notmuch Sebastian Spaeth
                     ` (6 subsequent siblings)
  8 siblings, 1 reply; 35+ messages in thread
From: Sebastian Spaeth @ 2010-04-22  9:07 UTC (permalink / raw)
  To: Notmuch developer list

From: Jesse Rosenthal <jrosenthal@jhu.edu>

(Embarassing -- this should be the last fix.)

Change the buffer name to a uniquified subject of the thread (i.e. the
subject of the first message in the thread) instead of the thread-id. This
is more meaningful to the user, and will make it easier to scroll through
numerous open buffers.

Note that this patch adds an optional `buffer-name' argument to notmuch
show.

This version supersedes V1--3 of this patch. It is rebased on HEAD,
ensures that the buffer names are unique, and that the `notmuch-show'
command can still be used interactively (fixing embarassing bugs in V2
and V3 which prevented that)

Signed-off-by: Jesse Rosenthal <jrosenthal@jhu.edu>
---
 emacs/notmuch.el |   25 ++++++++++++++++++++-----
 1 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/emacs/notmuch.el b/emacs/notmuch.el
index 439303a..4b93d8e 100644
--- a/emacs/notmuch.el
+++ b/emacs/notmuch.el
@@ -1178,7 +1178,7 @@ All currently available key bindings:
 	  (lambda()
 	    (hl-line-mode 1) ))
 
-(defun notmuch-show (thread-id &optional parent-buffer query-context)
+(defun notmuch-show (thread-id &optional parent-buffer query-context buffer-name)
   "Run \"notmuch show\" with the given thread ID and display results.
 
 The optional PARENT-BUFFER is the notmuch-search buffer from
@@ -1186,9 +1186,14 @@ which this notmuch-show command was executed, (so that the next
 thread from that buffer can be show when done with this one).
 
 The optional QUERY-CONTEXT is a notmuch search term. Only messages from the thread
-matching this search term are shown if non-nil. "
+matching this search term are shown if non-nil.
+
+The optional BUFFER-NAME provides the neame of the buffer in which the message thread is shown. If it is nil (which occurs when the command is called interactively) the argument to the function is used. "
   (interactive "sNotmuch show: ")
-  (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
+  (when (null buffer-name)
+    (setq buffer-name (concat "*notmuch-" thread-id "*")))
+  (let* ((thread-buffer-name (generate-new-buffer-name buffer-name))
+	 (buffer (get-buffer-create thread-buffer-name)))
     (switch-to-buffer buffer)
     (notmuch-show-mode)
     (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
@@ -1383,9 +1388,19 @@ Complete list of currently available key bindings:
 (defun notmuch-search-show-thread ()
   "Display the currently selected thread."
   (interactive)
-  (let ((thread-id (notmuch-search-find-thread-id)))
+  (let ((thread-id (notmuch-search-find-thread-id))
+	(subject (notmuch-search-find-subject))
+	buffer-name)
+    (when (string-match "^[ \t]*$" subject)
+      (setq subject "[No Subject]"))
+    (setq buffer-name (concat "*"
+			      (truncate-string-to-width subject 32 nil nil t)
+			      "*"))
     (if (> (length thread-id) 0)
-	(notmuch-show thread-id (current-buffer) notmuch-search-query-string)
+	(notmuch-show thread-id
+		      (current-buffer)
+		      notmuch-search-query-string
+		      buffer-name)
       (error "End of search results"))))
 
 (defun notmuch-search-reply-to-thread ()
-- 
1.6.3.3

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

* [PATCH 2/6] Integrate notmuch-maildir-fcc into notmuch
  2010-04-22  9:06 ` Sebastian Spaeth
  2010-04-22  9:07   ` [PATCH 1/6] Add elisp file for FCC to maildir solution Sebastian Spaeth
  2010-04-22  9:07   ` [PATCH] notmuch.el: Make notmuch-show buffer name first subject, instead of thread-id (supersedes V1--3) Sebastian Spaeth
@ 2010-04-22  9:07   ` Sebastian Spaeth
  2010-04-22  9:07   ` [PATCH 3/6] notmuch-maildir-fcc: rename all jkr/* functions to notmuch-maildir-fcc-* Sebastian Spaeth
                     ` (5 subsequent siblings)
  8 siblings, 0 replies; 35+ messages in thread
From: Sebastian Spaeth @ 2010-04-22  9:07 UTC (permalink / raw)
  To: Notmuch developer list

Require notmuch-maildir-fcc and also install it.

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
---
 emacs/Makefile.local         |    3 ++-
 emacs/notmuch-maildir-fcc.el |    4 +++-
 emacs/notmuch.el             |    1 +
 3 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/emacs/Makefile.local b/emacs/Makefile.local
index f759c0d..c80e0e3 100644
--- a/emacs/Makefile.local
+++ b/emacs/Makefile.local
@@ -6,7 +6,8 @@ emacs_sources := \
 	$(dir)/notmuch.el \
 	$(dir)/notmuch-query.el \
 	$(dir)/notmuch-show.el \
-	$(dir)/notmuch-wash.el
+	$(dir)/notmuch-wash.el \
+	$(dir)/notmuch-maildir-fcc.el
 
 emacs_bytecode := $(subst .el,.elc,$(emacs_sources))
 
diff --git a/emacs/notmuch-maildir-fcc.el b/emacs/notmuch-maildir-fcc.el
index e7fddf1..f18ccc8 100644
--- a/emacs/notmuch-maildir-fcc.el
+++ b/emacs/notmuch-maildir-fcc.el
@@ -112,4 +112,6 @@ return t if successful, and nil otherwise."
 		     (file-already-exists
 		      (throw 'link-error nil))))))
 	  (delete-file (concat destdir "/tmp/" msg-id))))
-      t)))
\ No newline at end of file
+      t)))
+
+(provide 'notmuch-maildir-fcc)
diff --git a/emacs/notmuch.el b/emacs/notmuch.el
index 57b7fcf..02760b4 100644
--- a/emacs/notmuch.el
+++ b/emacs/notmuch.el
@@ -53,6 +53,7 @@
 
 (require 'notmuch-lib)
 (require 'notmuch-show)
+(require 'notmuch-maildir-fcc)
 
 (defcustom notmuch-search-authors-width 20
   "Number of columns to use to display authors in a notmuch-search buffer."
-- 
1.7.0.4

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

* [PATCH 3/6] notmuch-maildir-fcc: rename all jkr/* functions to notmuch-maildir-fcc-*
  2010-04-22  9:06 ` Sebastian Spaeth
                     ` (2 preceding siblings ...)
  2010-04-22  9:07   ` [PATCH 2/6] Integrate notmuch-maildir-fcc into notmuch Sebastian Spaeth
@ 2010-04-22  9:07   ` Sebastian Spaeth
  2010-04-22  9:07   ` [PATCH 4/6] add documentation example Sebastian Spaeth
                     ` (4 subsequent siblings)
  8 siblings, 0 replies; 35+ messages in thread
From: Sebastian Spaeth @ 2010-04-22  9:07 UTC (permalink / raw)
  To: Notmuch developer list

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
---
 emacs/notmuch-maildir-fcc.el |   38 +++++++++++++++++++-------------------
 1 files changed, 19 insertions(+), 19 deletions(-)

diff --git a/emacs/notmuch-maildir-fcc.el b/emacs/notmuch-maildir-fcc.el
index f18ccc8..c91095d 100644
--- a/emacs/notmuch-maildir-fcc.el
+++ b/emacs/notmuch-maildir-fcc.el
@@ -28,18 +28,18 @@
 ;;
 ;;     (setq message-fcc-handler-function 
 ;;          '(lambda (destdir) 
-;;	     (jkr/maildir-write-buffer-to-maildir destdir t)))
+;;	     (notmuch-maildir-fcc-write-buffer-to-maildir destdir t)))
 ;;
 ;; if you want Fcc'd messages to be marked as new:
 ;;
 ;;     (setq message-fcc-handler-function 
 ;;          '(lambda (destdir) 
-;;	     (jkr/maildir-write-buffer-to-maildir destdir nil)))
+;;	     (notmuch-maildir-fcc-write-buffer-to-maildir destdir nil)))
 
 
-(defvar jkr/maildir-count 0)
+(defvar notmuch-maildir-fcc-count 0)
 
-(defun jkr/maildir-host-fixer (hostname)
+(defun notmuch-maildir-fcc-host-fixer (hostname)
   (replace-regexp-in-string "/\\|:"
 			    '(lambda (s)
 			         (cond ((string-equal s "/") "\\057")
@@ -49,31 +49,31 @@
 			    t
 			    t))
 
-(defun jkr/maildir-make-uniq-maildir-id ()
+(defun notmuch-maildir-fcc-make-uniq-maildir-id ()
    (let* ((ct (current-time))
 	  (timeid (+ (* (car ct) 65536) (cadr ct)))
 	  (microseconds (caddr ct))
-	  (hostname (jkr/maildir-host-fixer system-name)))
-     (setq jkr/maildir-count (+ jkr/maildir-count 1))
+	  (hostname (notmuch-maildir-fcc-host-fixer system-name)))
+     (setq notmuch-maildir-fcc-count (+ notmuch-maildir-fcc-count 1))
      (format "%d.%d_%d_%d.%s"
 	     timeid
 	     (emacs-pid)
 	     microseconds
-	     jkr/maildir-count
+	     notmuch-maildir-fcc-count
 	     hostname)))
 
-(defun jkr/maildir-dir-is-maildir-p (dir)
+(defun notmuch-maildir-fcc-dir-is-maildir-p (dir)
   (and (file-exists-p (concat dir "/cur/"))
        (file-exists-p (concat dir "/new/"))
        (file-exists-p (concat dir "/tmp/"))))
 
-(defun jkr/maildir-save-buffer-to-tmp (destdir)
+(defun notmuch-maildir-fcc-save-buffer-to-tmp (destdir)
   "Returns the msg id of the message written to the temp directory
 if successful, nil if not."
-  (let ((msg-id (jkr/maildir-make-uniq-maildir-id)))
+  (let ((msg-id (notmuch-maildir-fcc-make-uniq-maildir-id)))
     (while (file-exists-p (concat destdir "/tmp/" msg-id))
-      (setq msg-id (jkr/maildir-make-uniq-maildir-id)))
-    (cond ((jkr/maildir-dir-is-maildir-p destdir)
+      (setq msg-id (notmuch-maildir-fcc-make-uniq-maildir-id)))
+    (cond ((notmuch-maildir-fcc-dir-is-maildir-p destdir)
 	   (write-file (concat destdir "/tmp/" msg-id))
 	   msg-id)
 	  (t
@@ -81,17 +81,17 @@ if successful, nil if not."
 		     destdir))
 	   nil))))
 
-(defun jkr/maildir-move-tmp-to-new (destdir msg-id)
+(defun notmuch-maildir-fcc-move-tmp-to-new (destdir msg-id)
   (add-name-to-file 
    (concat destdir "/tmp/" msg-id)
    (concat destdir "/new/" msg-id ":2,")))
 
-(defun jkr/maildir-move-tmp-to-cur (destdir msg-id &optional mark-seen)
+(defun notmuch-maildir-fcc-move-tmp-to-cur (destdir msg-id &optional mark-seen)
   (add-name-to-file 
    (concat destdir "/tmp/" msg-id)
    (concat destdir "/cur/" msg-id ":2," (when mark-seen "S"))))
 
-(defun jkr/maildir-write-buffer-to-maildir (destdir &optional mark-seen)
+(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
 return t if successful, and nil otherwise."
@@ -99,16 +99,16 @@ return t if successful, and nil otherwise."
     (with-temp-buffer 
       (insert-buffer orig-buffer)
       (catch 'link-error
-	(let ((msg-id (jkr/maildir-save-buffer-to-tmp destdir)))
+	(let ((msg-id (notmuch-maildir-fcc-save-buffer-to-tmp destdir)))
 	  (when msg-id
 	    (cond (mark-seen
 		   (condition-case err
-		       (jkr/maildir-move-tmp-to-cur destdir msg-id t)
+		       (notmuch-maildir-fcc-move-tmp-to-cur destdir msg-id t)
 		     (file-already-exists
 		      (throw 'link-error nil))))
 		  (t 
 		   (condition-case err
-		       (jkr/maildir-move-tmp-to-new destdir msg-id)
+		       (notmuch-maildir-fcc-move-tmp-to-new destdir msg-id)
 		     (file-already-exists
 		      (throw 'link-error nil))))))
 	  (delete-file (concat destdir "/tmp/" msg-id))))
-- 
1.7.0.4

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

* [PATCH 4/6] add documentation example
  2010-04-22  9:06 ` Sebastian Spaeth
                     ` (3 preceding siblings ...)
  2010-04-22  9:07   ` [PATCH 3/6] notmuch-maildir-fcc: rename all jkr/* functions to notmuch-maildir-fcc-* Sebastian Spaeth
@ 2010-04-22  9:07   ` Sebastian Spaeth
  2010-04-22  9:07   ` [PATCH 5/6] notmuch-maildir-fcc: use insert-buffer-substring Sebastian Spaeth
                     ` (3 subsequent siblings)
  8 siblings, 0 replies; 35+ messages in thread
From: Sebastian Spaeth @ 2010-04-22  9:07 UTC (permalink / raw)
  To: Notmuch developer list

Show how to set up an FCC header hook. Some minor indentation fixes

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
---
 emacs/notmuch-maildir-fcc.el |   14 ++++++++++----
 1 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/emacs/notmuch-maildir-fcc.el b/emacs/notmuch-maildir-fcc.el
index c91095d..f63c1c9 100644
--- a/emacs/notmuch-maildir-fcc.el
+++ b/emacs/notmuch-maildir-fcc.el
@@ -35,16 +35,22 @@
 ;;     (setq message-fcc-handler-function 
 ;;          '(lambda (destdir) 
 ;;	     (notmuch-maildir-fcc-write-buffer-to-maildir destdir nil)))
-
+;;
+;; It will then honor the Fcc header that you set in your mail. They can
+;; be set up automatically via:
+;;
+;;     (add-hook 'message-send-hook 
+;;          '(lambda ()
+;;             (message-add-header "Fcc: ~/mail/INBOX.Sent")))
 
 (defvar notmuch-maildir-fcc-count 0)
 
 (defun notmuch-maildir-fcc-host-fixer (hostname)
   (replace-regexp-in-string "/\\|:"
 			    '(lambda (s)
-			         (cond ((string-equal s "/") "\\057")
-				       ((string-equal s ":") "\\072")
-				       (t s)))
+                               (cond ((string-equal s "/") "\\057")
+                                     ((string-equal s ":") "\\072")
+                                     (t s)))
 			    hostname
 			    t
 			    t))
-- 
1.7.0.4

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

* [PATCH 5/6] notmuch-maildir-fcc: use insert-buffer-substring
  2010-04-22  9:06 ` Sebastian Spaeth
                     ` (4 preceding siblings ...)
  2010-04-22  9:07   ` [PATCH 4/6] add documentation example Sebastian Spaeth
@ 2010-04-22  9:07   ` Sebastian Spaeth
  2010-04-22  9:07   ` [PATCH 6/6] notmuch-maildir-fcc: replace caddr with (car (cdr (cdr))) Sebastian Spaeth
                     ` (2 subsequent siblings)
  8 siblings, 0 replies; 35+ messages in thread
From: Sebastian Spaeth @ 2010-04-22  9:07 UTC (permalink / raw)
  To: Notmuch developer list

Rather than the insert-buffer. Emacs complains that it is for interactive use
and not for use within elisp. So use insert-buffer-substring which does the
same thing when not handed any 'begin' 'end' parameters.

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
---
 emacs/notmuch-maildir-fcc.el |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/emacs/notmuch-maildir-fcc.el b/emacs/notmuch-maildir-fcc.el
index f63c1c9..c47bfb3 100644
--- a/emacs/notmuch-maildir-fcc.el
+++ b/emacs/notmuch-maildir-fcc.el
@@ -103,7 +103,7 @@ non-nil, it will write it to cur/, and mark it as read. It should
 return t if successful, and nil otherwise."
   (let ((orig-buffer (buffer-name)))
     (with-temp-buffer 
-      (insert-buffer orig-buffer)
+      (insert-buffer-substring orig-buffer)
       (catch 'link-error
 	(let ((msg-id (notmuch-maildir-fcc-save-buffer-to-tmp destdir)))
 	  (when msg-id
-- 
1.7.0.4

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

* [PATCH 6/6] notmuch-maildir-fcc: replace caddr with (car (cdr (cdr)))
  2010-04-22  9:06 ` Sebastian Spaeth
                     ` (5 preceding siblings ...)
  2010-04-22  9:07   ` [PATCH 5/6] notmuch-maildir-fcc: use insert-buffer-substring Sebastian Spaeth
@ 2010-04-22  9:07   ` Sebastian Spaeth
  2010-04-22 23:17   ` Fcc, Maildir, and Emacs message-mode -- a bit of code Dirk Hohndel
  2010-04-23  9:38   ` [PATCH 7/7] " Sebastian Spaeth
  8 siblings, 0 replies; 35+ messages in thread
From: Sebastian Spaeth @ 2010-04-22  9:07 UTC (permalink / raw)
  To: Notmuch developer list

The former requires 'cl to be loaded and during make install emacs complained about not knowing it.

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
---
 emacs/notmuch-maildir-fcc.el |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/emacs/notmuch-maildir-fcc.el b/emacs/notmuch-maildir-fcc.el
index c47bfb3..3dc8283 100644
--- a/emacs/notmuch-maildir-fcc.el
+++ b/emacs/notmuch-maildir-fcc.el
@@ -58,7 +58,7 @@
 (defun notmuch-maildir-fcc-make-uniq-maildir-id ()
    (let* ((ct (current-time))
 	  (timeid (+ (* (car ct) 65536) (cadr ct)))
-	  (microseconds (caddr ct))
+	  (microseconds (car (cdr (cdr ct))))
 	  (hostname (notmuch-maildir-fcc-host-fixer system-name)))
      (setq notmuch-maildir-fcc-count (+ notmuch-maildir-fcc-count 1))
      (format "%d.%d_%d_%d.%s"
-- 
1.7.0.4

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

* Re: [PATCH] notmuch.el: Make notmuch-show buffer name first subject, instead of thread-id (supersedes V1--3)
  2010-04-22  9:07   ` [PATCH] notmuch.el: Make notmuch-show buffer name first subject, instead of thread-id (supersedes V1--3) Sebastian Spaeth
@ 2010-04-22 13:34     ` Sebastian Spaeth
  0 siblings, 0 replies; 35+ messages in thread
From: Sebastian Spaeth @ 2010-04-22 13:34 UTC (permalink / raw)
  To: Notmuch developer list

On 2010-04-22, Sebastian Spaeth wrote:
> From: Jesse Rosenthal <jrosenthal@jhu.edu>

This patch must have slipped in the patch series of the 6 other patches
(which are all correct). Please ignore it.

Note to myself, never do "git send-email 00*"

Sebastian

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

* Re: Fcc, Maildir, and Emacs message-mode -- a bit of code
  2010-04-22  9:06 ` Sebastian Spaeth
                     ` (6 preceding siblings ...)
  2010-04-22  9:07   ` [PATCH 6/6] notmuch-maildir-fcc: replace caddr with (car (cdr (cdr))) Sebastian Spaeth
@ 2010-04-22 23:17   ` Dirk Hohndel
  2010-04-23  7:13     ` Sebastian Spaeth
  2010-04-23  9:38   ` [PATCH 7/7] " Sebastian Spaeth
  8 siblings, 1 reply; 35+ messages in thread
From: Dirk Hohndel @ 2010-04-22 23:17 UTC (permalink / raw)
  To: Sebastian Spaeth, notmuch

On Thu, 22 Apr 2010 11:06:04 +0200, "Sebastian Spaeth" <Sebastian@SSpaeth.de> wrote:
> As Carl stated that he would probably merge this one if someone tested
> it, here it comes: I tested it and it works great. When I define a
> non-existing directory as maildir it aborts with an error message that
> is visible to the user.
> 
> I put this in a branch based on cworth/master and with Jesse's
> permission, I renamed the functions and did some further cleanup (It's
> also integrated in the build system)
> 
> The git branch is here:
> http://github.com/spaetz/notmuch-all-feature/tree/feature/elisp-fcc
> 
> It's not enabled by default. Enable it with:
> 
> (setq message-fcc-handler-function 
>       '(lambda (destdir) 
>          (notmuch-maildir-fcc-write-buffer-to-maildir destdir t)))
> 
> (add-hook 'message-send-hook 
>           '(lambda ()
>              (message-add-header "Fcc: ~/mail/INBOX.Sent")))

I am now using this patch together with this little snippet in my .emacs
file to FCC into per-from-address specific sent folders

;; This is the list of alternatives that should be configurable as
;; defcustom (or simply set in .emacs for now)
(setq notmuch-fcc-dirs '(
      ("Dirk Hohndel <dirk.hohndel@intel.com>" . "Maildir/Sent Items")
      ("Dirk Hohndel <hohndel@infradead.org>" . "MaildirInfradead/Sent")))

;This constructs a path, concatenating the content of the variable
;"message-directory" and the second part in the alist:
(defun my-fcc-header-setup ()
(let ((subdir (cdr (assoc (message-fetch-field "from") notmuch-fcc-dirs))))
     (message-add-header (concat "Fcc: " message-directory subdir))))
(add-hook 'message-send-hook 'my-fcc-header-setup)

(setq message-fcc-handler-function
       '(lambda (destdir)
	   (notmuch-maildir-fcc-write-buffer-to-maildir destdir t)))

I am using the message-send-hook (instead of the
message-header-setup-hook as Sebastian initially suggested) because I
tend to edit the From line in my message buffer and want to make sure
that it uses the final From line. Also, this way the FCC also works
correctly with replies (the message-header-setup-hook doesn't get called
by our notmuch-reply function, it seems).

The disadvantage is that with the message-send-hook I don't get to see /
correct the FCC line in the message buffer - but since I trust the logic
here (and tested it quite a bit), I'm less concerned about this.

Finally, for other emacs-newbies like me - this concats the
message-directory variable with the path that you setup in the
notmuch-fcc-dirs associative array... so the path to the sent folder
needs to be relative to that.

Carl - I'd love to see this in 0.3

/D

-- 
Dirk Hohndel
Intel Open Source Technology Center

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

* Re: Fcc, Maildir, and Emacs message-mode -- a bit of code
  2010-04-22 23:17   ` Fcc, Maildir, and Emacs message-mode -- a bit of code Dirk Hohndel
@ 2010-04-23  7:13     ` Sebastian Spaeth
  2010-04-23 10:08       ` Updated elisp FCC patches (was: Fcc, Maildir, and Emacs message-mode) Sebastian Spaeth
  0 siblings, 1 reply; 35+ messages in thread
From: Sebastian Spaeth @ 2010-04-23  7:13 UTC (permalink / raw)
  To: Dirk Hohndel, notmuch

On 2010-04-22, Dirk Hohndel wrote:
> ;; This is the list of alternatives that should be configurable as
> ;; defcustom (or simply set in .emacs for now)
> (setq notmuch-fcc-dirs '(
>       ("Dirk Hohndel <dirk.hohndel@intel.com>" . "Maildir/Sent Items")
>       ("Dirk Hohndel <hohndel@infradead.org>" . "MaildirInfradead/Sent")))
> 
> ;This constructs a path, concatenating the content of the variable
> ;"message-directory" and the second part in the alist:
> (defun my-fcc-header-setup ()
> (let ((subdir (cdr (assoc (message-fetch-field "from") notmuch-fcc-dirs))))
>      (message-add-header (concat "Fcc: " message-directory subdir))))
> (add-hook 'message-send-hook 'my-fcc-header-setup)

> The disadvantage is that with the message-send-hook I don't get to see /
> correct the FCC line in the message buffer - but since I trust the logic
> here (and tested it quite a bit), I'm less concerned about this.

Another disadvantage is that this does not provide a fallback, so if you
type "dirk" rather than "Dirk" in your from, the fcc dir will be nil, so
it tries to deliver just to "message-directory". The user should be able
to configure a default FCC dir, I think.

Sebastian

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

* [PATCH 7/7] Integrate notmuch-fcc mechansim
  2010-04-22  9:06 ` Sebastian Spaeth
                     ` (7 preceding siblings ...)
  2010-04-22 23:17   ` Fcc, Maildir, and Emacs message-mode -- a bit of code Dirk Hohndel
@ 2010-04-23  9:38   ` Sebastian Spaeth
  2010-04-24 23:10     ` Dirk Hohndel
  8 siblings, 1 reply; 35+ messages in thread
From: Sebastian Spaeth @ 2010-04-23  9:38 UTC (permalink / raw)
  To: Notmuch developer list

I have gone wild and added a defcustom "notmuch-fcc-dirs".
Depending on the value of that variable we will not do any
maildir fcc at all (nil, the default), or it is of the format
(("defaultsentbox")
 ("full name <email@address>" . "Work/sentbox")
 ("full name2 <email2@address2>" . "Work2/sentbox"))

The outbox name will be concatenated with the message mode
variable "message-directory" which is "~/Mail/" by default.

With this solution, no customization of a user's .emacs file
is needed at all.

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
---
I went wild and did a proper notmuch integration including customizations.
Maildir FCC is turned off by default and can be turned on to some universal
SENTBOX or to Dirk's sepcial use case with multiple outboxes depending on 
the from address. Some testing would be appreciated before it is merged.
It seems to work nicely here. And be lenient, remember I am an elisp noob.

 emacs/notmuch-maildir-fcc.el |   86 ++++++++++++++++++++++++++++--------------
 1 files changed, 57 insertions(+), 29 deletions(-)

diff --git a/emacs/notmuch-maildir-fcc.el b/emacs/notmuch-maildir-fcc.el
index b7e1fb3..9f54dfb 100644
--- a/emacs/notmuch-maildir-fcc.el
+++ b/emacs/notmuch-maildir-fcc.el
@@ -12,39 +12,66 @@
 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 ;; Boston, MA 02110-1301, USA.
-
-;; Commentary:
-;;
-;; This is the beginning of a solution for storing sent mail in a
-;; maildir in emacs message mode, presented because some people might
-;; find it useful. It is *not* fully tested, it *may* overwrite files,
-;; and any directories you point this at may no longer be there
-;; afterwards. Use at your own risk.
-;;
-;; To use this as the fcc handler for message-mode, put
-;; one of the following in your init file:
-;;
-;; if you want Fcc'd messages to be marked as read:
-;;
-;;     (setq message-fcc-handler-function
-;;          '(lambda (destdir)
-;;	     (notmuch-maildir-fcc-write-buffer-to-maildir destdir t)))
-;;
-;; if you want Fcc'd messages to be marked as new:
-;;
-;;     (setq message-fcc-handler-function
-;;          '(lambda (destdir)
-;;	     (notmuch-maildir-fcc-write-buffer-to-maildir destdir nil)))
-;;
-;; It will then honor the Fcc header that you set in your mail. They can
-;; be set up automatically via:
 ;;
-;;     (add-hook 'message-send-hook
-;;          '(lambda ()
-;;             (message-add-header "Fcc: ~/mail/INBOX.Sent")))
+;; To use this as the fcc handler for message-mode,
+;; customize the notmuch-fcc-dirs variable
+
+(require 'message)
 
 (defvar notmuch-maildir-fcc-count 0)
 
+(defcustom notmuch-fcc-dirs nil
+ "If set to non-nil, this will cause message mode to file (fcc) your mail in the specified directory, depending on your From address.
+
+ The first entry (a list) is used as a default fallback
+ when nothing matches. So in the easiest case notmuch-fcc-dirs is
+ just something like ((\"INBOX.Sent\"))
+
+ If you need a more fancy setup, where you want different Outboxes depending
+ on your From address, you use something like this:
+
+     (   (\"defaultinbox\")
+         (\"Sebastian Spaeth <Sebastian@SSpaeth.de>\" . \"privat\")
+         (\"Sebastian Spaeth <SSpaeth@ethz.ch>\" . \"uni\")
+     )
+ 
+ This will constructs a path, concatenating the content of the
+ variable 'message-directory' (a message mode variable
+ customizable via m-x
+ customize-variable<RET>message-directory<RET>) and the second
+ part in the alist."
+ :require 'notmuch-fcc-initialization
+ :group 'notmuch
+)
+
+(defun notmuch-fcc-initialization ()
+  "If notmuch-fcc-directories is set, 
+   hook them into the message-fcc-handler-function"
+(if (not (eq notmuch-fcc-dirs nil)) (progn
+    ;Set up the message-fcc-handler to move mails to the maildir in Fcc
+    ;The parameter is hardcoded to mark messages as "seen"
+    (setq message-fcc-handler-function
+          '(lambda (destdir)
+             (notmuch-maildir-fcc-write-buffer-to-maildir destdir t)))
+    ;add a hook to actually insert the Fcc header when sending
+    ;(preferrably we would use message-header-setup-up, but notmuch-reply 
+    ; munges headers after that is run, so it won't work for replies within
+    ; notmuch)
+    (add-hook 'message-send-hook 'notmuch-fcc-header-setup))))
+
+(defun notmuch-fcc-header-setup ()
+  "Can be added to message-send-hook and will set the FCC header
+      based on the values of notmuch-fcc-directories (see the
+      variable customization there for examples). It uses the
+      first entry as default fallback if no From address
+      matches."
+  ; only do something if notmuch-fcc-dirs is set
+  (if notmuch-fcc-dirs
+   (let ((subdir (cdr (assoc (message-fetch-field "from") notmuch-fcc-dirs))))
+     (if (eq subdir nil) (setq subdir (car (car notmuch-fcc-dirs))))
+     (message-remove-header "Fcc")
+     (message-add-header (concat "Fcc: " message-directory subdir)))))
+
 (defun notmuch-maildir-fcc-host-fixer (hostname)
   (replace-regexp-in-string "/\\|:"
 			    '(lambda (s)
@@ -120,4 +147,5 @@ return t if successful, and nil otherwise."
 	  (delete-file (concat destdir "/tmp/" msg-id))))
       t)))
 
+(notmuch-fcc-initialization)
 (provide 'notmuch-maildir-fcc)
-- 
1.7.0.4

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

* Updated elisp FCC patches (was: Fcc, Maildir, and Emacs message-mode)
  2010-04-23  7:13     ` Sebastian Spaeth
@ 2010-04-23 10:08       ` Sebastian Spaeth
  2010-04-23 14:01         ` Dirk Hohndel
  0 siblings, 1 reply; 35+ messages in thread
From: Sebastian Spaeth @ 2010-04-23 10:08 UTC (permalink / raw)
  To: Dirk Hohndel, Carl Worth, Notmuch development list

My last mail on this issue: I squashed the recent 7 patch series into 4 nicer
ones.

Rather than resending the patch series, here are the 4 commits from my
repo at git@github.com:spaetz/notmuch-all-feature.git (let me know if I
should mail them too).
(These 4 are in the feature/elisp feature branch based on current cworth)

9e27ea17e9cb2ec69f90f8b5288bd9fe29946356
Add (unchanged) elisp file for FCC to maildir solution

bdcccb5faa86233f0afd66001b566054ffdb6be3
Integrate notmuch-maildir-fcc into notmuch

e6d7456094431959f8621d3cf36e1862bfdbbf62
notmuch-maildir-fcc: elisp syntax fixes

b339830a8b2853a0c683773e2bdb03e0f6df2de7
Integrate notmuch-fcc mechansim

I don't know how to test the emacs interaction or I would have provided
some tests with it. Also, I am not sure if the error message still
shows up when the user configures a non-existing maildir. But other than
that it works nice and reliably here. I have now dumped my python
script. I feel pretty confident that this can go in.

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

* Re: Updated elisp FCC patches (was: Fcc, Maildir, and Emacs message-mode)
  2010-04-23 10:08       ` Updated elisp FCC patches (was: Fcc, Maildir, and Emacs message-mode) Sebastian Spaeth
@ 2010-04-23 14:01         ` Dirk Hohndel
  2010-04-23 19:01           ` [PATCH 1/4] Add elisp file for FCC to maildir solution Sebastian Spaeth
                             ` (3 more replies)
  0 siblings, 4 replies; 35+ messages in thread
From: Dirk Hohndel @ 2010-04-23 14:01 UTC (permalink / raw)
  To: Sebastian Spaeth, Carl Worth, Notmuch development list

On Fri, 23 Apr 2010 12:08:31 +0200, "Sebastian Spaeth" <Sebastian@SSpaeth.de> wrote:
> My last mail on this issue: I squashed the recent 7 patch series into 4 nicer
> ones.
> 
> Rather than resending the patch series, here are the 4 commits from my
> repo at git@github.com:spaetz/notmuch-all-feature.git (let me know if I
> should mail them too).

I had talked to Carl about this yesterday - I think we both would prefer
that patches be emailed, too. The git trees are great for pulling
patches from, but it seems easier to review them here in email...

> I don't know how to test the emacs interaction or I would have provided
> some tests with it. Also, I am not sure if the error message still
> shows up when the user configures a non-existing maildir. But other than
> that it works nice and reliably here. I have now dumped my python
> script. I feel pretty confident that this can go in.

I will play with it later today.

/D

-- 
Dirk Hohndel
Intel Open Source Technology Center

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

* [PATCH 1/4] Add elisp file for FCC to maildir solution
  2010-04-23 14:01         ` Dirk Hohndel
@ 2010-04-23 19:01           ` Sebastian Spaeth
  2010-04-23 19:01           ` [PATCH 2/4] Integrate notmuch-maildir-fcc into notmuch Sebastian Spaeth
                             ` (2 subsequent siblings)
  3 siblings, 0 replies; 35+ messages in thread
From: Sebastian Spaeth @ 2010-04-23 19:01 UTC (permalink / raw)
  To: Notmuch developer list

From: Jesse Rosenthal <jrosenthal@jhu.edu>

File grabbed from http://jkr.acm.jhu.edu/jkr-maildir.el
but not integrated yet.

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
---
As stated by Dirk, patches are preferred by mail. So here is the updated 
patch series of 4 patches by mail again.

 emacs/notmuch-maildir-fcc.el |  115 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 115 insertions(+), 0 deletions(-)
 create mode 100644 emacs/notmuch-maildir-fcc.el

diff --git a/emacs/notmuch-maildir-fcc.el b/emacs/notmuch-maildir-fcc.el
new file mode 100644
index 0000000..979428e
--- /dev/null
+++ b/emacs/notmuch-maildir-fcc.el
@@ -0,0 +1,115 @@
+;; This file is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published
+;; by the Free Software Foundation; either version 2, or (at your
+;; option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
+
+;; Commentary:
+;;
+;; This is the beginning of a solution for storing sent mail in a
+;; maildir in emacs message mode, presented because some people might
+;; find it useful. It is *not* fully tested, it *may* overwrite files,
+;; and any directories you point this at may no longer be there
+;; afterwards. Use at your own risk.
+;;
+;; To use this as the fcc handler for message-mode, put
+;; one of the following in your init file:
+;;
+;; if you want Fcc'd messages to be marked as read:
+;;
+;;     (setq message-fcc-handler-function
+;;          '(lambda (destdir)
+;;	     (jkr/maildir-write-buffer-to-maildir destdir t)))
+;;
+;; if you want Fcc'd messages to be marked as new:
+;;
+;;     (setq message-fcc-handler-function
+;;          '(lambda (destdir)
+;;	     (jkr/maildir-write-buffer-to-maildir destdir nil)))
+
+
+(defvar jkr/maildir-count 0)
+
+(defun jkr/maildir-host-fixer (hostname)
+  (replace-regexp-in-string "/\\|:"
+			    '(lambda (s)
+			         (cond ((string-equal s "/") "\\057")
+				       ((string-equal s ":") "\\072")
+				       (t s)))
+			    hostname
+			    t
+			    t))
+
+(defun jkr/maildir-make-uniq-maildir-id ()
+   (let* ((ct (current-time))
+	  (timeid (+ (* (car ct) 65536) (cadr ct)))
+	  (microseconds (caddr ct))
+	  (hostname (jkr/maildir-host-fixer system-name)))
+     (setq jkr/maildir-count (+ jkr/maildir-count 1))
+     (format "%d.%d_%d_%d.%s"
+	     timeid
+	     (emacs-pid)
+	     microseconds
+	     jkr/maildir-count
+	     hostname)))
+
+(defun jkr/maildir-dir-is-maildir-p (dir)
+  (and (file-exists-p (concat dir "/cur/"))
+       (file-exists-p (concat dir "/new/"))
+       (file-exists-p (concat dir "/tmp/"))))
+
+(defun jkr/maildir-save-buffer-to-tmp (destdir)
+  "Returns the msg id of the message written to the temp directory
+if successful, nil if not."
+  (let ((msg-id (jkr/maildir-make-uniq-maildir-id)))
+    (while (file-exists-p (concat destdir "/tmp/" msg-id))
+      (setq msg-id (jkr/maildir-make-uniq-maildir-id)))
+    (cond ((jkr/maildir-dir-is-maildir-p destdir)
+	   (write-file (concat destdir "/tmp/" msg-id))
+	   msg-id)
+	  (t
+	   (message (format "Can't write to %s. Not a maildir."
+		     destdir))
+	   nil))))
+
+(defun jkr/maildir-move-tmp-to-new (destdir msg-id)
+  (add-name-to-file
+   (concat destdir "/tmp/" msg-id)
+   (concat destdir "/new/" msg-id ":2,")))
+
+(defun jkr/maildir-move-tmp-to-cur (destdir msg-id &optional mark-seen)
+  (add-name-to-file
+   (concat destdir "/tmp/" msg-id)
+   (concat destdir "/cur/" msg-id ":2," (when mark-seen "S"))))
+
+(defun jkr/maildir-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
+return t if successful, and nil otherwise."
+  (let ((orig-buffer (buffer-name)))
+    (with-temp-buffer
+      (insert-buffer orig-buffer)
+      (catch 'link-error
+	(let ((msg-id (jkr/maildir-save-buffer-to-tmp destdir)))
+	  (when msg-id
+	    (cond (mark-seen
+		   (condition-case err
+		       (jkr/maildir-move-tmp-to-cur destdir msg-id t)
+		     (file-already-exists
+		      (throw 'link-error nil))))
+		  (t
+		   (condition-case err
+		       (jkr/maildir-move-tmp-to-new destdir msg-id)
+		     (file-already-exists
+		      (throw 'link-error nil))))))
+	  (delete-file (concat destdir "/tmp/" msg-id))))
+      t)))
\ No newline at end of file
-- 
1.7.0.4

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

* [PATCH 2/4] Integrate notmuch-maildir-fcc into notmuch
  2010-04-23 14:01         ` Dirk Hohndel
  2010-04-23 19:01           ` [PATCH 1/4] Add elisp file for FCC to maildir solution Sebastian Spaeth
@ 2010-04-23 19:01           ` Sebastian Spaeth
  2010-04-23 19:01           ` [PATCH 3/4] notmuch-maildir-fcc: elisp syntax fixes Sebastian Spaeth
  2010-04-23 19:01           ` [PATCH 4/4] Integrate notmuch-fcc mechansim Sebastian Spaeth
  3 siblings, 0 replies; 35+ messages in thread
From: Sebastian Spaeth @ 2010-04-23 19:01 UTC (permalink / raw)
  To: Notmuch developer list

Require notmuch-maildir-fcc and also install it.
Rename all jkr/* functions to notmuch-maildir-fcc-*

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
---
 emacs/Makefile.local         |    3 ++-
 emacs/notmuch-maildir-fcc.el |   42 ++++++++++++++++++++++--------------------
 emacs/notmuch.el             |    1 +
 3 files changed, 25 insertions(+), 21 deletions(-)

diff --git a/emacs/Makefile.local b/emacs/Makefile.local
index f759c0d..c80e0e3 100644
--- a/emacs/Makefile.local
+++ b/emacs/Makefile.local
@@ -6,7 +6,8 @@ emacs_sources := \
 	$(dir)/notmuch.el \
 	$(dir)/notmuch-query.el \
 	$(dir)/notmuch-show.el \
-	$(dir)/notmuch-wash.el
+	$(dir)/notmuch-wash.el \
+	$(dir)/notmuch-maildir-fcc.el
 
 emacs_bytecode := $(subst .el,.elc,$(emacs_sources))
 
diff --git a/emacs/notmuch-maildir-fcc.el b/emacs/notmuch-maildir-fcc.el
index 979428e..2117f54 100644
--- a/emacs/notmuch-maildir-fcc.el
+++ b/emacs/notmuch-maildir-fcc.el
@@ -28,18 +28,18 @@
 ;;
 ;;     (setq message-fcc-handler-function
 ;;          '(lambda (destdir)
-;;	     (jkr/maildir-write-buffer-to-maildir destdir t)))
+;;	     (notmuch-maildir-fcc-write-buffer-to-maildir destdir t)))
 ;;
 ;; if you want Fcc'd messages to be marked as new:
 ;;
 ;;     (setq message-fcc-handler-function
 ;;          '(lambda (destdir)
-;;	     (jkr/maildir-write-buffer-to-maildir destdir nil)))
+;;	     (notmuch-maildir-fcc-write-buffer-to-maildir destdir nil)))
 
 
-(defvar jkr/maildir-count 0)
+(defvar notmuch-maildir-fcc-count 0)
 
-(defun jkr/maildir-host-fixer (hostname)
+(defun notmuch-maildir-fcc-host-fixer (hostname)
   (replace-regexp-in-string "/\\|:"
 			    '(lambda (s)
 			         (cond ((string-equal s "/") "\\057")
@@ -49,31 +49,31 @@
 			    t
 			    t))
 
-(defun jkr/maildir-make-uniq-maildir-id ()
+(defun notmuch-maildir-fcc-make-uniq-maildir-id ()
    (let* ((ct (current-time))
 	  (timeid (+ (* (car ct) 65536) (cadr ct)))
 	  (microseconds (caddr ct))
-	  (hostname (jkr/maildir-host-fixer system-name)))
-     (setq jkr/maildir-count (+ jkr/maildir-count 1))
+	  (hostname (notmuch-maildir-fcc-host-fixer system-name)))
+     (setq notmuch-maildir-fcc-count (+ notmuch-maildir-fcc-count 1))
      (format "%d.%d_%d_%d.%s"
 	     timeid
 	     (emacs-pid)
 	     microseconds
-	     jkr/maildir-count
+	     notmuch-maildir-fcc-count
 	     hostname)))
 
-(defun jkr/maildir-dir-is-maildir-p (dir)
+(defun notmuch-maildir-fcc-dir-is-maildir-p (dir)
   (and (file-exists-p (concat dir "/cur/"))
        (file-exists-p (concat dir "/new/"))
        (file-exists-p (concat dir "/tmp/"))))
 
-(defun jkr/maildir-save-buffer-to-tmp (destdir)
+(defun notmuch-maildir-fcc-save-buffer-to-tmp (destdir)
   "Returns the msg id of the message written to the temp directory
 if successful, nil if not."
-  (let ((msg-id (jkr/maildir-make-uniq-maildir-id)))
+  (let ((msg-id (notmuch-maildir-fcc-make-uniq-maildir-id)))
     (while (file-exists-p (concat destdir "/tmp/" msg-id))
-      (setq msg-id (jkr/maildir-make-uniq-maildir-id)))
-    (cond ((jkr/maildir-dir-is-maildir-p destdir)
+      (setq msg-id (notmuch-maildir-fcc-make-uniq-maildir-id)))
+    (cond ((notmuch-maildir-fcc-dir-is-maildir-p destdir)
 	   (write-file (concat destdir "/tmp/" msg-id))
 	   msg-id)
 	  (t
@@ -81,17 +81,17 @@ if successful, nil if not."
 		     destdir))
 	   nil))))
 
-(defun jkr/maildir-move-tmp-to-new (destdir msg-id)
+(defun notmuch-maildir-fcc-move-tmp-to-new (destdir msg-id)
   (add-name-to-file
    (concat destdir "/tmp/" msg-id)
    (concat destdir "/new/" msg-id ":2,")))
 
-(defun jkr/maildir-move-tmp-to-cur (destdir msg-id &optional mark-seen)
+(defun notmuch-maildir-fcc-move-tmp-to-cur (destdir msg-id &optional mark-seen)
   (add-name-to-file
    (concat destdir "/tmp/" msg-id)
    (concat destdir "/cur/" msg-id ":2," (when mark-seen "S"))))
 
-(defun jkr/maildir-write-buffer-to-maildir (destdir &optional mark-seen)
+(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
 return t if successful, and nil otherwise."
@@ -99,17 +99,19 @@ return t if successful, and nil otherwise."
     (with-temp-buffer
       (insert-buffer orig-buffer)
       (catch 'link-error
-	(let ((msg-id (jkr/maildir-save-buffer-to-tmp destdir)))
+	(let ((msg-id (notmuch-maildir-fcc-save-buffer-to-tmp destdir)))
 	  (when msg-id
 	    (cond (mark-seen
 		   (condition-case err
-		       (jkr/maildir-move-tmp-to-cur destdir msg-id t)
+		       (notmuch-maildir-fcc-move-tmp-to-cur destdir msg-id t)
 		     (file-already-exists
 		      (throw 'link-error nil))))
 		  (t
 		   (condition-case err
-		       (jkr/maildir-move-tmp-to-new destdir msg-id)
+		       (notmuch-maildir-fcc-move-tmp-to-new destdir msg-id)
 		     (file-already-exists
 		      (throw 'link-error nil))))))
 	  (delete-file (concat destdir "/tmp/" msg-id))))
-      t)))
\ No newline at end of file
+      t)))
+
+(provide 'notmuch-maildir-fcc)
diff --git a/emacs/notmuch.el b/emacs/notmuch.el
index e947e5d..8cc71e1 100644
--- a/emacs/notmuch.el
+++ b/emacs/notmuch.el
@@ -53,6 +53,7 @@
 
 (require 'notmuch-lib)
 (require 'notmuch-show)
+(require 'notmuch-maildir-fcc)
 
 (defcustom notmuch-search-authors-width 20
   "Number of columns to use to display authors in a notmuch-search buffer."
-- 
1.7.0.4

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

* [PATCH 3/4] notmuch-maildir-fcc: elisp syntax fixes
  2010-04-23 14:01         ` Dirk Hohndel
  2010-04-23 19:01           ` [PATCH 1/4] Add elisp file for FCC to maildir solution Sebastian Spaeth
  2010-04-23 19:01           ` [PATCH 2/4] Integrate notmuch-maildir-fcc into notmuch Sebastian Spaeth
@ 2010-04-23 19:01           ` Sebastian Spaeth
  2010-04-23 19:01           ` [PATCH 4/4] Integrate notmuch-fcc mechansim Sebastian Spaeth
  3 siblings, 0 replies; 35+ messages in thread
From: Sebastian Spaeth @ 2010-04-23 19:01 UTC (permalink / raw)
  To: Notmuch developer list

1)use insert-buffer-substring

Rather than the insert-buffer. Emacs complains that it is for interactive use
and not for use within elisp. So use insert-buffer-substring which does the
same thing when not handed any 'begin' 'end' parameters.

2)replace caddr with (car (cdr (cdr)))

The former requires 'cl to be loaded and during make install emacs complained
about not knowing it.

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
---
 emacs/notmuch-maildir-fcc.el |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/emacs/notmuch-maildir-fcc.el b/emacs/notmuch-maildir-fcc.el
index 2117f54..84f4187 100644
--- a/emacs/notmuch-maildir-fcc.el
+++ b/emacs/notmuch-maildir-fcc.el
@@ -52,7 +52,7 @@
 (defun notmuch-maildir-fcc-make-uniq-maildir-id ()
    (let* ((ct (current-time))
 	  (timeid (+ (* (car ct) 65536) (cadr ct)))
-	  (microseconds (caddr ct))
+	  (microseconds (car (cdr (cdr ct))))
 	  (hostname (notmuch-maildir-fcc-host-fixer system-name)))
      (setq notmuch-maildir-fcc-count (+ notmuch-maildir-fcc-count 1))
      (format "%d.%d_%d_%d.%s"
@@ -97,7 +97,7 @@ non-nil, it will write it to cur/, and mark it as read. It should
 return t if successful, and nil otherwise."
   (let ((orig-buffer (buffer-name)))
     (with-temp-buffer
-      (insert-buffer orig-buffer)
+      (insert-buffer-substring orig-buffer)
       (catch 'link-error
 	(let ((msg-id (notmuch-maildir-fcc-save-buffer-to-tmp destdir)))
 	  (when msg-id
-- 
1.7.0.4

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

* [PATCH 4/4] Integrate notmuch-fcc mechansim
  2010-04-23 14:01         ` Dirk Hohndel
                             ` (2 preceding siblings ...)
  2010-04-23 19:01           ` [PATCH 3/4] notmuch-maildir-fcc: elisp syntax fixes Sebastian Spaeth
@ 2010-04-23 19:01           ` Sebastian Spaeth
  3 siblings, 0 replies; 35+ messages in thread
From: Sebastian Spaeth @ 2010-04-23 19:01 UTC (permalink / raw)
  To: Notmuch developer list

I have gone wild and added a defcustom "notmuch-fcc-dirs".
Depending on the value of that variable we will not do any
maildir fcc at all (nil, the default), or it is of the format
(("defaultsentbox")
 ("full name <email@address>" . "Work/sentbox")
 ("full name2 <email2@address2>" . "Work2/sentbox"))

The outbox name will be concatenated with the message mode
variable "message-directory" which is "~/Mail/" by default.

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
---
 emacs/notmuch-maildir-fcc.el |   84 +++++++++++++++++++++++++++++------------
 1 files changed, 59 insertions(+), 25 deletions(-)

diff --git a/emacs/notmuch-maildir-fcc.el b/emacs/notmuch-maildir-fcc.el
index 84f4187..9f54dfb 100644
--- a/emacs/notmuch-maildir-fcc.el
+++ b/emacs/notmuch-maildir-fcc.el
@@ -12,39 +12,72 @@
 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 ;; Boston, MA 02110-1301, USA.
-
-;; Commentary:
-;;
-;; This is the beginning of a solution for storing sent mail in a
-;; maildir in emacs message mode, presented because some people might
-;; find it useful. It is *not* fully tested, it *may* overwrite files,
-;; and any directories you point this at may no longer be there
-;; afterwards. Use at your own risk.
-;;
-;; To use this as the fcc handler for message-mode, put
-;; one of the following in your init file:
-;;
-;; if you want Fcc'd messages to be marked as read:
-;;
-;;     (setq message-fcc-handler-function
-;;          '(lambda (destdir)
-;;	     (notmuch-maildir-fcc-write-buffer-to-maildir destdir t)))
-;;
-;; if you want Fcc'd messages to be marked as new:
 ;;
-;;     (setq message-fcc-handler-function
-;;          '(lambda (destdir)
-;;	     (notmuch-maildir-fcc-write-buffer-to-maildir destdir nil)))
+;; To use this as the fcc handler for message-mode,
+;; customize the notmuch-fcc-dirs variable
 
+(require 'message)
 
 (defvar notmuch-maildir-fcc-count 0)
 
+(defcustom notmuch-fcc-dirs nil
+ "If set to non-nil, this will cause message mode to file (fcc) your mail in the specified directory, depending on your From address.
+
+ The first entry (a list) is used as a default fallback
+ when nothing matches. So in the easiest case notmuch-fcc-dirs is
+ just something like ((\"INBOX.Sent\"))
+
+ If you need a more fancy setup, where you want different Outboxes depending
+ on your From address, you use something like this:
+
+     (   (\"defaultinbox\")
+         (\"Sebastian Spaeth <Sebastian@SSpaeth.de>\" . \"privat\")
+         (\"Sebastian Spaeth <SSpaeth@ethz.ch>\" . \"uni\")
+     )
+ 
+ This will constructs a path, concatenating the content of the
+ variable 'message-directory' (a message mode variable
+ customizable via m-x
+ customize-variable<RET>message-directory<RET>) and the second
+ part in the alist."
+ :require 'notmuch-fcc-initialization
+ :group 'notmuch
+)
+
+(defun notmuch-fcc-initialization ()
+  "If notmuch-fcc-directories is set, 
+   hook them into the message-fcc-handler-function"
+(if (not (eq notmuch-fcc-dirs nil)) (progn
+    ;Set up the message-fcc-handler to move mails to the maildir in Fcc
+    ;The parameter is hardcoded to mark messages as "seen"
+    (setq message-fcc-handler-function
+          '(lambda (destdir)
+             (notmuch-maildir-fcc-write-buffer-to-maildir destdir t)))
+    ;add a hook to actually insert the Fcc header when sending
+    ;(preferrably we would use message-header-setup-up, but notmuch-reply 
+    ; munges headers after that is run, so it won't work for replies within
+    ; notmuch)
+    (add-hook 'message-send-hook 'notmuch-fcc-header-setup))))
+
+(defun notmuch-fcc-header-setup ()
+  "Can be added to message-send-hook and will set the FCC header
+      based on the values of notmuch-fcc-directories (see the
+      variable customization there for examples). It uses the
+      first entry as default fallback if no From address
+      matches."
+  ; only do something if notmuch-fcc-dirs is set
+  (if notmuch-fcc-dirs
+   (let ((subdir (cdr (assoc (message-fetch-field "from") notmuch-fcc-dirs))))
+     (if (eq subdir nil) (setq subdir (car (car notmuch-fcc-dirs))))
+     (message-remove-header "Fcc")
+     (message-add-header (concat "Fcc: " message-directory subdir)))))
+
 (defun notmuch-maildir-fcc-host-fixer (hostname)
   (replace-regexp-in-string "/\\|:"
 			    '(lambda (s)
-			         (cond ((string-equal s "/") "\\057")
-				       ((string-equal s ":") "\\072")
-				       (t s)))
+                               (cond ((string-equal s "/") "\\057")
+                                     ((string-equal s ":") "\\072")
+                                     (t s)))
 			    hostname
 			    t
 			    t))
@@ -114,4 +147,5 @@ return t if successful, and nil otherwise."
 	  (delete-file (concat destdir "/tmp/" msg-id))))
       t)))
 
+(notmuch-fcc-initialization)
 (provide 'notmuch-maildir-fcc)
-- 
1.7.0.4

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

* Re: [PATCH 7/7] Integrate notmuch-fcc mechansim
  2010-04-23  9:38   ` [PATCH 7/7] " Sebastian Spaeth
@ 2010-04-24 23:10     ` Dirk Hohndel
  2010-04-26  8:23       ` [PATCH v3 1/4] Add elisp file for FCC to maildir solution Sebastian Spaeth
                         ` (3 more replies)
  0 siblings, 4 replies; 35+ messages in thread
From: Dirk Hohndel @ 2010-04-24 23:10 UTC (permalink / raw)
  To: Sebastian Spaeth, Notmuch developer list


On Fri, 23 Apr 2010 11:38:57 +0200, Sebastian Spaeth <Sebastian@SSpaeth.de> wrote:
> I have gone wild and added a defcustom "notmuch-fcc-dirs".
> Depending on the value of that variable we will not do any
> maildir fcc at all (nil, the default), or it is of the format
> (("defaultsentbox")
>  ("full name <email@address>" . "Work/sentbox")
>  ("full name2 <email2@address2>" . "Work2/sentbox"))

I love this feature (unsurprising, as I was the one who requested it
repeatedly...).

One question from the elisp noop:

> +   (let ((subdir (cdr (assoc (message-fetch-field "from") notmuch-fcc-dirs))))

Why not make this 

    (let ((subdir (cdr (assoc-string (message-fetch-field "from") notmuch-fcc-dirs t))))

and have the association be case insensitive?

/D

-- 
Dirk Hohndel
Intel Open Source Technology Center

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

* [PATCH v3 1/4] Add elisp file for FCC to maildir solution
  2010-04-24 23:10     ` Dirk Hohndel
@ 2010-04-26  8:23       ` Sebastian Spaeth
  2010-04-26 21:21         ` Carl Worth
  2010-04-26  8:23       ` [PATCH v3 2/4] Integrate notmuch-maildir-fcc into notmuch Sebastian Spaeth
                         ` (2 subsequent siblings)
  3 siblings, 1 reply; 35+ messages in thread
From: Sebastian Spaeth @ 2010-04-26  8:23 UTC (permalink / raw)
  To: Notmuch developer list

From: Jesse Rosenthal <jrosenthal@jhu.edu>

File grabbed from http://jkr.acm.jhu.edu/jkr-maildir.el
but not integrated yet.

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
---
 The patch series needed rebasing as it conflicts now with some of the
 notmuch-hello and notmuch-mua additions. Also, I integrated Dirk's proposal
 to use assoc-string which makes the address lookup case-insensitive.

 emacs/notmuch-maildir-fcc.el |  115 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 115 insertions(+), 0 deletions(-)
 create mode 100644 emacs/notmuch-maildir-fcc.el

diff --git a/emacs/notmuch-maildir-fcc.el b/emacs/notmuch-maildir-fcc.el
new file mode 100644
index 0000000..979428e
--- /dev/null
+++ b/emacs/notmuch-maildir-fcc.el
@@ -0,0 +1,115 @@
+;; This file is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published
+;; by the Free Software Foundation; either version 2, or (at your
+;; option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
+
+;; Commentary:
+;;
+;; This is the beginning of a solution for storing sent mail in a
+;; maildir in emacs message mode, presented because some people might
+;; find it useful. It is *not* fully tested, it *may* overwrite files,
+;; and any directories you point this at may no longer be there
+;; afterwards. Use at your own risk.
+;;
+;; To use this as the fcc handler for message-mode, put
+;; one of the following in your init file:
+;;
+;; if you want Fcc'd messages to be marked as read:
+;;
+;;     (setq message-fcc-handler-function
+;;          '(lambda (destdir)
+;;	     (jkr/maildir-write-buffer-to-maildir destdir t)))
+;;
+;; if you want Fcc'd messages to be marked as new:
+;;
+;;     (setq message-fcc-handler-function
+;;          '(lambda (destdir)
+;;	     (jkr/maildir-write-buffer-to-maildir destdir nil)))
+
+
+(defvar jkr/maildir-count 0)
+
+(defun jkr/maildir-host-fixer (hostname)
+  (replace-regexp-in-string "/\\|:"
+			    '(lambda (s)
+			         (cond ((string-equal s "/") "\\057")
+				       ((string-equal s ":") "\\072")
+				       (t s)))
+			    hostname
+			    t
+			    t))
+
+(defun jkr/maildir-make-uniq-maildir-id ()
+   (let* ((ct (current-time))
+	  (timeid (+ (* (car ct) 65536) (cadr ct)))
+	  (microseconds (caddr ct))
+	  (hostname (jkr/maildir-host-fixer system-name)))
+     (setq jkr/maildir-count (+ jkr/maildir-count 1))
+     (format "%d.%d_%d_%d.%s"
+	     timeid
+	     (emacs-pid)
+	     microseconds
+	     jkr/maildir-count
+	     hostname)))
+
+(defun jkr/maildir-dir-is-maildir-p (dir)
+  (and (file-exists-p (concat dir "/cur/"))
+       (file-exists-p (concat dir "/new/"))
+       (file-exists-p (concat dir "/tmp/"))))
+
+(defun jkr/maildir-save-buffer-to-tmp (destdir)
+  "Returns the msg id of the message written to the temp directory
+if successful, nil if not."
+  (let ((msg-id (jkr/maildir-make-uniq-maildir-id)))
+    (while (file-exists-p (concat destdir "/tmp/" msg-id))
+      (setq msg-id (jkr/maildir-make-uniq-maildir-id)))
+    (cond ((jkr/maildir-dir-is-maildir-p destdir)
+	   (write-file (concat destdir "/tmp/" msg-id))
+	   msg-id)
+	  (t
+	   (message (format "Can't write to %s. Not a maildir."
+		     destdir))
+	   nil))))
+
+(defun jkr/maildir-move-tmp-to-new (destdir msg-id)
+  (add-name-to-file
+   (concat destdir "/tmp/" msg-id)
+   (concat destdir "/new/" msg-id ":2,")))
+
+(defun jkr/maildir-move-tmp-to-cur (destdir msg-id &optional mark-seen)
+  (add-name-to-file
+   (concat destdir "/tmp/" msg-id)
+   (concat destdir "/cur/" msg-id ":2," (when mark-seen "S"))))
+
+(defun jkr/maildir-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
+return t if successful, and nil otherwise."
+  (let ((orig-buffer (buffer-name)))
+    (with-temp-buffer
+      (insert-buffer orig-buffer)
+      (catch 'link-error
+	(let ((msg-id (jkr/maildir-save-buffer-to-tmp destdir)))
+	  (when msg-id
+	    (cond (mark-seen
+		   (condition-case err
+		       (jkr/maildir-move-tmp-to-cur destdir msg-id t)
+		     (file-already-exists
+		      (throw 'link-error nil))))
+		  (t
+		   (condition-case err
+		       (jkr/maildir-move-tmp-to-new destdir msg-id)
+		     (file-already-exists
+		      (throw 'link-error nil))))))
+	  (delete-file (concat destdir "/tmp/" msg-id))))
+      t)))
\ No newline at end of file
-- 
1.7.0.4

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

* [PATCH v3 2/4] Integrate notmuch-maildir-fcc into notmuch
  2010-04-24 23:10     ` Dirk Hohndel
  2010-04-26  8:23       ` [PATCH v3 1/4] Add elisp file for FCC to maildir solution Sebastian Spaeth
@ 2010-04-26  8:23       ` Sebastian Spaeth
  2010-04-26  8:23       ` [PATCH v3 3/4] notmuch-maildir-fcc: elisp syntax fixes Sebastian Spaeth
  2010-04-26  8:23       ` [PATCH v3 4/4] Integrate notmuch-fcc mechansim Sebastian Spaeth
  3 siblings, 0 replies; 35+ messages in thread
From: Sebastian Spaeth @ 2010-04-26  8:23 UTC (permalink / raw)
  To: Notmuch developer list

Require notmuch-maildir-fcc and also install it.
Rename all jkr/* functions to notmuch-maildir-fcc-*

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
---
 emacs/Makefile.local         |    1 +
 emacs/notmuch-maildir-fcc.el |   42 ++++++++++++++++++++++--------------------
 emacs/notmuch.el             |    1 +
 3 files changed, 24 insertions(+), 20 deletions(-)

diff --git a/emacs/Makefile.local b/emacs/Makefile.local
index 7537c3d..e446f97 100644
--- a/emacs/Makefile.local
+++ b/emacs/Makefile.local
@@ -9,6 +9,7 @@ emacs_sources := \
 	$(dir)/notmuch-wash.el \
 	$(dir)/notmuch-hello.el \
 	$(dir)/notmuch-mua.el \
+	$(dir)/notmuch-maildir-fcc.el \
 	$(dir)/notmuch-address.el
 
 emacs_images := \
diff --git a/emacs/notmuch-maildir-fcc.el b/emacs/notmuch-maildir-fcc.el
index 979428e..2117f54 100644
--- a/emacs/notmuch-maildir-fcc.el
+++ b/emacs/notmuch-maildir-fcc.el
@@ -28,18 +28,18 @@
 ;;
 ;;     (setq message-fcc-handler-function
 ;;          '(lambda (destdir)
-;;	     (jkr/maildir-write-buffer-to-maildir destdir t)))
+;;	     (notmuch-maildir-fcc-write-buffer-to-maildir destdir t)))
 ;;
 ;; if you want Fcc'd messages to be marked as new:
 ;;
 ;;     (setq message-fcc-handler-function
 ;;          '(lambda (destdir)
-;;	     (jkr/maildir-write-buffer-to-maildir destdir nil)))
+;;	     (notmuch-maildir-fcc-write-buffer-to-maildir destdir nil)))
 
 
-(defvar jkr/maildir-count 0)
+(defvar notmuch-maildir-fcc-count 0)
 
-(defun jkr/maildir-host-fixer (hostname)
+(defun notmuch-maildir-fcc-host-fixer (hostname)
   (replace-regexp-in-string "/\\|:"
 			    '(lambda (s)
 			         (cond ((string-equal s "/") "\\057")
@@ -49,31 +49,31 @@
 			    t
 			    t))
 
-(defun jkr/maildir-make-uniq-maildir-id ()
+(defun notmuch-maildir-fcc-make-uniq-maildir-id ()
    (let* ((ct (current-time))
 	  (timeid (+ (* (car ct) 65536) (cadr ct)))
 	  (microseconds (caddr ct))
-	  (hostname (jkr/maildir-host-fixer system-name)))
-     (setq jkr/maildir-count (+ jkr/maildir-count 1))
+	  (hostname (notmuch-maildir-fcc-host-fixer system-name)))
+     (setq notmuch-maildir-fcc-count (+ notmuch-maildir-fcc-count 1))
      (format "%d.%d_%d_%d.%s"
 	     timeid
 	     (emacs-pid)
 	     microseconds
-	     jkr/maildir-count
+	     notmuch-maildir-fcc-count
 	     hostname)))
 
-(defun jkr/maildir-dir-is-maildir-p (dir)
+(defun notmuch-maildir-fcc-dir-is-maildir-p (dir)
   (and (file-exists-p (concat dir "/cur/"))
        (file-exists-p (concat dir "/new/"))
        (file-exists-p (concat dir "/tmp/"))))
 
-(defun jkr/maildir-save-buffer-to-tmp (destdir)
+(defun notmuch-maildir-fcc-save-buffer-to-tmp (destdir)
   "Returns the msg id of the message written to the temp directory
 if successful, nil if not."
-  (let ((msg-id (jkr/maildir-make-uniq-maildir-id)))
+  (let ((msg-id (notmuch-maildir-fcc-make-uniq-maildir-id)))
     (while (file-exists-p (concat destdir "/tmp/" msg-id))
-      (setq msg-id (jkr/maildir-make-uniq-maildir-id)))
-    (cond ((jkr/maildir-dir-is-maildir-p destdir)
+      (setq msg-id (notmuch-maildir-fcc-make-uniq-maildir-id)))
+    (cond ((notmuch-maildir-fcc-dir-is-maildir-p destdir)
 	   (write-file (concat destdir "/tmp/" msg-id))
 	   msg-id)
 	  (t
@@ -81,17 +81,17 @@ if successful, nil if not."
 		     destdir))
 	   nil))))
 
-(defun jkr/maildir-move-tmp-to-new (destdir msg-id)
+(defun notmuch-maildir-fcc-move-tmp-to-new (destdir msg-id)
   (add-name-to-file
    (concat destdir "/tmp/" msg-id)
    (concat destdir "/new/" msg-id ":2,")))
 
-(defun jkr/maildir-move-tmp-to-cur (destdir msg-id &optional mark-seen)
+(defun notmuch-maildir-fcc-move-tmp-to-cur (destdir msg-id &optional mark-seen)
   (add-name-to-file
    (concat destdir "/tmp/" msg-id)
    (concat destdir "/cur/" msg-id ":2," (when mark-seen "S"))))
 
-(defun jkr/maildir-write-buffer-to-maildir (destdir &optional mark-seen)
+(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
 return t if successful, and nil otherwise."
@@ -99,17 +99,19 @@ return t if successful, and nil otherwise."
     (with-temp-buffer
       (insert-buffer orig-buffer)
       (catch 'link-error
-	(let ((msg-id (jkr/maildir-save-buffer-to-tmp destdir)))
+	(let ((msg-id (notmuch-maildir-fcc-save-buffer-to-tmp destdir)))
 	  (when msg-id
 	    (cond (mark-seen
 		   (condition-case err
-		       (jkr/maildir-move-tmp-to-cur destdir msg-id t)
+		       (notmuch-maildir-fcc-move-tmp-to-cur destdir msg-id t)
 		     (file-already-exists
 		      (throw 'link-error nil))))
 		  (t
 		   (condition-case err
-		       (jkr/maildir-move-tmp-to-new destdir msg-id)
+		       (notmuch-maildir-fcc-move-tmp-to-new destdir msg-id)
 		     (file-already-exists
 		      (throw 'link-error nil))))))
 	  (delete-file (concat destdir "/tmp/" msg-id))))
-      t)))
\ No newline at end of file
+      t)))
+
+(provide 'notmuch-maildir-fcc)
diff --git a/emacs/notmuch.el b/emacs/notmuch.el
index 378c004..127af2c 100644
--- a/emacs/notmuch.el
+++ b/emacs/notmuch.el
@@ -54,6 +54,7 @@
 (require 'notmuch-lib)
 (require 'notmuch-show)
 (require 'notmuch-mua)
+(require 'notmuch-maildir-fcc)
 
 (defcustom notmuch-search-result-format
   `(("date" . "%s ")
-- 
1.7.0.4

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

* [PATCH v3 3/4] notmuch-maildir-fcc: elisp syntax fixes
  2010-04-24 23:10     ` Dirk Hohndel
  2010-04-26  8:23       ` [PATCH v3 1/4] Add elisp file for FCC to maildir solution Sebastian Spaeth
  2010-04-26  8:23       ` [PATCH v3 2/4] Integrate notmuch-maildir-fcc into notmuch Sebastian Spaeth
@ 2010-04-26  8:23       ` Sebastian Spaeth
  2010-04-26  8:23       ` [PATCH v3 4/4] Integrate notmuch-fcc mechansim Sebastian Spaeth
  3 siblings, 0 replies; 35+ messages in thread
From: Sebastian Spaeth @ 2010-04-26  8:23 UTC (permalink / raw)
  To: Notmuch developer list

1)use insert-buffer-substring

Rather than the insert-buffer. Emacs complains that it is for interactive use
and not for use within elisp. So use insert-buffer-substring which does the
same thing when not handed any 'begin' 'end' parameters.

2)replace caddr with (car (cdr (cdr)))

The former requires 'cl to be loaded and during make install emacs complained
about not knowing it.

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
---
 emacs/notmuch-maildir-fcc.el |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/emacs/notmuch-maildir-fcc.el b/emacs/notmuch-maildir-fcc.el
index 2117f54..84f4187 100644
--- a/emacs/notmuch-maildir-fcc.el
+++ b/emacs/notmuch-maildir-fcc.el
@@ -52,7 +52,7 @@
 (defun notmuch-maildir-fcc-make-uniq-maildir-id ()
    (let* ((ct (current-time))
 	  (timeid (+ (* (car ct) 65536) (cadr ct)))
-	  (microseconds (caddr ct))
+	  (microseconds (car (cdr (cdr ct))))
 	  (hostname (notmuch-maildir-fcc-host-fixer system-name)))
      (setq notmuch-maildir-fcc-count (+ notmuch-maildir-fcc-count 1))
      (format "%d.%d_%d_%d.%s"
@@ -97,7 +97,7 @@ non-nil, it will write it to cur/, and mark it as read. It should
 return t if successful, and nil otherwise."
   (let ((orig-buffer (buffer-name)))
     (with-temp-buffer
-      (insert-buffer orig-buffer)
+      (insert-buffer-substring orig-buffer)
       (catch 'link-error
 	(let ((msg-id (notmuch-maildir-fcc-save-buffer-to-tmp destdir)))
 	  (when msg-id
-- 
1.7.0.4

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

* [PATCH v3 4/4] Integrate notmuch-fcc mechansim
  2010-04-24 23:10     ` Dirk Hohndel
                         ` (2 preceding siblings ...)
  2010-04-26  8:23       ` [PATCH v3 3/4] notmuch-maildir-fcc: elisp syntax fixes Sebastian Spaeth
@ 2010-04-26  8:23       ` Sebastian Spaeth
  2010-04-27  0:29         ` [PATCH] emacs: fcc should fail at the right time if it doesn't point to a maildir Jesse Rosenthal
  3 siblings, 1 reply; 35+ messages in thread
From: Sebastian Spaeth @ 2010-04-26  8:23 UTC (permalink / raw)
  To: Notmuch developer list

I have gone wild and added a defcustom "notmuch-fcc-dirs".
Depending on the value of that variable we will not do any
maildir fcc at all (nil, the default), or it is of the format
(("defaultsentbox")
 ("full name <email@address>" . "Work/sentbox")
 ("full name2 <email2@address2>" . "Work2/sentbox"))

The outbox name will be concatenated with the message mode
variable "message-directory" which is "~/Mail/" by default.

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
---
 emacs/notmuch-maildir-fcc.el |   85 +++++++++++++++++++++++++++++------------
 1 files changed, 60 insertions(+), 25 deletions(-)

diff --git a/emacs/notmuch-maildir-fcc.el b/emacs/notmuch-maildir-fcc.el
index 84f4187..cab1d59 100644
--- a/emacs/notmuch-maildir-fcc.el
+++ b/emacs/notmuch-maildir-fcc.el
@@ -12,39 +12,73 @@
 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 ;; Boston, MA 02110-1301, USA.
-
-;; Commentary:
-;;
-;; This is the beginning of a solution for storing sent mail in a
-;; maildir in emacs message mode, presented because some people might
-;; find it useful. It is *not* fully tested, it *may* overwrite files,
-;; and any directories you point this at may no longer be there
-;; afterwards. Use at your own risk.
-;;
-;; To use this as the fcc handler for message-mode, put
-;; one of the following in your init file:
-;;
-;; if you want Fcc'd messages to be marked as read:
 ;;
-;;     (setq message-fcc-handler-function
-;;          '(lambda (destdir)
-;;	     (notmuch-maildir-fcc-write-buffer-to-maildir destdir t)))
-;;
-;; if you want Fcc'd messages to be marked as new:
-;;
-;;     (setq message-fcc-handler-function
-;;          '(lambda (destdir)
-;;	     (notmuch-maildir-fcc-write-buffer-to-maildir destdir nil)))
+;; To use this as the fcc handler for message-mode,
+;; customize the notmuch-fcc-dirs variable
 
+(require 'message)
 
 (defvar notmuch-maildir-fcc-count 0)
 
+(defcustom notmuch-fcc-dirs nil
+ "If set to non-nil, this will cause message mode to file (fcc) your mail in the specified directory, depending on your From address.
+
+ The first entry (a list) is used as a default fallback
+ when nothing matches. So in the easiest case notmuch-fcc-dirs is
+ just something like ((\"INBOX.Sent\"))
+
+ If you need a more fancy setup, where you want different Outboxes depending
+ on your From address, you use something like this:
+
+     (   (\"defaultinbox\")
+         (\"Sebastian Spaeth <Sebastian@SSpaeth.de>\" . \"privat\")
+         (\"Sebastian Spaeth <SSpaeth@ethz.ch>\" . \"uni\")
+     )
+
+ This will constructs a path, concatenating the content of the
+ variable 'message-directory' (a message mode variable
+ customizable via m-x
+ customize-variable<RET>message-directory<RET>) and the second
+ part in the alist."
+ :require 'notmuch-fcc-initialization
+ :group 'notmuch
+)
+
+(defun notmuch-fcc-initialization ()
+  "If notmuch-fcc-directories is set,
+   hook them into the message-fcc-handler-function"
+(if (not (eq notmuch-fcc-dirs nil)) (progn
+    ;Set up the message-fcc-handler to move mails to the maildir in Fcc
+    ;The parameter is hardcoded to mark messages as "seen"
+    (setq message-fcc-handler-function
+          '(lambda (destdir)
+             (notmuch-maildir-fcc-write-buffer-to-maildir destdir t)))
+    ;add a hook to actually insert the Fcc header when sending
+    ;(preferrably we would use message-header-setup-up, but notmuch-reply
+    ; munges headers after that is run, so it won't work for replies within
+    ; notmuch)
+    (add-hook 'message-send-hook 'notmuch-fcc-header-setup))))
+
+(defun notmuch-fcc-header-setup ()
+  "Can be added to message-send-hook and will set the FCC header
+      based on the values of notmuch-fcc-directories (see the
+      variable customization there for examples). It uses the
+      first entry as default fallback if no From address
+      matches."
+  ; only do something if notmuch-fcc-dirs is set
+  (if notmuch-fcc-dirs
+   (let ((subdir 
+          (cdr (assoc-string (message-fetch-field "from") notmuch-fcc-dirs t))))
+     (if (eq subdir nil) (setq subdir (car (car notmuch-fcc-dirs))))
+     (message-remove-header "Fcc")
+     (message-add-header (concat "Fcc: " message-directory subdir)))))
+
 (defun notmuch-maildir-fcc-host-fixer (hostname)
   (replace-regexp-in-string "/\\|:"
 			    '(lambda (s)
-			         (cond ((string-equal s "/") "\\057")
-				       ((string-equal s ":") "\\072")
-				       (t s)))
+                               (cond ((string-equal s "/") "\\057")
+                                     ((string-equal s ":") "\\072")
+                                     (t s)))
 			    hostname
 			    t
 			    t))
@@ -114,4 +148,5 @@ return t if successful, and nil otherwise."
 	  (delete-file (concat destdir "/tmp/" msg-id))))
       t)))
 
+(notmuch-fcc-initialization)
 (provide 'notmuch-maildir-fcc)
-- 
1.7.0.4

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

* Re: [PATCH v3 1/4] Add elisp file for FCC to maildir solution
  2010-04-26  8:23       ` [PATCH v3 1/4] Add elisp file for FCC to maildir solution Sebastian Spaeth
@ 2010-04-26 21:21         ` Carl Worth
  0 siblings, 0 replies; 35+ messages in thread
From: Carl Worth @ 2010-04-26 21:21 UTC (permalink / raw)
  To: Sebastian Spaeth, Notmuch developer list

[-- Attachment #1: Type: text/plain, Size: 2912 bytes --]

On Mon, 26 Apr 2010 10:23:15 +0200, Sebastian Spaeth <Sebastian@SSpaeth.de> wrote:
>  The patch series needed rebasing as it conflicts now with some of the
>  notmuch-hello and notmuch-mua additions. Also, I integrated Dirk's proposal
>  to use assoc-string which makes the address lookup case-insensitive.

I've merged this now and pushed it out.

I did have some problems trying to use it, which would have prevented me
From pushing it out, I think, except that it's disabled by default.

But I would actually prefer to have it enabled by default. Here are
some of the things I'd like to see changed to let us do that:

  * Insert a '/' if necessary when concatenating message-directory to
    the configured directory.

	I was getting "/home/cworth/mailsent" instead of
	"/home/cworth/mail/sent" here.

  * Make the Fcc header visible when composing a message

	Otherwise it's hard to know if the Fcc code is actually getting
	invoked.

	And we do enough magic here, (the above concatenation, and also
	the a-list matching), that we should let the user see the
	result. This would also allow the user to modify the Fcc if
	necessary.

  * Fix code to create directories as necessary

	If I manually added an Fcc header before sending, (even by just
	calling `notmuch-fcc-header-setup'), there is some prompting
	about creating the directory. But I don't get this when I just
	send a message (hoping that the Fcc stuff will kick in and do
	it's job).

	But even if I just point to a directory, (without cur, new, and
	tmp), it complains about "not a regular file" or something. I'd
	rather just have it create the directories it needs.

	I found by trial-and-error that if I do create the directory,
	*and* create the cur, new, and tmp sub-directories then the Fcc
	actually works.

  * Fix to not depend on message-directory variable

	I think a relative directory specification should be interpreted
	with respect to the notmuch mail directory. It's currently a
	little "tricky" for emacs code to get at that since we don't
	have a command-line interface for querying configuration. [Does
	emacs have a parser for this style of configuration file?]

	The quick fix is probably to accept only an absolute path and
	fixup the documentation. Then if someone wants to do the
	additional work to support a directory relative to the notmuch
	mail directory, then that could be done in the future.

With the above in place, I'd be happy to have this all enabled by
default. (But I guess we can't do that until we *do* have support for a
relative path since notmuch won't be able to construct an absolute path
without configuration.)

Then, we can remove the Bcc from the "notmuch reply" output. And we can
add an option for people who would prefer Bcc (or even Cc as keithp
wants) instead of Fcc.

That will all be later than 0.3 though.

-Carl

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

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

* [PATCH] emacs: fcc should fail at the right time if it doesn't point to a maildir
  2010-04-26  8:23       ` [PATCH v3 4/4] Integrate notmuch-fcc mechansim Sebastian Spaeth
@ 2010-04-27  0:29         ` Jesse Rosenthal
  2010-04-27  1:33           ` [PATCH] emacs: add prompt to create maildir for fcc if it does not exist Jesse Rosenthal
                             ` (2 more replies)
  0 siblings, 3 replies; 35+ messages in thread
From: Jesse Rosenthal @ 2010-04-27  0:29 UTC (permalink / raw)
  To: Sebastian Spaeth, Notmuch developer list

Throw an error after the maildir is generated but before the message
is sent. This change allows the user to edit the maildir if it fails,
so that it will point to a correct place.

Note that this changes the previous behavior which always overwrote
the existing Fcc line. Now, an Fcc line is only auto-generated if
there isn't one already there.

The ideal change would be to prompt to create a maildir. This should
enable a place for doing that in a future patch.
---
 emacs/notmuch-maildir-fcc.el |    7 +++++--
 1 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/emacs/notmuch-maildir-fcc.el b/emacs/notmuch-maildir-fcc.el
index 64f60bc..34b1915 100644
--- a/emacs/notmuch-maildir-fcc.el
+++ b/emacs/notmuch-maildir-fcc.el
@@ -70,8 +70,11 @@
    (let ((subdir
           (cdr (assoc-string (message-fetch-field "from") notmuch-fcc-dirs t))))
      (if (eq subdir nil) (setq subdir (car (car notmuch-fcc-dirs))))
-     (message-remove-header "Fcc")
-     (message-add-header (concat "Fcc: " message-directory subdir)))))
+     (unless (message-fetch-field "fcc")
+       (message-add-header (concat "Fcc: " message-directory subdir)))
+     (unless (notmuch-maildir-fcc-dir-is-maildir-p 
+	      (message-fetch-field "fcc"))
+       (error (format "%s is not a maildir." (message-fetch-field "fcc")))))))
 
 (defun notmuch-maildir-fcc-host-fixer (hostname)
   (replace-regexp-in-string "/\\|:"
-- 
1.6.3.3

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

* [PATCH] emacs: add prompt to create maildir for fcc if it does not exist.
  2010-04-27  0:29         ` [PATCH] emacs: fcc should fail at the right time if it doesn't point to a maildir Jesse Rosenthal
@ 2010-04-27  1:33           ` Jesse Rosenthal
  2010-04-27  3:08             ` [PATCH] emacs: Ensure that message-directory for Fcc has a trailing slash Jesse Rosenthal
  2010-04-27  2:51           ` [PATCH] emacs: fcc should fail at the right time if it doesn't point to a maildir Dirk Hohndel
  2010-04-27  6:10           ` Carl Worth
  2 siblings, 1 reply; 35+ messages in thread
From: Jesse Rosenthal @ 2010-04-27  1:33 UTC (permalink / raw)
  To: Sebastian Spaeth, Notmuch developer list


If the user specifies a maildir that does not exist, prompt the user to
see whether a maildir should be created. This will fail, with the
relevant explanation, if the location is not writable, or if a file
already exists in that location. If the location is a dir, but not a
maildir, this will add /tmp/cur/new to it.
---
NB. This builds on the patch in the parent email:
id:87mxwpd9g8.fsf@jhu.edu

 emacs/notmuch-maildir-fcc.el |   26 ++++++++++++++++++++++----
 1 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/emacs/notmuch-maildir-fcc.el b/emacs/notmuch-maildir-fcc.el
index 34b1915..6d75b11 100644
--- a/emacs/notmuch-maildir-fcc.el
+++ b/emacs/notmuch-maildir-fcc.el
@@ -72,10 +72,16 @@
      (if (eq subdir nil) (setq subdir (car (car notmuch-fcc-dirs))))
      (unless (message-fetch-field "fcc")
        (message-add-header (concat "Fcc: " message-directory subdir)))
-     (unless (notmuch-maildir-fcc-dir-is-maildir-p 
-	      (message-fetch-field "fcc"))
-       (error (format "%s is not a maildir." (message-fetch-field "fcc")))))))
-
+     (let ((fcc-header (message-fetch-field "fcc")))
+     (unless (notmuch-maildir-fcc-dir-is-maildir-p fcc-header)
+       (cond ((not (file-writable-p fcc-header))
+	      (error (format "%s is not a maildir, but you don't have permission to create one." 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 "Not sending message."))))))))
+	      
 (defun notmuch-maildir-fcc-host-fixer (hostname)
   (replace-regexp-in-string "/\\|:"
 			    '(lambda (s)
@@ -104,6 +110,18 @@
        (file-exists-p (concat dir "/new/"))
        (file-exists-p (concat dir "/tmp/"))))
 
+(defun notmuch-maildir-fcc-create-maildir (path)
+  (cond ((or (not (file-exists-p path)) (file-directory-p path))
+	 (make-directory (concat path "/cur/") t)
+	 (make-directory (concat path "/new/") t)
+	 (make-directory (concat path "/tmp/") t))
+	((file-regular-p path)
+	 (error "%s is a file. Can't creat maildir." path))
+	(t
+	 (error "I don't know how to create a maildir here"))))
+	 
+  
+
 (defun notmuch-maildir-fcc-save-buffer-to-tmp (destdir)
   "Returns the msg id of the message written to the temp directory
 if successful, nil if not."
-- 
1.6.3.3

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

* Re: [PATCH] emacs: fcc should fail at the right time if it doesn't point to a maildir
  2010-04-27  0:29         ` [PATCH] emacs: fcc should fail at the right time if it doesn't point to a maildir Jesse Rosenthal
  2010-04-27  1:33           ` [PATCH] emacs: add prompt to create maildir for fcc if it does not exist Jesse Rosenthal
@ 2010-04-27  2:51           ` Dirk Hohndel
  2010-04-27  6:10           ` Carl Worth
  2 siblings, 0 replies; 35+ messages in thread
From: Dirk Hohndel @ 2010-04-27  2:51 UTC (permalink / raw)
  To: Jesse Rosenthal, Sebastian Spaeth, Notmuch developer list

On Mon, 26 Apr 2010 20:29:27 -0400, Jesse Rosenthal <jrosenthal@jhu.edu> wrote:
> Throw an error after the maildir is generated but before the message
> is sent. This change allows the user to edit the maildir if it fails,
> so that it will point to a correct place.
> 
> Note that this changes the previous behavior which always overwrote
> the existing Fcc line. Now, an Fcc line is only auto-generated if
> there isn't one already there.

I like this behavior
 
> The ideal change would be to prompt to create a maildir. This should
> enable a place for doing that in a future patch.

It would also be nice to catch the common mistake of not ending the
message-directory with a /

/D


-- 
Dirk Hohndel
Intel Open Source Technology Center

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

* [PATCH] emacs: Ensure that message-directory for Fcc has a trailing slash
  2010-04-27  1:33           ` [PATCH] emacs: add prompt to create maildir for fcc if it does not exist Jesse Rosenthal
@ 2010-04-27  3:08             ` Jesse Rosenthal
  0 siblings, 0 replies; 35+ messages in thread
From: Jesse Rosenthal @ 2010-04-27  3:08 UTC (permalink / raw)
  To: Sebastian Spaeth, Notmuch developer list


Use `file-name-as-directory' to ensure that message-directory has a
trailing slash so it can be combined with the notmuch-fcc-dirs
correctly.
---
 emacs/notmuch-maildir-fcc.el |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)

diff --git a/emacs/notmuch-maildir-fcc.el b/emacs/notmuch-maildir-fcc.el
index 6d75b11..ecb65e7 100644
--- a/emacs/notmuch-maildir-fcc.el
+++ b/emacs/notmuch-maildir-fcc.el
@@ -71,7 +71,9 @@
           (cdr (assoc-string (message-fetch-field "from") notmuch-fcc-dirs t))))
      (if (eq subdir nil) (setq subdir (car (car notmuch-fcc-dirs))))
      (unless (message-fetch-field "fcc")
-       (message-add-header (concat "Fcc: " message-directory subdir)))
+       (message-add-header (concat "Fcc: " 
+				   (file-name-as-directory message-directory) 
+				   subdir)))
      (let ((fcc-header (message-fetch-field "fcc")))
      (unless (notmuch-maildir-fcc-dir-is-maildir-p fcc-header)
        (cond ((not (file-writable-p fcc-header))
-- 
1.6.3.3

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

* Re: [PATCH] emacs: fcc should fail at the right time if it doesn't point to a maildir
  2010-04-27  0:29         ` [PATCH] emacs: fcc should fail at the right time if it doesn't point to a maildir Jesse Rosenthal
  2010-04-27  1:33           ` [PATCH] emacs: add prompt to create maildir for fcc if it does not exist Jesse Rosenthal
  2010-04-27  2:51           ` [PATCH] emacs: fcc should fail at the right time if it doesn't point to a maildir Dirk Hohndel
@ 2010-04-27  6:10           ` Carl Worth
  2 siblings, 0 replies; 35+ messages in thread
From: Carl Worth @ 2010-04-27  6:10 UTC (permalink / raw)
  To: Jesse Rosenthal, Sebastian Spaeth, Notmuch developer list

[-- Attachment #1: Type: text/plain, Size: 349 bytes --]

On Mon, 26 Apr 2010 20:29:27 -0400, Jesse Rosenthal <jrosenthal@jhu.edu> wrote:
> Throw an error after the maildir is generated but before the message
> is sent. This change allows the user to edit the maildir if it fails,
> so that it will point to a correct place.

Very nice. Just in time for 0.3, I merged this in, (and the 2
followups).

-Carl

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

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

end of thread, other threads:[~2010-04-27  6:10 UTC | newest]

Thread overview: 35+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-01-21 18:36 Fcc, Maildir, and Emacs message-mode -- a bit of code Jesse Rosenthal
2010-01-27 14:44 ` Jameson Rollins
2010-01-29 13:21   ` Sebastian Spaeth
2010-01-29 14:46     ` Jameson Rollins
2010-01-29 14:54     ` Jesse Rosenthal
2010-04-07 20:04   ` Dirk Hohndel
2010-04-22  9:06 ` Sebastian Spaeth
2010-04-22  9:07   ` [PATCH 1/6] Add elisp file for FCC to maildir solution Sebastian Spaeth
2010-04-22  9:07   ` [PATCH] notmuch.el: Make notmuch-show buffer name first subject, instead of thread-id (supersedes V1--3) Sebastian Spaeth
2010-04-22 13:34     ` Sebastian Spaeth
2010-04-22  9:07   ` [PATCH 2/6] Integrate notmuch-maildir-fcc into notmuch Sebastian Spaeth
2010-04-22  9:07   ` [PATCH 3/6] notmuch-maildir-fcc: rename all jkr/* functions to notmuch-maildir-fcc-* Sebastian Spaeth
2010-04-22  9:07   ` [PATCH 4/6] add documentation example Sebastian Spaeth
2010-04-22  9:07   ` [PATCH 5/6] notmuch-maildir-fcc: use insert-buffer-substring Sebastian Spaeth
2010-04-22  9:07   ` [PATCH 6/6] notmuch-maildir-fcc: replace caddr with (car (cdr (cdr))) Sebastian Spaeth
2010-04-22 23:17   ` Fcc, Maildir, and Emacs message-mode -- a bit of code Dirk Hohndel
2010-04-23  7:13     ` Sebastian Spaeth
2010-04-23 10:08       ` Updated elisp FCC patches (was: Fcc, Maildir, and Emacs message-mode) Sebastian Spaeth
2010-04-23 14:01         ` Dirk Hohndel
2010-04-23 19:01           ` [PATCH 1/4] Add elisp file for FCC to maildir solution Sebastian Spaeth
2010-04-23 19:01           ` [PATCH 2/4] Integrate notmuch-maildir-fcc into notmuch Sebastian Spaeth
2010-04-23 19:01           ` [PATCH 3/4] notmuch-maildir-fcc: elisp syntax fixes Sebastian Spaeth
2010-04-23 19:01           ` [PATCH 4/4] Integrate notmuch-fcc mechansim Sebastian Spaeth
2010-04-23  9:38   ` [PATCH 7/7] " Sebastian Spaeth
2010-04-24 23:10     ` Dirk Hohndel
2010-04-26  8:23       ` [PATCH v3 1/4] Add elisp file for FCC to maildir solution Sebastian Spaeth
2010-04-26 21:21         ` Carl Worth
2010-04-26  8:23       ` [PATCH v3 2/4] Integrate notmuch-maildir-fcc into notmuch Sebastian Spaeth
2010-04-26  8:23       ` [PATCH v3 3/4] notmuch-maildir-fcc: elisp syntax fixes Sebastian Spaeth
2010-04-26  8:23       ` [PATCH v3 4/4] Integrate notmuch-fcc mechansim Sebastian Spaeth
2010-04-27  0:29         ` [PATCH] emacs: fcc should fail at the right time if it doesn't point to a maildir Jesse Rosenthal
2010-04-27  1:33           ` [PATCH] emacs: add prompt to create maildir for fcc if it does not exist Jesse Rosenthal
2010-04-27  3:08             ` [PATCH] emacs: Ensure that message-directory for Fcc has a trailing slash Jesse Rosenthal
2010-04-27  2:51           ` [PATCH] emacs: fcc should fail at the right time if it doesn't point to a maildir Dirk Hohndel
2010-04-27  6:10           ` Carl Worth

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).