]> git.cworth.org Git - obsolete/notmuch-wiki/blob - emacstips.mdwn
Fix script for adding attachments from command line
[obsolete/notmuch-wiki] / emacstips.mdwn
1 [[!img notmuch-logo.png alt="Notmuch logo" class="left"]]
2 #Tips and Tricks for using notmuch with Emacs
3
4 The main Notmuch message reading client is **notmuch.el**, which is an
5 [emacs](http://www.gnu.org/software/emacs/) major mode, and is
6 included in the notmuch package.
7
8 ## Setup
9
10 To use the Notmuch emacs mode, first add the following line to your
11 `.emacs` rc file:
12
13         (require 'notmuch)
14
15 Then, either run "emacs -f notmuch", or execute the command `M-x
16 notmuch` from within a running emacs.
17
18 ## Navigating & reading mails
19
20 When first starting notmuch in emacs, you will be presented with the
21 notmuch "hello" page.  From here you can do searches, see lists of
22 recent searches, saved searches, message tags, help information, etc.
23
24 Executing a search will open a new buffer in `notmuch-search-mode`
25 displaying the search results.  Each line in the search results
26 represents a message thread.  Hitting the '?' key will show help for
27 this mode.
28
29 In general, the 'q' will kill the current notmuch buffer and return
30 you to the previous buffer (sort of like a 'pop').
31
32 In search mode, navigating to a thread and hitting return will then
33 open a new buffer in `notmuch-show-mode`, which will show the actual
34 message contents of the thread.
35
36 ## Sending mail
37
38 In any notmuch mode, you can start a new message by hitting the 'm'
39 key.  To reply to a message or thread, just hit the 'r' key.
40
41 When composing new messages, you will be entered in emacs's
42 `message-mode`, which is a powerful mode for composing and sending
43 messages.  When in message move, you can type `C-c ?` for help.
44
45 If you would like to use address autocompletion when composing
46 messages, see [address completion](#address_completion).
47
48 When you are ready to send a message, type `C-c C-c`.  By default
49 message mode will use your sendmail command to send mail, so make sure
50 that works.  One annoying standard configuration of message mode is
51 that it will hide the sent mail in your emacs frame stack, but it will
52 not close it. If you type several mails in an emacs session they will
53 accumulate and make switching between buffers more annoying. You can
54 avoid that behavior by adding `(setq message-kill-buffer-on-exit t)`
55 in your `.emacs` file which will really close the mail window after
56 sending it.
57
58 ## Attaching files
59
60 Using the `M-x mml-attach-file` command, you can attach any file to be
61 send with your mail. By default this command is bound to the menu item
62 *Attachments--Attach File* with the key binding `C-c C-a`.  The
63 variable `mml-dnd-attach-options` (`M-x
64 customize-variable<RET>mml-dnd-attach-options`) can be set to allow
65 the prompting for various attachment options (such as
66 inline/attachment) if you want to do that.
67
68 For those who prefer a more graphical interface, you can also simply
69 drag and drop files from a file manager into a mail composition window
70 to have them attached. In Ubuntu this works without any modifications
71 if files are dragged from the file manager.
72
73 And for those who prefer working from command line, the following
74 script opens new emacs window with empty message and attaches files
75 mentioned as script arguments. (Note: The script expects that you have
76 `(server-start)` in your .emacs)
77
78     #!/bin/sh
79     attach_cmds=""
80     while [ "$1" ]; do
81         fullpath=$(readlink --canonicalize $1)
82         attach_cmds="$attach_cmds (mml-attach-file \"$fullpath\")"
83         shift
84     done
85     emacsclient -a '' -c -e "(progn (compose-mail) $attach_cmds)"
86
87
88 -----
89
90 # Advanced tips and tweaks
91
92 ## Add a key binding to add/remove/toggle a tag
93
94 The `notmuch-{search,show}-{add,remove}-tag` functions are very useful
95 for making quick tag key bindings.  For instance, here's an example
96 of how to make a key binding to add the "spam" tag and remove the
97 "inbox" tag in notmuch-show-mode:
98
99                 (define-key notmuch-show-mode-map "S"
100                   (lambda ()
101                     "mark message as spam"
102                     (interactive)
103                     (notmuch-show-add-tag "spam")
104                     (notmuch-show-remove-tag "inbox")))
105
106 You can do the same for threads in `notmuch-search-mode` by just
107 replacing "show" with "search" in the called functions.
108
109 The definition above makes use of a lambda function, but you could
110 also define a separate function first:
111
112                 (defun notmuch-show-tag-spam()
113                   "mark message as spam"
114                   (interactive)
115                   (notmuch-show-add-tag "spam")
116                   (notmuch-show-remove-tag "inbox")))
117                 (define-key notmuch-show-mode-map "S" 'notmuch-show-tag-spam)
118
119 Here's a more complicated example of how to add a toggle "deleted"
120 key:
121
122                 (define-key notmuch-show-mode-map "d"
123                   (lambda ()
124                     "toggle deleted tag for message"
125                     (interactive)
126                     (if (member "deleted" (notmuch-show-get-tags))
127                         (notmuch-show-remove-tag "deleted")
128                       (notmuch-show-add-tag "deleted"))))
129
130 ## How to do FCC/BCC...
131
132 The Emacs interface to notmuch will automatically add an `Fcc`
133 header to your outgoing mail so that any messages you send will also
134 be saved in your mail store. You can control where this copy of the
135 message is saved by setting the variables `message-directory` (which
136 defines a base directory) and `notmuch-fcc-dirs` which defines the
137 subdirectory relative to `message-directory` in which to save the
138 mail. Enter a directory (without the maildir `/cur` ending which
139 will be appended automatically). To customize both variables at the
140 same time, use the fancy command:
141
142                 M-x customize-apropos<RET>\(notmuch-fcc-dirs\)\|\(message-directory\)
143
144 This mechanism also allows you to select different folders to be
145 used for the outgoing mail depending on your selected `From`
146 address. Please see the documentation for the variable
147 `notmuch-fcc-dirs` in the customization window for how to arrange
148 this.
149
150 ## How to customize `notmuch-saved-searches`
151
152 When starting notmuch, a list of saved searches and message counts is
153 displayed, replacing the older `notmuch-folders` command. The set of
154 saved searches displayed can be modified directly from the notmuch
155 interface (using the `[save]` button next to a previous search) or by
156 customising the variable `notmuch-saved-searches`.
157
158 An example setting might be:
159
160                 (setq notmuch-saved-searches '(("inbox" . "tag:inbox")
161                                 ("unread" . "tag:inbox AND tag:unread")
162                                 ("notmuch" . "tag:inbox AND to:notmuchmail.org")))
163
164 Of course, you can have any number of saved searches, each configured
165 with any supported search terms (see "notmuch help search-terms").
166
167 Some users find it useful to add `and not tag:delete` to those
168 searches, as they use the `delete` tag to mark messages as
169 deleted. This causes messages that are marked as deleted to be removed
170 from the commonly used views of messages.  Use whatever seems most
171 useful to you.
172
173 ## Viewing HTML messages with an external viewer
174
175 The emacs client can often display an HTML message inline, but it
176 sometimes fails for one reason or another, (or is perhaps inadequate
177 if you really need to see the graphical presentation of the HTML
178 message).
179
180 In this case, it can be useful to display the message in an external
181 viewer, such as a web browser. Here's a little script that Keith
182 Packard wrote, which he calls `view-html`:
183
184                 #!/bin/sh
185                 dir=3D`mktemp -d`
186                 trap "rm -r $dir" 0
187                 cat "$@" > "$dir"/msg
188                 if munpack -C "$dir" -t < "$dir"/msg 2>&1 | grep 'Did not find'; then
189                     sed -n '/[Hh][Tt][Mm][Ll]/,$p' "$dir"/msg > $dir/part1.html
190                     rm "$dir"/msg
191                 fi
192                 for i in "$dir"/part*; do
193                     if grep -q -i -e '<html>' -e 'text/html' "$i"; then
194                         iceweasel "$i" &
195                         sleep 3
196                         exit 0
197                     fi
198                 done
199
200 Save that script somewhere in your `${PATH}`, make it executable,
201 and change the invocation of `iceweasel` to any other HTML viewer if
202 necessary. Then within the emacs client, press '|' to pipe the
203 current message, then type "view-html".
204
205 Keith mentions the following caveat, "Note that if iceweasel isn't
206 already running, it seems to shut down when the script exits. I
207 don't know why."
208
209 ## msmtp, message mode and multiple accounts
210
211 As an alternative to running a mail server such as sendmail or
212 postfix just to send email, it is possible to use
213 [msmtp](http://msmtp.sourceforge.net/).  This small application will
214 look like `/usr/bin/sendmail` to a MUA such as emacs message mode, but
215 will just forward the email to an external SMTP server.  It's fairly
216 easy to set up and it support several account for using different
217 SMTP servers.  The msmtp pages have several examples.
218
219 A typical scenario is that you want to use the company SMTP server
220 for email coming from your company email address, and your personal
221 server for personal email.  If msmtp is passed the envelope address
222 on the command line (the -f/--from option) it will automatically
223 pick the matching account.  The only trick here seems to be getting
224 emacs to actually pass the envelope from.  There are a number of
225 overlapping configuration variables that control this, and it's a
226 little confusion, but setting these three works for me:
227
228  - `mail-specify-envelope-from`: `t`
229
230  - `message-sendmail-envelope-from`: `header`
231
232  - `mail-envelope-from`: `header`
233
234 With that in place, you need a `.msmtprc` with the accounts configured
235 for the domains you want to send out using specific SMTP servers and
236 the rest will go to the default account.
237
238 ## <span id="address_completion">Address completion when composing</span>
239
240 There are currently two solutions to this:
241
242 [bbdb](http://bbdb.sourceforge.net) is a contact database for emacs
243 that works quite nicely together with message mode, including
244 address autocompletion.
245
246 You can also use the notmuch database as a mail address book itself.
247 To do this you need a command line tool that outputs likely address
248 candidates based on a search string.  There are currently two
249 available:
250
251   * The python tool `notmuch_address.py` (`git clone
252     http://jkr.acm.jhu.edu/git/notmuch_addresses.git`) (slower, but
253     no compilation required so good for testing the setup)
254
255   * The vala-based
256     [addrlookup](http://github.com/spaetz/vala-notmuch) (faster, but
257     needs compiling).  The addrlookup binary needs to be compiled.
258     Grab
259     `http://github.com/spaetz/vala-notmuch/raw/static-sources/src/addrlookup.c`
260     and build it with:
261
262                     cc -o addrlookup addrlookup.c `pkg-config --cflags --libs gobject-2.0` -lnotmuch
263
264 EUDC is integrated into emacs and is needed for tab completion of
265 email addresses. See [this
266 mail](http://mid.gmane.org/87fx3uflkx.fsf@jhu.edu)
267 (id:87fx3uflkx.fsf@jhu.edu) for more information.
268
269 ## How to sign/encrypt messages with gpg
270
271 Messages can by signed using gpg by invoking `M-x
272 mml-secure-sign-pgpmime` (or `M-x
273 mml-secure-encrypt-pgpmime`). These functions are available via the
274 standard `message-mode` keybindings `C-c C-m s p` and `C-c C-m c
275 p`. To sign outgoing mail by default, use the `message-setup-hook`
276 in your `.emacs` file:
277
278                     ;; Sign messages by default.
279                     (add-hook 'message-setup-hook 'mml-secure-sign-pgpmime)
280
281 This inserts the required `<#part sign=pgpmime>` into the beginning
282 of the mail text body and will be converted into a pgp signature
283 when sending (so one can just manually delete that line if signing
284 is not required).