Skip to content

Commit 425761b

Browse files
vspinubbatsov
authored andcommitted
New utility function cider-run-chained-hook
1 parent 76b15f6 commit 425761b

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

cider-util.el

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,28 @@ PROP is the name of a text property."
212212
(when more-text (insert more-text))
213213
(when break (insert "\n")))
214214

215+
216+
;;; Hooks
217+
218+
(defun cider-run-chained-hook (hook arg)
219+
"Like `run-hook-with-args' but pass intermediate return values through.
220+
HOOK is a name of a hook (a symbol). You can use `add-hook' or
221+
`remove-hook' to add functions to this variable. ARG is passed to first
222+
function. Its return value is passed to the second function and so forth
223+
till all functions are called or one of them returns nil. Return the value
224+
return by the last called function."
225+
(let ((functions (copy-sequence (symbol-value hook))))
226+
(while (and functions arg)
227+
(if (eq (car functions) t)
228+
;; global value of the hook
229+
(let ((functions (default-value hook)))
230+
(while (and functions arg)
231+
(setq arg (funcall (car functions) arg))
232+
(setq functions (cdr functions))))
233+
(setq arg (funcall (car functions) arg)))
234+
(setq functions (cdr functions)))
235+
arg))
236+
215237

216238
;;; Font lock
217239

test/cider-util-tests.el

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@
5454
cider-codename "Victory")
5555
(expect (cider--version) :to-equal "0.11.0snapshot (package: 20160301.2217)")))
5656

57+
(defvar some-cider-hook)
58+
59+
(describe "cider-run-chained-hook"
60+
:var (some-cider-hook)
61+
62+
(it "chains correctly"
63+
(setq some-cider-hook (list #'upcase (lambda (x) (substring x 2 5))))
64+
(expect (cider-run-chained-hook 'some-cider-hook "abcdefg")
65+
:to-equal "CDE"))
66+
67+
(it "exits on first nil"
68+
(let (here)
69+
(setq some-cider-hook (list #'upcase (lambda (x) nil) (lambda (x) (setq here t))))
70+
(cider-run-chained-hook 'some-cider-hook "A")
71+
(expect here :to-be nil))))
72+
5773
(describe "cider-symbol-at-point"
5874
(it "doesn't move the cursor"
5975
(with-temp-buffer

0 commit comments

Comments
 (0)