Skip to content

Instantly share code, notes, and snippets.

@xuchunyang
Created February 11, 2015 03:00
Show Gist options
  • Save xuchunyang/db2418a19e0e458b18b3 to your computer and use it in GitHub Desktop.
Save xuchunyang/db2418a19e0e458b18b3 to your computer and use it in GitHub Desktop.
write-helm-extensions.el
;; URL: http://wikemacs.org/wiki/How_to_write_helm_extensions
;;
;; A list of candidates and an action
;;
(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))
;;
;; Dynamic candidates
;;
(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))
;;
;; Separate candidates' sets for searching and for actions
;;
(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))
(setq data '(("John" . "john@email.com")
("Jim" . "jim@email.com")
("Jane" . "jane@email.com")
("Jill" . "jill@email.com")))
;;
;; More than one action
;;
(defun open-email (candidates)
"Compose an email to the candidates. Fill in the addresses and
move point to the subject."
(compose-mail)
(message-goto-to)
(insert
(mapconcat
'identity
(helm-marked-candidates)
","))
(message-goto-subject))
(setq some-helm-source
`((name . "HELM at the Emacs")
(candidates . ,data)
(action . (("show email address" . (lambda (candidate)
(message-box
"selected: %s"
(helm-marked-candidates))))
("send email" . open-email)))))
(helm :sources '(some-helm-source))
;;
;; Handling multiple selections
;;
(defun some-action (candidate)
(loop for cand in (helm-marked-candidates)
do
(message-box "working on %s" cand)))
(helm :sources '(((name . "HELM")
(candidates . (1 2 3 4))
(action . (("open" . some-action))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment