Refactor search.el for cleanliness

Remove useless newlines and try to utilise thread-last where possible
to make transformations clearer to the reader.
This commit is contained in:
2025-02-15 21:45:36 +00:00
parent 9a4760068f
commit 36af4e163c

View File

@@ -24,6 +24,9 @@
;;; Code: ;;; Code:
(autoload #'grep "grep")
(autoload #'thread-last "subr-x")
(defvar +search/directories (defvar +search/directories
'("~/Dotfiles/" "~/Text/" "~/.local/src/dwm/" "~/.local/src/dwmblocks/" '("~/Dotfiles/" "~/Text/" "~/.local/src/dwm/" "~/.local/src/dwmblocks/"
"~/.local/src/st/" "~/.local/src/dmenu/" "~/Website/") "~/.local/src/st/" "~/.local/src/dmenu/" "~/Website/")
@@ -33,39 +36,32 @@
"Get files from DIRECTORY using `git ls-files`. "Get files from DIRECTORY using `git ls-files`.
Returns a list of files with the directory preprended to them." Returns a list of files with the directory preprended to them."
(let* ((default-directory directory) (let* ((default-directory directory)
(names (split-string (git-files (shell-command-to-string "git ls-files -z --full-name --"))
(shell-command-to-string "git ls-files -z --full-name --") (names (split-string git-files "\0")))
"\0"))) (mapcar #'(lambda (name) (concat directory name)) names)))
(mapcar #'(lambda (name)
(concat directory name))
names)))
(defun +search/get-all-candidates () (defun +search/get-all-candidates ()
(cl-reduce (thread-last (mapcar #'(lambda (directory) (expand-file-name directory)) +search/directories)
#'(lambda (x y) (append x y)) (mapcar #'(lambda (directory) (+search/get-candidates directory)))
(mapcar #'(lambda (directory) (cl-reduce #'(lambda (x y) (append x y)))))
(+search/get-candidates (expand-file-name directory)))
+search/directories)))
(defun +search/find-file () (defun +search/find-file ()
(interactive) (interactive)
(find-file (completing-read "Find file: " (+search/get-all-candidates) nil t))) (find-file (completing-read "Find file: " (+search/get-all-candidates) nil t)))
(defun +search/-format-grep-candidates () (defun +search/-format-grep-candidates ()
(string-join (thread-last (+search/get-all-candidates)
(mapcar (cl-remove-if #'directory-name-p)
#'(lambda (x) (concat "\"" x "\" ")) (mapcar #'(lambda (x) (concat "\"" x "\" ")))
(cl-remove-if #'directory-name-p (+search/get-all-candidates))))) (string-join)))
(autoload #'grep "grep")
(defun +search/search-all () (defun +search/search-all ()
(interactive) (interactive)
(let ((term (read-string "Search for: " (thing-at-point 'symbol))) (let ((term (read-string "Search for: " (thing-at-point 'symbol)))
(candidates (+search/-format-grep-candidates))) (candidates (+search/-format-grep-candidates)))
(grep (thread-last candidates
(format "grep --color=auto -nIHZe \"%s\" -- %s" (format "grep --color=auto -nIHZe \"%s\" -- %s" term)
term candidates)) (grep))
(next-error))) (next-error)))
(provide 'search) (provide 'search)