2

I'd like to discard modified hunks (and ideally, view the diff before) basing on the current buffer position.

I have diff-hl so that it's quite obvious which part of a buffer are modified. Now, how to view the diff for the current hunk and/or discard it?

One way I though this might work is some way to jump to magit's view, moving to the selected hunk - this would show the diff, and then k would discard it. I was also looking at vc-mode, but didn't find anything that would fit that use-case.

2 Answers 2

3
  • diff-hl: diff-hl-revert-hunk
  • git-gutter: git-gutter:revert-hunk
0
0

Something like this ?

(defun vc-diff-current-hunk ()
  "Find the hunk at point in a `vc-diff' buffer and narrow it there."
  (interactive)
  (let ((line (line-number-at-pos))
        found)
    (vc-diff)
    (widen)
    (goto-char 1)
    (while (and (not found)
                (re-search-forward
                 "^@@ -[0-9]+,[0-9]+ \\+\\([0-9]+\\),\\([0-9]+\\) @@" nil t))
      (let ((hunk-line (string-to-number (match-string 1)))
            (hunk-count (string-to-number (match-string 2))))
        (setq found (and (>= line hunk-line)
                         (< line (+ hunk-line hunk-count))))))
    (when found
      (save-excursion
        (narrow-to-region
         (point)
         (progn
           (if (ignore-errors (diff-hunk-next) t)
               (point)
             (point-max)))))
      (message "Hunk found"))
    (unless found
      (error "Hunk not found"))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.