aboutsummaryrefslogtreecommitdiff
path: root/Emacs/.config/emacs/elisp/search.el
blob: 4b0097c7827b1ad4dfec746b1519e5b7730150cc (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
;;; search.el --- Search a list of git directories at once!  -*- lexical-binding: t; -*-

;; Copyright (C) 2024  Aryadev Chavali

;; Author: Aryadev Chavali <aryadev@aryadevchavali.com>
;; Keywords:

;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License Version
;; 2 as published by the Free Software Foundation.

;; 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:

;; Given a list of git source directories, provide a completing-read
;; interface which will narrow and give you a file.

;;; Code:

(autoload #'swiper "swiper")

(defvar +search/directories
  '("~/Dotfiles/" "~/Text/" "~/.local/src/dwm/" "~/.local/src/dwmblocks/" "~/.local/src/st/" "~/Website/")
  "List of directories to get candidates from.")

(defun +search/get-candidates (directory)
  "Get files from DIRECTORY using counsel-git-cands.
Returns a list of files with the directory preprended to them."
  (let* ((default-directory directory)
         (names (split-string
                 (shell-command-to-string "git ls-files -z --full-name --")
                 "\0")))
    (mapcar #'(lambda (name)
           (concat directory name))
       names)))

(defun +search/get-all-candidates ()
  (cl-reduce
   #'(lambda (x y) (append x y))
   (mapcar #'(lambda (directory)
          (+search/get-candidates (expand-file-name directory)))
      +search/directories)))

(defun +search/find-file (&optional arg)
  (interactive "P")
  (let ((file-name (completing-read "Find file: " (+search/get-all-candidates) nil t)))
    (with-current-buffer (find-file file-name)
      (if arg
          (swiper)))))

(defun +search/-format-grep-candidates ()
  (string-join
   (mapcar
    #'(lambda (x) (concat "\"" x "\""))
    (cl-remove-if #'directory-name-p (+search/get-all-candidates)))))

(defun +search/search-all ()
  (interactive)
  (let ((format-str "grep --color=auto -nIH --null -e \"%s\" -- %s")
        (term (read-string "Search for: "))
        (candidates (+search/-format-grep-candidates)))
    (grep (format format-str term candidates))))

(provide 'search)
;;; search.el ends here