Revert recent add-to-ordered-list changes

* doc/lispref/lists.texi (List Variables): Revert.

* lisp/subr.el (add-to-ordered-list): Revert recent changes
because the semantics are too muddled.
This commit is contained in:
Lars Ingebrigtsen 2021-01-01 05:48:02 +01:00
parent 58bdfd7c54
commit d22e74795d
3 changed files with 18 additions and 49 deletions

View file

@ -807,14 +807,13 @@ foo ;; @r{@code{foo} was changed.}
(setq @var{var} (cons @var{value} @var{var})))
@end example
@defun add-to-ordered-list symbol element &optional order test-function
@defun add-to-ordered-list symbol element &optional order
This function sets the variable @var{symbol} by inserting
@var{element} into the old value, which must be a list, at the
position specified by @var{order}. If @var{element} is already a
member of the list, its position in the list is adjusted according to
@var{order}. Membership is tested using @var{test-function},
defaulting to @code{eq} if @var{test-function} isn't present. This
function returns the resulting list, whether updated or not.
member of the list, its position in the list is adjusted according
to @var{order}. Membership is tested using @code{eq}.
This function returns the resulting list, whether updated or not.
The @var{order} is typically a number (integer or float), and the
elements of the list are sorted in non-decreasing numerical order.

View file

@ -1971,10 +1971,9 @@ can do the job."
(cons element (symbol-value list-var))))))
(defun add-to-ordered-list (list-var element &optional order test-function)
(defun add-to-ordered-list (list-var element &optional order)
"Add ELEMENT to the value of LIST-VAR if it isn't there yet.
TEST-FUNCTION is used to test for the presence of ELEMENT, and
defaults to `eq'.
The test for presence of ELEMENT is done with `eq'.
The value of LIST-VAR is kept ordered based on the ORDER
parameter.
@ -1990,30 +1989,21 @@ The list order for each element is stored in LIST-VAR's
LIST-VAR cannot refer to a lexical variable.
The return value is the new value of LIST-VAR."
(let ((ordering (get list-var 'list-order))
missing)
;; Make a hash table for storing the ordering.
(let ((ordering (get list-var 'list-order)))
(unless ordering
(put list-var 'list-order
(setq ordering (make-hash-table :weakness 'key
:test (or test-function #'eq)))))
(when (and test-function
(not (eq test-function (hash-table-test ordering))))
(error "Conflicting test functions given"))
;; Add new values.
(when (setq missing (eq (gethash element ordering 'missing) 'missing))
(setq ordering (make-hash-table :weakness 'key :test 'eq))))
(when order
(puthash element (and (numberp order) order) ordering))
(unless (memq element (symbol-value list-var))
(set list-var (cons element (symbol-value list-var))))
;; Set/change the order.
(when (or order missing)
(setf (gethash element ordering) (and (numberp order) order)))
(set list-var
(sort (symbol-value list-var)
(lambda (a b)
(let ((oa (gethash a ordering))
(ob (gethash b ordering)))
(if (and oa ob)
(< oa ob)
oa)))))))
(set list-var (sort (symbol-value list-var)
(lambda (a b)
(let ((oa (gethash a ordering))
(ob (gethash b ordering)))
(if (and oa ob)
(< oa ob)
oa)))))))
(defun add-to-history (history-var newelt &optional maxelt keep-all)
"Add NEWELT to the history list stored in the variable HISTORY-VAR.

View file

@ -617,26 +617,6 @@ See https://debbugs.gnu.org/cgi/bugreport.cgi?bug=19350."
(add-to-ordered-list 'subr--ordered 'b 5)
(should (equal subr--ordered '(a c d b e))))
(defvar subr--ordered-s nil)
(ert-deftest subr--add-to-ordered-list-equal ()
(setq subr--ordered-s nil)
(add-to-ordered-list 'subr--ordered-s "b" 2 #'equal)
(should (equal subr--ordered-s '("b")))
(add-to-ordered-list 'subr--ordered-s "c" 3)
(should (equal subr--ordered-s '("b" "c")))
(add-to-ordered-list 'subr--ordered-s "a" 1)
(should (equal subr--ordered-s '("a" "b" "c")))
(add-to-ordered-list 'subr--ordered-s "e")
(should (equal subr--ordered-s '("a" "b" "c" "e")))
(add-to-ordered-list 'subr--ordered-s "d" 4)
(should (equal subr--ordered-s '("a" "b" "c" "d" "e")))
(add-to-ordered-list 'subr--ordered-s "e")
(should (equal subr--ordered-s '("a" "b" "c" "d" "e")))
(add-to-ordered-list 'subr--ordered-s "b" 5)
(should (equal subr--ordered-s '("a" "c" "d" "b" "e")))
(should-error (add-to-ordered-list 'subr--ordered-s "b" 5 #'eql)))
;;; Apropos.