--- /dev/null
+Return-Path: <tomi.ollila@iki.fi>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id 71A2B431FAF\r
+ for <notmuch@notmuchmail.org>; Fri, 4 Jul 2014 10:36:52 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
+ autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id JWDgw+fWRNyH for <notmuch@notmuchmail.org>;\r
+ Fri, 4 Jul 2014 10:36:44 -0700 (PDT)\r
+Received: from guru.guru-group.fi (guru.guru-group.fi [46.183.73.34])\r
+ by olra.theworths.org (Postfix) with ESMTP id 9E3A8431FAE\r
+ for <notmuch@notmuchmail.org>; Fri, 4 Jul 2014 10:36:44 -0700 (PDT)\r
+Received: from guru.guru-group.fi (localhost [IPv6:::1])\r
+ by guru.guru-group.fi (Postfix) with ESMTP id 893F7100033;\r
+ Fri, 4 Jul 2014 20:36:38 +0300 (EEST)\r
+From: Tomi Ollila <tomi.ollila@iki.fi>\r
+To: David Bremner <david@tethera.net>, notmuch@notmuchmail.org,\r
+ "Jani Nikula" <jani@nikula.org>\r
+Subject: Re: [PATCH] cli: add a tool for starting new message in the emacs ui\r
+In-Reply-To: <1404244957-3671-1-git-send-email-david@tethera.net>\r
+References: <1396799003-14855-1-git-send-email-jani@nikula.org>\r
+ <1404244957-3671-1-git-send-email-david@tethera.net>\r
+User-Agent: Notmuch/0.18.1+25~gdaf4b6f (http://notmuchmail.org) Emacs/24.3.1\r
+ (x86_64-unknown-linux-gnu)\r
+X-Face: HhBM'cA~<r"^Xv\KRN0P{vn'Y"Kd;zg_y3S[4)KSN~s?O\"QPoL\r
+ $[Xv_BD:i/F$WiEWax}R(MPS`^UaptOGD`*/=@\1lKoVa9tnrg0TW?"r7aRtgk[F\r
+ !)g;OY^,BjTbr)Np:%c_o'jj,Z\r
+Date: Fri, 04 Jul 2014 20:36:38 +0300\r
+Message-ID: <m2k37thvq1.fsf@guru.guru-group.fi>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=utf-8\r
+Content-Transfer-Encoding: quoted-printable\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Fri, 04 Jul 2014 17:36:52 -0000\r
+\r
+On Tue, Jul 01 2014, David Bremner <david@tethera.net> wrote:\r
+\r
+> From: Jani Nikula <jani@nikula.org>\r
+>\r
+> Add a tool to start composing an email in the Notmuch Emacs UI with\r
+> the specified subject, recipients, and message body.\r
+> ---\r
+>\r
+> I added the necessary lines to conf.py to get the man pages built and\r
+> installed\r
+>\r
+> I'd be happier with this if it could start emacs. I tried adding "-a\r
+> ''" to the emacsclient invokation, but that doesn't quite work; the\r
+> message-mode buffer is created in emacs but no frame is displayed. It\r
+> could be a peculiarity of my emacs setup, of course.\r
+\r
+Ah, this -a '' is new acquaintance to me -- I tried -a emacs and that\r
+obviously did not work. It would have been nicer is there is option\r
+to just run emacs instead... (that's what I did in that mailto: patch).\r
+\r
+But there are plenty of other options, which needs at least be discussed,\r
+is some tolerable subset can be agreed.\r
+\r
+First, this will "fail"\r
+\r
+$ emacs --daemon\r
+$ emacsclient --eval '(progn (require 'notmuch) (notmuch-hello))'\r
+\r
+The window is "nowhere"\r
+\r
+$ emacsclient -nw can be used to "attach" to the emacs session and then one\r
+can switch to the ``notmuch-hello`` -window\r
+\r
+-- but, someone may use such a supported setup using emacs/emacsclient\r
+\r
+This "problem" could be tacled so that if emasclient is to be used,\r
+option (to be added) ``-nw`` is not given and stdout (or stderr) is=20\r
+a tty (test -t 1 / test -t 2), after running emacsclient print\r
+a message to the output with content something like:\r
+\r
+"connected to running emacs ... if the access to that emacs is hidden\r
+ you can run ``emacsclient -nw`` to find it..."\r
+\r
+Ok. Using emacsclient could be opportunistic -- in case using it fails\r
+emacs(1) were used instead. The code checking this could be (*):\r
+\r
+ unset ALTERNATE_EDITOR\r
+ if "${EMACSCLIENT:=3Demacsclient}" --eval t >/dev/null 2>&1\r
+ then exec >/dev/null # (ok, use stderr for msg, or change this :D)\r
+ editor=3D$EMACSCLIENT\r
+ else editor=3D${EMACS:-emacs}\r
+ fi\r
+\r
+(*) to save my time, copied from id:1404237992-9456-1-git-send-email-tomi.o=\r
+llila@iki.fi\r
+\r
+When running emacs there is question whether to run it in background or\r
+foreground. Probably the only always working option is to run in foreground\r
+(unless adding an option) -- backgrounding would be possible only when all\r
+of these apply:\r
+ 1) user did not give ``-nw`` option\r
+ 2) DISPLAY is not null or unset\r
+ 3) emacs(1) does have X support!\r
+\r
+3 cannot be determined trivially.\r
+\r
+If backgrounding were supported the only way I see it can be done is:\r
+\r
+bg () {\r
+ perl -e 'use POSIX; exit if fork; POSIX::setsid(); exec @ARGV' "$@"\r
+}\r
+\r
+Ok, then about the tool:\r
+\r
+> doc/conf.py | 4 ++\r
+> doc/man1/notmuch-emacs-mua.rst | 50 +++++++++++++++++\r
+> notmuch-emacs-mua | 122 +++++++++++++++++++++++++++++++++++=\r
+++++++\r
+> 3 files changed, 176 insertions(+)\r
+> create mode 100644 doc/man1/notmuch-emacs-mua.rst\r
+> create mode 100755 notmuch-emacs-mua\r
+>\r
+> diff --git a/doc/conf.py b/doc/conf.py\r
+> index 70ba1b8..8ee19f4 100644\r
+> --- a/doc/conf.py\r
+> +++ b/doc/conf.py\r
+> @@ -74,6 +74,10 @@ man_pages =3D [\r
+> u'creates a plain-text dump of the tags of each message',\r
+> [u'Carl Worth and many others'], 1),\r
+>=20=20\r
+> +('man1/notmuch-emacs-mua','notmuch-emacs-mua',\r
+> + u'send mail with notmuch and emacs',\r
+> + [u'Carl Worth and many others'], 1),\r
+> +\r
+> ('man5/notmuch-hooks','notmuch-hooks',\r
+> u'hooks for notmuch',\r
+> [u'Carl Worth and many others'], 5),\r
+> diff --git a/doc/man1/notmuch-emacs-mua.rst b/doc/man1/notmuch-emacs-mua.=\r
+rst\r
+> new file mode 100644\r
+> index 0000000..6e63818\r
+> --- /dev/null\r
+> +++ b/doc/man1/notmuch-emacs-mua.rst\r
+> @@ -0,0 +1,50 @@\r
+> +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D\r
+> +notmuch-emacs-mua\r
+> +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D\r
+> +\r
+> +SYNOPSIS\r
+> +=3D=3D=3D=3D=3D=3D=3D=3D\r
+> +\r
+> +**notmuch-emacs-mua** [options ...] [<to-address> ...]\r
+> +\r
+> +DESCRIPTION\r
+> +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D\r
+> +\r
+> +Start composing an email in the Notmuch Emacs UI with the specified\r
+> +subject, recipients, and message body.\r
+> +\r
+> +For **notmuch-emacs-mua** to work, you need **emacsclient** and an\r
+> +already running Emacs with a server.\r
+> +\r
+> +Supported options for **notmuch-emacs-mua** include\r
+> +\r
+> + ``-h, --help``\r
+> + Display help.\r
+> +\r
+> + ``-s, --subject=3D``\ <subject>\r
+> + Specify the subject of the message.\r
+> +\r
+> + ``--to=3D``\ <to-address>\r
+> + Specify a recipient (To).\r
+> +\r
+> + ``-c, --cc=3D``\ <cc-address>\r
+> + Specify a carbon-copy (Cc) recipient.\r
+> +\r
+> + ``-b, --bcc=3D``\ <bcc-address>\r
+> + Specify a blind-carbon-copy (Bcc) recipient.\r
+> +\r
+> + ``-i, --body=3D``\ <file>\r
+> + Specify a file to include into the body of the message.\r
+\r
+-i option is consistent with mutt(1). mutt(1) does not have --body option.\r
+therefore I'd suggest that --body takes the body content from command line\r
+instead from file. If there is to be long-option along with -i it could\r
+be --include or --insert..\r
+\r
+> +\r
+> + ``--print``\r
+> + Output the resulting elisp to stdout instead of evaluating it.\r
+> +\r
+> +The supported positional parameters and short options are a compatible\r
+> +subset of the **mutt** MUA command-line options.\r
+> +\r
+> +Options may be specified multiple times.\r
+> +\r
+> +SEE ALSO\r
+> +=3D=3D=3D=3D=3D=3D=3D=3D\r
+> +\r
+> +**notmuch(1)**, **emacsclient(1)**, **mutt(1)**\r
+> diff --git a/notmuch-emacs-mua b/notmuch-emacs-mua\r
+> new file mode 100755\r
+> index 0000000..7f94271\r
+> --- /dev/null\r
+> +++ b/notmuch-emacs-mua\r
+> @@ -0,0 +1,122 @@\r
+> +#!/usr/bin/env bash\r
+> +#\r
+> +# notmuch-emacs-mua - start composing a mail on the command line\r
+> +#\r
+> +# Copyright =C2=A9 2014 Jani Nikula\r
+> +#\r
+> +# This program is free software: you can redistribute it and/or modify\r
+> +# it under the terms of the GNU General Public License as published by\r
+> +# the Free Software Foundation, either version 3 of the License, or\r
+> +# (at your option) any later version.\r
+> +#\r
+> +# This program is distributed in the hope that it will be useful,\r
+> +# but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+> +# GNU General Public License for more details.\r
+> +#\r
+> +# You should have received a copy of the GNU General Public License\r
+> +# along with this program. If not, see http://www.gnu.org/licenses/ .\r
+> +#\r
+> +# Authors: Jani Nikula <jani@nikula.org>\r
+> +#\r
+> +\r
+> +set -eu\r
+> +\r
+> +escape ()\r
+> +{\r
+> + echo "${1//\"/\\\"}"\r
+> +}\r
+\r
+This has 3 issues:\r
+\r
+ 1) This does not quote '\' which can be used to execute arbitrary code\r
+ (is that a feature)\r
+ 2) echo may escape things differently in other bashes\r
+ 3) running subshell just to this escape all args is not required,\r
+ there are bash builtins to do that during same process.\r
+\r
+If one wants to use function, escape -v var arg syntax could be deviced\r
+with\r
+\r
+escape () {\r
+ local var=3D${3//\"/\\\"}; var=3D${var//\\/\\\\}\r
+ eval $2=3D\$var\r
+}\r
+\r
+> +PRINT_ONLY=3D\r
+> +\r
+\r
+\r
+Tomi\r