aboutsummaryrefslogtreecommitdiff
path: root/compat-27.el
diff options
context:
space:
mode:
authorDaniel Mendler <mail@daniel-mendler.de>2023-01-20 15:26:42 +0100
committerDaniel Mendler <mail@daniel-mendler.de>2023-01-20 15:29:49 +0100
commitb27914c0b691eb9403e44137e01f6cf5a6c65653 (patch)
tree926ba4d60b620392e268bdaa15424d6002754f8a /compat-27.el
parentadbf07f9f1ee421fd450c5e3c410a1222e592022 (diff)
compat-27: Add ring-resize
Diffstat (limited to 'compat-27.el')
-rw-r--r--compat-27.el24
1 files changed, 24 insertions, 0 deletions
diff --git a/compat-27.el b/compat-27.el
index 9f9198e..16a6b12 100644
--- a/compat-27.el
+++ b/compat-27.el
@@ -714,5 +714,29 @@ and if a matching region is found, place point at the start of the region."
(and (not (eq ended t))
ended))))))
+;;;; Defined in ring.el
+
+(compat-defun ring-resize (ring size)
+ "Set the size of RING to SIZE.
+If the new size is smaller, then the oldest items in the ring are
+discarded."
+ :feature ring
+ (when (integerp size)
+ (let ((length (ring-length ring))
+ (new-vec (make-vector size nil)))
+ (if (= length 0)
+ (setcdr ring (cons 0 new-vec))
+ (let* ((hd (car ring))
+ (old-size (ring-size ring))
+ (old-vec (cddr ring))
+ (copy-length (min size length))
+ (copy-hd (mod (+ hd (- length copy-length)) length)))
+ (setcdr ring (cons copy-length new-vec))
+ ;; If the ring is wrapped, the existing elements must be written
+ ;; out in the right order.
+ (dotimes (j copy-length)
+ (aset new-vec j (aref old-vec (mod (+ copy-hd j) old-size))))
+ (setcar ring 0))))))
+
(provide 'compat-27)
;;; compat-27.el ends here