all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Joost Kremers <joost.m.kremers@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Faking local dynamic function bindings
Date: 29 Jan 2014 02:04:57 GMT	[thread overview]
Message-ID: <slrnlegoe9.6e2.joost.m.kremers@j.kremers4.news.arnhem.chello.nl> (raw)

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)


             reply	other threads:[~2014-01-29  2:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-29  2:04 Joost Kremers [this message]
2014-01-29  3:50 ` Faking local dynamic function bindings Stefan Monnier
     [not found] ` <mailman.13080.1390967480.10748.help-gnu-emacs@gnu.org>
2014-01-29  9:05   ` Joost Kremers
2014-01-29 13:16     ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=slrnlegoe9.6e2.joost.m.kremers@j.kremers4.news.arnhem.chello.nl \
    --to=joost.m.kremers@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.