]> git.notmuchmail.org Git - notmuch/blobdiff - notmuch-emacs-mua
notmuch-emacs-mua: add --auto-daemon option to start Emacs daemon as needed
[notmuch] / notmuch-emacs-mua
index 13f67bee4417f78569916e147e4610ffa39899c4..ac03a4a60d565e512f4062052757f8e20d267281 100755 (executable)
 
 set -eu
 
+# escape: "expand" '\' as '\\' and '"' as '\"'
+# calling convention: escape -v var "$arg" (like in bash printf).
 escape ()
 {
-    echo "${1//\"/\\\"}"
+    local __escape_arg__=${3//\\/\\\\}
+    printf -v $2 '%s' "${__escape_arg__//\"/\\\"}"
 }
 
 EMACS=${EMACS-emacs}
 EMACSCLIENT=${EMACSCLIENT-emacsclient}
 
 PRINT_ONLY=
+NO_WINDOW=
 USE_EMACSCLIENT=
-CLIENT_TYPE="-c"
+AUTO_DAEMON=
+CREATE_FRAME="-c"
 
 # The crux of it all: construct an elisp progn and eval it.
 ELISP="(prog1 'done (require 'notmuch) (notmuch-mua-new-mail)"
@@ -60,7 +65,7 @@ while getopts :s:c:b:i:h opt; do
                    opt=${opt%%=*}
                    ;;
                # Long options without arguments.
-               --help|--print|--no-window-system|--client)
+               --help|--print|--no-window-system|--client|--auto-daemon)
                    ;;
                *)
                    echo "$0: unknown long option ${opt}, or argument mismatch." >&2
@@ -72,17 +77,12 @@ while getopts :s:c:b:i:h opt; do
            ;;
     esac
 
-
-    OPTARG="${OPTARG-none}"
-    OPTARG="$(escape "${OPTARG}")"
+    escape -v OPTARG "${OPTARG-none}"
 
     case "${opt}" in
        --help|h)
            exec man notmuch-emacs-mua
            ;;
-       --client)
-           USE_EMACSCLIENT="yes"
-           ;;
        --subject|s)
            ELISP="${ELISP} (message-goto-subject) (insert \"${OPTARG}\")"
            ;;
@@ -102,7 +102,14 @@ while getopts :s:c:b:i:h opt; do
            PRINT_ONLY=1
            ;;
        --no-window-system)
-           CLIENT_TYPE="-t"
+           NO_WINDOW="-nw"
+           CREATE_FRAME=
+           ;;
+       --client)
+           USE_EMACSCLIENT="yes"
+           ;;
+       --auto-daemon)
+           AUTO_DAEMON="--alternate-editor="
            ;;
        *)
            # We should never end up here.
@@ -117,7 +124,7 @@ done
 
 # Positional parameters.
 for arg; do
-    arg="$(escape "${arg}")"
+    escape -v arg "${arg}"
     ELISP="${ELISP} (message-goto-to) (insert \"${arg}, \")"
 done
 
@@ -131,7 +138,7 @@ fi
 
 if [ -n "$USE_EMACSCLIENT" ]; then
     # Evaluate the progn.
-    exec ${EMACSCLIENT} ${CLIENT_TYPE} -a '' --eval "${ELISP}"
+    exec ${EMACSCLIENT} ${NO_WINDOW} ${CREATE_FRAME} ${AUTO_DAEMON} --eval "${ELISP}"
 else
-    exec ${EMACS} --eval "${ELISP}"
+    exec ${EMACS} ${NO_WINDOW} --eval "${ELISP}"
 fi