]> git.notmuchmail.org Git - notmuch/blob - notmuch-emacs-mua
Merge branch 'release'
[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 https://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=
41 ELISP=
42 MAILTO=
43
44 # Short options compatible with mutt(1).
45 while getopts :s:c:b:i:h opt; do
46     # Handle errors and long options.
47     case "${opt}" in
48         :)
49             echo "$0: short option -${OPTARG} requires an argument." >&2
50             exit 1
51             ;;
52         \?)
53             opt=$1
54             if [ "${OPTARG}" != "-" ]; then
55                 echo "$0: unknown short option -${OPTARG}." >&2
56                 exit 1
57             fi
58
59             case "${opt}" in
60                 # Long options with arguments.
61                 --subject=*|--to=*|--cc=*|--bcc=*|--body=*)
62                     OPTARG=${opt#--*=}
63                     opt=${opt%%=*}
64                     ;;
65                 # Long options without arguments.
66                 --help|--print|--no-window-system|--client|--auto-daemon|--create-frame)
67                     ;;
68                 *)
69                     echo "$0: unknown long option ${opt}, or argument mismatch." >&2
70                     exit 1
71                     ;;
72             esac
73             # getopts does not do this for what it considers errors.
74             OPTIND=$((OPTIND + 1))
75             ;;
76     esac
77
78     escape -v OPTARG "${OPTARG-none}"
79
80     case "${opt}" in
81         --help|h)
82             exec man notmuch-emacs-mua
83             ;;
84         --subject|s)
85             ELISP="${ELISP} (message-goto-subject) (insert \"${OPTARG}\")"
86             ;;
87         --to)
88             ELISP="${ELISP} (message-goto-to) (insert \"${OPTARG}, \")"
89             ;;
90         --cc|c)
91             ELISP="${ELISP} (message-goto-cc) (insert \"${OPTARG}, \")"
92             ;;
93         --bcc|b)
94             ELISP="${ELISP} (message-goto-bcc) (insert \"${OPTARG}, \")"
95             ;;
96         --body|i)
97             ELISP="${ELISP} (message-goto-body) (insert-file \"${OPTARG}\")"
98             ;;
99         --print)
100             PRINT_ONLY=1
101             ;;
102         --no-window-system)
103             NO_WINDOW="-nw"
104             ;;
105         --client)
106             USE_EMACSCLIENT="yes"
107             ;;
108         --auto-daemon)
109             AUTO_DAEMON="--alternate-editor="
110             CREATE_FRAME="-c"
111             ;;
112         --create-frame)
113             CREATE_FRAME="-c"
114             ;;
115         *)
116             # We should never end up here.
117             echo "$0: internal error (option ${opt})." >&2
118             exit 1
119             ;;
120     esac
121
122     shift $((OPTIND - 1))
123     OPTIND=1
124 done
125
126 # Positional parameters.
127 for arg; do
128     escape -v arg "${arg}"
129     case $arg in
130         mailto:*)
131             if [ -n "${MAILTO}" ]; then
132                 echo "$0: more than one mailto: argument." >&2
133                 exit 1
134             fi
135             MAILTO="${arg}"
136             ;;
137         *)
138             ELISP="${ELISP} (message-goto-to) (insert \"${arg}, \")"
139             ;;
140     esac
141 done
142
143 if [ -n "${MAILTO}" ]; then
144     if [ -n "${ELISP}" ]; then
145         echo "$0: mailto: is not compatible with other message parameters." >&2
146         exit 1
147     fi
148     ELISP="(browse-url-mail \"${MAILTO}\")"
149 else
150     ELISP="(notmuch-mua-new-mail) ${ELISP}"
151 fi
152
153 # Kill the terminal/frame if we're creating one.
154 if [ -z "$USE_EMACSCLIENT" -o -n "$CREATE_FRAME" -o -n "$NO_WINDOW" ]; then
155     ELISP="${ELISP} (message-add-action #'save-buffers-kill-terminal 'exit)"
156 fi
157
158 escape -v pwd "$PWD"
159
160 # The crux of it all: construct an elisp progn and eval it.
161 ELISP="(prog1 'done (require 'notmuch) (cd \"$pwd\") ${ELISP})"
162
163 if [ -n "$PRINT_ONLY" ]; then
164     echo ${ELISP}
165     exit 0
166 fi
167
168 if [ -n "$USE_EMACSCLIENT" ]; then
169     # Evaluate the progn.
170     exec ${EMACSCLIENT} ${NO_WINDOW} ${CREATE_FRAME} ${AUTO_DAEMON} --eval "${ELISP}"
171 else
172     exec ${EMACS} ${NO_WINDOW} --eval "${ELISP}"
173 fi