2

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)
  • 1
    The question is not very clear. It sounds like you want to create a command that you will use in some buffers to do something that incorporates most or all of what you have done in the code you show. If so, that doesn't have anything to do with creating a package. (I see nothing in your question that points to creating or using a package.) – Drew Sep 21 '17 at 13:56
  • Yes, that's what I want! Create a command. – jajpater Sep 21 '17 at 14:09
  • Hopefully my question is more clear now. – jajpater Sep 21 '17 at 14:22
  • 1
    buffer-file-name returns nil if the current buffer is not visiting a file. – Drew Sep 21 '17 at 14:32
  • Thanks @Drew your command helped me to find the solution. – jajpater Sep 21 '17 at 15:02
4

Because of the comment of Drew I was able to google further and I came up with the following code. I'll leave it up here just in case someone is having the same problem.

(require 'f)
(require 's)
(defun buffer-of-csv-to-list-to-mail ()
  "Put a buffer containing keys and values separated by a comma in a list and create a mail with those keys and values"
  (interactive)
  (setq buffername (buffer-file-name))
  (setq mylist (s-lines (s-chomp (f-read buffername))))
  (setq myHash (make-hash-table :test 'equal))
  (while mylist
    (setq subList (s-split "," (car mylist)))
    (while subList
      (print (car subList))
      (print (nth 1 subList))
      (puthash (car subList) (nth 1 subList) myHash)
      (setq subList (cdr subList)))
    (setq mylist (cdr 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)))
   (compose-mail
   "receiver@localhost.net"
   subject
   nil
   t)
  (insert mailbody))
  • 1
    You should also study let-bindings in order to restrict the visibility of local variables. Cheers. – InHarmsWay Sep 21 '17 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.