Function: macroexp--fgrep
macroexp--fgrep is a byte-compiled function defined in macroexp.el.gz.
Signature
(macroexp--fgrep BINDINGS SEXP)
Documentation
Return those of the BINDINGS which might be used in SEXP.
It is used as a poor-man's "free variables" test. It differs from a true
test of free variables in the following ways:
- It does not distinguish variables from functions, so it can be used
both to detect whether a given variable is used by SEXP and to
detect whether a given function is used by SEXP.
- It does not actually know ELisp syntax, so it only looks for the presence
of symbols in SEXP and can't distinguish if those symbols are truly
references to the given variable (or function). That can make the result
include bindings which actually aren't used.
- For the same reason it may cause the result to fail to include bindings
which will be used if SEXP is not yet fully macro-expanded and the
use of the binding will only be revealed by macro expansion.
Source Code
;; Defined in /usr/src/emacs/lisp/emacs-lisp/macroexp.el.gz
(defun macroexp--fgrep (bindings sexp)
"Return those of the BINDINGS which might be used in SEXP.
It is used as a poor-man's \"free variables\" test. It differs from a true
test of free variables in the following ways:
- It does not distinguish variables from functions, so it can be used
both to detect whether a given variable is used by SEXP and to
detect whether a given function is used by SEXP.
- It does not actually know ELisp syntax, so it only looks for the presence
of symbols in SEXP and can't distinguish if those symbols are truly
references to the given variable (or function). That can make the result
include bindings which actually aren't used.
- For the same reason it may cause the result to fail to include bindings
which will be used if SEXP is not yet fully macro-expanded and the
use of the binding will only be revealed by macro expansion."
(let ((res '())
;; Cyclic code should not happen, but code can contain cyclic data :-(
(seen (make-hash-table :test #'eq))
(sexpss (list (list sexp))))
;; Use a nested while loop to reduce the amount of heap allocations for
;; pushes to `sexpss' and the `gethash' overhead.
(while (and sexpss bindings)
(let ((sexps (pop sexpss)))
(unless (gethash sexps seen)
(puthash sexps t seen) ;; Using `setf' here causes bootstrap problems.
(if (vectorp sexps) (setq sexps (mapcar #'identity sexps)))
(let ((tortoise sexps) (skip t))
(while sexps
(let ((sexp (if (consp sexps) (pop sexps)
(prog1 sexps (setq sexps nil)))))
(if skip
(setq skip nil)
(setq tortoise (cdr tortoise))
(if (eq tortoise sexps)
(setq sexps nil) ;; Found a cycle: we're done!
(setq skip t)))
(cond
((or (consp sexp) (vectorp sexp)) (push sexp sexpss))
(t
(let ((tmp (assq sexp bindings)))
(when tmp
(push tmp res)
(setq bindings (remove tmp bindings))))))))))))
res))