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