unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Tomi Ollila <tomi.ollila@iki.fi>
To: David Bremner <david@tethera.net>,
	notmuch@notmuchmail.org, Jani Nikula <jani@nikula.org>
Cc: tomi.ollila@iki.fi
Subject: [DRAFT PATCH v2] modified notmuch-emacs-mua v2
Date: Tue, 20 Jan 2015 19:53:44 +0200	[thread overview]
Message-ID: <1421776424-24304-1-git-send-email-tomi.ollila@iki.fi> (raw)
In-Reply-To: <1421598115-4889-1-git-send-email-david@tethera.net>

This is second draft patch of (first being)

id:1405026779-29966-1-git-send-email-tomi.ollila@iki.fi

I saw potential problem with only supporting emacsclient(1) in
the version David sent:
id:1421598115-4889-1-git-send-email-david@tethera.net

(no emacs server running and no tty -- new X client not started)

therefore I started to modify that part -- and soon adding all features
I'd like to see there. As I expect some bikeshedding to continue I skip
doc update for the time being (to avoid unnecessary work), therefore
calling this as "draft patch".

This implIments many of my first draft features:

mailto: is handled if given as first argument (not yet all the nice stuff
Jameson suggested, we'll perhaps get there later...)

--from option when sending non-mailto: way

And, -nw (*) in a new way...

The -i and --body are done as in initial Jani's version ( also as in
id:1421598115-4889-1-git-send-email-david@tethera.net )

Also, --long SPC value is not implemented, format is --long=value

In case emacsclient(1) is used and no --no-window-system, '-c' arg is
given to emacsclient like in the version David sent.

(this means that if emacs is not running on X, user may get this message:
 "emacsclient: could not get terminal name" -- we need to document user
 to give -nw (--no-window-system) option then)

Other "new" things:

Option --bodytext to give body content from command line (not documented
yet, but I'll add privacy warning when updating NaMual page.

Final cursor position goes based on last option given from command line,
unless to: or subject: is missing -- cursor is positioned after these
headers in this case.

(*) -nw works so that the 'n' is given to getopts and it expects an
argument; the argument is checked being w (this means -nw and -n w are
accepted). this also gives interesting output when one attempts to use
plain '-n' (error message shows next arg concatenated into this or...
"./notmuch-emacs-mua: short option -n requires an argument." -- OK,
forgot to handle this special case at this time...)

Anyway, I'd rather have -nw and some peculiarity than no -nw at all ;)

Tomi
---
 notmuch-emacs-mua | 217 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 217 insertions(+)
 create mode 100755 notmuch-emacs-mua

diff --git a/notmuch-emacs-mua b/notmuch-emacs-mua
new file mode 100755
index 000000000000..42275cd1a563
--- /dev/null
+++ b/notmuch-emacs-mua
@@ -0,0 +1,217 @@
+#!/usr/bin/env bash
+# -*- mode: shell-script; sh-basic-offset: 4; tab-width: 8 -*-
+#
+# notmuch-emacs-mua - start composing a mail on the command line
+#
+# Copyright © 2014 Jani Nikula
+#
+# This program 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 3 of the License, 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 this program.  If not, see http://www.gnu.org/licenses/ .
+#
+# Authors: Jani Nikula <jani@nikula.org>
+#          Tomi Ollila <tomi.ollila@iki.fi>
+#
+
+set -eu
+
+# Cannot use [[ ]] until we know we have bash.
+case ${BASH_VERSION-} in '')
+    echo "Not BASH!" >&2
+    exit 1
+esac
+
+# escape: "expand" '\' to '\\' & '"' to '\"'
+# Calling convention: escape -v var "$arg" (like in bash printf).
+escape ()
+{
+	local arg=${3//\\/\\\\}
+	eval $2='${arg//\"/\\\"}'
+}
+
+unset ALTERNATE_EDITOR
+exec_mua ()
+{
+    if "${EMACSCLIENT:=emacsclient}" --eval t >/dev/null 2>&1
+    then
+	emacs=$EMACSCLIENT
+	# Close stdout in case no --no-window-system (and no --print).
+	test -n "$W$X" || exec >/dev/null
+	# W/ emacsclient, use '-c' in case no --no-window-system.
+	[[ -n $W ]] || W=-c
+    else
+	emacs=${EMACS:-emacs}
+    fi
+    ${X:-exec} "${emacs}" $W --eval "(prog1 'done $*)"
+    exit
+}
+
+X=  # This is chaged to 'echo' when --print is used.
+W=  # This is changed to '-nw' when --no-window-system is used.
+
+SUBJECT= TO= CC= BCC= BODY= FROM=
+
+unset message_goto # Final elisp function to execute, when defined.
+cddone=false
+
+# "Short circuit" mailto handling.
+case ${1-} in mailto:*)
+	oIFS=$IFS; IFS=
+	escape -v OPTARG "$*"
+	IFS=$oIFS
+	exec_mua "(require 'notmuch) (browse-url-mail \"$OPTARG\")"
+	exit
+esac
+
+while getopts :s:c:b:i:n:h opt; do
+    # Handle errors and long options.
+    case "${opt}" in
+	:)
+	    echo "$0: short option -${OPTARG} requires an argument." >&2
+	    exit 1
+	    ;;
+	\?)
+	    opt=$1
+	    if [[ ${OPTARG} != '-' ]]; then
+		echo "$0: unknown short option -${OPTARG}." >&2
+		exit 1
+	    fi
+
+	    case "${opt}" in
+		# Long options with arguments.
+		--subject=*|--to=*|--cc=*|--bcc=*|--body=*|--from=*|--bodytext=*)
+		    OPTARG=${opt#--*=}
+		    opt=${opt%%=*}
+		    ;;
+		# Long options without arguments.
+		--help|--print|--no-window-system)
+		    ;;
+		*)
+		    echo "$0: unknown long option ${opt}, or argument mismatch." >&2
+		    exit 1
+		    ;;
+	    esac
+	    # Getopts does not do this for what it considers errors.
+	    OPTIND=$((OPTIND + 1))
+	    ;;
+    esac
+
+    escape -v OPTARG "${OPTARG}"
+
+    case "${opt}" in
+	--help|h)
+	    exec man notmuch-emacs-mua
+	    ;;
+	--from)
+	    escape -v FROM "${OPTARG}"
+	    ;;
+	--subject|s)
+	    escape -v OPTARG "${OPTARG}"
+	    SUBJECT=${SUBJECT:+$SUBJECT }${OPTARG}
+	    message_goto='(message-goto-subject)'
+	    ;;
+	--to)
+	    escape -v OPTARG "${OPTARG}"
+	    TO=${TO:+$TO, }${OPTARG}
+	    message_goto='(message-goto-to)'
+	    ;;
+	--cc|c)
+	    escape -v OPTARG "${OPTARG}"
+	    CC=${CC:+$CC, }${OPTARG}
+	    message_goto='(message-goto-cc)'
+	    ;;
+	--bcc|b)
+	    escape -v OPTARG "${OPTARG}"
+	    BCC=${BCC:+$BCC, }${OPTARG}
+	    message_goto='(message-goto-bcc)'
+	    ;;
+	--body|i)
+	    escape -v OPTARG "${OPTARG}"
+	    if [[ ! -f ${OPTARG} ]]; then
+	        echo "$0: '${OPTARG}': no such file" >&2
+		exit 1
+	    fi
+	    if [[ $cddone == 'false' ]]; then
+		BODY=${BODY}$'\n'"  (cd \"${PWD}\")"
+		cddone=true
+	    fi
+	    BODY=${BODY}$'\n'"  (insert-file \"${OPTARG}\")"
+	    BODY=${BODY}$'\n'"  (if (/= (point) (line-beginning-position)) (insert \"\\n\"))"
+	    unset message_goto
+	    ;;
+	--bodytext)
+	    escape -v OPTARG "${OPTARG}"
+	    BODY=${BODY}$'\n'"  (insert \"${OPTARG}\\n\")"
+	    unset message_goto
+	    ;;
+	--no-window-system)
+	    W=-nw
+	    ;;
+	n) # -nw !!!
+	    if [[ $OPTARG == 'w' ]]; then
+		W=-nw
+	    else
+		echo "$0: unknown option -n${OPTARG}, or argument mismatch." >&2
+		exit 1
+	    fi
+	    ;;
+	--print)
+	    X=echo
+	    ;;
+	*)
+	    # We should never end up here.
+	    echo "$0: internal error (option ${opt})." >&2
+	    exit 1
+	    ;;
+    esac
+
+    shift $((OPTIND - 1))
+    OPTIND=1
+done
+
+# Positional parameters.
+for arg; do
+    escape -v arg "${arg}"
+    TO=${TO:+$TO, }${arg}
+    message_goto='(message-goto-to)'
+done
+
+# The newlines are here for --print (only) output.
+NL=$'\n'
+ELISP="\
+${CC:+$NL  (message-goto-cc) (insert \"$CC\")}\
+${BCC:+$NL  (message-goto-bcc) (insert \"$BCC\")}\
+${BODY:+$NL  (message-goto-body)$BODY}"
+
+if [[ $TO == '' && $SUBJECT == '' && $ELISP == '' ]]
+then
+    exec_mua "(require 'notmuch) (notmuch-hello)"
+else
+    [[ $FROM != '' ]] && OH="(list (cons 'From \"$FROM\"))" || OH=nil
+
+    if [[ $SUBJECT == '' ]]; then
+	SUBJECT=nil
+	message_goto='(message-goto-subject)'
+    else
+	SUBJECT=\"$SUBJECT\"
+    fi
+    if [[ $TO == '' ]]; then
+	TO=nil
+	message_goto='(message-goto-to)'
+    else
+	TO=\"$TO\"
+    fi
+    exec_mua "(require 'notmuch)
+  (notmuch-mua-mail ${TO} ${SUBJECT}
+	${OH} nil (notmuch-mua-get-switch-function))\
+${ELISP}${NL}  (set-buffer-modified-p nil)${message_goto+ $message_goto}"
+fi
-- 
2.0.0

  parent reply	other threads:[~2015-01-20 17:54 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-19 19:25 [PATCH] cli: add a tool for starting new message in the emacs ui Jani Nikula
2014-03-19 21:24 ` Tomi Ollila
2014-03-19 21:54   ` Jani Nikula
2014-03-20 10:31     ` Tomi Ollila
2014-04-06 15:43       ` [PATCH v2] " Jani Nikula
2014-04-07  3:59         ` Jameson Graef Rollins
2014-07-01 20:02         ` [PATCH] " David Bremner
2014-07-04 17:36           ` Tomi Ollila
2015-01-18 16:21           ` David Bremner
2015-01-18 21:07             ` Tomi Ollila
2015-01-19 16:32             ` Tomi Ollila
2015-01-20 17:53             ` Tomi Ollila [this message]
2015-01-20 18:58               ` [DRAFT PATCH v2] modified notmuch-emacs-mua v2 David Bremner
2015-01-21  9:38                 ` Tomi Ollila
2015-01-21 16:50                   ` David Bremner
2015-02-22 20:34             ` [PATCH] cli: add a tool for starting new message in the emacs ui David Bremner
2015-03-06  7:06               ` David Bremner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1421776424-24304-1-git-send-email-tomi.ollila@iki.fi \
    --to=tomi.ollila@iki.fi \
    --cc=david@tethera.net \
    --cc=jani@nikula.org \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).