1 ;;; cl-lib.el --- Properly prefixed CL functions and macros -*- coding: utf-8 -*-
3 ;; Copyright (C) 2012, 2013 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; vcomment: Emacs-24.3's version is 1.0 so this has to stay below.
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
24 ;; This is a forward compatibility package, which provides (a subset of) the
25 ;; features of the cl-lib package introduced in Emacs-24.3, for use on
28 ;; Make sure this is installed *late* in your `load-path`, i.e. after Emacs's
29 ;; built-in .../lisp/emacs-lisp directory, so that if/when you upgrade to
30 ;; Emacs-24.3, the built-in version of the file will take precedence, otherwise
31 ;; you could get into trouble (although we try to hack our way around the
32 ;; problem in case it happens).
34 ;; This code is largely copied from Emacs-24.3's cl.el, with the alias bindings
39 (when (functionp 'macroexp--compiler-macro)
40 ;; `macroexp--compiler-macro' was introduced as part of the big CL
41 ;; reorganization which moved/reimplemented some of CL into core (mostly the
42 ;; setf and compiler-macro support), so its presence indicates we're running
43 ;; in an Emacs that comes with the new cl-lib.el, where this file should
45 (message "Real cl-lib shadowed by compatibility cl-lib? (%s)" load-file-name)
47 ;; (message "Let's try to patch things up")
48 (let ((loaddir (file-name-directory load-file-name))
50 ;; Find the problematic directory from load-path.
51 (dolist (dir load-path)
52 (if (equal loaddir (expand-file-name (file-name-as-directory dir)))
53 (setq load-path-dir dir)))
55 ;; (message "Let's move the offending dir to the end")
56 (setq load-path (append (remove load-path-dir load-path)
57 (list load-path-dir)))
58 ;; Here we could manually load cl-lib and then return immediately.
59 ;; But Emacs currently doesn't provide any way for a file to "return
60 ;; immediately", so instead we make sure the rest of the file does not
61 ;; throw away any pre-existing definition.
66 ;; Some of Emacs-24.3's cl.el definition are not just aliases, because either
67 ;; the feature was dropped from cl-lib.el or because the cl-lib version is
68 ;; not fully compatible.
69 ;; Let's just not include them here, since it is very important that if code
70 ;; works with this cl-lib.el it should also work with Emacs-24.3's cl-lib.el,
71 ;; whereas the reverse is much less important.
82 ;; lambda-list-keywords
83 float-negative-epsilon
85 least-negative-normalized-float
86 least-positive-normalized-float
91 ;; custom-print-functions
93 (let ((new (intern (format "cl-%s" var))))
94 (unless (boundp new) (defvaralias new var))))
102 (truncate* . cl-truncate)
103 (ceiling* . cl-ceiling)
105 (rassoc* . cl-rassoc)
107 (member* . cl-member)
108 (delete* . cl-delete)
109 (remove* . cl-remove)
110 (defsubst* . cl-defsubst)
112 (function* . cl-function)
113 (defmacro* . cl-defmacro)
115 (mapcar* . cl-mapcar)
194 define-compiler-macro
208 (define-setf-method . define-setf-expander)
302 (let ((new (if (consp fun) (prog1 (cdr fun) (setq fun (car fun)))
303 (intern (format "cl-%s" fun)))))
304 (unless (fboundp new) (defalias new fun))))
306 ;; `cl-labels' is not 100% compatible with `labels' when using dynamic scoping
307 ;; (mostly because it does not turn lambdas that refer to those functions into
308 ;; closures). OTOH it is compatible when using lexical scoping.
310 (unless (fboundp 'cl-labels)
311 (defmacro cl-labels (&rest args)
312 (unless (and (boundp 'lexical-binding) lexical-binding)
313 ;; We used to signal an error rather than a message, but in many uses of
314 ;; cl-labels, the value of lexical-binding doesn't actually matter.
315 ;; More importantly, the value of `lexical-binding' here is unreliable
316 ;; (it does not necessarily reflect faithfully whether the output of this
317 ;; macro will be interpreted as lexically bound code or not).
318 (message "This `cl-labels' requires `lexical-binding' to be non-nil"))
322 ;;; cl-lib.el ends here