-
Notifications
You must be signed in to change notification settings - Fork 2
/
consult-ghq.el
112 lines (92 loc) · 3.64 KB
/
consult-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
;;; consult-ghq.el --- Ghq interface using consult -*- lexical-binding: t; -*-
;; Copyright (C) 2021 Tomoya Otake
;; Author: Tomoya Otake <[email protected]>
;; Version: 0.0.5
;; Homepage: https://github.com/tomoya/consult-ghq
;; Keywords: convenience, usability, consult, ghq
;; Package-Requires: ((emacs "26.1") (consult "0.8"))
;; License: GPL-3.0-or-later
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This packaage provides ghq interface using Consult.
;;
;; Its main entry points are the commands `consult-ghq-find' and
;; `consult-ghq-grep`. Default find-function is affe-find, if it's
;; installed, otherwise consult-find. If you want to use consult-find
;; despite having affe installed, you can change like below:
;;
;; (setq consult-ghq-find-function #'consult-find)
;;; Code:
(require 'consult)
(defgroup consult-ghq nil
"Ghq interface using consult."
:prefix "consult-ghq-"
:group 'consult)
(defcustom consult-ghq-command
"ghq"
"*A ghq command."
:type 'string
:group 'consult-ghq)
(defcustom consult-ghq-find-function (if (fboundp 'affe-find)
#'affe-find
#'consult-find)
"Find function that find files after selected repo."
:type 'function
:group 'consult-ghq)
(defcustom consult-ghq-grep-function (if (fboundp 'affe-grep)
#'affe-grep
#'consult-grep)
"Grep function that find files after selected repo."
:type 'function
:group 'consult-ghq)
(defcustom consult-ghq-switch-project-function #'project-switch-project
"Switch project function that opens a project in that project manager."
:type 'function
:group 'consult-ghq)
(defun consult-ghq--list-candidates ()
"Return ghq list candidate."
(with-temp-buffer
(unless (zerop (apply #'call-process
consult-ghq-command nil t nil
'("list" "--full-path")))
(error "Failed: Cannot get ghq list candidates"))
(let ((paths))
(goto-char (point-min))
(while (not (eobp))
(push
(buffer-substring-no-properties
(line-beginning-position) (line-end-position))
paths)
(forward-line 1))
(nreverse paths))))
;;;###autoload
(defun consult-ghq-find ()
"Find file from selected repo using ghq."
(interactive)
(let* ((repo (consult--read (consult-ghq--list-candidates) :prompt "Repo: "))
(default-directory repo))
(funcall consult-ghq-find-function repo)))
;;;###autoload
(defun consult-ghq-grep ()
"Grep from selected repo using ghq."
(interactive)
(let* ((repo (consult--read (consult-ghq--list-candidates) :prompt "Repo: "))
(default-directory repo))
(funcall consult-ghq-grep-function repo)))
;;;###autoload
(defun consult-ghq-switch-project ()
"Switch project from ghq."
(interactive)
(let ((repo (consult--read (consult-ghq--list-candidates) :prompt "Repo: ")))
(funcall consult-ghq-switch-project-function repo)))
(provide 'consult-ghq)
;;; consult-ghq.el ends here