Fix compilation error with simultaneous dynamic+lexical scoping.
[bpt/emacs.git] / lisp / emacs-lisp / cl-lib.el
1 ;;; cl-lib.el --- Common Lisp extensions for Emacs -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 1993, 2001-2013 Free Software Foundation, Inc.
4
5 ;; Author: Dave Gillespie <daveg@synaptics.com>
6 ;; Version: 1.0
7 ;; Keywords: extensions
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; These are extensions to Emacs Lisp that provide a degree of
27 ;; Common Lisp compatibility, beyond what is already built-in
28 ;; in Emacs Lisp.
29 ;;
30 ;; This package was written by Dave Gillespie; it is a complete
31 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986.
32 ;;
33 ;; Bug reports, comments, and suggestions are welcome!
34
35 ;; This file contains the portions of the Common Lisp extensions
36 ;; package which should always be present.
37
38
39 ;;; Future notes:
40
41 ;; Once Emacs 19 becomes standard, many things in this package which are
42 ;; messy for reasons of compatibility can be greatly simplified. For now,
43 ;; I prefer to maintain one unified version.
44
45
46 ;;; Change Log:
47
48 ;; Version 2.02 (30 Jul 93):
49 ;; * Added "cl-compat.el" file, extra compatibility with old package.
50 ;; * Added `lexical-let' and `lexical-let*'.
51 ;; * Added `define-modify-macro', `callf', and `callf2'.
52 ;; * Added `ignore-errors'.
53 ;; * Changed `(setf (nthcdr N PLACE) X)' to work when N is zero.
54 ;; * Merged `*gentemp-counter*' into `*gensym-counter*'.
55 ;; * Extended `subseq' to allow negative START and END like `substring'.
56 ;; * Added `in-ref', `across-ref', `elements of-ref' loop clauses.
57 ;; * Added `concat', `vconcat' loop clauses.
58 ;; * Cleaned up a number of compiler warnings.
59
60 ;; Version 2.01 (7 Jul 93):
61 ;; * Added support for FSF version of Emacs 19.
62 ;; * Added `add-hook' for Emacs 18 users.
63 ;; * Added `defsubst*' and `symbol-macrolet'.
64 ;; * Added `maplist', `mapc', `mapl', `mapcan', `mapcon'.
65 ;; * Added `map', `concatenate', `reduce', `merge'.
66 ;; * Added `revappend', `nreconc', `tailp', `tree-equal'.
67 ;; * Added `assert', `check-type', `typecase', `typep', and `deftype'.
68 ;; * Added destructuring and `&environment' support to `defmacro*'.
69 ;; * Added destructuring to `loop', and added the following clauses:
70 ;; `elements', `frames', `overlays', `intervals', `buffers', `key-seqs'.
71 ;; * Renamed `delete' to `delete*' and `remove' to `remove*'.
72 ;; * Completed support for all keywords in `remove*', `substitute', etc.
73 ;; * Added `most-positive-float' and company.
74 ;; * Fixed hash tables to work with latest Lucid Emacs.
75 ;; * `proclaim' forms are no longer compile-time-evaluating; use `declaim'.
76 ;; * Syntax for `warn' declarations has changed.
77 ;; * Improved implementation of `random*'.
78 ;; * Moved most sequence functions to a new file, cl-seq.el.
79 ;; * Moved `eval-when' into cl-macs.el.
80 ;; * Moved `pushnew' and `adjoin' to cl.el for most common cases.
81 ;; * Moved `provide' forms down to ends of files.
82 ;; * Changed expansion of `pop' to something that compiles to better code.
83 ;; * Changed so that no patch is required for Emacs 19 byte compiler.
84 ;; * Made more things dependent on `optimize' declarations.
85 ;; * Added a partial implementation of struct print functions.
86 ;; * Miscellaneous minor changes.
87
88 ;; Version 2.00:
89 ;; * First public release of this package.
90
91
92 ;;; Code:
93
94 (require 'macroexp)
95
96 (defvar cl--optimize-speed 1)
97 (defvar cl--optimize-safety 1)
98
99 ;;;###autoload
100 (define-obsolete-variable-alias
101 ;; This alias is needed for compatibility with .elc files that use defstruct
102 ;; and were compiled with Emacs<24.3.
103 'custom-print-functions 'cl-custom-print-functions "24.3")
104
105 ;;;###autoload
106 (defvar cl-custom-print-functions nil
107 "This is a list of functions that format user objects for printing.
108 Each function is called in turn with three arguments: the object, the
109 stream, and the print level (currently ignored). If it is able to
110 print the object it returns true; otherwise it returns nil and the
111 printer proceeds to the next function on the list.
112
113 This variable is not used at present, but it is defined in hopes that
114 a future Emacs interpreter will be able to use it.")
115
116 ;;; Generalized variables.
117 ;; These macros are defined here so that they
118 ;; can safely be used in init files.
119
120 (defmacro cl-incf (place &optional x)
121 "Increment PLACE by X (1 by default).
122 PLACE may be a symbol, or any generalized variable allowed by `setf'.
123 The return value is the incremented value of PLACE."
124 (declare (debug (place &optional form)))
125 (if (symbolp place)
126 (list 'setq place (if x (list '+ place x) (list '1+ place)))
127 (list 'cl-callf '+ place (or x 1))))
128
129 (defmacro cl-decf (place &optional x)
130 "Decrement PLACE by X (1 by default).
131 PLACE may be a symbol, or any generalized variable allowed by `setf'.
132 The return value is the decremented value of PLACE."
133 (declare (debug cl-incf))
134 (if (symbolp place)
135 (list 'setq place (if x (list '- place x) (list '1- place)))
136 (list 'cl-callf '- place (or x 1))))
137
138 (defmacro cl-pushnew (x place &rest keys)
139 "(cl-pushnew X PLACE): insert X at the head of the list if not already there.
140 Like (push X PLACE), except that the list is unmodified if X is `eql' to
141 an element already on the list.
142 \nKeywords supported: :test :test-not :key
143 \n(fn X PLACE [KEYWORD VALUE]...)"
144 (declare (debug
145 (form place &rest
146 &or [[&or ":test" ":test-not" ":key"] function-form]
147 [keywordp form])))
148 (if (symbolp place)
149 (if (null keys)
150 (macroexp-let2 nil var x
151 `(if (memql ,var ,place)
152 ;; This symbol may later on expand to actual code which then
153 ;; trigger warnings like "value unused" since cl-pushnew's
154 ;; return value is rarely used. It should not matter that
155 ;; other warnings may be silenced, since `place' is used
156 ;; earlier and should have triggered them already.
157 (with-no-warnings ,place)
158 (setq ,place (cons ,var ,place))))
159 `(setq ,place (cl-adjoin ,x ,place ,@keys)))
160 `(cl-callf2 cl-adjoin ,x ,place ,@keys)))
161
162 (defun cl--set-elt (seq n val)
163 (if (listp seq) (setcar (nthcdr n seq) val) (aset seq n val)))
164
165 (defun cl--set-buffer-substring (start end val)
166 (save-excursion (delete-region start end)
167 (goto-char start)
168 (insert val)
169 val))
170
171 (defun cl--set-substring (str start end val)
172 (if end (if (< end 0) (cl-incf end (length str)))
173 (setq end (length str)))
174 (if (< start 0) (cl-incf start (length str)))
175 (concat (and (> start 0) (substring str 0 start))
176 val
177 (and (< end (length str)) (substring str end))))
178
179
180 ;;; Blocks and exits.
181
182 (defalias 'cl--block-wrapper 'identity)
183 (defalias 'cl--block-throw 'throw)
184
185
186 ;;; Multiple values.
187 ;; True multiple values are not supported, or even
188 ;; simulated. Instead, cl-multiple-value-bind and friends simply expect
189 ;; the target form to return the values as a list.
190
191 (defun cl--defalias (cl-f el-f &optional doc)
192 (defalias cl-f el-f doc)
193 (put cl-f 'byte-optimizer 'byte-compile-inline-expand))
194
195 (cl--defalias 'cl-values #'list
196 "Return multiple values, Common Lisp style.
197 The arguments of `cl-values' are the values
198 that the containing function should return.
199
200 \(fn &rest VALUES)")
201
202 (cl--defalias 'cl-values-list #'identity
203 "Return multiple values, Common Lisp style, taken from a list.
204 LIST specifies the list of values
205 that the containing function should return.
206
207 \(fn LIST)")
208
209 (defsubst cl-multiple-value-list (expression)
210 "Return a list of the multiple values produced by EXPRESSION.
211 This handles multiple values in Common Lisp style, but it does not
212 work right when EXPRESSION calls an ordinary Emacs Lisp function
213 that returns just one value."
214 expression)
215
216 (defsubst cl-multiple-value-apply (function expression)
217 "Evaluate EXPRESSION to get multiple values and apply FUNCTION to them.
218 This handles multiple values in Common Lisp style, but it does not work
219 right when EXPRESSION calls an ordinary Emacs Lisp function that returns just
220 one value."
221 (apply function expression))
222
223 (defalias 'cl-multiple-value-call 'apply
224 "Apply FUNCTION to ARGUMENTS, taking multiple values into account.
225 This implementation only handles the case where there is only one argument.")
226
227 (cl--defalias 'cl-nth-value #'nth
228 "Evaluate EXPRESSION to get multiple values and return the Nth one.
229 This handles multiple values in Common Lisp style, but it does not work
230 right when EXPRESSION calls an ordinary Emacs Lisp function that returns just
231 one value.
232
233 \(fn N EXPRESSION)")
234
235 ;;; Declarations.
236
237 (defvar cl--compiling-file nil)
238 (defun cl--compiling-file ()
239 (or cl--compiling-file
240 (and (boundp 'byte-compile--outbuffer)
241 (bufferp (symbol-value 'byte-compile--outbuffer))
242 (equal (buffer-name (symbol-value 'byte-compile--outbuffer))
243 " *Compiler Output*"))))
244
245 (defvar cl--proclaims-deferred nil)
246
247 (defun cl-proclaim (spec)
248 "Record a global declaration specified by SPEC."
249 (if (fboundp 'cl--do-proclaim) (cl--do-proclaim spec t)
250 (push spec cl--proclaims-deferred))
251 nil)
252
253 (defmacro cl-declaim (&rest specs)
254 "Like `cl-proclaim', but takes any number of unevaluated, unquoted arguments.
255 Puts `(cl-eval-when (compile load eval) ...)' around the declarations
256 so that they are registered at compile-time as well as run-time."
257 (let ((body (mapcar (lambda (x) `(cl-proclaim ',x)) specs)))
258 (if (cl--compiling-file) `(cl-eval-when (compile load eval) ,@body)
259 `(progn ,@body)))) ; Avoid loading cl-macs.el for cl-eval-when.
260
261
262 ;;; Symbols.
263
264 (defun cl--random-time ()
265 (let* ((time (copy-sequence (current-time-string))) (i (length time)) (v 0))
266 (while (>= (cl-decf i) 0) (setq v (+ (* v 3) (aref time i))))
267 v))
268
269 (defvar cl--gensym-counter (* (logand (cl--random-time) 1023) 100))
270
271
272 ;;; Numbers.
273
274 (define-obsolete-function-alias 'cl-floatp-safe 'floatp "24.4")
275
276 (defsubst cl-plusp (number)
277 "Return t if NUMBER is positive."
278 (> number 0))
279
280 (defsubst cl-minusp (number)
281 "Return t if NUMBER is negative."
282 (< number 0))
283
284 (defun cl-oddp (integer)
285 "Return t if INTEGER is odd."
286 (eq (logand integer 1) 1))
287
288 (defun cl-evenp (integer)
289 "Return t if INTEGER is even."
290 (eq (logand integer 1) 0))
291
292 (defvar cl--random-state
293 (vector 'cl--random-state-tag -1 30 (cl--random-time)))
294
295 (defconst cl-most-positive-float nil
296 "The largest value that a Lisp float can hold.
297 If your system supports infinities, this is the largest finite value.
298 For IEEE machines, this is approximately 1.79e+308.
299 Call `cl-float-limits' to set this.")
300
301 (defconst cl-most-negative-float nil
302 "The largest negative value that a Lisp float can hold.
303 This is simply -`cl-most-positive-float'.
304 Call `cl-float-limits' to set this.")
305
306 (defconst cl-least-positive-float nil
307 "The smallest value greater than zero that a Lisp float can hold.
308 For IEEE machines, it is about 4.94e-324 if denormals are supported,
309 or 2.22e-308 if they are not.
310 Call `cl-float-limits' to set this.")
311
312 (defconst cl-least-negative-float nil
313 "The smallest value less than zero that a Lisp float can hold.
314 This is simply -`cl-least-positive-float'.
315 Call `cl-float-limits' to set this.")
316
317 (defconst cl-least-positive-normalized-float nil
318 "The smallest normalized Lisp float greater than zero.
319 This is the smallest value for which IEEE denormalization does not lose
320 precision. For IEEE machines, this value is about 2.22e-308.
321 For machines that do not support the concept of denormalization
322 and gradual underflow, this constant equals `cl-least-positive-float'.
323 Call `cl-float-limits' to set this.")
324
325 (defconst cl-least-negative-normalized-float nil
326 "The smallest normalized Lisp float less than zero.
327 This is simply -`cl-least-positive-normalized-float'.
328 Call `cl-float-limits' to set this.")
329
330 (defconst cl-float-epsilon nil
331 "The smallest positive float that adds to 1.0 to give a distinct value.
332 Adding a number less than this to 1.0 returns 1.0 due to roundoff.
333 For IEEE machines, epsilon is about 2.22e-16.
334 Call `cl-float-limits' to set this.")
335
336 (defconst cl-float-negative-epsilon nil
337 "The smallest positive float that subtracts from 1.0 to give a distinct value.
338 For IEEE machines, it is about 1.11e-16.
339 Call `cl-float-limits' to set this.")
340
341
342 ;;; Sequence functions.
343
344 (cl--defalias 'cl-copy-seq 'copy-sequence)
345
346 (declare-function cl--mapcar-many "cl-extra" (cl-func cl-seqs))
347
348 (defun cl-mapcar (cl-func cl-x &rest cl-rest)
349 "Apply FUNCTION to each element of SEQ, and make a list of the results.
350 If there are several SEQs, FUNCTION is called with that many arguments,
351 and mapping stops as soon as the shortest list runs out. With just one
352 SEQ, this is like `mapcar'. With several, it is like the Common Lisp
353 `mapcar' function extended to arbitrary sequence types.
354 \n(fn FUNCTION SEQ...)"
355 (if cl-rest
356 (if (or (cdr cl-rest) (nlistp cl-x) (nlistp (car cl-rest)))
357 (cl--mapcar-many cl-func (cons cl-x cl-rest))
358 (let ((cl-res nil) (cl-y (car cl-rest)))
359 (while (and cl-x cl-y)
360 (push (funcall cl-func (pop cl-x) (pop cl-y)) cl-res))
361 (nreverse cl-res)))
362 (mapcar cl-func cl-x)))
363
364 (cl--defalias 'cl-svref 'aref)
365
366 ;;; List functions.
367
368 (cl--defalias 'cl-first 'car)
369 (cl--defalias 'cl-second 'cadr)
370 (cl--defalias 'cl-rest 'cdr)
371 (cl--defalias 'cl-endp 'null)
372
373 (cl--defalias 'cl-third 'cl-caddr "Return the third element of the list X.")
374 (cl--defalias 'cl-fourth 'cl-cadddr "Return the fourth element of the list X.")
375
376 (defsubst cl-fifth (x)
377 "Return the fifth element of the list X."
378 (declare (gv-setter (lambda (store) `(setcar (nthcdr 4 ,x) ,store))))
379 (nth 4 x))
380
381 (defsubst cl-sixth (x)
382 "Return the sixth element of the list X."
383 (declare (gv-setter (lambda (store) `(setcar (nthcdr 5 ,x) ,store))))
384 (nth 5 x))
385
386 (defsubst cl-seventh (x)
387 "Return the seventh element of the list X."
388 (declare (gv-setter (lambda (store) `(setcar (nthcdr 6 ,x) ,store))))
389 (nth 6 x))
390
391 (defsubst cl-eighth (x)
392 "Return the eighth element of the list X."
393 (declare (gv-setter (lambda (store) `(setcar (nthcdr 7 ,x) ,store))))
394 (nth 7 x))
395
396 (defsubst cl-ninth (x)
397 "Return the ninth element of the list X."
398 (declare (gv-setter (lambda (store) `(setcar (nthcdr 8 ,x) ,store))))
399 (nth 8 x))
400
401 (defsubst cl-tenth (x)
402 "Return the tenth element of the list X."
403 (declare (gv-setter (lambda (store) `(setcar (nthcdr 9 ,x) ,store))))
404 (nth 9 x))
405
406 (defun cl-caaar (x)
407 "Return the `car' of the `car' of the `car' of X."
408 (declare (compiler-macro cl--compiler-macro-cXXr))
409 (car (car (car x))))
410
411 (defun cl-caadr (x)
412 "Return the `car' of the `car' of the `cdr' of X."
413 (declare (compiler-macro cl--compiler-macro-cXXr))
414 (car (car (cdr x))))
415
416 (defun cl-cadar (x)
417 "Return the `car' of the `cdr' of the `car' of X."
418 (declare (compiler-macro cl--compiler-macro-cXXr))
419 (car (cdr (car x))))
420
421 (defun cl-caddr (x)
422 "Return the `car' of the `cdr' of the `cdr' of X."
423 (declare (compiler-macro cl--compiler-macro-cXXr))
424 (car (cdr (cdr x))))
425
426 (defun cl-cdaar (x)
427 "Return the `cdr' of the `car' of the `car' of X."
428 (declare (compiler-macro cl--compiler-macro-cXXr))
429 (cdr (car (car x))))
430
431 (defun cl-cdadr (x)
432 "Return the `cdr' of the `car' of the `cdr' of X."
433 (declare (compiler-macro cl--compiler-macro-cXXr))
434 (cdr (car (cdr x))))
435
436 (defun cl-cddar (x)
437 "Return the `cdr' of the `cdr' of the `car' of X."
438 (declare (compiler-macro cl--compiler-macro-cXXr))
439 (cdr (cdr (car x))))
440
441 (defun cl-cdddr (x)
442 "Return the `cdr' of the `cdr' of the `cdr' of X."
443 (declare (compiler-macro cl--compiler-macro-cXXr))
444 (cdr (cdr (cdr x))))
445
446 (defun cl-caaaar (x)
447 "Return the `car' of the `car' of the `car' of the `car' of X."
448 (declare (compiler-macro cl--compiler-macro-cXXr))
449 (car (car (car (car x)))))
450
451 (defun cl-caaadr (x)
452 "Return the `car' of the `car' of the `car' of the `cdr' of X."
453 (declare (compiler-macro cl--compiler-macro-cXXr))
454 (car (car (car (cdr x)))))
455
456 (defun cl-caadar (x)
457 "Return the `car' of the `car' of the `cdr' of the `car' of X."
458 (declare (compiler-macro cl--compiler-macro-cXXr))
459 (car (car (cdr (car x)))))
460
461 (defun cl-caaddr (x)
462 "Return the `car' of the `car' of the `cdr' of the `cdr' of X."
463 (declare (compiler-macro cl--compiler-macro-cXXr))
464 (car (car (cdr (cdr x)))))
465
466 (defun cl-cadaar (x)
467 "Return the `car' of the `cdr' of the `car' of the `car' of X."
468 (declare (compiler-macro cl--compiler-macro-cXXr))
469 (car (cdr (car (car x)))))
470
471 (defun cl-cadadr (x)
472 "Return the `car' of the `cdr' of the `car' of the `cdr' of X."
473 (declare (compiler-macro cl--compiler-macro-cXXr))
474 (car (cdr (car (cdr x)))))
475
476 (defun cl-caddar (x)
477 "Return the `car' of the `cdr' of the `cdr' of the `car' of X."
478 (declare (compiler-macro cl--compiler-macro-cXXr))
479 (car (cdr (cdr (car x)))))
480
481 (defun cl-cadddr (x)
482 "Return the `car' of the `cdr' of the `cdr' of the `cdr' of X."
483 (declare (compiler-macro cl--compiler-macro-cXXr))
484 (car (cdr (cdr (cdr x)))))
485
486 (defun cl-cdaaar (x)
487 "Return the `cdr' of the `car' of the `car' of the `car' of X."
488 (declare (compiler-macro cl--compiler-macro-cXXr))
489 (cdr (car (car (car x)))))
490
491 (defun cl-cdaadr (x)
492 "Return the `cdr' of the `car' of the `car' of the `cdr' of X."
493 (declare (compiler-macro cl--compiler-macro-cXXr))
494 (cdr (car (car (cdr x)))))
495
496 (defun cl-cdadar (x)
497 "Return the `cdr' of the `car' of the `cdr' of the `car' of X."
498 (declare (compiler-macro cl--compiler-macro-cXXr))
499 (cdr (car (cdr (car x)))))
500
501 (defun cl-cdaddr (x)
502 "Return the `cdr' of the `car' of the `cdr' of the `cdr' of X."
503 (declare (compiler-macro cl--compiler-macro-cXXr))
504 (cdr (car (cdr (cdr x)))))
505
506 (defun cl-cddaar (x)
507 "Return the `cdr' of the `cdr' of the `car' of the `car' of X."
508 (declare (compiler-macro cl--compiler-macro-cXXr))
509 (cdr (cdr (car (car x)))))
510
511 (defun cl-cddadr (x)
512 "Return the `cdr' of the `cdr' of the `car' of the `cdr' of X."
513 (declare (compiler-macro cl--compiler-macro-cXXr))
514 (cdr (cdr (car (cdr x)))))
515
516 (defun cl-cdddar (x)
517 "Return the `cdr' of the `cdr' of the `cdr' of the `car' of X."
518 (declare (compiler-macro cl--compiler-macro-cXXr))
519 (cdr (cdr (cdr (car x)))))
520
521 (defun cl-cddddr (x)
522 "Return the `cdr' of the `cdr' of the `cdr' of the `cdr' of X."
523 (declare (compiler-macro cl--compiler-macro-cXXr))
524 (cdr (cdr (cdr (cdr x)))))
525
526 ;;(defun last* (x &optional n)
527 ;; "Returns the last link in the list LIST.
528 ;;With optional argument N, returns Nth-to-last link (default 1)."
529 ;; (if n
530 ;; (let ((m 0) (p x))
531 ;; (while (consp p) (cl-incf m) (pop p))
532 ;; (if (<= n 0) p
533 ;; (if (< n m) (nthcdr (- m n) x) x)))
534 ;; (while (consp (cdr x)) (pop x))
535 ;; x))
536
537 (defun cl-list* (arg &rest rest)
538 "Return a new list with specified ARGs as elements, consed to last ARG.
539 Thus, `(cl-list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to
540 `(cons A (cons B (cons C D)))'.
541 \n(fn ARG...)"
542 (declare (compiler-macro cl--compiler-macro-list*))
543 (cond ((not rest) arg)
544 ((not (cdr rest)) (cons arg (car rest)))
545 (t (let* ((n (length rest))
546 (copy (copy-sequence rest))
547 (last (nthcdr (- n 2) copy)))
548 (setcdr last (car (cdr last)))
549 (cons arg copy)))))
550
551 (defun cl-ldiff (list sublist)
552 "Return a copy of LIST with the tail SUBLIST removed."
553 (let ((res nil))
554 (while (and (consp list) (not (eq list sublist)))
555 (push (pop list) res))
556 (nreverse res)))
557
558 (defun cl-copy-list (list)
559 "Return a copy of LIST, which may be a dotted list.
560 The elements of LIST are not copied, just the list structure itself."
561 (if (consp list)
562 (let ((res nil))
563 (while (consp list) (push (pop list) res))
564 (prog1 (nreverse res) (setcdr res list)))
565 (car list)))
566
567 ;; Autoloaded, but we have not loaded cl-loaddefs yet.
568 (declare-function cl-floor "cl-extra" (x &optional y))
569 (declare-function cl-ceiling "cl-extra" (x &optional y))
570 (declare-function cl-truncate "cl-extra" (x &optional y))
571 (declare-function cl-round "cl-extra" (x &optional y))
572 (declare-function cl-mod "cl-extra" (x y))
573
574 (defun cl-adjoin (cl-item cl-list &rest cl-keys)
575 "Return ITEM consed onto the front of LIST only if it's not already there.
576 Otherwise, return LIST unmodified.
577 \nKeywords supported: :test :test-not :key
578 \n(fn ITEM LIST [KEYWORD VALUE]...)"
579 (declare (compiler-macro cl--compiler-macro-adjoin))
580 (cond ((or (equal cl-keys '(:test eq))
581 (and (null cl-keys) (not (numberp cl-item))))
582 (if (memq cl-item cl-list) cl-list (cons cl-item cl-list)))
583 ((or (equal cl-keys '(:test equal)) (null cl-keys))
584 (if (member cl-item cl-list) cl-list (cons cl-item cl-list)))
585 (t (apply 'cl--adjoin cl-item cl-list cl-keys))))
586
587 (defun cl-subst (cl-new cl-old cl-tree &rest cl-keys)
588 "Substitute NEW for OLD everywhere in TREE (non-destructively).
589 Return a copy of TREE with all elements `eql' to OLD replaced by NEW.
590 \nKeywords supported: :test :test-not :key
591 \n(fn NEW OLD TREE [KEYWORD VALUE]...)"
592 (if (or cl-keys (and (numberp cl-old) (not (integerp cl-old))))
593 (apply 'cl-sublis (list (cons cl-old cl-new)) cl-tree cl-keys)
594 (cl--do-subst cl-new cl-old cl-tree)))
595
596 (defun cl--do-subst (cl-new cl-old cl-tree)
597 (cond ((eq cl-tree cl-old) cl-new)
598 ((consp cl-tree)
599 (let ((a (cl--do-subst cl-new cl-old (car cl-tree)))
600 (d (cl--do-subst cl-new cl-old (cdr cl-tree))))
601 (if (and (eq a (car cl-tree)) (eq d (cdr cl-tree)))
602 cl-tree (cons a d))))
603 (t cl-tree)))
604
605 (defun cl-acons (key value alist)
606 "Add KEY and VALUE to ALIST.
607 Return a new list with (cons KEY VALUE) as car and ALIST as cdr."
608 (cons (cons key value) alist))
609
610 (defun cl-pairlis (keys values &optional alist)
611 "Make an alist from KEYS and VALUES.
612 Return a new alist composed by associating KEYS to corresponding VALUES;
613 the process stops as soon as KEYS or VALUES run out.
614 If ALIST is non-nil, the new pairs are prepended to it."
615 (nconc (cl-mapcar 'cons keys values) alist))
616
617
618 ;;; Generalized variables.
619
620 ;; These used to be in cl-macs.el since all macros that use them (like setf)
621 ;; were autoloaded from cl-macs.el. But now that setf, push, and pop are in
622 ;; core Elisp, they need to either be right here or be autoloaded via
623 ;; cl-loaddefs.el, which is more trouble than it is worth.
624
625 ;; Some more Emacs-related place types.
626 (gv-define-simple-setter buffer-file-name set-visited-file-name t)
627 (gv-define-setter buffer-modified-p (flag &optional buf)
628 `(with-current-buffer ,buf
629 (set-buffer-modified-p ,flag)))
630 (gv-define-simple-setter buffer-name rename-buffer t)
631 (gv-define-setter buffer-string (store)
632 `(insert (prog1 ,store (erase-buffer))))
633 (gv-define-simple-setter buffer-substring cl--set-buffer-substring)
634 (gv-define-simple-setter current-buffer set-buffer)
635 (gv-define-simple-setter current-case-table set-case-table)
636 (gv-define-simple-setter current-column move-to-column t)
637 (gv-define-simple-setter current-global-map use-global-map t)
638 (gv-define-setter current-input-mode (store)
639 `(progn (apply #'set-input-mode ,store) ,store))
640 (gv-define-simple-setter current-local-map use-local-map t)
641 (gv-define-simple-setter current-window-configuration
642 set-window-configuration t)
643 (gv-define-simple-setter default-file-modes set-default-file-modes t)
644 (gv-define-simple-setter documentation-property put)
645 (gv-define-setter face-background (x f &optional s)
646 `(set-face-background ,f ,x ,s))
647 (gv-define-setter face-background-pixmap (x f &optional s)
648 `(set-face-background-pixmap ,f ,x ,s))
649 (gv-define-setter face-font (x f &optional s) `(set-face-font ,f ,x ,s))
650 (gv-define-setter face-foreground (x f &optional s)
651 `(set-face-foreground ,f ,x ,s))
652 (gv-define-setter face-underline-p (x f &optional s)
653 `(set-face-underline ,f ,x ,s))
654 (gv-define-simple-setter file-modes set-file-modes t)
655 (gv-define-simple-setter frame-height set-screen-height t)
656 (gv-define-simple-setter frame-parameters modify-frame-parameters t)
657 (gv-define-simple-setter frame-visible-p cl--set-frame-visible-p)
658 (gv-define-simple-setter frame-width set-screen-width t)
659 (gv-define-simple-setter getenv setenv t)
660 (gv-define-simple-setter get-register set-register)
661 (gv-define-simple-setter global-key-binding global-set-key)
662 (gv-define-simple-setter local-key-binding local-set-key)
663 (gv-define-simple-setter mark set-mark t)
664 (gv-define-simple-setter mark-marker set-mark t)
665 (gv-define-simple-setter marker-position set-marker t)
666 (gv-define-setter mouse-position (store scr)
667 `(set-mouse-position ,scr (car ,store) (cadr ,store)
668 (cddr ,store)))
669 (gv-define-simple-setter point goto-char)
670 (gv-define-simple-setter point-marker goto-char t)
671 (gv-define-setter point-max (store)
672 `(progn (narrow-to-region (point-min) ,store) ,store))
673 (gv-define-setter point-min (store)
674 `(progn (narrow-to-region ,store (point-max)) ,store))
675 (gv-define-setter read-mouse-position (store scr)
676 `(set-mouse-position ,scr (car ,store) (cdr ,store)))
677 (gv-define-simple-setter screen-height set-screen-height t)
678 (gv-define-simple-setter screen-width set-screen-width t)
679 (gv-define-simple-setter selected-window select-window)
680 (gv-define-simple-setter selected-screen select-screen)
681 (gv-define-simple-setter selected-frame select-frame)
682 (gv-define-simple-setter standard-case-table set-standard-case-table)
683 (gv-define-simple-setter syntax-table set-syntax-table)
684 (gv-define-simple-setter visited-file-modtime set-visited-file-modtime t)
685 (gv-define-setter window-height (store)
686 `(progn (enlarge-window (- ,store (window-height))) ,store))
687 (gv-define-setter window-width (store)
688 `(progn (enlarge-window (- ,store (window-width)) t) ,store))
689 (gv-define-simple-setter x-get-secondary-selection x-own-secondary-selection t)
690 (gv-define-simple-setter x-get-selection x-own-selection t)
691
692 ;; More complex setf-methods.
693
694 ;; This is a hack that allows (setf (eq a 7) B) to mean either
695 ;; (setq a 7) or (setq a nil) depending on whether B is nil or not.
696 ;; This is useful when you have control over the PLACE but not over
697 ;; the VALUE, as is the case in define-minor-mode's :variable.
698 ;; It turned out that :variable needed more flexibility anyway, so
699 ;; this doesn't seem too useful now.
700 (gv-define-expander eq
701 (lambda (do place val)
702 (gv-letplace (getter setter) place
703 (macroexp-let2 nil val val
704 (funcall do `(eq ,getter ,val)
705 (lambda (v)
706 `(cond
707 (,v ,(funcall setter val))
708 ((eq ,getter ,val) ,(funcall setter `(not ,val))))))))))
709
710 (gv-define-expander substring
711 (lambda (do place from &optional to)
712 (gv-letplace (getter setter) place
713 (macroexp-let2 nil start from
714 (macroexp-let2 nil end to
715 (funcall do `(substring ,getter ,start ,end)
716 (lambda (v)
717 (funcall setter `(cl--set-substring
718 ,getter ,start ,end ,v)))))))))
719
720 ;;; Miscellaneous.
721
722 ;;;###autoload
723 (progn
724 ;; Make sure functions defined with cl-defsubst can be inlined even in
725 ;; packages which do not require CL. We don't put an autoload cookie
726 ;; directly on that function, since those cookies only go to cl-loaddefs.
727 (autoload 'cl--defsubst-expand "cl-macs")
728 ;; Autoload, so autoload.el and font-lock can use it even when CL
729 ;; is not loaded.
730 (put 'cl-defun 'doc-string-elt 3)
731 (put 'cl-defmacro 'doc-string-elt 3)
732 (put 'cl-defsubst 'doc-string-elt 3)
733 (put 'cl-defstruct 'doc-string-elt 2))
734
735 (load "cl-loaddefs" nil 'quiet)
736
737 (provide 'cl-lib)
738
739 ;; Local variables:
740 ;; byte-compile-dynamic: t
741 ;; End:
742
743 ;;; cl-lib.el ends here