summaryrefslogtreecommitdiff
path: root/lisp/km-ol-git-link.el
blob: efe5cd801d9d380d0e9e607c73d69b25da25cceb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
;;; km-ol-git-link.el --- Tweaks to ol-git-link

;; Copyright (C) 2020 Kyle Meyer <kyle@kyleam.com>

;; Author: Kyle Meyer <kyle@kyleam.com>
;; URL: https://git.kyleam.com/emacs.d

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Code:

(require 'magit)
(require 'ol-git-link)

(defun km/org-git-store-link ()
  "Like `org-git-store-link', but work from Magit file buffers.
Regular file buffers are left untouched."
  (when-let ((rev magit-buffer-revision)
             (fname magit-buffer-file-name)
             (repo (magit-toplevel)))
    (org-link-store-props
     :type "git"
     :link (concat "git:"
                   (abbreviate-file-name fname)
                   "::" rev
                   "::" (number-to-string (line-number-at-pos)))
     :description
     (format "(%s)%s"
             (file-name-nondirectory
              (directory-file-name (file-name-as-directory repo)))
             (file-relative-name fname repo)))))

(defun km/org-git-open-link (str _)
  "Open a git: link using `magit-find-file-other-window'."
  (pcase-let ((`(,fname ,rev ,line) (org-git-split-string str)))
    (let ((default-directory (file-name-directory fname)))
      (magit-find-file-other-window rev fname))
    (when (string-match-p "\\`[0-9]+\\'" line)
      (save-restriction
        (widen)
        (goto-char (point-min))
        (forward-line (1- (string-to-number line)))))))

(provide 'km-ol-git-link)
;;; km-ol-git-link.el ends here