-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaoni-search.el
52 lines (46 loc) · 1.93 KB
/
yaoni-search.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
;; -*- lexical-binding: t; -*-
(require 's)
(require 'counsel)
(defun yaoni/get-current-dir ()
"Get the current directory in absolute path."
(let ((cur-dir
(condition-case nil
(string-join
(butlast (split-string (buffer-file-name) "/")) "/")
;; remove the trailing / from (dired-current-directory)
;; so the result matches result from above code
(error (substring (dired-current-directory)
0
(-
(length (dired-current-directory)) 1))))))
cur-dir))
(defun yaoni/search-current-folder ()
"Search for string in the current folder."
(interactive)
(let ((cur-dir (yaoni/get-current-dir)))
(message "cur-dir=%s" cur-dir)
(if (vc-backend (buffer-file-name))
(counsel-git-grep nil cur-dir nil "git grep cur dir: ")
(counsel-rg nil cur-dir nil "grep cur dir:"))))
(defun filter-pred (prefix str)
(s-starts-with? prefix str))
(defun yaoni/get-cands ()
"Get the candidates for yaoni/search-current-folder-file."
(interactive)
;; we remove the (counsel-locate-git-root) from the string
;; to make it shorter as some projects will have deeply
;; nested folder structure
(mapcar (lambda (str) (string-join (split-string str (counsel-locate-git-root))))
(-filter (apply-partially 'filter-pred (yaoni/get-current-dir))
(mapcar (lambda (str) (concat (counsel-locate-git-root) str))
(split-string
(shell-command-to-string counsel-git-cmd)
"\0"
t)))))
(defun yaoni/search-current-folder-file ()
(interactive)
(ivy-read "Find file cur dir: " (yaoni/get-cands)
:action (lambda (file) (find-file (concat (counsel-locate-git-root) file)))
:history 'counsel-git-history
:caller 'counsel-git))
(provide 'yaoni-search)