comparison lisp/emacs-lisp/cl-extra.el @ 28565:69dea80bbb87

Don't quote keywords. (cl-old-mapc): New variable. (mapc): Use it. (cl-map-intervals): Use with-current-buffer. Don't check for next-property-change. (cl-map-overlays): Use with-current-buffer. (cl-expt): Remove. (copy-tree, remprop): Define unconditionally.
author Dave Love <fx@gnu.org>
date Thu, 13 Apr 2000 19:03:34 +0000
parents 567639571c84
children 0569ba69aa2b
comparison
equal deleted inserted replaced
28564:e79438733ef2 28565:69dea80bbb87
150 (while cl-list 150 (while cl-list
151 (cl-push (funcall cl-func cl-list) cl-res) 151 (cl-push (funcall cl-func cl-list) cl-res)
152 (setq cl-list (cdr cl-list))) 152 (setq cl-list (cdr cl-list)))
153 (nreverse cl-res)))) 153 (nreverse cl-res))))
154 154
155 (defvar cl-old-mapc (symbol-function 'mapc))
156
155 (defun mapc (cl-func cl-seq &rest cl-rest) 157 (defun mapc (cl-func cl-seq &rest cl-rest)
156 "Like `mapcar', but does not accumulate values returned by the function." 158 "Like `mapcar', but does not accumulate values returned by the function."
157 (if cl-rest 159 (if cl-rest
158 (apply 'map nil cl-func cl-seq cl-rest) 160 (progn (apply 'map nil cl-func cl-seq cl-rest)
159 (mapcar cl-func cl-seq)) 161 cl-seq)
160 cl-seq) 162 (funcall #'cl-old-mapc cl-func cl-seq)))
161 163
162 (defun mapl (cl-func cl-list &rest cl-rest) 164 (defun mapl (cl-func cl-list &rest cl-rest)
163 "Like `maplist', but does not accumulate values returned by the function." 165 "Like `maplist', but does not accumulate values returned by the function."
164 (if cl-rest 166 (if cl-rest
165 (apply 'maplist cl-func cl-list cl-rest) 167 (apply 'maplist cl-func cl-list cl-rest)
242 244
243 (defun cl-map-intervals (cl-func &optional cl-what cl-prop cl-start cl-end) 245 (defun cl-map-intervals (cl-func &optional cl-what cl-prop cl-start cl-end)
244 (or cl-what (setq cl-what (current-buffer))) 246 (or cl-what (setq cl-what (current-buffer)))
245 (if (bufferp cl-what) 247 (if (bufferp cl-what)
246 (let (cl-mark cl-mark2 (cl-next t) cl-next2) 248 (let (cl-mark cl-mark2 (cl-next t) cl-next2)
247 (save-excursion 249 (with-current-buffer cl-what
248 (set-buffer cl-what)
249 (setq cl-mark (copy-marker (or cl-start (point-min)))) 250 (setq cl-mark (copy-marker (or cl-start (point-min))))
250 (setq cl-mark2 (and cl-end (copy-marker cl-end)))) 251 (setq cl-mark2 (and cl-end (copy-marker cl-end))))
251 (while (and cl-next (or (not cl-mark2) (< cl-mark cl-mark2))) 252 (while (and cl-next (or (not cl-mark2) (< cl-mark cl-mark2)))
252 (setq cl-next (and (fboundp 'next-property-change) 253 (setq cl-next (if cl-prop (next-single-property-change
253 (if cl-prop (next-single-property-change 254 cl-mark cl-prop cl-what)
254 cl-mark cl-prop cl-what) 255 (next-property-change cl-mark cl-what))
255 (next-property-change cl-mark cl-what))) 256 cl-next2 (or cl-next (with-current-buffer cl-what
256 cl-next2 (or cl-next (save-excursion 257 (point-max))))
257 (set-buffer cl-what) (point-max))))
258 (funcall cl-func (prog1 (marker-position cl-mark) 258 (funcall cl-func (prog1 (marker-position cl-mark)
259 (set-marker cl-mark cl-next2)) 259 (set-marker cl-mark cl-next2))
260 (if cl-mark2 (min cl-next2 cl-mark2) cl-next2))) 260 (if cl-mark2 (min cl-next2 cl-mark2) cl-next2)))
261 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil))) 261 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil)))
262 (or cl-start (setq cl-start 0)) 262 (or cl-start (setq cl-start 0))
263 (or cl-end (setq cl-end (length cl-what))) 263 (or cl-end (setq cl-end (length cl-what)))
264 (while (< cl-start cl-end) 264 (while (< cl-start cl-end)
265 (let ((cl-next (or (and (fboundp 'next-property-change) 265 (let ((cl-next (or (if cl-prop (next-single-property-change
266 (if cl-prop (next-single-property-change 266 cl-start cl-prop cl-what)
267 cl-start cl-prop cl-what) 267 (next-property-change cl-start cl-what))
268 (next-property-change cl-start cl-what)))
269 cl-end))) 268 cl-end)))
270 (funcall cl-func cl-start (min cl-next cl-end)) 269 (funcall cl-func cl-start (min cl-next cl-end))
271 (setq cl-start cl-next))))) 270 (setq cl-start cl-next)))))
272 271
273 (defun cl-map-overlays (cl-func &optional cl-buffer cl-start cl-end cl-arg) 272 (defun cl-map-overlays (cl-func &optional cl-buffer cl-start cl-end cl-arg)
274 (or cl-buffer (setq cl-buffer (current-buffer))) 273 (or cl-buffer (setq cl-buffer (current-buffer)))
275 (if (fboundp 'overlay-lists) 274 (if (fboundp 'overlay-lists)
276 275
277 ;; This is the preferred algorithm, though overlay-lists is undocumented. 276 ;; This is the preferred algorithm, though overlay-lists is undocumented.
278 (let (cl-ovl) 277 (let (cl-ovl)
279 (save-excursion 278 (with-current-buffer cl-buffer
280 (set-buffer cl-buffer)
281 (setq cl-ovl (overlay-lists)) 279 (setq cl-ovl (overlay-lists))
282 (if cl-start (setq cl-start (copy-marker cl-start))) 280 (if cl-start (setq cl-start (copy-marker cl-start)))
283 (if cl-end (setq cl-end (copy-marker cl-end)))) 281 (if cl-end (setq cl-end (copy-marker cl-end))))
284 (setq cl-ovl (nconc (car cl-ovl) (cdr cl-ovl))) 282 (setq cl-ovl (nconc (car cl-ovl) (cdr cl-ovl)))
285 (while (and cl-ovl 283 (while (and cl-ovl
290 (setq cl-ovl (cdr cl-ovl))) 288 (setq cl-ovl (cdr cl-ovl)))
291 (if cl-start (set-marker cl-start nil)) 289 (if cl-start (set-marker cl-start nil))
292 (if cl-end (set-marker cl-end nil))) 290 (if cl-end (set-marker cl-end nil)))
293 291
294 ;; This alternate algorithm fails to find zero-length overlays. 292 ;; This alternate algorithm fails to find zero-length overlays.
295 (let ((cl-mark (save-excursion (set-buffer cl-buffer) 293 (let ((cl-mark (with-current-buffer cl-buffer
296 (copy-marker (or cl-start (point-min))))) 294 (copy-marker (or cl-start (point-min)))))
297 (cl-mark2 (and cl-end (save-excursion (set-buffer cl-buffer) 295 (cl-mark2 (and cl-end (with-current-buffer cl-buffer
298 (copy-marker cl-end)))) 296 (copy-marker cl-end))))
299 cl-pos cl-ovl) 297 cl-pos cl-ovl)
300 (while (save-excursion 298 (while (save-excursion
301 (and (setq cl-pos (marker-position cl-mark)) 299 (and (setq cl-pos (marker-position cl-mark))
302 (< cl-pos (or cl-mark2 (point-max))) 300 (< cl-pos (or cl-mark2 (point-max)))
303 (progn 301 (progn
365 g2) 363 g2)
366 (while (< (setq g2 (/ (+ g (/ a g)) 2)) g) 364 (while (< (setq g2 (/ (+ g (/ a g)) 2)) g)
367 (setq g g2)) 365 (setq g g2))
368 g) 366 g)
369 (if (eq a 0) 0 (signal 'arith-error nil)))) 367 (if (eq a 0) 0 (signal 'arith-error nil))))
370
371 (defun cl-expt (x y)
372 "Return X raised to the power of Y. Works only for integer arguments."
373 (if (<= y 0) (if (= y 0) 1 (if (memq x '(-1 1)) (cl-expt x (- y)) 0))
374 (* (if (= (% y 2) 0) 1 x) (cl-expt (* x x) (/ y 2)))))
375 (or (and (fboundp 'expt) (subrp (symbol-function 'expt)))
376 (defalias 'expt 'cl-expt))
377 368
378 (defun floor* (x &optional y) 369 (defun floor* (x &optional y)
379 "Return a list of the floor of X and the fractional part of X. 370 "Return a list of the floor of X and the fractional part of X.
380 With two arguments, return floor and remainder of their quotient." 371 With two arguments, return floor and remainder of their quotient."
381 (let ((q (floor x y))) 372 (let ((q (floor x y)))
591 (if (and vecp (vectorp tree)) 582 (if (and vecp (vectorp tree))
592 (let ((i (length (setq tree (copy-sequence tree))))) 583 (let ((i (length (setq tree (copy-sequence tree)))))
593 (while (>= (setq i (1- i)) 0) 584 (while (>= (setq i (1- i)) 0)
594 (aset tree i (cl-copy-tree (aref tree i) vecp)))))) 585 (aset tree i (cl-copy-tree (aref tree i) vecp))))))
595 tree) 586 tree)
596 (or (and (fboundp 'copy-tree) (subrp (symbol-function 'copy-tree))) 587 (defalias 'copy-tree 'cl-copy-tree)
597 (defalias 'copy-tree 'cl-copy-tree))
598 588
599 589
600 ;;; Property lists. 590 ;;; Property lists.
601 591
602 (defun get* (sym tag &optional def) ; See compiler macro in cl-macs.el 592 (defun get* (sym tag &optional def) ; See compiler macro in cl-macs.el
635 "Remove from SYMBOL's plist the property PROP and its value." 625 "Remove from SYMBOL's plist the property PROP and its value."
636 (let ((plist (symbol-plist sym))) 626 (let ((plist (symbol-plist sym)))
637 (if (and plist (eq tag (car plist))) 627 (if (and plist (eq tag (car plist)))
638 (progn (setplist sym (cdr (cdr plist))) t) 628 (progn (setplist sym (cdr (cdr plist))) t)
639 (cl-do-remf plist tag)))) 629 (cl-do-remf plist tag))))
640 (or (and (fboundp 'remprop) (subrp (symbol-function 'remprop))) 630 (defalias 'remprop 'cl-remprop)
641 (defalias 'remprop 'cl-remprop))
642 631
643 632
644 633
645 ;;; Hash tables. 634 ;;; Hash tables.
646 635
647 (defun cl-make-hash-table (&rest cl-keys) 636 (defun cl-make-hash-table (&rest cl-keys)
648 "Make an empty Common Lisp-style hash-table. 637 "Make an empty Common Lisp-style hash-table.
649 Keywords supported: :test :size 638 Keywords supported: :test :size
650 The Common Lisp keywords :rehash-size and :rehash-threshold are ignored." 639 The Common Lisp keywords :rehash-size and :rehash-threshold are ignored."
651 (let ((cl-test (or (car (cdr (memq ':test cl-keys))) 'eql)) 640 (let ((cl-test (or (car (cdr (memq :test cl-keys))) 'eql))
652 (cl-size (or (car (cdr (memq ':size cl-keys))) 20))) 641 (cl-size (or (car (cdr (memq :size cl-keys))) 20)))
653 (make-hash-table :size cl-size :test cl-size))) 642 (make-hash-table :size cl-size :test cl-size)))
654 643
655 (defun cl-hash-table-p (x) 644 (defun cl-hash-table-p (x)
656 "Return t if OBJECT is a hash table." 645 "Return t if OBJECT is a hash table."
657 (or (hash-table-p x) 646 (or (hash-table-p x)
676 (setq sym (symbol-value (intern-soft str array)))) 665 (setq sym (symbol-value (intern-soft str array))))
677 (list (and sym (cond ((or (eq test 'eq) 666 (list (and sym (cond ((or (eq test 'eq)
678 (and (eq test 'eql) (not (numberp key)))) 667 (and (eq test 'eql) (not (numberp key))))
679 (assq key sym)) 668 (assq key sym))
680 ((memq test '(eql equal)) (assoc key sym)) 669 ((memq test '(eql equal)) (assoc key sym))
681 (t (assoc* key sym ':test test)))) 670 (t (assoc* key sym :test test))))
682 sym str))) 671 sym str)))
683 672
684 (defun cl-gethash (key table &optional def) 673 (defun cl-gethash (key table &optional def)
685 "Look up KEY in HASH-TABLE; return corresponding value, or DEFAULT." 674 "Look up KEY in HASH-TABLE; return corresponding value, or DEFAULT."
686 (if (consp table) 675 (if (consp table)