Org-mode Signature Code
I’ve been trying to learn a little elisp lately, and have written a useful routine that I thought someone might like. It’s interesting because it uses both elisp and org-mode tables.
Lately I have been setting up mu4e to read my personal email. As part of using that I wanted to have a signature that would generate a wise saying from a country, but to make it a bit more interesting I wanted the signature to choose a randomly selected saying from a different country each time I generated the signature.
I have an org-mode file that contains a two-column table. The first column contains the country and the second contains the quote.
#+NAME: SayingsTab | Afghanistan | Don't show me the palm tree, show me the dates | | Afghanistan | No one says his own buttermilk is sour | | Afghanistan | What you see in yourself is what you see in the world | | Africa | The tree that has been axed will never forget | | Albania | Don't put gold buttons on a torn coat | | Albania | Patience is the key to paradise | | Albania | When you have no companion, consult your walking stick | | Algeria | Do bad and remember, do good and forget | | America | A pessimist is a person who has lived with an optimist | | Armenia | You cannot start a fast with baklava in your hand | | Australia | There are none so deaf as those who will not hear | |-------------------------------------------------------------------------------------------------------------| #+name: quote #+begin_src elisp :exports results :var sayings=SayingsTab (defun genquote() "Generate a random quote from an org-mode table. The table contains the country name in col 0 and the quote in col 1. Add more quotes to the table as needed, the function selects from the current number of rows." (interactive) (let ((selection (nth (random (length sayings)) sayings))) (message "As they say in %s, \"%s\"." (car selection) (cadr selection)))) (genquote) #+end_src
Running the source block here generates a random quote from the list in the table as a message. Hitting C-c C-c in the code will run it and generate a quote like
As they say in America, "A pessimist is a person who has lived with an optimist".
I like using the org-table because the new items can be sorted alphabetically, and its easy to add new sayings when I find them.
What I’d like to be able to do is to load the org table using an elisp function and then do the same thing as part of a signature-generating function. Currently I can’ do that, but I can read the text from a tab-delimited text file. Fortunately, I can generate the tabular data that can be read by elisp by putting the point at the table and using the M-x org-table-export command. I save the table to a tab-delimited file called sayings.txt whenever I add to the org-table.
Then in my init.el file, I define the following elisp function to generate an email signature using the data in the text file.
(defun signature() "Generates a random quote from a tab-delimited file for use as an email signature quote. Table of quotes is in ~/ownCloud/Org/sayings.txt The tab-delimited table contains the country name in col 0 and the quote in col 1. Add more quotes to the table as needed, the function selects from the current number of rows." (interactive) (setq sayings (read-lines "~/ownCloud/Org/sayings.txt")) (setq sel (nth (random (length sayings)) sayings)) (insert "\n-- \n" "John Doe \n \n" "firstname.lastname@example.org \n" "Ph: 867 5309 \n") (insert "As they say in ") (insert (remove-trailing (car (split-string sel "\t" t))) ", '") (insert (remove-trailing (cadr (split-string sel "\t" t))) "'. \n") )
This should produce something like
-- John Doe email@example.com Ph: 867 5309 As they say in Malaysia, 'One buffalo brings mud and all the herd are smeared with it'.
where the last line is the randomly generated quote. So then, when I’m editing my email, I just need to call the signature function when editing an email in mu4e and I get my signature with a new quote every time.
If you know how to read direct from the org-table, rather than from a text file, I’d love to know how you do it, as that would remove the need to update the table from time to time.
Leave a Reply