[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Faking local dynamic function bindings
From: |
Joost Kremers |
Subject: |
Faking local dynamic function bindings |
Date: |
29 Jan 2014 02:04:57 GMT |
User-agent: |
slrn/pre1.0.0-18 (Linux) |
Hi all,
I sorta trialed and errored my way to the following code (simplified[1]):
```
(defun foo (sexpr)
(eval
`(cl-macrolet ((contains (regexp)
`(string-match ,regexp string)))
(sort (delq nil (mapcar #'(lambda (item)
(let ((string (assq item strings-alist)))
(when ,sexpr)
string))
items-alist))
'string<))))
```
Assume that `items-alist` is an alist with (SYMBOL . STRING) items.
`sexpr` is an interactively built expression that is used to filter
items-alist. It is a single s-expression and is runnable Elisp code,
provided that a macro `contains` is defined. Because I don't want to
define this macro globally (I don't want to have to put a "package"
prefix on it), I tried to define it locally.
That didn't work at first, because the expression `sexpr` is generated
at run-time, which means it cannot be in the lexical scope of
cl-macrolet. So I ended up creating a larger expression containing the
cl-macrolet and eval'ing that.
I have two questions:
- Is there a better ways of doing this? Of course, flet creates dynamic
function bindings, but it's deprecated. The suggested alternative,
`(cl-letf (symbol-value 'bar) ...)` doesn't work, because it requires
that `bar' has a (global) symbol-value, which `contains` in this
example does not.
- Can't this function be defined as a macro? It seems to me that a
backquoted expression that is subsequently eval'ed is essentially the
same as a macro, but my attempts to write foo as a macro have failed.
TIA
Joost
[1] The actual code is here:
https://github.com/joostkremers/ebib/blob/master/ebib.el#L3478
--
Joost Kremers joostkremers@fastmail.fm
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)
- Faking local dynamic function bindings,
Joost Kremers <=