Function: outline-head-from-level
outline-head-from-level is a byte-compiled function defined in
outline.el.gz.
Signature
(outline-head-from-level LEVEL HEAD &optional ALIST)
Documentation
Get new heading with level LEVEL, closest to HEAD, from ALIST.
If there are no such entries, return nil.
ALIST defaults to outline-heading-alist.
Similar to (car (rassoc LEVEL ALIST)).
If there are several different entries with same new level, choose the
one with the smallest distance to the association of HEAD in the alist.
This makes it possible for promotion to work in modes with several
independent sets of headings (numbered, unnumbered, appendix...).
Source Code
;; Defined in /usr/src/emacs/lisp/outline.el.gz
(defun outline-head-from-level (level head &optional alist)
"Get new heading with level LEVEL, closest to HEAD, from ALIST.
If there are no such entries, return nil.
ALIST defaults to `outline-heading-alist'.
Similar to (car (rassoc LEVEL ALIST)).
If there are several different entries with same new level, choose the
one with the smallest distance to the association of HEAD in the alist.
This makes it possible for promotion to work in modes with several
independent sets of headings (numbered, unnumbered, appendix...)."
(unless alist (setq alist outline-heading-alist))
(let ((l (rassoc level alist))
ll h hl l2 l2l)
(cond
((null l) nil)
;; If there's no HEAD after L, any other entry for LEVEL after L
;; can't be much better than L.
((null (setq h (assoc head (setq ll (memq l alist))))) (car l))
;; If there's no other entry for LEVEL, just keep L.
((null (setq l2 (rassoc level (cdr ll)))) (car l))
;; Now we have L, L2, and H: see if L2 seems better than L.
;; If H is after L2, L2 is better.
((memq h (setq l2l (memq l2 (cdr ll))))
(outline-head-from-level level head l2l))
;; Now we have H between L and L2.
;; If there's a separator between L and H, prefer L2.
((memq h (memq nil ll))
(outline-head-from-level level head l2l))
;; If there's a separator between L2 and H, prefer L.
((memq l2 (memq nil (setq hl (memq h ll)))) (car l))
;; No separator between L and L2, check the distance.
((< (* 2 (length hl)) (+ (length ll) (length l2l)))
(outline-head-from-level level head l2l))
;; If all else fails, just keep L.
(t (car l)))))