[DRAFT PATCH v2] modified notmuch-emacs-mua v2

Tomi Ollila tomi.ollila at iki.fi
Tue Jan 20 09:53:44 PST 2015


This is second draft patch of (first being)

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

I saw potential problem with only supporting emacsclient(1) in
the version David sent:
id:1421598115-4889-1-git-send-email-david at 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 at 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 at nikula.org>
+#          Tomi Ollila <tomi.ollila at 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



More information about the notmuch mailing list