960 |
|
|
961 |
(defun add-to-ordered-list (list-var element &optional order) |
(defun add-to-ordered-list (list-var element &optional order) |
962 |
"Add to the value of LIST-VAR the element ELEMENT if it isn't there yet. |
"Add to the value of LIST-VAR the element ELEMENT if it isn't there yet. |
963 |
The test for presence of ELEMENT is done with `equal'. |
The test for presence of ELEMENT is done with `eq'. |
964 |
|
|
965 |
The resulting list is reordered so that the elements are in the |
The resulting list is reordered so that the elements are in the |
966 |
order given by each element's numeric list order. |
order given by each element's numeric list order. Elements |
967 |
Elements without a numeric list order are placed at the end of |
without a numeric list order are placed at the end of the list. |
|
the list. |
|
968 |
|
|
969 |
If the third optional argument ORDER is non-nil, set the |
If the third optional argument ORDER is non-nil, set the |
970 |
element's list order to the given value. |
element's list order to the given value. |
978 |
(put list-var 'list-order |
(put list-var 'list-order |
979 |
(setq ordering (make-hash-table :weakness 'key :test 'eq)))) |
(setq ordering (make-hash-table :weakness 'key :test 'eq)))) |
980 |
(when order |
(when order |
981 |
(puthash element order ordering)) |
(puthash element (and (numberp order) order) ordering)) |
982 |
(add-to-list list-var element) |
(unless (memq element (symbol-value list-var)) |
983 |
|
(set list-var (cons element (symbol-value list-var)))) |
984 |
(set list-var (sort (symbol-value list-var) |
(set list-var (sort (symbol-value list-var) |
985 |
(lambda (a b) |
(lambda (a b) |
986 |
(let ((oa (gethash a ordering)) |
(let ((oa (gethash a ordering)) |
987 |
(ob (gethash b ordering))) |
(ob (gethash b ordering))) |
988 |
(cond |
(if (and oa ob) |
989 |
((not oa) nil) |
(< oa ob) |
990 |
((not ob) t) |
oa))))))) |
|
(t (< oa ob))))))))) |
|
991 |
|
|
992 |
|
|
993 |
;;; Load history |
;;; Load history |