I'm a total noob so be patient with me. I wrote a piece of elisp code that reads a .csv
file (FILE.CSV
) on which every line has the format Key, Value
It composes a mail using the keys and values in this CSV file. When reading from the file everything works. However I'd like to run this code on a buffer. So I tried using buffer-file-name
to get the name of the default buffer and run the functions on this buffer. This doesn't work.
So the question is how to create a command M-x schedule-as-email
that takes a buffer (having the same contents as the file) and creates a mail along the lines of the code below.
contents of file: FILE.CSV
Key,Value
John,BlahBlah
Alexander,BlahDiBlah
Andrew,Blaaah
Victor,Blablabla
Pablo,lalalalalah
Group,B
code I hacked together (sorry I'm new to programming):
(require 'f)
(require 's)
(setq myHash (make-hash-table :test 'equal))
;;(setq filename "~/FILE.CSV") - works
(setq filename (buffer-file-name)) ;;does not work
(setq mylist (s-lines (s-chomp (f-read filename))))
(defun elements-of-list-to-hash (list)
"Put each element of a LIST of keys and values in myHash."
(while list
(setq subList (s-split "," (car list)))
(while subList
(print (car subList))
(print (nth 1 subList))
(puthash (car subList) (nth 1 subList) myHash)
(setq subList (cdr subList)))
(setq list (cdr list))))
(elements-of-list-to-hash mylist)
(setq john (gethash "John" myHash))
(setq alexander (gethash "Alexander" myHash))
(setq andrew (gethash "Andrew" myHash))
(setq victor (gethash "Victor" myHash))
(setq pablo (gethash "Pablo" myHash))
(setq group (gethash "Group" myHash))
(setq next-friday (s-chomp(shell-command-to-string "/bin/date --date='next Fri' +'%e %B %Y'")))
(setq subject (format "What they should have learned before friday %s\n" next-friday))
(if (string-suffix-p group "A") (setq mailbody (format "Hi\n\n I want to tell you what our students have to learn before friday %s:\n\n John: %s\n Alexander: %s\n Andrew: %s\n" next-friday john alexander andrew)) (setq mailbody (format "Hi\n\n I want to tell you what our students have to learn before friday %s:\n\n Victor: %s\n Pablo: %s\n" next-friday victor pablo)))
(defun my-schedule-as-email ()
"Get mail form variables"
(interactive)
(compose-mail
"receiver@localhost.net"
subject
nil
t)
(insert mailbody))
(provide 'schedule-as-email)
buffer-file-name
returnsnil
if the current buffer is not visiting a file. – Drew Sep 21 '17 at 14:32