Created
February 11, 2015 03:00
-
-
Save xuchunyang/db2418a19e0e458b18b3 to your computer and use it in GitHub Desktop.
write-helm-extensions.el
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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