Subject: 31.0.50; [Feature Request][VC] How can I get the current revision of the buffer opened by =vc-find-revision=?
From: jixiuf <[email-masked]>
To: [email-masked]
Date: Fri, 04 Apr 2025 12:09:47 +0800
List-Id: <debbugs-submit.debbugs.gnu.org>

How can I retrieve the current revision when I'm in a buffer opened by
=vc-find-revision= ?

I'm attempting to write two commands: =vc-next-revision= and
=vc-prev-revision=, similar to =magit-blob-next= and
=magit-blob-previous=.

Currently, I obtain the revision from the buffer name (which might
support Git), but I don't consider this to be the best approach. Should
we add a local variable called =vc-revision=, similar to
=vc-parent-buffer=?

Alternatively, should Emacs implement =vc-next-revision= and
=vc-prev-revision= directly in =vc.el=?

----------------------------------

;;;###autoload
(defun vc-next-revision ()
  "Visit the next revision which modified the current file."
  (interactive)
  (let* ((buffname (buffer-name))
         (prev-buffer (current-buffer))
         (parent-buffer vc-parent-buffer)
         (fileset-arg (vc-deduce-fileset nil t))
         (backend (car fileset-arg))
         (filename (car (cadr fileset-arg)))
         rev next)
    (when parent-buffer
      ;; FIXME: support other bankend for find rev?
      (when (string-match "^\\([^~]+?\\)\\(?:\\.~\\([^~]+\\)~\\)?$" buffname)
        (setq rev (match-string 2 buffname)))
      (setq next (vc-call-backend backend 'next-revision
                                  filename rev))
      (kill-buffer prev-buffer)
      (if next
          (switch-to-buffer (vc-find-revision filename next))
        (find-file filename)
        (user-error "vc timemachine: You have reached the end of time")))))
;;;###autoload
(defun vct-prev-revision ()
  "Visit the prev revision which modified the current file."
  (interactive)
  (let* ((buffname (buffer-name))
         (cur-buffer (current-buffer))
         (parent-buffer vc-parent-buffer)
         (fileset-arg (vc-deduce-fileset nil t))
         (backend (car fileset-arg))
         (filename (car (cadr fileset-arg)))
         rev prev)
    ;; FIXME: support other bankend for find rev?
    (when (string-match "^\\([^~]+?\\)\\(?:\\.~\\([^~]+\\)~\\)?$" buffname)
      (setq rev (match-string 2 buffname)))
    (if rev
        (setq prev (vc-call-backend backend 'previous-revision
                                    filename rev))
      (setq prev (vc-short-revision filename)))
    (if prev
        (progn  (switch-to-buffer (vc-find-revision filename prev))
                (when parent-buffer (kill-buffer cur-buffer)))
      (user-error "vc timemachine: You have reached the beginning of time"))))