diff options
| author | Juanma Barranquero | 2007-11-17 02:51:49 +0000 |
|---|---|---|
| committer | Juanma Barranquero | 2007-11-17 02:51:49 +0000 |
| commit | efb67a5e8dea3b4bb9af250757a9e97db5c5a9a0 (patch) | |
| tree | 71bd3b3bed5b52d68a3acb792fc0b44812d866ce | |
| parent | 82bb5643229173955379d4b51cd25fee547046df (diff) | |
| download | emacs-efb67a5e8dea3b4bb9af250757a9e97db5c5a9a0.tar.gz emacs-efb67a5e8dea3b4bb9af250757a9e97db5c5a9a0.zip | |
(ring-size, ring-p, ring-insert, ring-length, ring-empty-p): Use c[ad]dr.
(ring-plus1): Use `1+'.
(ring-minus1): Use `zerop'.
(ring-remove): Use c[ad]dr. Use `when'.
(ring-copy): Use c[ad]dr. Use `let', not `let*'.
(ring-ref): Use `let', not `let*'.
(ring-insert-at-beginning): Use c[ad]dr. Doc fix.
(ring-insert+extend): Use c[ad]dr. Fix typo in docstring.
(ring-member): Simplify. Doc fix.
(ring-convert-sequence-to-ring): Simplify.
| -rw-r--r-- | lisp/ChangeLog | 23 | ||||
| -rw-r--r-- | lisp/emacs-lisp/ring.el | 76 |
2 files changed, 57 insertions, 42 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog index d0fb5ef28da..b1a571ef96b 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog | |||
| @@ -1,3 +1,20 @@ | |||
| 1 | 2007-11-17 Juanma Barranquero <lekktu@gmail.com> | ||
| 2 | |||
| 3 | * emacs-lisp/backquote.el (backquote): | ||
| 4 | Improve argument/docstring consistency. | ||
| 5 | |||
| 6 | * emacs-lisp/ring.el (ring-size, ring-p, ring-insert) | ||
| 7 | (ring-length, ring-empty-p): Use c[ad]dr. | ||
| 8 | (ring-plus1): Use `1+'. | ||
| 9 | (ring-minus1): Use `zerop'. | ||
| 10 | (ring-remove): Use c[ad]dr. Use `when'. | ||
| 11 | (ring-copy): Use c[ad]dr. Use `let', not `let*'. | ||
| 12 | (ring-ref): Use `let', not `let*'. | ||
| 13 | (ring-insert-at-beginning): Use c[ad]dr. Doc fix. | ||
| 14 | (ring-insert+extend): Use c[ad]dr. Fix typo in docstring. | ||
| 15 | (ring-member): Simplify. Doc fix. | ||
| 16 | (ring-convert-sequence-to-ring): Simplify. | ||
| 17 | |||
| 1 | 2007-11-17 Juri Linkov <juri@jurta.org> | 18 | 2007-11-17 Juri Linkov <juri@jurta.org> |
| 2 | 19 | ||
| 3 | * dired-aux.el (dired-create-directory): Allow creating | 20 | * dired-aux.el (dired-create-directory): Allow creating |
| @@ -36,12 +53,12 @@ | |||
| 36 | * emulation/pc-select.el (pc-select-shifted-mark): New var. | 53 | * emulation/pc-select.el (pc-select-shifted-mark): New var. |
| 37 | (ensure-mark): Set it. | 54 | (ensure-mark): Set it. |
| 38 | (maybe-deactivate-mark): New fun. | 55 | (maybe-deactivate-mark): New fun. |
| 39 | Use it everywhere instead of (setq mark-active nil) | 56 | Use it everywhere instead of (setq mark-active nil). |
| 40 | 57 | ||
| 41 | 2007-11-16 Dan Nicolaescu <dann@ics.uci.edu> | 58 | 2007-11-16 Dan Nicolaescu <dann@ics.uci.edu> |
| 42 | 59 | ||
| 43 | * textmodes/reftex-dcr.el (reftex-start-itimer-once): Add check | 60 | * textmodes/reftex-dcr.el (reftex-start-itimer-once): |
| 44 | for XEmacs. | 61 | Add check for XEmacs. |
| 45 | 62 | ||
| 46 | * calc/calc-menu.el (calc-mode-map): Pacify byte compiler. | 63 | * calc/calc-menu.el (calc-mode-map): Pacify byte compiler. |
| 47 | 64 | ||
diff --git a/lisp/emacs-lisp/ring.el b/lisp/emacs-lisp/ring.el index 93cf434292a..d9ce48e23a6 100644 --- a/lisp/emacs-lisp/ring.el +++ b/lisp/emacs-lisp/ring.el | |||
| @@ -51,8 +51,8 @@ | |||
| 51 | (defun ring-p (x) | 51 | (defun ring-p (x) |
| 52 | "Return t if X is a ring; nil otherwise." | 52 | "Return t if X is a ring; nil otherwise." |
| 53 | (and (consp x) (integerp (car x)) | 53 | (and (consp x) (integerp (car x)) |
| 54 | (consp (cdr x)) (integerp (car (cdr x))) | 54 | (consp (cdr x)) (integerp (cadr x)) |
| 55 | (vectorp (cdr (cdr x))))) | 55 | (vectorp (cddr x)))) |
| 56 | 56 | ||
| 57 | ;;;###autoload | 57 | ;;;###autoload |
| 58 | (defun make-ring (size) | 58 | (defun make-ring (size) |
| @@ -60,11 +60,11 @@ | |||
| 60 | (cons 0 (cons 0 (make-vector size nil)))) | 60 | (cons 0 (cons 0 (make-vector size nil)))) |
| 61 | 61 | ||
| 62 | (defun ring-insert-at-beginning (ring item) | 62 | (defun ring-insert-at-beginning (ring item) |
| 63 | "Add to RING the item ITEM. Add it at the front, as the oldest item." | 63 | "Add to RING the item ITEM, at the front, as the oldest item." |
| 64 | (let* ((vec (cdr (cdr ring))) | 64 | (let* ((vec (cddr ring)) |
| 65 | (veclen (length vec)) | 65 | (veclen (length vec)) |
| 66 | (hd (car ring)) | 66 | (hd (car ring)) |
| 67 | (ln (car (cdr ring)))) | 67 | (ln (cadr ring))) |
| 68 | (setq ln (min veclen (1+ ln)) | 68 | (setq ln (min veclen (1+ ln)) |
| 69 | hd (ring-minus1 hd veclen)) | 69 | hd (ring-minus1 hd veclen)) |
| 70 | (aset vec hd item) | 70 | (aset vec hd item) |
| @@ -73,16 +73,16 @@ | |||
| 73 | 73 | ||
| 74 | (defun ring-plus1 (index veclen) | 74 | (defun ring-plus1 (index veclen) |
| 75 | "Return INDEX+1, with wraparound." | 75 | "Return INDEX+1, with wraparound." |
| 76 | (let ((new-index (+ index 1))) | 76 | (let ((new-index (1+ index))) |
| 77 | (if (= new-index veclen) 0 new-index))) | 77 | (if (= new-index veclen) 0 new-index))) |
| 78 | 78 | ||
| 79 | (defun ring-minus1 (index veclen) | 79 | (defun ring-minus1 (index veclen) |
| 80 | "Return INDEX-1, with wraparound." | 80 | "Return INDEX-1, with wraparound." |
| 81 | (- (if (= 0 index) veclen index) 1)) | 81 | (- (if (zerop index) veclen index) 1)) |
| 82 | 82 | ||
| 83 | (defun ring-length (ring) | 83 | (defun ring-length (ring) |
| 84 | "Return the number of elements in the RING." | 84 | "Return the number of elements in the RING." |
| 85 | (car (cdr ring))) | 85 | (cadr ring)) |
| 86 | 86 | ||
| 87 | (defun ring-index (index head ringlen veclen) | 87 | (defun ring-index (index head ringlen veclen) |
| 88 | "Convert nominal ring index INDEX to an internal index. | 88 | "Convert nominal ring index INDEX to an internal index. |
| @@ -95,26 +95,26 @@ VECLEN is the size of the vector in the ring." | |||
| 95 | 95 | ||
| 96 | (defun ring-empty-p (ring) | 96 | (defun ring-empty-p (ring) |
| 97 | "Return t if RING is empty; nil otherwise." | 97 | "Return t if RING is empty; nil otherwise." |
| 98 | (zerop (car (cdr ring)))) | 98 | (zerop (cadr ring))) |
| 99 | 99 | ||
| 100 | (defun ring-size (ring) | 100 | (defun ring-size (ring) |
| 101 | "Return the size of RING, the maximum number of elements it can contain." | 101 | "Return the size of RING, the maximum number of elements it can contain." |
| 102 | (length (cdr (cdr ring)))) | 102 | (length (cddr ring))) |
| 103 | 103 | ||
| 104 | (defun ring-copy (ring) | 104 | (defun ring-copy (ring) |
| 105 | "Return a copy of RING." | 105 | "Return a copy of RING." |
| 106 | (let* ((vec (cdr (cdr ring))) | 106 | (let ((vec (cddr ring)) |
| 107 | (hd (car ring)) | 107 | (hd (car ring)) |
| 108 | (ln (car (cdr ring)))) | 108 | (ln (cadr ring))) |
| 109 | (cons hd (cons ln (copy-sequence vec))))) | 109 | (cons hd (cons ln (copy-sequence vec))))) |
| 110 | 110 | ||
| 111 | (defun ring-insert (ring item) | 111 | (defun ring-insert (ring item) |
| 112 | "Insert onto ring RING the item ITEM, as the newest (last) item. | 112 | "Insert onto ring RING the item ITEM, as the newest (last) item. |
| 113 | If the ring is full, dump the oldest item to make room." | 113 | If the ring is full, dump the oldest item to make room." |
| 114 | (let* ((vec (cdr (cdr ring))) | 114 | (let* ((vec (cddr ring)) |
| 115 | (veclen (length vec)) | 115 | (veclen (length vec)) |
| 116 | (hd (car ring)) | 116 | (hd (car ring)) |
| 117 | (ln (car (cdr ring)))) | 117 | (ln (cadr ring))) |
| 118 | (prog1 | 118 | (prog1 |
| 119 | (aset vec (mod (+ hd ln) veclen) item) | 119 | (aset vec (mod (+ hd ln) veclen) item) |
| 120 | (if (= ln veclen) | 120 | (if (= ln veclen) |
| @@ -128,13 +128,13 @@ numeric, remove the element indexed." | |||
| 128 | (if (ring-empty-p ring) | 128 | (if (ring-empty-p ring) |
| 129 | (error "Ring empty") | 129 | (error "Ring empty") |
| 130 | (let* ((hd (car ring)) | 130 | (let* ((hd (car ring)) |
| 131 | (ln (car (cdr ring))) | 131 | (ln (cadr ring)) |
| 132 | (vec (cdr (cdr ring))) | 132 | (vec (cddr ring)) |
| 133 | (veclen (length vec)) | 133 | (veclen (length vec)) |
| 134 | (tl (mod (1- (+ hd ln)) veclen)) | 134 | (tl (mod (1- (+ hd ln)) veclen)) |
| 135 | oldelt) | 135 | oldelt) |
| 136 | (if (null index) | 136 | (when (null index) |
| 137 | (setq index (1- ln))) | 137 | (setq index (1- ln))) |
| 138 | (setq index (ring-index index hd ln veclen)) | 138 | (setq index (ring-index index hd ln veclen)) |
| 139 | (setq oldelt (aref vec index)) | 139 | (setq oldelt (aref vec index)) |
| 140 | (while (/= index tl) | 140 | (while (/= index tl) |
| @@ -152,7 +152,9 @@ INDEX need not be <= the ring length; the appropriate modulo operation | |||
| 152 | will be performed." | 152 | will be performed." |
| 153 | (if (ring-empty-p ring) | 153 | (if (ring-empty-p ring) |
| 154 | (error "Accessing an empty ring") | 154 | (error "Accessing an empty ring") |
| 155 | (let* ((hd (car ring)) (ln (car (cdr ring))) (vec (cdr (cdr ring)))) | 155 | (let ((hd (car ring)) |
| 156 | (ln (cadr ring)) | ||
| 157 | (vec (cddr ring))) | ||
| 156 | (aref vec (ring-index index hd ln (length vec)))))) | 158 | (aref vec (ring-index index hd ln (length vec)))))) |
| 157 | 159 | ||
| 158 | (defun ring-elements (ring) | 160 | (defun ring-elements (ring) |
| @@ -165,15 +167,12 @@ will be performed." | |||
| 165 | (push (aref vect (mod (+ start var) size)) lst)))) | 167 | (push (aref vect (mod (+ start var) size)) lst)))) |
| 166 | 168 | ||
| 167 | (defun ring-member (ring item) | 169 | (defun ring-member (ring item) |
| 168 | "Return index of ITEM if on RING, else nil. Comparison via `equal'. | 170 | "Return index of ITEM if on RING, else nil. |
| 169 | The index is 0-based." | 171 | Comparison is done via `equal'. The index is 0-based." |
| 170 | (let ((ind 0) | 172 | (catch 'found |
| 171 | (len (1- (ring-length ring))) | 173 | (dotimes (ind (ring-length ring) nil) |
| 172 | (memberp nil)) | 174 | (when (equal item (ring-ref ring ind)) |
| 173 | (while (and (<= ind len) | 175 | (throw 'found ind))))) |
| 174 | (not (setq memberp (equal item (ring-ref ring ind))))) | ||
| 175 | (setq ind (1+ ind))) | ||
| 176 | (and memberp ind))) | ||
| 177 | 176 | ||
| 178 | (defun ring-next (ring item) | 177 | (defun ring-next (ring item) |
| 179 | "Return the next item in the RING, after ITEM. | 178 | "Return the next item in the RING, after ITEM. |
| @@ -190,12 +189,12 @@ Raise error if ITEM is not in the RING." | |||
| 190 | (ring-ref ring (ring-minus1 curr-index (ring-length ring))))) | 189 | (ring-ref ring (ring-minus1 curr-index (ring-length ring))))) |
| 191 | 190 | ||
| 192 | (defun ring-insert+extend (ring item &optional grow-p) | 191 | (defun ring-insert+extend (ring item &optional grow-p) |
| 193 | "Like ring-insert, but if GROW-P is non-nil, then enlarge ring. | 192 | "Like `ring-insert', but if GROW-P is non-nil, then enlarge ring. |
| 194 | Insert onto ring RING the item ITEM, as the newest (last) item. | 193 | Insert onto ring RING the item ITEM, as the newest (last) item. |
| 195 | If the ring is full, behavior depends on GROW-P: | 194 | If the ring is full, behavior depends on GROW-P: |
| 196 | If GROW-P is non-nil, enlarge the ring to accommodate the new item. | 195 | If GROW-P is non-nil, enlarge the ring to accommodate the new item. |
| 197 | If GROW-P is nil, dump the oldest item to make room for the new." | 196 | If GROW-P is nil, dump the oldest item to make room for the new." |
| 198 | (let* ((vec (cdr (cdr ring))) | 197 | (let* ((vec (cddr ring)) |
| 199 | (veclen (length vec)) | 198 | (veclen (length vec)) |
| 200 | (hd (car ring)) | 199 | (hd (car ring)) |
| 201 | (ringlen (ring-length ring))) | 200 | (ringlen (ring-length ring))) |
| @@ -218,7 +217,8 @@ If the RING is full, behavior depends on GROW-P: | |||
| 218 | If GROW-P is non-nil, enlarge the ring to accommodate the new ITEM. | 217 | If GROW-P is non-nil, enlarge the ring to accommodate the new ITEM. |
| 219 | If GROW-P is nil, dump the oldest item to make room for the new." | 218 | If GROW-P is nil, dump the oldest item to make room for the new." |
| 220 | (let (ind) | 219 | (let (ind) |
| 221 | (while (setq ind (ring-member ring item)) (ring-remove ring ind))) | 220 | (while (setq ind (ring-member ring item)) |
| 221 | (ring-remove ring ind))) | ||
| 222 | (ring-insert+extend ring item grow-p)) | 222 | (ring-insert+extend ring item grow-p)) |
| 223 | 223 | ||
| 224 | (defun ring-convert-sequence-to-ring (seq) | 224 | (defun ring-convert-sequence-to-ring (seq) |
| @@ -227,13 +227,11 @@ If SEQ is already a ring, return it." | |||
| 227 | (if (ring-p seq) | 227 | (if (ring-p seq) |
| 228 | seq | 228 | seq |
| 229 | (let* ((size (length seq)) | 229 | (let* ((size (length seq)) |
| 230 | (ring (make-ring size)) | 230 | (ring (make-ring size))) |
| 231 | (count 0)) | 231 | (dotimes (count size) |
| 232 | (while (< count size) | 232 | (when (or (ring-empty-p ring) |
| 233 | (if (or (ring-empty-p ring) | 233 | (not (equal (ring-ref ring 0) (elt seq count)))) |
| 234 | (not (equal (ring-ref ring 0) (elt seq count)))) | 234 | (ring-insert-at-beginning ring (elt seq count)))) |
| 235 | (ring-insert-at-beginning ring (elt seq count))) | ||
| 236 | (setq count (1+ count))) | ||
| 237 | ring))) | 235 | ring))) |
| 238 | 236 | ||
| 239 | ;;; provide ourself: | 237 | ;;; provide ourself: |