-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhelm-ghq.el
242 lines (203 loc) · 7.33 KB
/
helm-ghq.el
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
;;; helm-ghq.el --- Ghq with helm interface -*- lexical-binding: t; -*-
;; Copyright (C) 2015 by Takashi Masuda
;; Author: Takashi Masuda <masutaka.net@gmail.com>
;; URL: https://github.com/masutaka/emacs-helm-ghq
;; Version: 1.8.1
;; Package-Requires: ((emacs "24") (helm "3.8.0"))
;; 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:
;; helm-ghq.el provides a helm interface to "ghq".
;;; Code:
(require 'helm)
(require 'helm-mode)
(require 'helm-files)
(defgroup helm-ghq nil
"ghq with helm interface"
:prefix "helm-ghq-"
:group 'helm)
(defcustom helm-ghq-command-ghq
"ghq"
"*A ghq command"
:type 'string
:group 'helm-ghq)
(defcustom helm-ghq-command-ghq-arg-root
'("root")
"*Arguments for getting ghq root path using ghq command"
:type '(repeqt string)
:group 'helm-ghq)
(defcustom helm-ghq-command-ghq-arg-list
'("list" "--full-path")
"*Arguments for getting ghq list"
:type '(repeqt string)
:group 'helm-ghq)
(defcustom helm-ghq-command-ghq-arg-update-repo
'("get" "-u")
"*Arguments for updating a repository"
:type '(repeqt string)
:group 'helm-ghq)
(defcustom helm-ghq-command-git
"git"
"*A git command"
:type 'string
:group 'helm-ghq)
(defcustom helm-ghq-command-git-arg-root
'("config" "ghq.root")
"*Arguments for getting ghq root path using git command"
:type '(repeqt string)
:group 'helm-ghq)
(defcustom helm-ghq-command-git-arg-ls-files
'("ls-files")
"*Arguments for getting file list in git repository"
:type '(repeqt string)
:group 'helm-ghq)
(defcustom helm-ghq-command-hg
"hg"
"*A hg command"
:type 'string
:group 'helm-ghq)
(defcustom helm-ghq-command-hg-arg-ls-files
'("manifest")
"*Arguments for getting file list in hg repository"
:type '(repeqt string)
:group 'helm-ghq)
(defcustom helm-ghq-command-svn
"svn"
"*A svn command"
:type 'string
:group 'helm-ghq)
(defcustom helm-ghq-command-svn-arg-ls-files
'("list" "--recursive")
"*Arguments for getting files (and directories) list in svn repository"
:type '(repeqt string)
:group 'helm-ghq)
(defun helm-ghq--open-dired (file)
(dired (file-name-directory file)))
(defvar helm-ghq--action
'(("Open File" . find-file)
("Open File other window" . find-file-other-window)
("Open File other frame" . find-file-other-frame)
("Open Directory" . helm-ghq--open-dired)))
(define-obsolete-variable-alias 'helm-source-ghq
'helm-ghq-source "1.8.0")
(defvar helm-ghq-source
(helm-build-sync-source "ghq"
:candidates #'helm-ghq--list-candidates
:match #'helm-ghq--files-match-only-basename
:filter-one-by-one #'helm-ghq--filter-one-by-one
:keymap helm-generic-files-map
:help-message helm-generic-file-help-message
:action helm-ghq--action)
"Helm source for ghq.")
(defun helm-ghq--files-match-only-basename (candidate)
"Allow matching only basename of file when \" -b\" is added at end of pattern.
If pattern contain one or more spaces, fallback to match-plugin
even is \" -b\" is specified."
(let ((source (helm-get-current-source)))
(if (string-match "\\([^ ]*\\) -b\\'" helm-pattern)
(progn
(helm-set-attr 'no-matchplugin nil source)
(string-match (match-string 1 helm-pattern)
(helm-basename candidate)))
;; Disable no-matchplugin by side effect.
(helm-aif (assq 'no-matchplugin source)
(setq source (delete it source)))
(string-match
(replace-regexp-in-string " -b\\'" "" helm-pattern)
candidate))))
(defun helm-ghq--filter-one-by-one (candidate)
(if helm-ff-transformer-show-only-basename
(cons (helm-basename candidate) candidate)
candidate))
(defmacro helm-ghq--line-string ()
`(buffer-substring-no-properties
(line-beginning-position) (line-end-position)))
(defun helm-ghq--root-fallback ()
(erase-buffer)
(unless (zerop (apply #'process-file
helm-ghq-command-git nil t nil
helm-ghq-command-git-arg-root))
(error "Failed: Can't find ghq root"))
(goto-char (point-min))
(expand-file-name (helm-ghq--line-string)))
(defun helm-ghq--root ()
(with-temp-buffer
(apply #'process-file
helm-ghq-command-ghq nil t nil
helm-ghq-command-ghq-arg-root)
(goto-char (point-min))
(let ((output (helm-ghq--line-string)))
(if (string-match-p "\\`No help topic" output)
(helm-ghq--root-fallback)
(expand-file-name output)))))
(defun helm-ghq--list-candidates ()
(with-temp-buffer
(unless (zerop (apply #'call-process
helm-ghq-command-ghq nil t nil
helm-ghq-command-ghq-arg-list))
(error "Failed: Can't get ghq list candidates"))
(let ((paths))
(goto-char (point-min))
(while (not (eobp))
(push (helm-ghq--line-string) paths)
(forward-line 1))
(reverse paths))))
(defun helm-ghq--ls-files ()
(with-current-buffer (helm-candidate-buffer 'global)
(unless (or (zerop (apply #'call-process
helm-ghq-command-git nil '(t nil) nil
helm-ghq-command-git-arg-ls-files))
(zerop (apply #'call-process
helm-ghq-command-svn nil '(t nil) nil
helm-ghq-command-svn-arg-ls-files))
(zerop (apply #'call-process
helm-ghq-command-hg nil t nil
helm-ghq-command-hg-arg-ls-files)))
(error "Failed: Can't get file list candidates"))))
(defun helm-ghq--source (repo)
(let ((name (file-name-nondirectory (directory-file-name repo))))
(helm-build-in-buffer-source name
:init #'helm-ghq--ls-files
:action helm-ghq--action)))
(defun helm-ghq--repo-to-user-project (repo)
(cond ((string-match "github.com/\\(.+\\)" repo)
(match-string-no-properties 1 repo))
((string-match "code.google.com/\\(.+\\)" repo)
(match-string-no-properties 1 repo))))
(defsubst helm-ghq--concat-as-command (args)
(mapconcat 'identity args " "))
(defun helm-ghq--update-repository (repo)
(let* ((user-project (helm-ghq--repo-to-user-project repo))
(command (helm-ghq--concat-as-command
(list
helm-ghq-command-ghq
(helm-ghq--concat-as-command
helm-ghq-command-ghq-arg-update-repo)
user-project))))
(async-shell-command command)))
(defun helm-ghq--source-update (repo)
(helm-build-sync-source "Update Repository"
:candidates '(" ") ; dummy
:action (lambda (_c)
(helm-ghq--update-repository repo))))
;;;###autoload
(defun helm-ghq ()
(interactive)
(let ((repo (helm-comp-read "ghq-list: "
(helm-ghq--list-candidates)
:name "ghq list"
:must-match t)))
(let ((default-directory (file-name-as-directory repo)))
(helm :sources (list (helm-ghq--source default-directory)
(helm-ghq--source-update repo))
:buffer "*helm-ghq-list*"))))
(provide 'helm-ghq)
;;; helm-ghq.el ends here