From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Joost Kremers Newsgroups: gmane.emacs.help Subject: Faking local dynamic function bindings Date: 29 Jan 2014 02:04:57 GMT Message-ID: NNTP-Posting-Host: plane.gmane.org X-Trace: ger.gmane.org 1390961107 32468 80.91.229.3 (29 Jan 2014 02:05:07 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 29 Jan 2014 02:05:07 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Jan 29 03:05:16 2014 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1W8KWh-0006Fn-U6 for geh-help-gnu-emacs@m.gmane.org; Wed, 29 Jan 2014 03:05:16 +0100 Original-Received: from localhost ([::1]:40172 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W8KWh-0001nf-EL for geh-help-gnu-emacs@m.gmane.org; Tue, 28 Jan 2014 21:05:15 -0500 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 59 Original-X-Trace: individual.net sCfF69nV1vLQGgywQri2YQef/CQyJAKnKsang5aQmGMdyxjOQH Cancel-Lock: sha1:/HViG8w2PFm4JpqhhoY6U0UIRI4= User-Agent: slrn/pre1.0.0-18 (Linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:203473 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:95742 Archived-At: 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)