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