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