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