]> git.notmuchmail.org Git - notmuch/blob - notmuch-emacs-mua
ac03a4a60d565e512f4062052757f8e20d267281
[notmuch] / notmuch-emacs-mua
1 #!/usr/bin/env bash
2 #
3 # notmuch-emacs-mua - start composing a mail on the command line
4 #
5 # Copyright © 2014 Jani Nikula
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see http://www.gnu.org/licenses/ .
19 #
20 # Authors: Jani Nikula <jani@nikula.org>
21 #
22
23 set -eu
24
25 # escape: "expand" '\' as '\\' and '"' as '\"'
26 # calling convention: escape -v var "$arg" (like in bash printf).
27 escape ()
28 {
29     local __escape_arg__=${3//\\/\\\\}
30     printf -v $2 '%s' "${__escape_arg__//\"/\\\"}"
31 }
32
33 EMACS=${EMACS-emacs}
34 EMACSCLIENT=${EMACSCLIENT-emacsclient}
35
36 PRINT_ONLY=
37 NO_WINDOW=
38 USE_EMACSCLIENT=
39 AUTO_DAEMON=
40 CREATE_FRAME="-c"
41
42 # The crux of it all: construct an elisp progn and eval it.
43 ELISP="(prog1 'done (require 'notmuch) (notmuch-mua-new-mail)"
44 ELISP="${ELISP} (setq message-exit-actions (list #'save-buffers-kill-terminal))"
45
46 # Short options compatible with mutt(1).
47 while getopts :s:c:b:i:h opt; do
48     # Handle errors and long options.
49     case "${opt}" in
50         :)
51             echo "$0: short option -${OPTARG} requires an argument." >&2
52             exit 1
53             ;;
54         \?)
55             opt=$1
56             if [ "${OPTARG}" != "-" ]; then
57                 echo "$0: unknown short option -${OPTARG}." >&2
58                 exit 1
59             fi
60
61             case "${opt}" in
62                 # Long options with arguments.
63                 --subject=*|--to=*|--cc=*|--bcc=*|--body=*)
64                     OPTARG=${opt#--*=}
65                     opt=${opt%%=*}
66                     ;;
67                 # Long options without arguments.
68                 --help|--print|--no-window-system|--client|--auto-daemon)
69                     ;;
70                 *)
71                     echo "$0: unknown long option ${opt}, or argument mismatch." >&2
72                     exit 1
73                     ;;
74             esac
75             # getopts does not do this for what it considers errors.
76             OPTIND=$((OPTIND + 1))
77             ;;
78     esac
79
80     escape -v OPTARG "${OPTARG-none}"
81
82     case "${opt}" in
83         --help|h)
84             exec man notmuch-emacs-mua
85             ;;
86         --subject|s)
87             ELISP="${ELISP} (message-goto-subject) (insert \"${OPTARG}\")"
88             ;;
89         --to)
90             ELISP="${ELISP} (message-goto-to) (insert \"${OPTARG}, \")"
91             ;;
92         --cc|c)
93             ELISP="${ELISP} (message-goto-cc) (insert \"${OPTARG}, \")"
94             ;;
95         --bcc|b)
96             ELISP="${ELISP} (message-goto-bcc) (insert \"${OPTARG}, \")"
97             ;;
98         --body|i)
99             ELISP="${ELISP} (message-goto-body) (cd \"${PWD}\") (insert-file \"${OPTARG}\")"
100             ;;
101         --print)
102             PRINT_ONLY=1
103             ;;
104         --no-window-system)
105             NO_WINDOW="-nw"
106             CREATE_FRAME=
107             ;;
108         --client)
109             USE_EMACSCLIENT="yes"
110             ;;
111         --auto-daemon)
112             AUTO_DAEMON="--alternate-editor="
113             ;;
114         *)
115             # We should never end up here.
116             echo "$0: internal error (option ${opt})." >&2
117             exit 1
118             ;;
119     esac
120
121     shift $((OPTIND - 1))
122     OPTIND=1
123 done
124
125 # Positional parameters.
126 for arg; do
127     escape -v arg "${arg}"
128     ELISP="${ELISP} (message-goto-to) (insert \"${arg}, \")"
129 done
130
131 # End progn.
132 ELISP="${ELISP})"
133
134 if [ -n "$PRINT_ONLY" ]; then
135     echo ${ELISP}
136     exit 0
137 fi
138
139 if [ -n "$USE_EMACSCLIENT" ]; then
140     # Evaluate the progn.
141     exec ${EMACSCLIENT} ${NO_WINDOW} ${CREATE_FRAME} ${AUTO_DAEMON} --eval "${ELISP}"
142 else
143     exec ${EMACS} ${NO_WINDOW} --eval "${ELISP}"
144 fi