306
306
return commandline
309
class EmacsMailMode(ExternalMailClient):
310
"""Call emacsclient in mail-mode.
312
This only work for emacs >= 22.1.
309
class EmacsMail(ExternalMailClient):
310
"""Call emacsclient to have a mail buffer.
312
This only work for emacs >= 22.1 due to recent -e/--eval support.
314
The good news is that this implementation will work with all mail
315
agents registered against ``mail-user-agent``. So there is no need
316
to instantiate ExternalMailClient for each and every GNU Emacs
319
Users just have to ensure that ``mail-user-agent`` is set according
314
323
_client_commands = ['emacsclient']
325
def _prepare_send_function(self):
326
"""Write our wrapper function into a temporary file.
328
This temporary file will be loaded at runtime in
329
_get_compose_commandline function.
331
FIXME: this function does not remove the file. That's a wanted
332
behaviour since _get_compose_commandline won't run the send
333
mail function directly but return the eligible command line.
334
Removing our temporary file here would prevent our sendmail
337
A possible workaround could be to load the function here with
338
emacsclient --eval '(load temp)' but this is not robust since
339
emacs could have been stopped between here and the call to
343
_defun = r"""(defun bzr-add-mime-att (file)
344
"Attach FILe to a mail buffer as a MIME attachment."
345
(let ((agent mail-user-agent))
348
(if (and file (file-exists-p file))
350
((eq agent 'sendmail-user-agent)
352
((or (eq agent 'message-user-agent)(eq agent 'gnus-user-agent))
353
(mml-attach-file file "text/x-patch" "BZR merge" "attachment"))
355
(message "Unhandled MUA")))
356
(error "File %s does not exist." file))))
359
fd, temp_file = tempfile.mkstemp(prefix="emacs-bzr-send-",
364
os.close(fd) # Just close the handle but do not remove the file.
316
367
def _get_compose_commandline(self, to, subject, attach_path):
317
368
commandline = ["--eval"]
318
# Ensure we can at least have an empty mail-mode buffer
323
374
_to = ("\"%s\"" % self._encode_safe(to))
324
375
if subject is not None:
325
376
_subject = ("\"%s\"" % self._encode_safe(subject))
326
mmform = "(mail nil %s %s)" % (_to ,_subject)
328
# call mail-mode, move the point to body and insert a new blank line
329
# we *must* force this point movement for the case when To is not passed
330
# with --mail-to. Without this, the patch could be inserted at the wrong place
331
commandline.append(mmform)
332
commandline.append("(mail-text)")
333
commandline.append("(newline)")
335
# ... and put a MIME attachment (if any)
378
# Funcall the default mail composition function
379
# This will work with any mail mode including default mail-mode
380
# User must tweak mail-user-agent variable to tell what function
381
# will be called inside compose-mail.
382
mail_cmd = "(compose-mail %s %s)" % (_to, _subject)
383
commandline.append(mail_cmd)
385
# Try to attach a MIME attachment using our wrapper function
336
386
if attach_path is not None:
337
ifform = "(attach \"%s\")" % self._encode_path(attach_path,'attachment')
338
commandline.append(ifform)
387
# Do not create a file if there is no attachment
388
lmmform = '(load "%s")' % self._prepare_send_function()
389
mmform = '(bzr-add-mime-att "%s")' % \
390
self._encode_path(attach_path, 'attachment')
391
commandline.append(lmmform)
392
commandline.append(mmform)
339
394
return commandline