emacs/vc: replace git-link w/ browse-at-remote

git-link is no longer maintained and browse-at-remote has support for
more remotes.

- Bind '<leader> g o h' to open homepage in browser
- Rebind '<leader> g o o' to open file or region (omits the #L<N> suffix
  if no selection is active).
This commit is contained in:
Henrik Lissner 2019-12-15 01:55:02 -05:00
parent 20a733a861
commit bb702acfd0
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
4 changed files with 44 additions and 45 deletions

View file

@ -1,46 +1,41 @@
;;; emacs/vc/autoload/vc.el -*- lexical-binding: t; -*-
;;;###autoload
(defun +vc-git-root-url ()
"Return the root git repo URL for the current file."
(require 'git-link)
(let* ((remote (git-link--select-remote))
(remote-url (git-link--remote-url remote))
(remote-info (if remote-url (git-link--parse-remote remote-url))))
(if remote-info
(format "https://%s/%s" (car remote-info) (cadr remote-info))
(error "Remote `%s' is unknown or contains an unsupported URL" remote))))
(defvar git-link-default-branch)
(defvar git-link-open-in-browser)
(defvar git-link-use-commit)
;;;###autoload
(defun +vc/git-browse-region-or-line (&optional arg)
"Open the website for the current line of this version controlled file.
Uses the currently checked out branch. If prefix ARG, then use 'master' branch.
If an url can't be ascertained, opens the repository's root."
(interactive "P")
(require 'git-link)
(let ((git-link-default-branch (if arg "master" git-link-default-branch))
current-prefix-arg ; don't propagate to `git-link'
git-link-use-commit)
(cl-destructuring-bind (beg end)
(if buffer-file-name (git-link--get-region))
(let ((git-link-open-in-browser t))
(git-link (git-link--select-remote) beg end)))))
(defun +vc/browse-at-remote-file-or-region ()
"Open the current file at remote in your browser.
If a selection is active, highlight them. Otherwise omits the #L<N> suffix in
the URL."
(interactive)
(if (or (use-region-p)
(ignore-errors (evil-visual-state-p)))
(browse-at-remote)
(browse-url (browse-at-remote--file-url (buffer-file-name)))))
;;;###autoload
(defun +vc-update-header-line-a (revision)
"Show revision details in the header-line, instead of the minibuffer.
(defun +vc/browse-at-remote-kill-file-or-region ()
"Copy the current file's remote URL to your clipboard.
If a selection is active, highlight them. Otherwise omits the #L<N> suffix in
the URL."
(interactive)
(if (or (use-region-p)
(ignore-errors (evil-visual-state-p)))
(browse-at-remote-kill)
(kill-new (browse-at-remote--file-url (buffer-file-name)))))
Sometimes I forget `git-timemachine' is enabled in a buffer. Putting revision
info in the `header-line-format' is a good indication."
(let* ((date-relative (nth 3 revision))
(date-full (nth 4 revision))
(author (if git-timemachine-show-author (concat (nth 6 revision) ": ") ""))
(sha-or-subject (if (eq git-timemachine-minibuffer-detail 'commit) (car revision) (nth 5 revision))))
(setq header-line-format
(format "%s%s [%s (%s)]"
(propertize author 'face 'git-timemachine-minibuffer-author-face)
(propertize sha-or-subject 'face 'git-timemachine-minibuffer-detail-face)
date-full date-relative))))
(defun +vc--remote-homepage ()
(or (let ((url (browse-at-remote--remote-ref)))
(cdr (browse-at-remote--get-url-from-remote (car url))))
(user-error "Can't find homepage for current project")))
;;;###autoload
(defun +vc/browse-at-remote-homepage ()
"Open homepage for current project in browser."
(interactive)
(browse-url (+vc--remote-homepage)))
;;;###autoload
(defun +vc/browse-at-remote-kill-homepage ()
"Copy homepage URL of current project to clipboard."
(interactive)
(kill-new (+vc--remote-homepage)))

View file

@ -5,7 +5,7 @@
(package! vc-annotate :built-in t)
(package! smerge-mode :built-in t)
(package! git-link)
(package! browse-at-remote)
(package! git-timemachine)
(package! gitconfig-mode)
(package! gitignore-mode)