-
-
Save alphapapa/7dd3849d6a519fa4870cbe6ebb482cd6 to your computer and use it in GitHub Desktop.
GitHub links for org mode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Original by Stefan van der Walt at <https://gist.github.com/stefanv/7984e8594ce0cfa00b6587f537ae3968> | |
;; Updated slightly to be more "packageable." | |
;; Allow org to understand github links | |
;; | |
;; Examples: | |
;; | |
;; gh:scikit-image/scikit-image#1000 | |
;; | |
;; By customizing org-link-github-shortcuts, you can have even shorter URLs: | |
;; | |
;; gh:skimage#1000 | |
;; | |
;; It should be an association list of shortcut-repo: | |
;; | |
;; (setq org-link-github-shortcuts | |
;; '(("skimage" . "scikit-image/scikit-image") | |
;; ("numpy" . "numpy/numpy"))) | |
(require 'map) | |
(require 'ol) | |
(defgroup org-link-github nil | |
"Easy links to GitHub repositories." | |
:group 'ol) | |
(defcustom org-link-github-shortcuts nil | |
"Shortcuts to org/repo recognized by org gh links." | |
:group 'org-link | |
:type '(alist :key-type (string :tag "Short form") | |
:value-type (string :tag "Full form" :doc "e.g. USERNAME/REPOSITORY"))) | |
(defun org-link-github-expand-target (target) | |
"Return full URL to issue on GitHub based on TARGET. | |
TARGET is a shortcut found in `org-link-github-shortcuts' | |
followed by a \"#\" and an issue number." | |
(if (string-match (rx (group (one-or-more any)) "/" (group (one-or-more any)) | |
"#" (group (one-or-more digit))) | |
target) | |
;; Target has both org and repo. | |
(let ((org (match-string 1 target)) | |
(repo (match-string 2 target)) | |
(nr (match-string 3 target))) | |
(format "https://github.com/%s/%s/pull/%s" org repo nr)) | |
;; Target uses a shortcut. | |
(unless (string-match (rx (group (one-or-more any)) "#" (group (one-or-more digit))) target) | |
(error "Invalid target format")) | |
(pcase-let* ((repo-shortcut (match-string 1 target)) | |
(nr (match-string 2 target)) | |
(org-repo (or (map-elt org-link-github-shortcuts repo-shortcut) | |
(error "Could not find repo %s in org-link-github-shortcuts" repo-shortcut))) | |
(`(,org ,repo) (split-string org-repo "/"))) | |
(format "https://github.com/%s/%s/pull/%s" org repo nr)))) | |
(defun org-link-github-open (target _) | |
"Open GitHub link to PR or issue. | |
TARGET is of the format org/repo#issue-or-pr" | |
(browse-url (org-link-expand-target target))) | |
(defun org-link-github-export (link description format _) | |
"Return a GitHub link for exporting according to LINK, DESCRIPTION, and FORMAT." | |
(let ((path (org-link-expand-target link)) | |
(desc (or description link))) | |
(pcase format | |
(`html (format "<a target=\"_blank\" href=\"%s\">%s</a>" path desc)) | |
(`latex (format "\\href{%s}{%s}" path desc)) | |
(`texinfo (format "@uref{%s,%s}" path desc)) | |
(`ascii (format "%s (%s)" desc path)) | |
(t path)))) | |
(org-link-set-parameters "gh" | |
:follow #'org-link-github-open | |
:export #'org-link-github-export) | |
(provide 'org-link-github) | |
;; | |
(use-package org-link-github | |
:custom (org-link-github-shortcuts '(("skimage" . "scikit-image/scikit-image") | |
("numpy" . "numpy/numpy")))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Seems to work. e.g.