]> git.notmuchmail.org Git - notmuch/blob - notmuch-emacs-mua
test: improve the time parsing tests
[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 while getopts :s:c:b:i:hC opt; do
42     # Handle errors and long options.
43     case "${opt}" in
44         :)
45             echo "$0: short option -${OPTARG} requires an argument." >&2
46             exit 1
47             ;;
48         \?)
49             opt=$1
50             if [ "${OPTARG}" != "-" ]; then
51                 echo "$0: unknown short option -${OPTARG}." >&2
52                 exit 1
53             fi
54
55             case "${opt}" in
56                 # Long options with arguments.
57                 --subject=*|--to=*|--cc=*|--bcc=*|--body=*)
58                     OPTARG=${opt#--*=}
59                     opt=${opt%%=*}
60                     ;;
61                 # Long options without arguments.
62                 --help|--print|--no-window-system|--client)
63                     ;;
64                 *)
65                     echo "$0: unknown long option ${opt}, or argument mismatch." >&2
66                     exit 1
67                     ;;
68             esac
69             # getopts does not do this for what it considers errors.
70             OPTIND=$((OPTIND + 1))
71             ;;
72     esac
73
74
75     OPTARG="${OPTARG-none}"
76     OPTARG="$(escape "${OPTARG}")"
77
78     case "${opt}" in
79         --help|h)
80             exec man notmuch-emacs-mua
81             ;;
82         --client|C)
83             USE_EMACSCLIENT="yes"
84             ;;
85         --subject|s)
86             ELISP="${ELISP} (message-goto-subject) (insert \"${OPTARG}\")"
87             ;;
88         --to)
89             ELISP="${ELISP} (message-goto-to) (insert \"${OPTARG}, \")"
90             ;;
91         --cc|c)
92             ELISP="${ELISP} (message-goto-cc) (insert \"${OPTARG}, \")"
93             ;;
94         --bcc|b)
95             ELISP="${ELISP} (message-goto-bcc) (insert \"${OPTARG}, \")"
96             ;;
97         --body|i)
98             ELISP="${ELISP} (message-goto-body) (cd \"${PWD}\") (insert-file \"${OPTARG}\")"
99             ;;
100         --print)
101             PRINT_ONLY=1
102             ;;
103         --no-window-system)
104             CLIENT_TYPE="-t"
105             ;;
106         *)
107             # We should never end up here.
108             echo "$0: internal error (option ${opt})." >&2
109             exit 1
110             ;;
111     esac
112
113     shift $((OPTIND - 1))
114     OPTIND=1
115 done
116
117 # Positional parameters.
118 for arg; do
119     arg="$(escape "${arg}")"
120     ELISP="${ELISP} (message-goto-to) (insert \"${arg}, \")"
121 done
122
123 # End progn.
124 ELISP="${ELISP})"
125
126 if [ -n "$PRINT_ONLY" ]; then
127     echo ${ELISP}
128     exit 0
129 fi
130
131 if [ -n "$USE_EMACSCLIENT" ]; then
132     # Evaluate the progn.
133     exec ${EMACSCLIENT} ${CLIENT_TYPE} -a '' --eval "${ELISP}"
134 else
135     exec ${EMACS} --eval "${ELISP}"
136 fi