]> git.notmuchmail.org Git - notmuch/blob - notmuch-emacs-mua
notmuch-emacs-mua: remove -C to keep short options compatible with mutt
[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 ()
26 {
27     echo "${1//\"/\\\"}"
28 }
29
30 EMACS=${EMACS-emacs}
31 EMACSCLIENT=${EMACSCLIENT-emacsclient}
32
33 PRINT_ONLY=
34 USE_EMACSCLIENT=
35 CLIENT_TYPE="-c"
36
37 # The crux of it all: construct an elisp progn and eval it.
38 ELISP="(prog1 'done (require 'notmuch) (notmuch-mua-new-mail)"
39 ELISP="${ELISP} (setq message-exit-actions (list #'save-buffers-kill-terminal))"
40
41 # Short options compatible with mutt(1).
42 while getopts :s:c:b:i:h opt; do
43     # Handle errors and long options.
44     case "${opt}" in
45         :)
46             echo "$0: short option -${OPTARG} requires an argument." >&2
47             exit 1
48             ;;
49         \?)
50             opt=$1
51             if [ "${OPTARG}" != "-" ]; then
52                 echo "$0: unknown short option -${OPTARG}." >&2
53                 exit 1
54             fi
55
56             case "${opt}" in
57                 # Long options with arguments.
58                 --subject=*|--to=*|--cc=*|--bcc=*|--body=*)
59                     OPTARG=${opt#--*=}
60                     opt=${opt%%=*}
61                     ;;
62                 # Long options without arguments.
63                 --help|--print|--no-window-system|--client)
64                     ;;
65                 *)
66                     echo "$0: unknown long option ${opt}, or argument mismatch." >&2
67                     exit 1
68                     ;;
69             esac
70             # getopts does not do this for what it considers errors.
71             OPTIND=$((OPTIND + 1))
72             ;;
73     esac
74
75
76     OPTARG="${OPTARG-none}"
77     OPTARG="$(escape "${OPTARG}")"
78
79     case "${opt}" in
80         --help|h)
81             exec man notmuch-emacs-mua
82             ;;
83         --client)
84             USE_EMACSCLIENT="yes"
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             CLIENT_TYPE="-t"
106             ;;
107         *)
108             # We should never end up here.
109             echo "$0: internal error (option ${opt})." >&2
110             exit 1
111             ;;
112     esac
113
114     shift $((OPTIND - 1))
115     OPTIND=1
116 done
117
118 # Positional parameters.
119 for arg; do
120     arg="$(escape "${arg}")"
121     ELISP="${ELISP} (message-goto-to) (insert \"${arg}, \")"
122 done
123
124 # End progn.
125 ELISP="${ELISP})"
126
127 if [ -n "$PRINT_ONLY" ]; then
128     echo ${ELISP}
129     exit 0
130 fi
131
132 if [ -n "$USE_EMACSCLIENT" ]; then
133     # Evaluate the progn.
134     exec ${EMACSCLIENT} ${CLIENT_TYPE} -a '' --eval "${ELISP}"
135 else
136     exec ${EMACS} --eval "${ELISP}"
137 fi