Fix imenu--sort-by-position for non-pairs parameters (bug#26457)
[emacs.git] / lisp / emacs-lisp / cl-lib.el
blob1f8615fad3e46f83f3cc1198561aa848fe73de37
1 ;;; cl-lib.el --- Common Lisp extensions for Emacs -*- lexical-binding: t -*-
3 ;; Copyright (C) 1993, 2001-2017 Free Software Foundation, Inc.
5 ;; Author: Dave Gillespie <daveg@synaptics.com>
6 ;; Version: 1.0
7 ;; Keywords: extensions
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
24 ;;; Commentary:
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.
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.
33 ;; Bug reports, comments, and suggestions are welcome!
35 ;; This file contains the portions of the Common Lisp extensions
36 ;; package which should always be present.
39 ;;; Change Log:
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.
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.
81 ;; Version 2.00:
82 ;; * First public release of this package.
85 ;;; Code:
87 (require 'macroexp)
89 (defvar cl--optimize-speed 1)
90 (defvar cl--optimize-safety 1)
92 ;;;###autoload
93 (define-obsolete-variable-alias
94 ;; This alias is needed for compatibility with .elc files that use defstruct
95 ;; and were compiled with Emacs<24.3.
96 'custom-print-functions 'cl-custom-print-functions "24.3")
98 ;;;###autoload
99 (defvar cl-custom-print-functions nil
100 "This is a list of functions that format user objects for printing.
101 Each function is called in turn with three arguments: the object, the
102 stream, and the print level (currently ignored). If it is able to
103 print the object it returns true; otherwise it returns nil and the
104 printer proceeds to the next function on the list.
106 This variable is not used at present, but it is defined in hopes that
107 a future Emacs interpreter will be able to use it.")
109 ;;; Generalized variables.
110 ;; These macros are defined here so that they
111 ;; can safely be used in init files.
113 (defmacro cl-incf (place &optional x)
114 "Increment PLACE by X (1 by default).
115 PLACE may be a symbol, or any generalized variable allowed by `setf'.
116 The 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))))
122 (defmacro cl-decf (place &optional x)
123 "Decrement PLACE by X (1 by default).
124 PLACE may be a symbol, or any generalized variable allowed by `setf'.
125 The 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))))
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.
133 Like (push X PLACE), except that the list is unmodified if X is `eql' to
134 an 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)
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))))
152 `(setq ,place (cl-adjoin ,x ,place ,@keys)))
153 `(cl-callf2 cl-adjoin ,x ,place ,@keys)))
155 (defun cl--set-buffer-substring (start end val)
156 (save-excursion (delete-region start end)
157 (goto-char start)
158 (insert val)
159 val))
161 (defun cl--set-substring (str start end val)
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))
167 (and (< end (length str)) (substring str end))))
170 ;;; Blocks and exits.
172 (defalias 'cl--block-wrapper 'identity)
173 (defalias 'cl--block-throw 'throw)
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.
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))
185 (cl--defalias 'cl-values #'list
186 "Return multiple values, Common Lisp style.
187 The arguments of `cl-values' are the values
188 that the containing function should return.
190 \(fn &rest VALUES)")
192 (cl--defalias 'cl-values-list #'identity
193 "Return multiple values, Common Lisp style, taken from a list.
194 LIST specifies the list of values
195 that the containing function should return.
197 \(fn LIST)")
199 (defsubst cl-multiple-value-list (expression)
200 "Return a list of the multiple values produced by EXPRESSION.
201 This handles multiple values in Common Lisp style, but it does not
202 work right when EXPRESSION calls an ordinary Emacs Lisp function
203 that returns just one value."
204 expression)
206 (defsubst cl-multiple-value-apply (function expression)
207 "Evaluate EXPRESSION to get multiple values and apply FUNCTION to them.
208 This handles multiple values in Common Lisp style, but it does not work
209 right when EXPRESSION calls an ordinary Emacs Lisp function that returns just
210 one value."
211 (apply function expression))
213 (defalias 'cl-multiple-value-call 'apply
214 "Apply FUNCTION to ARGUMENTS, taking multiple values into account.
215 This implementation only handles the case where there is only one argument.")
217 (cl--defalias 'cl-nth-value #'nth
218 "Evaluate EXPRESSION to get multiple values and return the Nth one.
219 This handles multiple values in Common Lisp style, but it does not work
220 right when EXPRESSION calls an ordinary Emacs Lisp function that returns just
221 one value.
223 \(fn N EXPRESSION)")
225 ;;; Declarations.
227 (defvar cl--compiling-file nil)
228 (defun cl--compiling-file ()
229 (or cl--compiling-file
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*"))))
235 (defvar cl--proclaims-deferred nil)
237 (defun cl-proclaim (spec)
238 "Record a global declaration specified by SPEC."
239 (if (fboundp 'cl--do-proclaim) (cl--do-proclaim spec t)
240 (push spec cl--proclaims-deferred))
241 nil)
243 (defmacro cl-declaim (&rest specs)
244 "Like `cl-proclaim', but takes any number of unevaluated, unquoted arguments.
245 Puts `(cl-eval-when (compile load eval) ...)' around the declarations
246 so that they are registered at compile-time as well as run-time."
247 (let ((body (mapcar (lambda (x) `(cl-proclaim ',x)) specs)))
248 (if (cl--compiling-file) `(cl-eval-when (compile load eval) ,@body)
249 `(progn ,@body)))) ; Avoid loading cl-macs.el for cl-eval-when.
252 ;;; Numbers.
254 (define-obsolete-function-alias 'cl-floatp-safe 'floatp "24.4")
256 (defsubst cl-plusp (number)
257 "Return t if NUMBER is positive."
258 (> number 0))
260 (defsubst cl-minusp (number)
261 "Return t if NUMBER is negative."
262 (< number 0))
264 (defun cl-oddp (integer)
265 "Return t if INTEGER is odd."
266 (eq (logand integer 1) 1))
268 (defun cl-evenp (integer)
269 "Return t if INTEGER is even."
270 (eq (logand integer 1) 0))
272 (defconst cl-digit-char-table
273 (let* ((digits (make-vector 256 nil))
274 (populate (lambda (start end base)
275 (mapc (lambda (i)
276 (aset digits i (+ base (- i start))))
277 (number-sequence start end)))))
278 (funcall populate ?0 ?9 0)
279 (funcall populate ?A ?Z 10)
280 (funcall populate ?a ?z 10)
281 digits))
283 (defun cl-digit-char-p (char &optional radix)
284 "Test if CHAR is a digit in the specified RADIX (default 10).
285 If true return the decimal value of digit CHAR in RADIX."
286 (or (<= 2 (or radix 10) 36)
287 (signal 'args-out-of-range (list 'radix radix '(2 36))))
288 (let ((n (aref cl-digit-char-table char)))
289 (and n (< n (or radix 10)) n)))
291 (defun cl--random-time ()
292 (let* ((time (copy-sequence (current-time-string))) (i (length time)) (v 0))
293 (while (>= (cl-decf i) 0) (setq v (+ (* v 3) (aref time i))))
296 (defvar cl--random-state
297 (vector 'cl--random-state-tag -1 30 (cl--random-time)))
299 (defconst cl-most-positive-float nil
300 "The largest value that a Lisp float can hold.
301 If your system supports infinities, this is the largest finite value.
302 For IEEE machines, this is approximately 1.79e+308.
303 Call `cl-float-limits' to set this.")
305 (defconst cl-most-negative-float nil
306 "The largest negative value that a Lisp float can hold.
307 This is simply -`cl-most-positive-float'.
308 Call `cl-float-limits' to set this.")
310 (defconst cl-least-positive-float nil
311 "The smallest value greater than zero that a Lisp float can hold.
312 For IEEE machines, it is about 4.94e-324 if denormals are supported,
313 or 2.22e-308 if they are not.
314 Call `cl-float-limits' to set this.")
316 (defconst cl-least-negative-float nil
317 "The smallest value less than zero that a Lisp float can hold.
318 This is simply -`cl-least-positive-float'.
319 Call `cl-float-limits' to set this.")
321 (defconst cl-least-positive-normalized-float nil
322 "The smallest normalized Lisp float greater than zero.
323 This is the smallest value for which IEEE denormalization does not lose
324 precision. For IEEE machines, this value is about 2.22e-308.
325 For machines that do not support the concept of denormalization
326 and gradual underflow, this constant equals `cl-least-positive-float'.
327 Call `cl-float-limits' to set this.")
329 (defconst cl-least-negative-normalized-float nil
330 "The smallest normalized Lisp float less than zero.
331 This is simply -`cl-least-positive-normalized-float'.
332 Call `cl-float-limits' to set this.")
334 (defconst cl-float-epsilon nil
335 "The smallest positive float that adds to 1.0 to give a distinct value.
336 Adding a number less than this to 1.0 returns 1.0 due to roundoff.
337 For IEEE machines, epsilon is about 2.22e-16.
338 Call `cl-float-limits' to set this.")
340 (defconst cl-float-negative-epsilon nil
341 "The smallest positive float that subtracts from 1.0 to give a distinct value.
342 For IEEE machines, it is about 1.11e-16.
343 Call `cl-float-limits' to set this.")
346 ;;; Sequence functions.
348 (cl--defalias 'cl-copy-seq 'copy-sequence)
350 (declare-function cl--mapcar-many "cl-extra" (cl-func cl-seqs &optional acc))
352 ;;;###autoload
353 (defun cl-mapcar (cl-func cl-x &rest cl-rest)
354 "Apply FUNCTION to each element of SEQ, and make a list of the results.
355 If there are several SEQs, FUNCTION is called with that many arguments,
356 and mapping stops as soon as the shortest list runs out. With just one
357 SEQ, this is like `mapcar'. With several, it is like the Common Lisp
358 `mapcar' function extended to arbitrary sequence types.
359 \n(fn FUNCTION SEQ...)"
360 (if cl-rest
361 (if (or (cdr cl-rest) (nlistp cl-x) (nlistp (car cl-rest)))
362 (cl--mapcar-many cl-func (cons cl-x cl-rest) 'accumulate)
363 (let ((cl-res nil) (cl-y (car cl-rest)))
364 (while (and cl-x cl-y)
365 (push (funcall cl-func (pop cl-x) (pop cl-y)) cl-res))
366 (nreverse cl-res)))
367 (mapcar cl-func cl-x)))
369 (cl--defalias 'cl-svref 'aref)
371 ;;; List functions.
373 (cl--defalias 'cl-first 'car)
374 (cl--defalias 'cl-second 'cadr)
375 (cl--defalias 'cl-rest 'cdr)
377 (defun cl-endp (x)
378 "Return true if X is the empty list; false if it is a cons.
379 Signal an error if X is not a list."
380 (if (listp x)
381 (null x)
382 (signal 'wrong-type-argument (list 'listp x 'x))))
384 (cl--defalias 'cl-third 'cl-caddr "Return the third element of the list X.")
385 (cl--defalias 'cl-fourth 'cl-cadddr "Return the fourth element of the list X.")
387 (defsubst cl-fifth (x)
388 "Return the fifth element of the list X."
389 (declare (gv-setter (lambda (store) `(setcar (nthcdr 4 ,x) ,store))))
390 (nth 4 x))
392 (defsubst cl-sixth (x)
393 "Return the sixth element of the list X."
394 (declare (gv-setter (lambda (store) `(setcar (nthcdr 5 ,x) ,store))))
395 (nth 5 x))
397 (defsubst cl-seventh (x)
398 "Return the seventh element of the list X."
399 (declare (gv-setter (lambda (store) `(setcar (nthcdr 6 ,x) ,store))))
400 (nth 6 x))
402 (defsubst cl-eighth (x)
403 "Return the eighth element of the list X."
404 (declare (gv-setter (lambda (store) `(setcar (nthcdr 7 ,x) ,store))))
405 (nth 7 x))
407 (defsubst cl-ninth (x)
408 "Return the ninth element of the list X."
409 (declare (gv-setter (lambda (store) `(setcar (nthcdr 8 ,x) ,store))))
410 (nth 8 x))
412 (defsubst cl-tenth (x)
413 "Return the tenth element of the list X."
414 (declare (gv-setter (lambda (store) `(setcar (nthcdr 9 ,x) ,store))))
415 (nth 9 x))
417 (defalias 'cl-caaar 'caaar)
418 (defalias 'cl-caadr 'caadr)
419 (defalias 'cl-cadar 'cadar)
420 (defalias 'cl-caddr 'caddr)
421 (defalias 'cl-cdaar 'cdaar)
422 (defalias 'cl-cdadr 'cdadr)
423 (defalias 'cl-cddar 'cddar)
424 (defalias 'cl-cdddr 'cdddr)
425 (defalias 'cl-caaaar 'caaaar)
426 (defalias 'cl-caaadr 'caaadr)
427 (defalias 'cl-caadar 'caadar)
428 (defalias 'cl-caaddr 'caaddr)
429 (defalias 'cl-cadaar 'cadaar)
430 (defalias 'cl-cadadr 'cadadr)
431 (defalias 'cl-caddar 'caddar)
432 (defalias 'cl-cadddr 'cadddr)
433 (defalias 'cl-cdaaar 'cdaaar)
434 (defalias 'cl-cdaadr 'cdaadr)
435 (defalias 'cl-cdadar 'cdadar)
436 (defalias 'cl-cdaddr 'cdaddr)
437 (defalias 'cl-cddaar 'cddaar)
438 (defalias 'cl-cddadr 'cddadr)
439 (defalias 'cl-cdddar 'cdddar)
440 (defalias 'cl-cddddr 'cddddr)
442 ;;(defun last* (x &optional n)
443 ;; "Returns the last link in the list LIST.
444 ;;With optional argument N, returns Nth-to-last link (default 1)."
445 ;; (if n
446 ;; (let ((m 0) (p x))
447 ;; (while (consp p) (cl-incf m) (pop p))
448 ;; (if (<= n 0) p
449 ;; (if (< n m) (nthcdr (- m n) x) x)))
450 ;; (while (consp (cdr x)) (pop x))
451 ;; x))
453 (defun cl-list* (arg &rest rest)
454 "Return a new list with specified ARGs as elements, consed to last ARG.
455 Thus, `(cl-list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to
456 `(cons A (cons B (cons C D)))'.
457 \n(fn ARG...)"
458 (declare (compiler-macro cl--compiler-macro-list*))
459 (cond ((not rest) arg)
460 ((not (cdr rest)) (cons arg (car rest)))
461 (t (let* ((n (length rest))
462 (copy (copy-sequence rest))
463 (last (nthcdr (- n 2) copy)))
464 (setcdr last (car (cdr last)))
465 (cons arg copy)))))
467 (defun cl-ldiff (list sublist)
468 "Return a copy of LIST with the tail SUBLIST removed."
469 (let ((res nil))
470 (while (and (consp list) (not (eq list sublist)))
471 (push (pop list) res))
472 (nreverse res)))
474 (defun cl-copy-list (list)
475 "Return a copy of LIST, which may be a dotted list.
476 The elements of LIST are not copied, just the list structure itself."
477 (if (consp list)
478 (let ((res nil))
479 (while (consp list) (push (pop list) res))
480 (prog1 (nreverse res) (setcdr res list)))
481 (car list)))
483 ;; Autoloaded, but we have not loaded cl-loaddefs yet.
484 (declare-function cl-floor "cl-extra" (x &optional y))
485 (declare-function cl-ceiling "cl-extra" (x &optional y))
486 (declare-function cl-truncate "cl-extra" (x &optional y))
487 (declare-function cl-round "cl-extra" (x &optional y))
488 (declare-function cl-mod "cl-extra" (x y))
490 (defun cl-adjoin (cl-item cl-list &rest cl-keys)
491 "Return ITEM consed onto the front of LIST only if it's not already there.
492 Otherwise, return LIST unmodified.
493 \nKeywords supported: :test :test-not :key
494 \n(fn ITEM LIST [KEYWORD VALUE]...)"
495 (declare (compiler-macro cl--compiler-macro-adjoin))
496 (cond ((or (equal cl-keys '(:test eq))
497 (and (null cl-keys) (not (numberp cl-item))))
498 (if (memq cl-item cl-list) cl-list (cons cl-item cl-list)))
499 ((or (equal cl-keys '(:test equal)) (null cl-keys))
500 (if (member cl-item cl-list) cl-list (cons cl-item cl-list)))
501 (t (apply 'cl--adjoin cl-item cl-list cl-keys))))
503 (defun cl-subst (cl-new cl-old cl-tree &rest cl-keys)
504 "Substitute NEW for OLD everywhere in TREE (non-destructively).
505 Return a copy of TREE with all elements `eql' to OLD replaced by NEW.
506 \nKeywords supported: :test :test-not :key
507 \n(fn NEW OLD TREE [KEYWORD VALUE]...)"
508 (if (or cl-keys (and (numberp cl-old) (not (integerp cl-old))))
509 (apply 'cl-sublis (list (cons cl-old cl-new)) cl-tree cl-keys)
510 (cl--do-subst cl-new cl-old cl-tree)))
512 (defun cl--do-subst (cl-new cl-old cl-tree)
513 (cond ((eq cl-tree cl-old) cl-new)
514 ((consp cl-tree)
515 (let ((a (cl--do-subst cl-new cl-old (car cl-tree)))
516 (d (cl--do-subst cl-new cl-old (cdr cl-tree))))
517 (if (and (eq a (car cl-tree)) (eq d (cdr cl-tree)))
518 cl-tree (cons a d))))
519 (t cl-tree)))
521 (defun cl-acons (key value alist)
522 "Add KEY and VALUE to ALIST.
523 Return a new list with (cons KEY VALUE) as car and ALIST as cdr."
524 (cons (cons key value) alist))
526 (defun cl-pairlis (keys values &optional alist)
527 "Make an alist from KEYS and VALUES.
528 Return a new alist composed by associating KEYS to corresponding VALUES;
529 the process stops as soon as KEYS or VALUES run out.
530 If ALIST is non-nil, the new pairs are prepended to it."
531 (nconc (cl-mapcar 'cons keys values) alist))
533 ;;; Generalized variables.
535 ;; These used to be in cl-macs.el since all macros that use them (like setf)
536 ;; were autoloaded from cl-macs.el. But now that setf, push, and pop are in
537 ;; core Elisp, they need to either be right here or be autoloaded via
538 ;; cl-loaddefs.el, which is more trouble than it is worth.
540 ;; Some more Emacs-related place types.
541 (gv-define-simple-setter buffer-file-name set-visited-file-name t)
542 (gv-define-setter buffer-modified-p (flag &optional buf)
543 `(with-current-buffer ,buf
544 (set-buffer-modified-p ,flag)))
545 (gv-define-simple-setter buffer-name rename-buffer t)
546 (gv-define-setter buffer-string (store)
547 `(insert (prog1 ,store (erase-buffer))))
548 (gv-define-simple-setter buffer-substring cl--set-buffer-substring)
549 (gv-define-simple-setter current-buffer set-buffer)
550 (gv-define-simple-setter current-column move-to-column t)
551 (gv-define-simple-setter current-global-map use-global-map t)
552 (gv-define-setter current-input-mode (store)
553 `(progn (apply #'set-input-mode ,store) ,store))
554 (gv-define-simple-setter current-local-map use-local-map t)
555 (gv-define-simple-setter current-window-configuration
556 set-window-configuration t)
557 (gv-define-simple-setter default-file-modes set-default-file-modes t)
558 (gv-define-simple-setter documentation-property put)
559 (gv-define-setter face-background (x f &optional s)
560 `(set-face-background ,f ,x ,s))
561 (gv-define-setter face-background-pixmap (x f &optional s)
562 `(set-face-background-pixmap ,f ,x ,s))
563 (gv-define-setter face-font (x f &optional s) `(set-face-font ,f ,x ,s))
564 (gv-define-setter face-foreground (x f &optional s)
565 `(set-face-foreground ,f ,x ,s))
566 (gv-define-setter face-underline-p (x f &optional s)
567 `(set-face-underline ,f ,x ,s))
568 (gv-define-simple-setter file-modes set-file-modes t)
569 (gv-define-setter frame-height (x &optional frame)
570 `(set-frame-height (or ,frame (selected-frame)) ,x))
571 (gv-define-simple-setter frame-parameters modify-frame-parameters t)
572 (gv-define-simple-setter frame-visible-p cl--set-frame-visible-p)
573 (gv-define-setter frame-width (x &optional frame)
574 `(set-frame-width (or ,frame (selected-frame)) ,x))
575 (gv-define-simple-setter getenv setenv t)
576 (gv-define-simple-setter get-register set-register)
577 (gv-define-simple-setter global-key-binding global-set-key)
578 (gv-define-simple-setter local-key-binding local-set-key)
579 (gv-define-simple-setter mark set-mark t)
580 (gv-define-simple-setter mark-marker set-mark t)
581 (gv-define-simple-setter marker-position set-marker t)
582 (gv-define-setter mouse-position (store scr)
583 `(set-mouse-position ,scr (car ,store) (cadr ,store)
584 (cddr ,store)))
585 (gv-define-simple-setter point goto-char)
586 (gv-define-simple-setter point-marker goto-char t)
587 (gv-define-setter point-max (store)
588 `(progn (narrow-to-region (point-min) ,store) ,store))
589 (gv-define-setter point-min (store)
590 `(progn (narrow-to-region ,store (point-max)) ,store))
591 (gv-define-setter read-mouse-position (store scr)
592 `(set-mouse-position ,scr (car ,store) (cdr ,store)))
593 (gv-define-simple-setter screen-height set-screen-height t)
594 (gv-define-simple-setter screen-width set-screen-width t)
595 (gv-define-simple-setter selected-window select-window)
596 (gv-define-simple-setter selected-screen select-screen)
597 (gv-define-simple-setter selected-frame select-frame)
598 (gv-define-simple-setter standard-case-table set-standard-case-table)
599 (gv-define-simple-setter syntax-table set-syntax-table)
600 (gv-define-simple-setter visited-file-modtime set-visited-file-modtime t)
601 (gv-define-setter window-height (store)
602 `(progn (enlarge-window (- ,store (window-height))) ,store))
603 (gv-define-setter window-width (store)
604 `(progn (enlarge-window (- ,store (window-width)) t) ,store))
605 (gv-define-simple-setter x-get-secondary-selection x-own-secondary-selection t)
607 ;; More complex setf-methods.
609 ;; This is a hack that allows (setf (eq a 7) B) to mean either
610 ;; (setq a 7) or (setq a nil) depending on whether B is nil or not.
611 ;; This is useful when you have control over the PLACE but not over
612 ;; the VALUE, as is the case in define-minor-mode's :variable.
613 ;; It turned out that :variable needed more flexibility anyway, so
614 ;; this doesn't seem too useful now.
615 (gv-define-expander eq
616 (lambda (do place val)
617 (gv-letplace (getter setter) place
618 (macroexp-let2 nil val val
619 (funcall do `(eq ,getter ,val)
620 (lambda (v)
621 `(cond
622 (,v ,(funcall setter val))
623 ((eq ,getter ,val) ,(funcall setter `(not ,val))))))))))
625 (gv-define-expander substring
626 (lambda (do place from &optional to)
627 (gv-letplace (getter setter) place
628 (macroexp-let2* nil ((start from) (end to))
629 (funcall do `(substring ,getter ,start ,end)
630 (lambda (v)
631 (funcall setter `(cl--set-substring
632 ,getter ,start ,end ,v))))))))
634 ;;; Miscellaneous.
636 (provide 'cl-lib)
637 (unless (load "cl-loaddefs" 'noerror 'quiet)
638 ;; When bootstrapping, cl-loaddefs hasn't been built yet!
639 (require 'cl-macs)
640 (require 'cl-seq))
642 (defun cl--old-struct-type-of (orig-fun object)
643 (or (and (vectorp object)
644 (let ((tag (aref object 0)))
645 (when (and (symbolp tag)
646 (string-prefix-p "cl-struct-" (symbol-name tag)))
647 (unless (eq (symbol-function tag)
648 :quick-object-witness-check)
649 ;; Old-style old-style struct:
650 ;; Convert to new-style old-style struct!
651 (let* ((type (intern (substring (symbol-name tag)
652 (length "cl-struct-"))))
653 (class (cl--struct-get-class type)))
654 ;; If the `cl-defstruct' was recompiled after the code
655 ;; which constructed `object', `cl--struct-get-class' may
656 ;; not have called `cl-struct-define' and setup the tag
657 ;; symbol for us.
658 (unless (eq (symbol-function tag)
659 :quick-object-witness-check)
660 (set tag class)
661 (fset tag :quick-object-witness-check))))
662 (cl--class-name (symbol-value tag)))))
663 (funcall orig-fun object)))
665 ;;;###autoload
666 (define-minor-mode cl-old-struct-compat-mode
667 "Enable backward compatibility with old-style structs.
668 This can be needed when using code byte-compiled using the old
669 macro-expansion of `cl-defstruct' that used vectors objects instead
670 of record objects."
671 :global t
672 (cond
673 (cl-old-struct-compat-mode
674 (advice-add 'type-of :around #'cl--old-struct-type-of))
676 (advice-remove 'type-of #'cl--old-struct-type-of))))
678 ;; Local variables:
679 ;; byte-compile-dynamic: t
680 ;; End:
682 ;;; cl-lib.el ends here