Function: diff-hl-diff-skip-to

diff-hl-diff-skip-to is a natively compiled function defined in diff-hl.el.

Signature

(diff-hl-diff-skip-to LINE &optional FILENAME)

Documentation

In diff-mode, skip to the hunk and line corresponding to LINE in the source file, or the last line of the hunk above it.

When passed FILENAME, ensure that the line is in the section belonging to that file, if it's present.

Source Code

;; Defined in /nix/store/cfz56rl3lp066226i0bmmgiicls3gcq7-emacs-packages-deps/share/emacs/site-lisp/elpa/diff-hl-20261001.135/diff-hl.el
(defun diff-hl-diff-skip-to (line &optional filename)
  "In `diff-mode', skip to the hunk and line corresponding to LINE
in the source file, or the last line of the hunk above it.

When passed FILENAME, ensure that the line is in the section belonging to
that file, if it's present."
  (goto-char (point-min)) ; Counteract any similar behavior in diff-mode.
  (let (found
        (end-pos (point-max)))
    (diff-hunk-next)
    (when filename
      (while (and (not (equal (diff-find-file-name nil t)
                              filename))
                  (not (eobp)))
        (diff-file-next))
      (if (not (eobp))
          ;; Found our file in the changed set. Ensure that we don't go past.
          (setq end-pos (save-excursion
                          (diff-end-of-file)
                          (point)))
        (goto-char (point-min))
        (setq line 0)))
    (while (and (looking-at diff-hunk-header-re-unified)
                (not found)
                (< (point) end-pos))
      (let ((hunk-line (string-to-number (match-string 3)))
            (len (let ((m (match-string 4)))
                   (if m (string-to-number m) 1))))
        (if (> line (+ hunk-line len))
            (diff-hunk-next)
          (setq found t)
          (if (< line hunk-line)
              ;; Retreat to the previous hunk.
              (forward-line -1)
            (let ((to-go (1+ (- line hunk-line))))
              (while (cl-plusp to-go)
                (forward-line 1)
                (unless (looking-at "^[-\\]")
                  (cl-decf to-go))))))))
    (when (> (point) end-pos)
      (diff-hunk-prev)
      (diff-end-of-hunk))))