## Anatomy of a helm source

| categories: | tags: | View Comments

I have been integrating helm into my emacs work flows almost anywhere I need to make interactive selections and do something with them. In this post, I will go through the simplest helm examples I can think of that get you to writing your own example.

To run a helm selection process you basically just call a function that calls this minimal function:

(helm :sources '(some-helm-source))


In that code, the symbol some-helm-source will provide the input for the helm buffer. Let us look at the simplest example here. Each source should have a name, a list of candidates, and an action that works on the selected candidate. We construct a source as a list of cons cells. Here, we make a source with the name "HELM at the Emacs", a static list of candidates, which are simply a list of numbers, and a single action that will operate on the selected candidate.

If you run this block, you will get a helm buffer, you can select an entry, press enter, and you should see a message box pop up telling you what entry you selected. I like to separate the source definition from the helm call like this, but only for readability.

(setq some-helm-source
'((name . "HELM at the Emacs")
(candidates . (1 2 3 4))
(action . (lambda (candidate)
(message-box "%s" candidate)))))

(helm :sources '(some-helm-source))

3


Not bad, but what if we want some dynamic candidates? The usual way we will do that is to define a function that calculates the candidates for us. Let us work out an example that just shows us random numbers between 0 and 10 to select from. In a real example, you would use this function to generate a list of candidates like bibtex keys, email-addresses, etc…

(defun random-candidates ()
"Return a list of 4 random numbers from 0 to 10"
(loop for i below 4 collect (random 10)))

(setq some-helm-source
'((name . "HELM at the Emacs")
(candidates . random-candidates)
(action . (lambda (candidate)
(message "%s" candidate)))))

(helm :sources '(some-helm-source))


So far, we have looked at the simplest list of candidates: a simple list. It may be that this is not the most convenient way to see the candidates. We might like to have one set of candidates that we use for searching, but another set of equivalent candidates used for the action. For example, we might want a list of names for selecting, but then have the action work on the corresponding email address. Let us consider a case where we have a list of cons cells of names and email addresses.

We use the , way to create the source variable to make sure our list of candidates is constructed. Then, in our function we take the selection and get the corresponding entry in the data a-list.

(setq data '(("John" . "john@email.com")
("Jim" . "jim@email.com")
("Jane" . "jane@email.com")
("Jill" . "jill@email.com")))

(setq some-helm-source
((name . "HELM at the Emacs")
(candidates . ,(mapcar 'car data))
(action . (lambda (candidate)
(message "%s" (cdr (assoc candidate data)))))))

(helm :sources '(some-helm-source))

jim@email.com


That is not too bad, and might be a general way to get to the data you want. But, helm can integrate this directly by using the a-list directly as the list of candidates. Helm will show you the car of each cell, but return the cdr of the selected entry.

Let us try this to make a function that will give us a helm buffer to select some names from, and then insert a comma separated list of emails from our selection at the point. We make our action function just return the list of marked candidates. Then we create a function that calls helm, and inserts a concatenated string.

(setq data '(("John" . "john@email.com")
("Jim" . "jim@email.com")
("Jane" . "jane@email.com")
("Jill" . "jill@email.com")))

(setq some-helm-source
((name . "HELM at the Emacs")
(candidates . ,data)
(action . (lambda (candidate)
(helm-marked-candidates)))))

(defun helm-select-and-insert-emails ()
(interactive)
(insert
(mapconcat 'identity
(helm :sources '(some-helm-source))
",")))

helm-select-and-insert-emails
`

Here is what I get when I run the command, select John and Jill, and press enter: john@email.com,jill@email.com

That is it for this post. We looked at:

1. the simplest kind of helm interface with a fixed set of candidates
2. A simple dynamic set of candidates
3. A simple fixed set of candidates from a list of cons cells.

This barely scratches the surface of helm, but is already enough to do some useful things.