Subject: Re: bug#51809: 29.0.50; [PATCH] Support for outline default state in Diff buffers

From: Juri Linkov <[email-masked]>
Date: Sat, 13 Nov 2021 21:29:12 +0200

>>> (...)  Could the above hook be replaced with customization of
>>> diff-outline-default-state?
>
> With the attached patch, Outline minor mode is automatically enabled
> when `diff-outline-default-state' is customized. Tested within "emacs
> -q". That said, I am not a proficient elisper and my use of the
> autoload, declare-function, eval-when-compile machinery may not be
> standard. Sorry for this.

> +(autoload 'outline-minor-mode "outline")

Actually, outline-minor-mode is already autoloaded, so no special
handling is needed for it.

But the problem is that too many outline functions are used in
diff-mode.el in your patch.  This is a clear indication that
some part should be moved to outline.el.

Note that the following function has no diff-specific code, so it could
be refactored and some generalized function added to outline.el.  Such
function could be like `outline-map-region' to accept arguments `beg', `end'
and a predicate function that defines whether to hide or show the body.

> +  (when outline-minor-mode
> +    (save-excursion
> +      (let* (outline-view-change-hook
> +             (beg (progn
> +                    (goto-char (point-min))
> +                    ;; Skip the prelude, if any.
> +                    (unless (outline-on-heading-p t) (outline-next-heading))
> +                    (point)))
> +             (end (progn
> +                    (goto-char (point-max))
> +                    ;; Keep empty last line, if available.
> +                    (if (bolp) (1- (point)) (point)))))
> +	(if (< end beg)
> +            (setq beg (prog1 end (setq end beg))))
> +	;; First hide sublevels
> +	(outline-hide-sublevels 1)
> +	;; Then unhide short subtrees
> +	(outline-map-region
> +	 (lambda ()
> +           (when (= (funcall outline-level) 1)
> +             (goto-char (match-end 0))
> +             (let ((overlays (overlays-at (point))))
> +               (while overlays
> +		 (let ((overlay (car overlays)))
> +		   (progn
> +                     (when (eq (overlay-get overlay 'invisible) 'outline)
> +                       (let ((size (count-lines
> +                                    (overlay-end overlay)
> +                                    (overlay-start overlay))))
> +                         (goto-char (match-beginning 0))
> +			 (if (< size diff-file-outline-threshold)
> +			     (outline-show-subtree)
> +                           (outline-show-branches))))
> +                     (setq overlays (cdr overlays))))))))
> +	 beg end)))))