annotate lisp/emacs-lisp/ring.el @ 814:38b2499cb3e9

*** empty log message ***
author Eric S. Raymond <esr@snark.thyrsus.com>
date Fri, 17 Jul 1992 20:24:00 +0000
parents e694e0879463
children 20674ae6bf52
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
658
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
1 ;;; ring.el --- handle rings of marks
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
2
811
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 658
diff changeset
3 ;; Maintainer: FSF
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 658
diff changeset
4 ;; Last-Modified: 22 Apr 1991
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 658
diff changeset
5 ;; Keywords: extensions
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 658
diff changeset
6
658
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
7 ;; Copyright (C) 1992 Free Software Foundation, Inc.
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
8
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
9 ;; This file is part of GNU Emacs.
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
10
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
12 ;; it under the terms of the GNU General Public License as published by
811
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 658
diff changeset
13 ;; the Free Software Foundation; either version 2, or (at your option)
658
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
14 ;; any later version.
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
15
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
16 ;; GNU Emacs is distributed in the hope that it will be useful,
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
19 ;; GNU General Public License for more details.
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
20
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
21 ;; You should have received a copy of the GNU General Public License
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
24
811
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 658
diff changeset
25 ;;; Commentary:
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 658
diff changeset
26
124
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
27 ;;; This code defines a ring data structure. A ring is a
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
28 ;;; (hd-index tl-index . vector)
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
29 ;;; list. You can insert to, remove from, and rotate a ring. When the ring
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
30 ;;; fills up, insertions cause the oldest elts to be quietly dropped.
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
31 ;;;
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
32 ;;; HEAD = index of the newest item on the ring.
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
33 ;;; TAIL = index of the oldest item on the ring.
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
34 ;;;
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
35 ;;; These functions are used by the input history mechanism, but they can
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
36 ;;; be used for other purposes as well.
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
37
811
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 658
diff changeset
38 ;;; Code:
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 658
diff changeset
39
124
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
40 (provide 'history)
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
41
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
42 (defun ring-p (x)
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
43 "T if X is a ring; NIL otherwise."
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
44 (and (consp x) (integerp (car x))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
45 (consp (cdr x)) (integerp (car (cdr x)))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
46 (vectorp (cdr (cdr x)))))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
47
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
48 (defun make-ring (size)
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
49 "Make a ring that can contain SIZE elts"
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
50 (cons 1 (cons 0 (make-vector (+ size 1) nil))))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
51
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
52 (defun ring-plus1 (index veclen)
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
53 "INDEX+1, with wraparound"
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
54 (let ((new-index (+ index 1)))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
55 (if (= new-index veclen) 0 new-index)))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
56
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
57 (defun ring-minus1 (index veclen)
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
58 "INDEX-1, with wraparound"
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
59 (- (if (= 0 index) veclen index) 1))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
60
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
61 (defun ring-length (ring)
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
62 "Number of elts in the ring."
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
63 (let ((hd (car ring)) (tl (car (cdr ring))) (siz (length (cdr (cdr ring)))))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
64 (let ((len (if (<= hd tl) (+ 1 (- tl hd)) (+ 1 tl (- siz hd)))))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
65 (if (= len siz) 0 len))))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
66
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
67 (defun ring-empty-p (ring)
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
68 (= 0 (ring-length ring)))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
69
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
70 (defun ring-insert (ring item)
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
71 "Insert a new item onto the ring. If the ring is full, dump the oldest
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
72 item to make room."
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
73 (let* ((vec (cdr (cdr ring))) (len (length vec))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
74 (new-hd (ring-minus1 (car ring) len)))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
75 (setcar ring new-hd)
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
76 (aset vec new-hd item)
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
77 (if (ring-empty-p ring) ;overflow -- dump one off the tail.
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
78 (setcar (cdr ring) (ring-minus1 (car (cdr ring)) len)))))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
79
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
80 (defun ring-remove (ring)
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
81 "Remove the oldest item retained on the ring."
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
82 (if (ring-empty-p ring) (error "Ring empty")
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
83 (let ((tl (car (cdr ring))) (vec (cdr (cdr ring))))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
84 (set-car (cdr ring) (ring-minus1 tl (length vec)))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
85 (aref vec tl))))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
86
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
87 ;;; This isn't actually used in this package. I just threw it in in case
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
88 ;;; someone else wanted it. If you want rotating-ring behavior on your history
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
89 ;;; retrieval (analagous to kill ring behavior), this function is what you
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
90 ;;; need. I should write the yank-input and yank-pop-input-or-kill to go with
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
91 ;;; this, and not bind it to a key by default, so it would be available to
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
92 ;;; people who want to bind it to a key. But who would want it? Blech.
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
93 (defun ring-rotate (ring n)
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
94 (if (not (= n 0))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
95 (if (ring-empty-p ring) ;Is this the right error check?
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
96 (error "ring empty")
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
97 (let ((hd (car ring)) (tl (car (cdr ring))) (vec (cdr (cdr ring))))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
98 (let ((len (length vec)))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
99 (while (> n 0)
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
100 (setq tl (ring-plus1 tl len))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
101 (aset ring tl (aref ring hd))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
102 (setq hd (ring-plus1 hd len))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
103 (setq n (- n 1)))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
104 (while (< n 0)
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
105 (setq hd (ring-minus1 hd len))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
106 (aset vec hd (aref vec tl))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
107 (setq tl (ring-minus1 tl len))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
108 (setq n (- n 1))))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
109 (set-car ring hd)
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
110 (set-car (cdr ring) tl)))))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
111
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
112 (defun comint-mod (n m)
245
b8930c9c15ee *** empty log message ***
Brian Preble <rassilon@gnu.org>
parents: 124
diff changeset
113 "Returns N mod M. M is positive.
b8930c9c15ee *** empty log message ***
Brian Preble <rassilon@gnu.org>
parents: 124
diff changeset
114 Answer is guaranteed to be non-negative, and less than m."
124
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
115 (let ((n (% n m)))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
116 (if (>= n 0) n
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
117 (+ n
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
118 (if (>= m 0) m (- m)))))) ; (abs m)
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
119
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
120 (defun ring-ref (ring index)
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
121 (let ((numelts (ring-length ring)))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
122 (if (= numelts 0) (error "indexed empty ring")
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
123 (let* ((hd (car ring)) (tl (car (cdr ring))) (vec (cdr (cdr ring)))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
124 (index (comint-mod index numelts))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
125 (vec-index (comint-mod (+ index hd)
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
126 (length vec))))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
127 (aref vec vec-index)))))
658
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
128
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
129 ;;; ring.el ends here