neomutt: Add attaching/saving files with file manager

This commit is contained in:
Marty Oehme 2025-09-08 21:56:40 +02:00
parent fcb6bcb924
commit 519e4ba803
Signed by: Marty
GPG key ID: 4E535BC19C61886E
2 changed files with 77 additions and 0 deletions

View file

@ -74,6 +74,10 @@ bind pager G bottom
# markdown to html for composition
macro compose ,m "F pandoc -s -f markdown -t html \ny^T^Utext/html; charset=UTF-8\n" "Convert from MD to HTML"
# attach and save file attachments
macro compose a '<enter-command>source "neomutt-filer attach"|<enter>' "Attach with file manager"
macro attach s '<enter-command>source "neomutt-filer saveto"|<enter>' "Save attachment to dir"
# since we unbound the original g
bind index,pager r noop # to avoid accidentally sending replies
bind index,pager rr group-reply

73
office/.local/bin/neomutt-filer Executable file
View file

@ -0,0 +1,73 @@
#!/usr/bin/env bash
# A simple utility script for neomutt to use your favorite cli
# file manager to work with files in neomutt.
# Can add attachments to emails or save attachments in a chosen directory.
#
# Use it as `neomutt-filer attach <my-base-dir>`
# or `neomutt-filer saveto <my-base-dir>`
#
# Can be bound to neomutt macros like the following:
#
# macro compose A '<enter-command>source "neomutt-filer attach"|<enter>' "Attach with file manager"
# macro attach S '<enter-command>source "neomutt-filer saveto"|<enter>' "Save attachment to dir"
#
# read more at the following GH issues:
# https://github.com/neomutt/neomutt/issues/1515 -- switch folders with external program
# https://github.com/neomutt/neomutt/issues/1669 -- attach files with fzf
_file_picker() {
if command -v vifm >/dev/null 2>&1; then
vifm --choose-files - "$@"
# yazi & ranger untested
elif command -v yazi >/dev/null 2>&1; then
yazi "$@" --chooser-file -
elif command -v ranger >/dev/null 2>&1; then
ranger --choosefiles - "$@"
elif command -v fzf >/dev/null 2>&1; then
fzf -m --prompt='Choose one/multiple file(s) to attach >' "$@"
fi
}
_dir_picker() {
if command -v vifm >/dev/null 2>&1; then
vifm --choose-dir - --on-choose exit "$@"
# yazi & ranger untested
# elif command -v yazi >/dev/null 2>&1; then
# yazi "$@" --chooser-file -
# elif command -v ranger >/dev/null 2>&1; then
# ranger --choosefiles - "$@"
elif command -v fzf >/dev/null 2>&1; then
find "$@" -type d -print | fzf --prompt='Choose dir >'
fi
}
attachfile() {
_file_picker "$@" |
while IFS=$'\n' read -r attachment; do
printf "push '<attach-file>%s<enter>'\n" "$attachment"
done
}
savetodir() {
_dir_picker "$@" | xargs printf "push '<save-entry>%s<enter>y<enter>'\n"
}
_usage() {
echo """
Usage: neomutt-filer <subcommand> [options...]
Subcommands:
attach Pick files to attach
saveto Pick a directory to save into
"""
}
case "${1:-}" in
attach)
shift
attachfile "$@"
;;
saveto)
shift
savetodir "$@"
;;
-h | --help | *) _usage ;;
esac