[PATCH] notmuch-emacs-mua: more options, some fixes

2015-03-07 Thread Jani Nikula
On Sat, 07 Mar 2015, Tomi Ollila  wrote:
> Two new options added:
>--from: specify alternate From: header
>--bodytext: possibility to give body content from command line
>
> Non-forking escape() functionality, also escaping \ (backslash) characters.
>
> Without content arguments start emacs/emacsclient and run (notmuch-hello),
> in this case without assigning anything to message-exit-actions.
>
> Point is now positioned at the end of the content given last on command
> line, unless to: or subject: is missing -- in which case point is
> positioned at one of these headers (is both missing, at to:)
>
> The -nw option is now given to emacs in case --no-window-system command
> line option is provided (--no-window-system used to work with emacsclient
> only).
>
> In case EMACS or EMACSCLIENT environment variable was defined but being
> empty (null string), use emacs/emacsclient as command instead (null string
> as executable name gives confusing error message). Also $EMACS/$EMACSCLIENT
> executable name is now quoted to *NOT* split it to separate command line
> arguments on any $IFS variables there might be.
>
> Reorganized content argument handling to combine the content of same option
> given multiple times and make it look more readable when --print option is
> used. --body option also now checks the existence of the file to be
> included and if the file included does not end with newline trailing
> newline is inserted to the message buffer.
>
> As bash supports [[ ]] command and it's special expansion (or lack thereof)
> of variables (and ==, && and || inside) the few [ ]'s there were are now
> changed to this more advanced format.

As I mentioned on IRC, I think there are too many changes here for one
patch. One change at a time! In particular because I'm not sure if all
of this is really needed... like message_goto and point placement
handling. Starts to feel like all of this would be better handled with a
dedicated function in emacs!

BR,
Jani.

> ---
>
> The changes *NOT* taken from my 2 draft patches (*)
>
> 1) no -nw hacking
>
> 2) no mailto: handling
>
> (*) id:1421776424-24304-1-git-send-email-tomi.ollila at iki.fi (later, linking
> previous)
>
>
>  doc/man1/notmuch-emacs-mua.rst |   8 ++-
>  notmuch-emacs-mua  | 123 
> ++---
>  2 files changed, 99 insertions(+), 32 deletions(-)
>
> diff --git a/doc/man1/notmuch-emacs-mua.rst b/doc/man1/notmuch-emacs-mua.rst
> index eb47098..29f7e0c 100644
> --- a/doc/man1/notmuch-emacs-mua.rst
> +++ b/doc/man1/notmuch-emacs-mua.rst
> @@ -22,6 +22,9 @@ Supported options for **notmuch-emacs-mua** include
>  Use emacsclient, rather than emacs. This will start
>  an emacs daemon process if necessary.
>  
> +``--from=``\ 
> +Specify alternate sender (From) of the message.
> +
>  ``-s, --subject=``\ 
>  Specify the subject of the message.
>  
> @@ -37,6 +40,9 @@ Supported options for **notmuch-emacs-mua** include
>  ``-i, --body=``\ 
>  Specify a file to include into the body of the message.
>  
> +``--bodytext=``\ 
> +Specify text content to be inserted into the body of the message.
> +
>  ``--no-window-system``
>  Even if a window system is available, use the current terminal
>  
> @@ -60,4 +66,4 @@ Name of emacsclient comment to invoke
>  SEE ALSO
>  
>  
> -**notmuch(1)**, **emacsclient(1)**, **mutt(1)**
> +**notmuch(1)**, **emacs(1)**, **emacsclient(1)**, **mutt(1)**
> diff --git a/notmuch-emacs-mua b/notmuch-emacs-mua
> index b8cbc82..17365b9 100755
> --- a/notmuch-emacs-mua
> +++ b/notmuch-emacs-mua
> @@ -1,4 +1,5 @@
>  #!/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
>  #
> @@ -18,25 +19,47 @@
>  # along with this program.  If not, see http://www.gnu.org/licenses/ .
>  #
>  # Authors: Jani Nikula 
> +#  Tomi Ollila 
>  #
>  
>  set -eu
>  
> +# escape: "expand" '\' to '\\' & '"' to '\"'
> +# Calling convention: escape -v var "$arg" (like in bash printf).
>  escape ()
>  {
> -echo "${1//\"/\\\"}"
> +local arg=${3//\\/}
> +eval $2='${arg//\"/\\\"}'
>  }
>  
> -EMACS=${EMACS-emacs}
> -EMACSCLIENT=${EMACSCLIENT-emacsclient}
> +EMACS=${EMACS:-emacs}
> +EMACSCLIENT=${EMACSCLIENT:-emacsclient}
>  
>  PRINT_ONLY=
>  USE_EMACSCLIENT=
> -CLIENT_TYPE="-c"
> +EMACSCLIENT_TYPE='-c'
> +EMACS_NW=
>  
> -# The crux of it all: construct an elisp progn and eval it.
> -ELISP="(prog1 'done (require 'notmuch) (notmuch-mua-new-mail)"
> -ELISP="${ELISP} (setq message-exit-actions (list 
> #'save-buffers-kill-terminal))"
> +exec_mua ()
> +{
> +if [[ -n $PRINT_ONLY ]]; then
> + echo "$1"
> + exit
> +fi
> +
> +if [[ -n $USE_EMACSCLIENT ]]; then
> + # Evaluate the progn.
> + exec "${EMACSCLIENT}" ${EMACSCLIENT_TYPE} -a '' --eval "$1"
> +else
> + exec "${EMACS}" ${EMACS_NW} --eval "$1"

[PATCH] notmuch-emacs-mua: more options, some fixes

2015-03-07 Thread Tomi Ollila
Two new options added:
   --from: specify alternate From: header
   --bodytext: possibility to give body content from command line

Non-forking escape() functionality, also escaping \ (backslash) characters.

Without content arguments start emacs/emacsclient and run (notmuch-hello),
in this case without assigning anything to message-exit-actions.

Point is now positioned at the end of the content given last on command
line, unless to: or subject: is missing -- in which case point is
positioned at one of these headers (is both missing, at to:)

The -nw option is now given to emacs in case --no-window-system command
line option is provided (--no-window-system used to work with emacsclient
only).

In case EMACS or EMACSCLIENT environment variable was defined but being
empty (null string), use emacs/emacsclient as command instead (null string
as executable name gives confusing error message). Also $EMACS/$EMACSCLIENT
executable name is now quoted to *NOT* split it to separate command line
arguments on any $IFS variables there might be.

Reorganized content argument handling to combine the content of same option
given multiple times and make it look more readable when --print option is
used. --body option also now checks the existence of the file to be
included and if the file included does not end with newline trailing
newline is inserted to the message buffer.

As bash supports [[ ]] command and it's special expansion (or lack thereof)
of variables (and ==, && and || inside) the few [ ]'s there were are now
changed to this more advanced format.
---

The changes *NOT* taken from my 2 draft patches (*)

1) no -nw hacking

2) no mailto: handling

(*) id:1421776424-24304-1-git-send-email-tomi.ollila at iki.fi (later, linking
previous)


 doc/man1/notmuch-emacs-mua.rst |   8 ++-
 notmuch-emacs-mua  | 123 ++---
 2 files changed, 99 insertions(+), 32 deletions(-)

diff --git a/doc/man1/notmuch-emacs-mua.rst b/doc/man1/notmuch-emacs-mua.rst
index eb47098..29f7e0c 100644
--- a/doc/man1/notmuch-emacs-mua.rst
+++ b/doc/man1/notmuch-emacs-mua.rst
@@ -22,6 +22,9 @@ Supported options for **notmuch-emacs-mua** include
 Use emacsclient, rather than emacs. This will start
 an emacs daemon process if necessary.

+``--from=``\ 
+Specify alternate sender (From) of the message.
+
 ``-s, --subject=``\ 
 Specify the subject of the message.

@@ -37,6 +40,9 @@ Supported options for **notmuch-emacs-mua** include
 ``-i, --body=``\ 
 Specify a file to include into the body of the message.

+``--bodytext=``\ 
+Specify text content to be inserted into the body of the message.
+
 ``--no-window-system``
 Even if a window system is available, use the current terminal

@@ -60,4 +66,4 @@ Name of emacsclient comment to invoke
 SEE ALSO
 

-**notmuch(1)**, **emacsclient(1)**, **mutt(1)**
+**notmuch(1)**, **emacs(1)**, **emacsclient(1)**, **mutt(1)**
diff --git a/notmuch-emacs-mua b/notmuch-emacs-mua
index b8cbc82..17365b9 100755
--- a/notmuch-emacs-mua
+++ b/notmuch-emacs-mua
@@ -1,4 +1,5 @@
 #!/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
 #
@@ -18,25 +19,47 @@
 # along with this program.  If not, see http://www.gnu.org/licenses/ .
 #
 # Authors: Jani Nikula 
+#  Tomi Ollila 
 #

 set -eu

+# escape: "expand" '\' to '\\' & '"' to '\"'
+# Calling convention: escape -v var "$arg" (like in bash printf).
 escape ()
 {
-echo "${1//\"/\\\"}"
+local arg=${3//\\/}
+eval $2='${arg//\"/\\\"}'
 }

-EMACS=${EMACS-emacs}
-EMACSCLIENT=${EMACSCLIENT-emacsclient}
+EMACS=${EMACS:-emacs}
+EMACSCLIENT=${EMACSCLIENT:-emacsclient}

 PRINT_ONLY=
 USE_EMACSCLIENT=
-CLIENT_TYPE="-c"
+EMACSCLIENT_TYPE='-c'
+EMACS_NW=

-# The crux of it all: construct an elisp progn and eval it.
-ELISP="(prog1 'done (require 'notmuch) (notmuch-mua-new-mail)"
-ELISP="${ELISP} (setq message-exit-actions (list 
#'save-buffers-kill-terminal))"
+exec_mua ()
+{
+if [[ -n $PRINT_ONLY ]]; then
+   echo "$1"
+   exit
+fi
+
+if [[ -n $USE_EMACSCLIENT ]]; then
+   # Evaluate the progn.
+   exec "${EMACSCLIENT}" ${EMACSCLIENT_TYPE} -a '' --eval "$1"
+else
+   exec "${EMACS}" ${EMACS_NW} --eval "$1"
+fi
+exit not reached
+}
+
+SUBJECT= TO= CC= BCC= BODY= FROM=
+
+unset message_goto # Final elisp function to execute, when defined.
+cddone=false

 while getopts :s:c:b:i:hC opt; do
 # Handle errors and long options.
@@ -47,14 +70,14 @@ while getopts :s:c:b:i:hC opt; do
;;
\?)
opt=$1
-   if [ "${OPTARG}" != "-" ]; then
+   if [[ ${OPTARG} != '-' ]]; then
echo "$0: unknown short option -${OPTARG}." >&2
exit 1
fi

case "${opt}" in
# Long options with arguments.
-