forked from kidd/org-protocol-github-lines
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorg-protocol-github-lines.el
More file actions
113 lines (94 loc) · 4.07 KB
/
org-protocol-github-lines.el
File metadata and controls
113 lines (94 loc) · 4.07 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
;;; org-protocol-github-lines.el --- Open files and lines in emacs from your browser (when on github)
;; Copyright (C) 2012 Raimon Grau
;; Author: Raimon Grau <raimonster@gmail.com>
;; Keywords: tools, extensions
;; 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/>.
;;; Commentary:
;;
;;; Code:
(require 'org-protocol)
(require 'cl-macs)
(defgroup org-protocol-github nil
"Browser to Emacs interface for GitHub"
:prefix "org-protocol-github-"
:group 'tools)
(defcustom org-protocol-github-projects nil
"Map of GitHub projects to directories.
See also `org-protocol-github-project-directories'."
:group 'org-protocol-github
:type '(repeat (cons (string :tag "GitHub project name (user/project)")
(directory :tag "Project directory"))))
(defcustom org-protocol-github-project-directories (list temporary-file-directory)
"List of directories where projects are stored.
See also `org-protocol-github-projects'."
:group 'org-protocol-github
:type '(repeat directory))
(defun org-protocol-github--find-project-directory (user project)
"Find the github project specified by USER and PROJECT.
If there is no mapping in `org-protocol-github-projects' then
`org-protocol-github-project-directories' is searched for a directory named
after the project."
(let ((key (concat user "/" project)))
(or
(cdr (assoc key org-protocol-github-projects))
(cl-loop for d in org-protocol-github-project-directories
with file-name
do (setq file-name (expand-file-name project
(file-name-as-directory d)))
when (file-exists-p file-name)
return file-name))))
;;;###autoload
(defun org-protocol-github-lines (data)
"Handle github-lines protocol.
DATA contains the user/project/file/line information."
(let* ((content (org-protocol-split-data data t))
(user (car content))
(project (cadr content))
(file (butlast (cddr content)))
(line (car (last content)))
(dir (org-protocol-github--find-project-directory user
project)))
(message "%s" content)
(unless dir
(error "Project %s/%s not found on local machine." user project))
(with-current-buffer
(find-file
(mapconcat 'identity (cons dir file) "/"))
(when line
(goto-char (point-min))
(forward-line (1- (string-to-number line))))))
nil)
(defun org-protocol-github-repo (data)
(let* ((content (org-protocol-split-data data t))
(user (car content))
(project (cadr content))
(dir (org-protocol-github--find-project-directory user
project)))
(if dir
(message "repo %s/%s is already in" user project)
(org-protocol-github--clone user project))
(format "%s%s/" (car org-protocol-github-project-directories) project)))
(defun org-protocol-github--clone (user project)
(let ((default-directory (car org-protocol-github-project-directories)))
(async-shell-command (format "git clone git@github.com:%s/%s.git" user project))))
;;;###autoload
(add-to-list 'org-protocol-protocol-alist
'("Open files from GitHub."
:protocol "github-lines"
:function org-protocol-github-lines))
;;;###autoload
(add-to-list 'org-protocol-protocol-alist
'("Clone repos from GitHub."
:protocol "github-clone"
:function org-protocol-github-repo))
(provide 'org-protocol-github-lines)
;;; org-protocol-github-lines.el ends here