I want to be able to use the results of a regexp search as an helm source. The goal here is to find all the labels of a latex document, this in part because I find this useful but also to learn Helm and elisp. Here is what I came up with:
(defun my/matches-in-buffer (regexp)
"return a list of matches of REGEXP."
(let ((matches))
(save-match-data
(save-excursion
(save-restriction
(widen)
(goto-char 1)
(while (re-search-forward regexp nil t)
(push (substring (match-string 1) 7 -1) matches))))
)
matches
))
(defun my/find-labels ()
(my/matches-in-buffer "\\(\\\\label{\\(.*\\)}\\)")
)
(setq label-helm-source
'((name . "HELM label")
(candidates . my/find-labels)
(action . (lambda (candidate)
(message "%s" candidate)))))
(defun helm-label ()
(interactive)
(helm :sources '(label-helm-source)))
However this does not seem to work as helm gives me no proposition when I do M-x helm-labels
. On the other hand, executing (my/find-labels)
in an org-mode file containing a \label{test}
gives me the list with element test
as expected.
What should I change in the code to have Helm use the results of the search from my/find-labels
as a source?