Commit | Line | Data |
---|---|---|
6e9590e2 | 1 | ;;; cl.el --- Compatibility aliases for the old CL library. -*- lexical-binding: t -*- |
fcd73769 | 2 | |
ba318903 | 3 | ;; Copyright (C) 2012-2014 Free Software Foundation, Inc. |
fcd73769 | 4 | |
7c1898a7 | 5 | ;; Author: Stefan Monnier <monnier@iro.umontreal.ca> |
fcd73769 RS |
6 | ;; Keywords: extensions |
7 | ||
8 | ;; This file is part of GNU Emacs. | |
9 | ||
d6cba7ae | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
fcd73769 | 11 | ;; it under the terms of the GNU General Public License as published by |
d6cba7ae GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
fcd73769 RS |
14 | |
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
d6cba7ae | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
fcd73769 | 22 | |
07b3798c | 23 | ;;; Commentary: |
fcd73769 | 24 | |
7c1898a7 SM |
25 | ;; This is a compatibility file which provides the old names provided by CL |
26 | ;; before we cleaned up its namespace usage. | |
fcd73769 | 27 | |
07b3798c | 28 | ;;; Code: |
fcd73769 | 29 | |
7c1898a7 | 30 | (require 'cl-lib) |
de7e2b36 | 31 | (require 'macroexp) |
543e3c63 | 32 | (require 'gv) |
7c1898a7 SM |
33 | |
34 | ;; (defun cl--rename () | |
35 | ;; (let ((vdefs ()) | |
36 | ;; (fdefs ()) | |
37 | ;; (case-fold-search nil) | |
38 | ;; (files '("cl.el" "cl-macs.el" "cl-seq.el" "cl-extra.el"))) | |
39 | ;; (dolist (file files) | |
40 | ;; (with-current-buffer (find-file-noselect file) | |
41 | ;; (goto-char (point-min)) | |
42 | ;; (while (re-search-forward | |
43 | ;; "^(\\(def[^ \t\n]*\\) +'?\\(\\(\\sw\\|\\s_\\)+\\)" nil t) | |
44 | ;; (let ((name (match-string-no-properties 2)) | |
45 | ;; (type (match-string-no-properties 1))) | |
46 | ;; (unless (string-match-p "\\`cl-" name) | |
47 | ;; (cond | |
48 | ;; ((member type '("defvar" "defconst")) | |
49 | ;; (unless (member name vdefs) (push name vdefs))) | |
50 | ;; ((member type '("defun" "defsubst" "defalias" "defmacro")) | |
51 | ;; (unless (member name fdefs) (push name fdefs))) | |
52 | ;; ((member type '("def-edebug-spec" "defsetf" "define-setf-method" | |
53 | ;; "define-compiler-macro")) | |
54 | ;; nil) | |
55 | ;; (t (error "Unknown type %S" type)))))))) | |
56 | ;; (let ((re (concat "\\_<" (regexp-opt (append vdefs fdefs)) "\\_>")) | |
57 | ;; (conflicts ())) | |
58 | ;; (dolist (file files) | |
59 | ;; (with-current-buffer (find-file-noselect file) | |
60 | ;; (goto-char (point-min)) | |
61 | ;; (while (re-search-forward re nil t) | |
62 | ;; (replace-match "cl-\\&")) | |
63 | ;; (save-buffer)))) | |
64 | ;; (with-current-buffer (find-file-noselect "cl-rename.el") | |
65 | ;; (dolist (def vdefs) | |
66 | ;; (insert (format "(defvaralias '%s 'cl-%s)\n" def def))) | |
67 | ;; (dolist (def fdefs) | |
68 | ;; (insert (format "(defalias '%s 'cl-%s)\n" def def))) | |
69 | ;; (save-buffer)))) | |
70 | ||
71 | ;; (defun cl--unrename () | |
72 | ;; ;; Taken from "Naming Conventions" node of the doc. | |
73 | ;; (let* ((names '(defun* defsubst* defmacro* function* member* | |
74 | ;; assoc* rassoc* get* remove* delete* | |
75 | ;; mapcar* sort* floor* ceiling* truncate* | |
76 | ;; round* mod* rem* random*)) | |
77 | ;; (files '("cl.el" "cl-lib.el" "cl-macs.el" "cl-seq.el" "cl-extra.el")) | |
78 | ;; (re (concat "\\_<cl-" (regexp-opt (mapcar #'symbol-name names)) | |
79 | ;; "\\_>"))) | |
80 | ;; (dolist (file files) | |
81 | ;; (with-current-buffer (find-file-noselect file) | |
82 | ;; (goto-char (point-min)) | |
83 | ;; (while (re-search-forward re nil t) | |
84 | ;; (delete-region (1- (point)) (point))) | |
85 | ;; (save-buffer))))) | |
2ee3d7f0 | 86 | |
82e1f390 SM |
87 | (defun cl-unload-function () |
88 | "Stop unloading of the Common Lisp extensions." | |
89 | (message "Cannot unload the feature `cl'") | |
90 | ;; Stop standard unloading! | |
91 | t) | |
92 | ||
2ee3d7f0 SM |
93 | ;;; Aliases to cl-lib's features. |
94 | ||
7c1898a7 SM |
95 | (dolist (var '( |
96 | ;; loop-result-var | |
97 | ;; loop-result | |
98 | ;; loop-initially | |
99 | ;; loop-finally | |
100 | ;; loop-bindings | |
101 | ;; loop-args | |
102 | ;; bind-inits | |
103 | ;; bind-block | |
104 | ;; lambda-list-keywords | |
105 | float-negative-epsilon | |
106 | float-epsilon | |
107 | least-negative-normalized-float | |
108 | least-positive-normalized-float | |
109 | least-negative-float | |
110 | least-positive-float | |
111 | most-negative-float | |
112 | most-positive-float | |
113 | ;; custom-print-functions | |
114 | )) | |
115 | (defvaralias var (intern (format "cl-%s" var)))) | |
116 | ||
117 | (dolist (fun '( | |
118 | (get* . cl-get) | |
119 | (random* . cl-random) | |
120 | (rem* . cl-rem) | |
121 | (mod* . cl-mod) | |
122 | (round* . cl-round) | |
123 | (truncate* . cl-truncate) | |
124 | (ceiling* . cl-ceiling) | |
125 | (floor* . cl-floor) | |
126 | (rassoc* . cl-rassoc) | |
127 | (assoc* . cl-assoc) | |
128 | (member* . cl-member) | |
129 | (delete* . cl-delete) | |
130 | (remove* . cl-remove) | |
131 | (defsubst* . cl-defsubst) | |
132 | (sort* . cl-sort) | |
133 | (function* . cl-function) | |
134 | (defmacro* . cl-defmacro) | |
135 | (defun* . cl-defun) | |
136 | (mapcar* . cl-mapcar) | |
137 | ||
138 | remprop | |
139 | getf | |
140 | tailp | |
141 | list-length | |
142 | nreconc | |
143 | revappend | |
144 | concatenate | |
145 | subseq | |
146 | random-state-p | |
147 | make-random-state | |
148 | signum | |
149 | isqrt | |
150 | lcm | |
151 | gcd | |
152 | notevery | |
153 | notany | |
154 | every | |
155 | some | |
156 | mapcon | |
157 | mapcan | |
158 | mapl | |
159 | maplist | |
160 | map | |
161 | equalp | |
162 | coerce | |
163 | tree-equal | |
164 | nsublis | |
165 | sublis | |
166 | nsubst-if-not | |
167 | nsubst-if | |
168 | nsubst | |
169 | subst-if-not | |
170 | subst-if | |
171 | subsetp | |
172 | nset-exclusive-or | |
173 | set-exclusive-or | |
174 | nset-difference | |
175 | set-difference | |
176 | nintersection | |
177 | intersection | |
178 | nunion | |
179 | union | |
180 | rassoc-if-not | |
181 | rassoc-if | |
182 | assoc-if-not | |
183 | assoc-if | |
184 | member-if-not | |
185 | member-if | |
186 | merge | |
187 | stable-sort | |
188 | search | |
189 | mismatch | |
190 | count-if-not | |
191 | count-if | |
192 | count | |
193 | position-if-not | |
194 | position-if | |
195 | position | |
196 | find-if-not | |
197 | find-if | |
198 | find | |
199 | nsubstitute-if-not | |
200 | nsubstitute-if | |
201 | nsubstitute | |
202 | substitute-if-not | |
203 | substitute-if | |
204 | substitute | |
205 | delete-duplicates | |
206 | remove-duplicates | |
207 | delete-if-not | |
208 | delete-if | |
209 | remove-if-not | |
210 | remove-if | |
211 | replace | |
212 | fill | |
213 | reduce | |
214 | compiler-macroexpand | |
215 | define-compiler-macro | |
216 | assert | |
217 | check-type | |
218 | typep | |
219 | deftype | |
220 | defstruct | |
7c1898a7 SM |
221 | callf2 |
222 | callf | |
223 | letf* | |
eb8006c6 | 224 | letf |
7c1898a7 SM |
225 | rotatef |
226 | shiftf | |
227 | remf | |
228 | psetf | |
2ee3d7f0 | 229 | (define-setf-method . define-setf-expander) |
7c1898a7 SM |
230 | the |
231 | locally | |
232 | multiple-value-setq | |
233 | multiple-value-bind | |
7c1898a7 SM |
234 | symbol-macrolet |
235 | macrolet | |
7c1898a7 SM |
236 | progv |
237 | psetq | |
238 | do-all-symbols | |
239 | do-symbols | |
7c1898a7 SM |
240 | do* |
241 | do | |
242 | loop | |
243 | return-from | |
244 | return | |
245 | block | |
246 | etypecase | |
247 | typecase | |
248 | ecase | |
249 | case | |
250 | load-time-value | |
251 | eval-when | |
252 | destructuring-bind | |
253 | gentemp | |
254 | gensym | |
255 | pairlis | |
256 | acons | |
257 | subst | |
258 | adjoin | |
259 | copy-list | |
260 | ldiff | |
261 | list* | |
262 | cddddr | |
263 | cdddar | |
264 | cddadr | |
265 | cddaar | |
266 | cdaddr | |
267 | cdadar | |
268 | cdaadr | |
269 | cdaaar | |
270 | cadddr | |
271 | caddar | |
272 | cadadr | |
273 | cadaar | |
274 | caaddr | |
275 | caadar | |
276 | caaadr | |
277 | caaaar | |
278 | cdddr | |
279 | cddar | |
280 | cdadr | |
281 | cdaar | |
282 | caddr | |
283 | cadar | |
284 | caadr | |
285 | caaar | |
286 | tenth | |
287 | ninth | |
288 | eighth | |
289 | seventh | |
290 | sixth | |
291 | fifth | |
292 | fourth | |
293 | third | |
294 | endp | |
295 | rest | |
296 | second | |
297 | first | |
298 | svref | |
299 | copy-seq | |
300 | evenp | |
301 | oddp | |
302 | minusp | |
303 | plusp | |
304 | floatp-safe | |
305 | declaim | |
306 | proclaim | |
307 | nth-value | |
308 | multiple-value-call | |
309 | multiple-value-apply | |
310 | multiple-value-list | |
311 | values-list | |
312 | values | |
313 | pushnew | |
7c1898a7 SM |
314 | decf |
315 | incf | |
316 | )) | |
317 | (let ((new (if (consp fun) (prog1 (cdr fun) (setq fun (car fun))) | |
318 | (intern (format "cl-%s" fun))))) | |
7abaf5cc | 319 | (defalias fun new))) |
416a2c45 | 320 | |
413d4689 SM |
321 | (defun cl--wrap-in-nil-block (fun &rest args) |
322 | `(cl-block nil ,(apply fun args))) | |
323 | (advice-add 'dolist :around #'cl--wrap-in-nil-block) | |
324 | (advice-add 'dotimes :around #'cl--wrap-in-nil-block) | |
325 | ||
326 | (defun cl--pass-args-to-cl-declare (&rest specs) | |
327 | (macroexpand `(cl-declare ,@specs))) | |
328 | (advice-add 'declare :after #'cl--pass-args-to-cl-declare) | |
329 | ||
2ee3d7f0 SM |
330 | ;;; Features provided a bit differently in Elisp. |
331 | ||
332 | ;; First, the old lexical-let is now better served by `lexical-binding', tho | |
333 | ;; it's not 100% compatible. | |
334 | ||
de7e2b36 SM |
335 | (defvar cl-closure-vars nil) |
336 | (defvar cl--function-convert-cache nil) | |
337 | ||
338 | (defun cl--function-convert (f) | |
339 | "Special macro-expander for special cases of (function F). | |
340 | The two cases that are handled are: | |
341 | - closure-conversion of lambda expressions for `lexical-let'. | |
342 | - renaming of F when it's a function defined via `cl-labels' or `labels'." | |
343 | (require 'cl-macs) | |
bb3faf5b | 344 | (declare-function cl--expr-contains-any "cl-macs" (x y)) |
de7e2b36 SM |
345 | (cond |
346 | ;; ¡¡Big Ugly Hack!! We can't use a compiler-macro because those are checked | |
347 | ;; *after* handling `function', but we want to stop macroexpansion from | |
348 | ;; being applied infinitely, so we use a cache to return the exact `form' | |
349 | ;; being expanded even though we don't receive it. | |
350 | ((eq f (car cl--function-convert-cache)) (cdr cl--function-convert-cache)) | |
351 | ((eq (car-safe f) 'lambda) | |
352 | (let ((body (mapcar (lambda (f) | |
353 | (macroexpand-all f macroexpand-all-environment)) | |
354 | (cddr f)))) | |
355 | (if (and cl-closure-vars | |
356 | (cl--expr-contains-any body cl-closure-vars)) | |
357 | (let* ((new (mapcar 'cl-gensym cl-closure-vars)) | |
358 | (sub (cl-pairlis cl-closure-vars new)) (decls nil)) | |
359 | (while (or (stringp (car body)) | |
360 | (eq (car-safe (car body)) 'interactive)) | |
361 | (push (list 'quote (pop body)) decls)) | |
362 | (put (car (last cl-closure-vars)) 'used t) | |
363 | `(list 'lambda '(&rest --cl-rest--) | |
364 | ,@(cl-sublis sub (nreverse decls)) | |
365 | (list 'apply | |
366 | (list 'quote | |
367 | #'(lambda ,(append new (cadr f)) | |
368 | ,@(cl-sublis sub body))) | |
369 | ,@(nconc (mapcar (lambda (x) `(list 'quote ,x)) | |
370 | cl-closure-vars) | |
371 | '((quote --cl-rest--)))))) | |
372 | (let* ((newf `(lambda ,(cadr f) ,@body)) | |
373 | (res `(function ,newf))) | |
374 | (setq cl--function-convert-cache (cons newf res)) | |
375 | res)))) | |
376 | (t | |
377 | (let ((found (assq f macroexpand-all-environment))) | |
378 | (if (and found (ignore-errors | |
379 | (eq (cadr (cl-caddr found)) 'cl-labels-args))) | |
380 | (cadr (cl-caddr (cl-cadddr found))) | |
381 | (let ((res `(function ,f))) | |
382 | (setq cl--function-convert-cache (cons f res)) | |
383 | res)))))) | |
384 | ||
385 | (defmacro lexical-let (bindings &rest body) | |
386 | "Like `let', but lexically scoped. | |
387 | The main visible difference is that lambdas inside BODY will create | |
388 | lexical closures as in Common Lisp. | |
389 | \n(fn BINDINGS BODY)" | |
390 | (declare (indent 1) (debug let)) | |
391 | (let* ((cl-closure-vars cl-closure-vars) | |
392 | (vars (mapcar (function | |
393 | (lambda (x) | |
394 | (or (consp x) (setq x (list x))) | |
395 | (push (make-symbol (format "--cl-%s--" (car x))) | |
396 | cl-closure-vars) | |
397 | (set (car cl-closure-vars) [bad-lexical-ref]) | |
398 | (list (car x) (cadr x) (car cl-closure-vars)))) | |
399 | bindings)) | |
400 | (ebody | |
401 | (macroexpand-all | |
402 | `(cl-symbol-macrolet | |
403 | ,(mapcar (lambda (x) | |
404 | `(,(car x) (symbol-value ,(cl-caddr x)))) | |
405 | vars) | |
406 | ,@body) | |
407 | (cons (cons 'function #'cl--function-convert) | |
408 | macroexpand-all-environment)))) | |
409 | (if (not (get (car (last cl-closure-vars)) 'used)) | |
410 | ;; Turn (let ((foo (cl-gensym))) | |
411 | ;; (set foo <val>) ...(symbol-value foo)...) | |
412 | ;; into (let ((foo <val>)) ...(symbol-value 'foo)...). | |
413 | ;; This is good because it's more efficient but it only works with | |
414 | ;; dynamic scoping, since with lexical scoping we'd need | |
415 | ;; (let ((foo <val>)) ...foo...). | |
416 | `(progn | |
417 | ,@(mapcar (lambda (x) `(defvar ,(cl-caddr x))) vars) | |
418 | (let ,(mapcar (lambda (x) (list (cl-caddr x) (cadr x))) vars) | |
419 | ,(cl-sublis (mapcar (lambda (x) | |
420 | (cons (cl-caddr x) | |
421 | `',(cl-caddr x))) | |
422 | vars) | |
423 | ebody))) | |
424 | `(let ,(mapcar (lambda (x) | |
425 | (list (cl-caddr x) | |
426 | `(make-symbol ,(format "--%s--" (car x))))) | |
427 | vars) | |
2ee3d7f0 | 428 | (setf ,@(apply #'append |
de7e2b36 SM |
429 | (mapcar (lambda (x) |
430 | (list `(symbol-value ,(cl-caddr x)) (cadr x))) | |
431 | vars))) | |
432 | ,ebody)))) | |
433 | ||
434 | (defmacro lexical-let* (bindings &rest body) | |
435 | "Like `let*', but lexically scoped. | |
436 | The main visible difference is that lambdas inside BODY, and in | |
437 | successive bindings within BINDINGS, will create lexical closures | |
438 | as in Common Lisp. This is similar to the behavior of `let*' in | |
439 | Common Lisp. | |
440 | \n(fn BINDINGS BODY)" | |
441 | (declare (indent 1) (debug let)) | |
442 | (if (null bindings) (cons 'progn body) | |
443 | (setq bindings (reverse bindings)) | |
444 | (while bindings | |
445 | (setq body (list `(lexical-let (,(pop bindings)) ,@body)))) | |
446 | (car body))) | |
447 | ||
448 | ;; This should really have some way to shadow 'byte-compile properties, etc. | |
de7e2b36 | 449 | (defmacro flet (bindings &rest body) |
d5c6faf9 SM |
450 | "Make temporary overriding function definitions. |
451 | This is an analogue of a dynamically scoped `let' that operates on the function | |
452 | cell of FUNCs rather than their value cell. | |
453 | If you want the Common-Lisp style of `flet', you should use `cl-flet'. | |
454 | The FORMs are evaluated with the specified function definitions in place, | |
455 | then the definitions are undone (the FUNCs go back to their previous | |
456 | definitions, or lack thereof). | |
de7e2b36 SM |
457 | |
458 | \(fn ((FUNC ARGLIST BODY...) ...) FORM...)" | |
a464a6c7 | 459 | (declare (indent 1) (debug cl-flet) |
277f0cfa | 460 | (obsolete "use either `cl-flet' or `cl-letf'." "24.3")) |
a464a6c7 SM |
461 | `(letf ,(mapcar |
462 | (lambda (x) | |
463 | (if (or (and (fboundp (car x)) | |
464 | (eq (car-safe (symbol-function (car x))) 'macro)) | |
465 | (cdr (assq (car x) macroexpand-all-environment))) | |
466 | (error "Use `labels', not `flet', to rebind macro names")) | |
467 | (let ((func `(cl-function | |
468 | (lambda ,(cadr x) | |
469 | (cl-block ,(car x) ,@(cddr x)))))) | |
470 | (when (cl--compiling-file) | |
471 | ;; Bug#411. It would be nice to fix this. | |
472 | (and (get (car x) 'byte-compile) | |
473 | (error "Byte-compiling a redefinition of `%s' \ | |
de7e2b36 | 474 | will not work - use `labels' instead" (symbol-name (car x)))) |
a464a6c7 SM |
475 | ;; FIXME This affects the rest of the file, when it |
476 | ;; should be restricted to the flet body. | |
477 | (and (boundp 'byte-compile-function-environment) | |
478 | (push (cons (car x) (eval func)) | |
479 | byte-compile-function-environment))) | |
480 | (list `(symbol-function ',(car x)) func))) | |
481 | bindings) | |
de7e2b36 SM |
482 | ,@body)) |
483 | ||
484 | (defmacro labels (bindings &rest body) | |
485 | "Make temporary function bindings. | |
a464a6c7 SM |
486 | Like `cl-labels' except that the lexical scoping is handled via `lexical-let' |
487 | rather than relying on `lexical-binding'." | |
2a1e2476 | 488 | (declare (indent 1) (debug cl-flet) (obsolete cl-labels "24.3")) |
de7e2b36 SM |
489 | (let ((vars nil) (sets nil) (newenv macroexpand-all-environment)) |
490 | (dolist (binding bindings) | |
491 | ;; It's important that (not (eq (symbol-name var1) (symbol-name var2))) | |
492 | ;; because these var's *names* get added to the macro-environment. | |
493 | (let ((var (make-symbol (format "--cl-%s--" (car binding))))) | |
494 | (push var vars) | |
495 | (push `(cl-function (lambda . ,(cdr binding))) sets) | |
496 | (push var sets) | |
497 | (push (cons (car binding) | |
498 | `(lambda (&rest cl-labels-args) | |
499 | (cl-list* 'funcall ',var | |
500 | cl-labels-args))) | |
501 | newenv))) | |
502 | (macroexpand-all `(lexical-let ,vars (setq ,@sets) ,@body) newenv))) | |
503 | ||
2ee3d7f0 SM |
504 | ;; Generalized variables are provided by gv.el, but some details are |
505 | ;; not 100% compatible: not worth the trouble to add them to cl-lib.el, but we | |
04901786 | 506 | ;; still need to support old users of cl.el. |
2ee3d7f0 | 507 | |
36cec983 SM |
508 | (defun cl--gv-adapt (cl-gv do) |
509 | ;; This function is used by all .elc files that use define-setf-expander and | |
2a1e2476 | 510 | ;; were compiled with Emacs>=24.3. |
2ee3d7f0 SM |
511 | (let ((vars (nth 0 cl-gv)) |
512 | (vals (nth 1 cl-gv)) | |
513 | (binds ()) | |
514 | (substs ())) | |
515 | ;; Use cl-sublis as was done in cl-setf-do-modify. | |
516 | (while vars | |
517 | (if (macroexp-copyable-p (car vals)) | |
518 | (push (cons (pop vars) (pop vals)) substs) | |
519 | (push (list (pop vars) (pop vals)) binds))) | |
520 | (macroexp-let* | |
521 | binds | |
522 | (funcall do (cl-sublis substs (nth 4 cl-gv)) | |
523 | ;; We'd like to do something like | |
524 | ;; (lambda ,(nth 2 cl-gv) ,(nth 3 cl-gv)). | |
525 | (lambda (exp) | |
526 | (macroexp-let2 macroexp-copyable-p v exp | |
527 | (cl-sublis (cons (cons (car (nth 2 cl-gv)) v) | |
528 | substs) | |
529 | (nth 3 cl-gv)))))))) | |
530 | ||
531 | (defmacro define-setf-expander (name arglist &rest body) | |
532 | "Define a `setf' method. | |
031b2ea7 GM |
533 | This method shows how to handle `setf's to places of the form |
534 | \(NAME ARGS...). The argument forms ARGS are bound according to | |
535 | ARGLIST, as if NAME were going to be expanded as a macro, then | |
536 | the BODY forms are executed and must return a list of five elements: | |
537 | a temporary-variables list, a value-forms list, a store-variables list | |
538 | \(of length one), a store-form, and an access- form. | |
539 | ||
540 | See `gv-define-expander', and `gv-define-setter' for better and | |
541 | simpler ways to define setf-methods." | |
2ee3d7f0 SM |
542 | (declare (debug |
543 | (&define name cl-lambda-list cl-declarations-or-string def-body))) | |
544 | `(progn | |
545 | ,@(if (stringp (car body)) | |
546 | (list `(put ',name 'setf-documentation ,(pop body)))) | |
547 | (gv-define-expander ,name | |
548 | (cl-function | |
549 | (lambda (do ,@arglist) | |
550 | (cl--gv-adapt (progn ,@body) do)))))) | |
551 | ||
552 | (defmacro defsetf (name arg1 &rest args) | |
553 | "Define a `setf' method. | |
031b2ea7 GM |
554 | This macro is an easy-to-use substitute for `define-setf-expander' |
555 | that works well for simple place forms. | |
556 | ||
557 | In the simple `defsetf' form, `setf's of the form (setf (NAME | |
558 | ARGS...) VAL) are transformed to function or macro calls of the | |
559 | form (FUNC ARGS... VAL). For example: | |
2ee3d7f0 | 560 | |
a0ccbcbd | 561 | (defsetf aref aset) |
2ee3d7f0 | 562 | |
031b2ea7 GM |
563 | You can replace this form with `gv-define-simple-setter'. |
564 | ||
a0ccbcbd | 565 | Alternate form: (defsetf NAME ARGLIST (STORE) BODY...). |
031b2ea7 GM |
566 | |
567 | Here, the above `setf' call is expanded by binding the argument | |
568 | forms ARGS according to ARGLIST, binding the value form VAL to | |
569 | STORE, then executing BODY, which must return a Lisp form that | |
570 | does the necessary `setf' operation. Actually, ARGLIST and STORE | |
571 | may be bound to temporary variables which are introduced | |
572 | automatically to preserve proper execution order of the arguments. | |
573 | For example: | |
2ee3d7f0 | 574 | |
a0ccbcbd | 575 | (defsetf nth (n x) (v) `(setcar (nthcdr ,n ,x) ,v)) |
2ee3d7f0 | 576 | |
031b2ea7 GM |
577 | You can replace this form with `gv-define-setter'. |
578 | ||
2ee3d7f0 SM |
579 | \(fn NAME [FUNC | ARGLIST (STORE) BODY...])" |
580 | (declare (debug | |
581 | (&define name | |
582 | [&or [symbolp &optional stringp] | |
583 | [cl-lambda-list (symbolp)]] | |
584 | cl-declarations-or-string def-body))) | |
585 | (if (and (listp arg1) (consp args)) | |
586 | ;; Like `gv-define-setter' but with `cl-function'. | |
587 | `(gv-define-expander ,name | |
588 | (lambda (do &rest args) | |
589 | (gv--defsetter ',name | |
590 | (cl-function | |
591 | (lambda (,@(car args) ,@arg1) ,@(cdr args))) | |
592 | do args))) | |
d57c286e | 593 | `(gv-define-simple-setter ,name ,arg1 ,(car args)))) |
2ee3d7f0 SM |
594 | |
595 | ;; FIXME: CL used to provide a setf method for `apply', but I haven't been able | |
596 | ;; to find a case where it worked. The code below tries to handle it as well. | |
597 | ;; (defun cl--setf-apply (form last-witness last) | |
598 | ;; (cond | |
599 | ;; ((not (consp form)) form) | |
600 | ;; ((eq (ignore-errors (car (last form))) last-witness) | |
601 | ;; `(apply #',(car form) ,@(butlast (cdr form)) ,last)) | |
602 | ;; ((and (memq (car form) '(let let*)) | |
603 | ;; (rassoc (list last-witness) (cadr form))) | |
604 | ;; (let ((rebind (rassoc (list last-witness) (cadr form)))) | |
605 | ;; `(,(car form) ,(remq rebind (cadr form)) | |
606 | ;; ,@(mapcar (lambda (form) (cl--setf-apply form (car rebind) last)) | |
607 | ;; (cddr form))))) | |
608 | ;; (t (mapcar (lambda (form) (cl--setf-apply form last-witness last)) form)))) | |
609 | ;; (gv-define-setter apply (val fun &rest args) | |
610 | ;; (pcase fun (`#',(and (pred symbolp) f) (setq fun f)) | |
611 | ;; (_ (error "First arg to apply in setf is not #'SYM: %S" fun))) | |
612 | ;; (let* ((butlast (butlast args)) | |
613 | ;; (last (car (last args))) | |
614 | ;; (last-witness (make-symbol "--cl-tailarg--")) | |
615 | ;; (setter (macroexpand `(setf (,fun ,@butlast ,last-witness) ,val) | |
616 | ;; macroexpand-all-environment))) | |
617 | ;; (cl--setf-apply setter last-witness last))) | |
618 | ||
619 | ||
620 | ;; FIXME: CL used to provide get-setf-method, which was used by some | |
621 | ;; setf-expanders, but now that we use gv.el, it is a lot more difficult | |
622 | ;; and in general impossible to provide get-setf-method. Hopefully, it | |
623 | ;; won't be needed. If needed, we'll have to do something nasty along the | |
624 | ;; lines of | |
625 | ;; (defun get-setf-method (place &optional env) | |
626 | ;; (let* ((witness (list 'cl-gsm)) | |
627 | ;; (expansion (gv-letplace (getter setter) place | |
628 | ;; `(,witness ,getter ,(funcall setter witness))))) | |
629 | ;; ...find "let prefix" of expansion, extract getter and setter from | |
630 | ;; ...the rest, and build the 5-tuple)) | |
2a1e2476 | 631 | (make-obsolete 'get-setf-method 'gv-letplace "24.3") |
2ee3d7f0 SM |
632 | |
633 | (defmacro define-modify-macro (name arglist func &optional doc) | |
634 | "Define a `setf'-like modify macro. | |
031b2ea7 GM |
635 | If NAME is called, it combines its PLACE argument with the other |
636 | arguments from ARGLIST using FUNC. For example: | |
637 | ||
638 | (define-modify-macro incf (&optional (n 1)) +) | |
639 | ||
640 | You can replace this macro with `gv-letplace'." | |
2ee3d7f0 SM |
641 | (declare (debug |
642 | (&define name cl-lambda-list ;; should exclude &key | |
643 | symbolp &optional stringp))) | |
644 | (if (memq '&key arglist) | |
645 | (error "&key not allowed in define-modify-macro")) | |
646 | (let ((place (make-symbol "--cl-place--"))) | |
647 | `(cl-defmacro ,name (,place ,@arglist) | |
648 | ,doc | |
649 | (,(if (memq '&rest arglist) #'cl-list* #'list) | |
650 | #'cl-callf ',func ,place | |
651 | ,@(cl--arglist-args arglist))))) | |
652 | ||
653 | ;;; Additional compatibility code. | |
6fa6c4ae SM |
654 | ;; For names that were clean but really aren't needed any more. |
655 | ||
2a1e2476 | 656 | (define-obsolete-function-alias 'cl-macroexpand 'macroexpand "24.3") |
de7e2b36 | 657 | (define-obsolete-variable-alias 'cl-macro-environment |
2a1e2476 GM |
658 | 'macroexpand-all-environment "24.3") |
659 | (define-obsolete-function-alias 'cl-macroexpand-all 'macroexpand-all "24.3") | |
6fa6c4ae SM |
660 | |
661 | ;;; Hash tables. | |
662 | ;; This is just kept for compatibility with code byte-compiled by Emacs-20. | |
663 | ||
664 | ;; No idea if this might still be needed. | |
6e9590e2 | 665 | (defun cl-not-hash-table (x &optional y &rest _z) |
2a1e2476 | 666 | (declare (obsolete nil "24.3")) |
6fa6c4ae SM |
667 | (signal 'wrong-type-argument (list 'cl-hash-table-p (or y x)))) |
668 | ||
669 | (defvar cl-builtin-gethash (symbol-function 'gethash)) | |
2a1e2476 | 670 | (make-obsolete-variable 'cl-builtin-gethash nil "24.3") |
6fa6c4ae | 671 | (defvar cl-builtin-remhash (symbol-function 'remhash)) |
2a1e2476 | 672 | (make-obsolete-variable 'cl-builtin-remhash nil "24.3") |
6fa6c4ae | 673 | (defvar cl-builtin-clrhash (symbol-function 'clrhash)) |
2a1e2476 | 674 | (make-obsolete-variable 'cl-builtin-clrhash nil "24.3") |
6fa6c4ae SM |
675 | (defvar cl-builtin-maphash (symbol-function 'maphash)) |
676 | ||
2a1e2476 GM |
677 | (make-obsolete-variable 'cl-builtin-maphash nil "24.3") |
678 | (define-obsolete-function-alias 'cl-map-keymap 'map-keymap "24.3") | |
679 | (define-obsolete-function-alias 'cl-copy-tree 'copy-tree "24.3") | |
680 | (define-obsolete-function-alias 'cl-gethash 'gethash "24.3") | |
681 | (define-obsolete-function-alias 'cl-puthash 'puthash "24.3") | |
682 | (define-obsolete-function-alias 'cl-remhash 'remhash "24.3") | |
683 | (define-obsolete-function-alias 'cl-clrhash 'clrhash "24.3") | |
684 | (define-obsolete-function-alias 'cl-maphash 'maphash "24.3") | |
685 | (define-obsolete-function-alias 'cl-make-hash-table 'make-hash-table "24.3") | |
686 | (define-obsolete-function-alias 'cl-hash-table-p 'hash-table-p "24.3") | |
687 | (define-obsolete-function-alias 'cl-hash-table-count 'hash-table-count "24.3") | |
6fa6c4ae | 688 | |
4250fdf5 | 689 | (define-obsolete-function-alias 'cl-map-keymap-recursively |
2a1e2476 GM |
690 | 'cl--map-keymap-recursively "24.3") |
691 | (define-obsolete-function-alias 'cl-map-intervals 'cl--map-intervals "24.3") | |
692 | (define-obsolete-function-alias 'cl-map-extents 'cl--map-overlays "24.3") | |
beb6d07d | 693 | (define-obsolete-function-alias 'cl-set-getf 'cl--set-getf "24.3") |
4250fdf5 | 694 | |
bb3faf5b | 695 | (defun cl-maclisp-member (item list) |
2a1e2476 | 696 | (declare (obsolete member "24.3")) |
bb3faf5b SM |
697 | (while (and list (not (equal item (car list)))) (setq list (cdr list))) |
698 | list) | |
699 | ||
2ee3d7f0 SM |
700 | ;; Used in the expansion of the old `defstruct'. |
701 | (defun cl-struct-setf-expander (x name accessor pred-form pos) | |
2a1e2476 | 702 | (declare (obsolete nil "24.3")) |
2ee3d7f0 SM |
703 | (let* ((temp (make-symbol "--cl-x--")) (store (make-symbol "--cl-store--"))) |
704 | (list (list temp) (list x) (list store) | |
705 | `(progn | |
706 | ,@(and pred-form | |
707 | (list `(or ,(cl-subst temp 'cl-x pred-form) | |
708 | (error ,(format | |
709 | "%s storing a non-%s" | |
710 | accessor name))))) | |
711 | ,(if (eq (car (get name 'cl-struct-type)) 'vector) | |
712 | `(aset ,temp ,pos ,store) | |
713 | `(setcar | |
714 | ,(if (<= pos 5) | |
715 | (let ((xx temp)) | |
716 | (while (>= (setq pos (1- pos)) 0) | |
717 | (setq xx `(cdr ,xx))) | |
718 | xx) | |
719 | `(nthcdr ,pos ,temp)) | |
720 | ,store))) | |
721 | (list accessor temp)))) | |
722 | ||
7467c796 | 723 | (provide 'cl) |
82e1f390 SM |
724 | |
725 | (run-hooks 'cl-load-hook) | |
726 | ||
fcd73769 | 727 | ;;; cl.el ends here |