Last active
June 3, 2017 15:03
-
-
Save sigma/4360650 to your computer and use it in GitHub Desktop.
#emacs dynamic flet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; dflet.el --- dynamically-scoped flet | |
;; Copyright (C) 2012 Yann Hodique | |
;; Copyright (C) 1993, 2001-2012 Free Software Foundation, Inc. | |
;; Author: Yann Hodique <[email protected]> | |
;; Keywords: lisp | |
;; Version: 0.1 | |
;; This file is free software; you can redistribute it and/or modify | |
;; it under the terms of the GNU General Public License as published by | |
;; the Free Software Foundation; either version 2, or (at your option) | |
;; any later version. | |
;; This file is distributed in the hope that it will be useful, | |
;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
;; GNU General Public License for more details. | |
;; You should have received a copy of the GNU General Public License | |
;; along with GNU Emacs; see the file COPYING. If not, write to | |
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
;; Boston, MA 02111-1307, USA. | |
;;; Commentary: | |
;; This is bringing back the historical definition of `flet', in all its global | |
;; and dynamic splendor. | |
;;; Code: | |
(eval-when-compile | |
(require 'cl)) | |
;;;###autoload | |
(if (version< emacs-version "24.3") ;; before that, flet is not obsolete | |
(defalias 'dflet 'flet) | |
;; This should really have some way to shadow 'byte-compile properties, etc. | |
(defmacro dflet (bindings &rest body) | |
"Make temporary overriding function definitions. | |
This is an analogue of a dynamically scoped `let' that operates on the function | |
cell of FUNCs rather than their value cell. | |
If you want the Common-Lisp style of `flet', you should use `cl-flet'. | |
The FORMs are evaluated with the specified function definitions in place, | |
then the definitions are undone (the FUNCs go back to their previous | |
definitions, or lack thereof). | |
\(fn ((FUNC ARGLIST BODY...) ...) FORM...)" | |
(declare (indent 1) (debug cl-flet)) | |
(let ((compiling-file (or (and (fboundp 'cl--compiling-file) | |
'cl--compiling-file) | |
'cl-compiling-file))) | |
`(letf ,(mapcar | |
(lambda (x) | |
(if (or (and (fboundp (car x)) | |
(eq (car-safe (symbol-function (car x))) 'macro)) | |
(cdr (assq (car x) macroexpand-all-environment))) | |
(error "Use `labels', not `dflet', to rebind macro names")) | |
(let ((func `(cl-function | |
(lambda ,(cadr x) | |
(cl-block ,(car x) ,@(cddr x)))))) | |
(when (funcall compiling-file) | |
;; Bug#411. It would be nice to fix this. | |
(and (get (car x) 'byte-compile) | |
(error "Byte-compiling a redefinition of `%s' \ | |
will not work - use `labels' instead" (symbol-name (car x)))) | |
;; FIXME This affects the rest of the file, when it | |
;; should be restricted to the flet body. | |
(and (boundp 'byte-compile-function-environment) | |
(push (cons (car x) (eval func)) | |
byte-compile-function-environment))) | |
(list `(symbol-function ',(car x)) func))) | |
bindings) | |
,@body)))) | |
(provide 'dflet) | |
;;; dflet.el ends here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment