I have the following python code stored in my
#!/usr/bin/python3 from sys import argv # open up the email contents file infile = open(argv, 'r') # read it into an array of strings w/o new lines body = [line.rstrip('\n') for line in infile] # close the input file infile.close() # returns if a line is OK to join with one above it # all but blank lines, # those that start with a > (because they are quotes) # ones starting with space (because they are blocks of code e.g.) def line_joinable(line): if len(line) == 0: return False if line == '>': return False if line in [' ', '\t']: return False return True # go through each line i = 0 while i < (len(body) - 1): line = body[i] next_line = body[i + 1] # if both lines are joinable, join them if line_joinable(line) and line_joinable(next_line): body[i] = body[i] + ' ' + body[i + 1] del body[i + 1] continue else: i += 1 # open the output file to overwrite it in place outfile = open(argv, 'w') for line in body: outfile.write("%s\n" % line) outfile.close()
What it does is take a file as input argument and remove line-breaks unless the line has an indentation, keyword (pertaining to Subject, From, To, Cc, Bcc, etc). Ideal for typing out an email in
nvim that has settings set at say 72 characters, so pleasant to read/edit while typing, because this code converts into free-flowing text for easier reading on all devices. Details are here: https://ianfinlayson.net/posts/10-mutt however, I think that the solution there (to go through every file in
/tmp/mutt-*) is unnecessary and (what is worse) does not work with
nvim. However, while unncessary, it does work with
vim because I have tried it there, and it works.
nvim as editor. What I would like to do is to, after exiting the editor and before it gets saved as an attachment on
mutt go through this program. How do I do this? I know that I can edit
$HOME/.vim/ftplugin/mail.vim but I can not figure out how to do this with either
vimscript. I do know that I have to invoke an
VimLeave but I can not figure out how to process the particular file that has been saved by
:wq to be piped through the
I tried the following:
augroup autocom autocmd! "executes the command on quit autocmd VimLeave /tmp/mutt-* !/home/finlayson/bin/email-process % /bin/email-process % augroup END
This gives, on the mutt window:
Error running "/usr/bin/nvim '/tmp/mutt-localhost-1000-336530-16967570324965601212'"!
But apppears to do the work. But the error message on the window is not relevant and does not pertain to the filename just edited. I guess the problem is that the python script
email-process is being run on more files than just the one that has been created and edited. Is it possible to (1) get rid of the error message and (2) more importantly, only restrict the script to the file that was edited?
Instead therefore, I tried the following:
augroup autocom autocmd! "executes the command on write autocmd BufWritePost,FileWritePost /tmp/mutt-* !/home/finlayson/bin/email-process % augroup END
Now, I do not get an error when I do
:w (and watch the formatting happen, that is a bit cool) and then
:q but I get the error when I do
:wq so what is the issue here?