2023-11-05 21:18:23 +00:00
|
|
|
|
;;; quick-find-files.el --- Quickly find files in directories and by extension -*- lexical-binding: t -*-
|
2021-05-02 15:16:07 +00:00
|
|
|
|
|
|
|
|
|
;; Author: Lucien Cartier-Tilet <lucien@phundrak.com>
|
|
|
|
|
;; Maintainer: Lucien Cartier-Tilet <lucien@phundrak.com>
|
2023-12-29 15:16:14 +00:00
|
|
|
|
;; Version: 0.3.0
|
2023-12-06 18:49:59 +00:00
|
|
|
|
;; Package-Requires: ((emacs "26"))
|
2023-11-05 21:18:23 +00:00
|
|
|
|
;; Homepage: https://labs.phundrak.com/phundrak/quick-find-files.el
|
2023-12-06 18:49:59 +00:00
|
|
|
|
;; Keywords: convenience
|
2021-05-02 15:16:07 +00:00
|
|
|
|
|
|
|
|
|
;; This file is not part of GNU Emacs
|
|
|
|
|
|
|
|
|
|
;; 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:
|
|
|
|
|
|
2023-11-05 21:18:23 +00:00
|
|
|
|
;; quick-find-files.el is a utlity to quickly find files in a specific
|
2023-11-05 22:06:16 +00:00
|
|
|
|
;; directory, with maybe a specific file extension. It can use both
|
2023-11-05 21:18:23 +00:00
|
|
|
|
;; the shell utilities find and fd to quickly find your files and let
|
2023-11-05 22:06:16 +00:00
|
|
|
|
;; you select the file you’re looking for in a completing read prompt.
|
|
|
|
|
;; Refer to the README for more information.
|
2021-05-02 15:16:07 +00:00
|
|
|
|
|
2023-11-05 21:18:23 +00:00
|
|
|
|
;;; Code:
|
2021-05-02 15:16:07 +00:00
|
|
|
|
|
|
|
|
|
; Group ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
2023-11-05 21:18:23 +00:00
|
|
|
|
(defgroup quick-find-files nil
|
2021-05-02 15:16:07 +00:00
|
|
|
|
"Quickly find files by directory and extension."
|
2021-11-27 01:52:43 +00:00
|
|
|
|
:group 'convenience
|
2023-11-05 21:18:23 +00:00
|
|
|
|
:prefix "quick-find-files-"
|
2023-12-29 15:16:14 +00:00
|
|
|
|
:link '(url-link :tag "Repository" "https://labs.phundrak.com/phundrak/quick-find-files.el")
|
|
|
|
|
:link '(url-link :tag "GitHub" "https://github.com/phundrak/quick-find-files.el"))
|
2021-05-02 15:16:07 +00:00
|
|
|
|
|
|
|
|
|
; Custom variables ;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
2023-12-29 15:16:14 +00:00
|
|
|
|
(defcustom quick-find-files-program 'find
|
2021-05-02 15:16:07 +00:00
|
|
|
|
"Program to find files on your system.
|
|
|
|
|
|
2023-12-06 18:49:59 +00:00
|
|
|
|
By default, the value is \\='fd, but you can change it to
|
|
|
|
|
\\='find too. For now, no other program is supported.
|
2021-05-02 15:16:07 +00:00
|
|
|
|
|
2023-11-05 21:18:23 +00:00
|
|
|
|
By default, `quick-find-files' will try to find fd or find in
|
|
|
|
|
your path. You can customize the executable to use with
|
|
|
|
|
`quick-find-files-fd-executable' and
|
2021-05-02 15:16:07 +00:00
|
|
|
|
`quind-find-files-find-executable'."
|
2023-11-05 21:18:23 +00:00
|
|
|
|
:group 'quick-find-files
|
2023-11-05 22:06:16 +00:00
|
|
|
|
:type 'symbol
|
|
|
|
|
:options '(fd find))
|
2021-05-02 15:16:07 +00:00
|
|
|
|
|
2023-11-05 21:18:23 +00:00
|
|
|
|
(defcustom quick-find-files-fd-executable (executable-find "fd")
|
2021-05-02 15:16:07 +00:00
|
|
|
|
"Executable name or path to the executable of fd."
|
2023-11-05 21:18:23 +00:00
|
|
|
|
:group 'quick-find-files
|
2021-05-02 15:16:07 +00:00
|
|
|
|
:type 'string)
|
|
|
|
|
|
2023-11-05 21:18:23 +00:00
|
|
|
|
(defcustom quick-find-files-find-executable (executable-find "find")
|
2021-05-02 15:16:07 +00:00
|
|
|
|
"Executable name or path to the executable of find."
|
2023-11-05 21:18:23 +00:00
|
|
|
|
:group 'quick-find-files
|
2021-05-02 15:16:07 +00:00
|
|
|
|
:type 'string)
|
|
|
|
|
|
2023-11-05 22:06:16 +00:00
|
|
|
|
(defcustom quick-find-files-dirs-and-exts nil
|
2021-05-02 15:16:07 +00:00
|
|
|
|
"List of pairs of directories and extensions.
|
|
|
|
|
|
|
|
|
|
Each element should be a pair of a directory path and an
|
|
|
|
|
extension, such as
|
|
|
|
|
|
2023-12-06 18:49:59 +00:00
|
|
|
|
\\='((\"~/Documents/org/\" . \"org\"))"
|
2023-11-05 21:18:23 +00:00
|
|
|
|
:group 'quick-find-files
|
2021-05-02 15:16:07 +00:00
|
|
|
|
:type 'list)
|
|
|
|
|
|
2023-12-29 15:16:14 +00:00
|
|
|
|
(make-obsolete-variable 'quick-find-files-dirs-and-exts 'quick-find-files-dirs "0.3")
|
|
|
|
|
|
|
|
|
|
(defcustom quick-find-files-dirs nil
|
|
|
|
|
"List of directories and their rules.
|
|
|
|
|
|
|
|
|
|
This is a list of property lists which contain at most three
|
|
|
|
|
properties:
|
|
|
|
|
- :dir (compulsory): a single path as a string which indicates
|
|
|
|
|
the root directory in which to search for files
|
|
|
|
|
- :ext (optional): an array of strings listing what file
|
|
|
|
|
extension to look for
|
|
|
|
|
|
|
|
|
|
- :ignored (optional): an array of paths as strings which
|
|
|
|
|
indicates which paths to ignore (files or directories).
|
|
|
|
|
Absolute paths are kept as is, while relative paths will be
|
|
|
|
|
understood as paths beginning in the :dir path. For instance:
|
|
|
|
|
|
|
|
|
|
(:dir \"~/org\" :ignored \\='(\"~/org/config\" \"config2\"))
|
|
|
|
|
|
|
|
|
|
is equivalent to
|
|
|
|
|
|
|
|
|
|
(:dir \"~/org\" :ignored \\='(\"~/org/config\" \"~/org/config2\"))"
|
|
|
|
|
:group 'quick-find-files
|
|
|
|
|
:type 'list)
|
|
|
|
|
|
|
|
|
|
(defcustom quick-find-files-ignored-paths nil
|
|
|
|
|
"List of paths to ignore.
|
|
|
|
|
|
|
|
|
|
If a file found matches at least one of these paths, or if one of
|
|
|
|
|
these paths is one of its ancestors, then the file is ignored."
|
|
|
|
|
:group 'quick-find-files
|
|
|
|
|
:type 'list)
|
|
|
|
|
|
2023-11-05 21:18:23 +00:00
|
|
|
|
(defcustom quick-find-files-fd-additional-options ""
|
2023-12-06 18:49:59 +00:00
|
|
|
|
"Additional command line options for fd."
|
2023-11-05 21:18:23 +00:00
|
|
|
|
:group 'quick-find-files
|
2023-11-05 22:06:16 +00:00
|
|
|
|
:type 'string
|
|
|
|
|
:safe #'stringp)
|
2023-07-08 10:21:09 +00:00
|
|
|
|
|
2023-11-05 21:18:23 +00:00
|
|
|
|
(defcustom quick-find-files-find-additional-options ""
|
2023-12-06 18:49:59 +00:00
|
|
|
|
"Additional command line options for find."
|
2023-11-05 21:18:23 +00:00
|
|
|
|
:group 'quick-find-files
|
2023-07-08 10:21:09 +00:00
|
|
|
|
:type 'string)
|
|
|
|
|
|
2023-11-05 21:18:23 +00:00
|
|
|
|
(defcustom quick-find-files-completing-read #'completing-read
|
|
|
|
|
"Completing read function.
|
|
|
|
|
|
|
|
|
|
The function must accept a prompt as its first argument and the
|
|
|
|
|
collection of elements to choose from as its second argument."
|
2023-12-06 18:49:59 +00:00
|
|
|
|
:group 'quick-find-files
|
|
|
|
|
:type 'function)
|
2023-11-05 21:18:23 +00:00
|
|
|
|
|
2021-05-02 15:16:07 +00:00
|
|
|
|
; Internal functions ;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
2023-11-05 21:18:23 +00:00
|
|
|
|
(defun quick-find-files--split-lines (str &optional omit-null)
|
2021-05-02 15:16:07 +00:00
|
|
|
|
"Split a multilines `STR' into a list of strings.
|
|
|
|
|
|
|
|
|
|
If `OMIT-NULL' is non-null, ignore empty strings."
|
|
|
|
|
(declare (side-effect-free t))
|
|
|
|
|
(split-string str "\\(\r\n\\|[\n\r]\\)" omit-null))
|
|
|
|
|
|
2023-12-29 15:16:14 +00:00
|
|
|
|
(defun quick-find-files--normalize-ignored-paths (ignored-paths root-dir)
|
|
|
|
|
"Normalize IGNORED-PATHS.
|
|
|
|
|
|
|
|
|
|
Change members of IGNORED-PATHS so that they are all absolute
|
|
|
|
|
paths. Paths that are relative paths are considered to be
|
|
|
|
|
relative to ROOT-DIR."
|
|
|
|
|
(when ignored-paths
|
|
|
|
|
(mapcar (lambda (path)
|
|
|
|
|
(expand-file-name path root-dir))
|
|
|
|
|
ignored-paths)))
|
|
|
|
|
|
|
|
|
|
(defun quick-find-files--filter-out-files (files ignored-paths)
|
|
|
|
|
"Remove files in FILES matching IGNORED-PATHS.
|
|
|
|
|
|
|
|
|
|
A file matches IGNORED-PATHS if any of the latter's paths equals
|
|
|
|
|
or is an ancestor of said file."
|
|
|
|
|
(seq-filter (lambda (file)
|
|
|
|
|
(not (seq-some (lambda (ignored-path)
|
|
|
|
|
(or (equal file ignored-path)
|
|
|
|
|
(string-prefix-p ignored-path file)))
|
|
|
|
|
ignored-paths)))
|
|
|
|
|
files))
|
|
|
|
|
|
|
|
|
|
(defun quick-find-files--find-files (dir ext ignored-paths)
|
2021-11-27 01:52:43 +00:00
|
|
|
|
"Find files in directory DIR with extension EXT.
|
2021-05-02 15:16:07 +00:00
|
|
|
|
|
2023-12-29 15:16:14 +00:00
|
|
|
|
If EXT is nil, return all files in DIR.
|
|
|
|
|
|
|
|
|
|
Ignore files matching IGNORED-PATHS. See
|
|
|
|
|
`quick-find-files--filter-out-files' on how this argument is
|
|
|
|
|
used.
|
|
|
|
|
|
2023-11-05 21:18:23 +00:00
|
|
|
|
Use fd or find depending on `quick-find-files-program'.
|
2021-11-27 01:52:43 +00:00
|
|
|
|
Return files as a list of absolute paths."
|
2021-05-02 15:16:07 +00:00
|
|
|
|
(declare (side-effect-free t))
|
2023-12-29 15:16:14 +00:00
|
|
|
|
(let ((ignored-paths (quick-find-files--normalize-ignored-paths ignored-paths dir)))
|
|
|
|
|
(quick-find-files--filter-out-files
|
|
|
|
|
(quick-find-files--split-lines
|
|
|
|
|
(shell-command-to-string
|
|
|
|
|
(pcase quick-find-files-program
|
|
|
|
|
('fd (format "%s . %s %s -c never %s"
|
|
|
|
|
quick-find-files-fd-executable
|
|
|
|
|
dir
|
|
|
|
|
(if ext (concat "-e " ext) "")
|
|
|
|
|
quick-find-files-fd-additional-options))
|
|
|
|
|
('find (format "%s %s %s %s"
|
|
|
|
|
quick-find-files-find-executable
|
|
|
|
|
dir
|
|
|
|
|
(if ext (format "-name \"*.%s" ext) "")
|
|
|
|
|
quick-find-files-find-additional-options))
|
|
|
|
|
(otherwise (error "Find program %s not implemented" otherwise)))))
|
|
|
|
|
ignored-paths)))
|
2021-05-02 15:16:07 +00:00
|
|
|
|
|
|
|
|
|
; Public functions ;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
2023-12-29 15:16:14 +00:00
|
|
|
|
(defun quick-find-files-list-files (dir ext ignored-paths)
|
2021-05-02 15:16:07 +00:00
|
|
|
|
"List files in directories and with specific extensions.
|
|
|
|
|
|
|
|
|
|
The directories and extensions are specified in the variable
|
2023-12-29 15:16:14 +00:00
|
|
|
|
`quick-find-files-dirs'.
|
2021-05-02 15:16:07 +00:00
|
|
|
|
|
2023-12-29 15:16:14 +00:00
|
|
|
|
If DIR is non-nil, search only in DIR for files with an extension
|
|
|
|
|
matching EXT. If EXT is nil, return all files in DIR.
|
|
|
|
|
|
|
|
|
|
When DIR is non-nil, any file whose path matches or who is a
|
|
|
|
|
descendant of any value in IGNORED-PATHS will be filtered out.
|
|
|
|
|
|
|
|
|
|
If DIR is nil, use `quick-find-files-dirs' instead.
|
2023-11-05 22:06:16 +00:00
|
|
|
|
|
2021-05-02 15:16:07 +00:00
|
|
|
|
Return a list of paths to files."
|
|
|
|
|
(declare (side-effect-free t))
|
2023-12-29 15:16:14 +00:00
|
|
|
|
(if dir
|
|
|
|
|
(quick-find-files--find-files dir ext ignored-paths)
|
|
|
|
|
(mapcan (lambda (dir)
|
|
|
|
|
(quick-find-files--find-files (plist-get dir :dir)
|
|
|
|
|
(plist-get dir :ext)
|
|
|
|
|
(plist-get dir :ignored)))
|
|
|
|
|
quick-find-files-dirs)))
|
2021-05-02 15:16:07 +00:00
|
|
|
|
|
2021-05-02 19:13:34 +00:00
|
|
|
|
;;;###autoload
|
2023-12-29 15:16:14 +00:00
|
|
|
|
(defun quick-find-files (&optional arg dir extension ignored-paths)
|
2021-05-02 15:16:07 +00:00
|
|
|
|
"Quickly find and open files in directories with specific extensions.
|
|
|
|
|
|
|
|
|
|
Directories in which to look for files with specific extensions
|
2023-12-29 15:16:14 +00:00
|
|
|
|
are specified in `quick-find-files-dirs'.
|
2023-11-05 22:06:16 +00:00
|
|
|
|
|
|
|
|
|
When called interactively with a prefix (i.e. non-nil ARG), ask
|
|
|
|
|
user for the root directory of their search and the file
|
|
|
|
|
extention they are looking for. When the file extension is left
|
2023-12-29 15:16:14 +00:00
|
|
|
|
empty, all files are to be looked for.
|
|
|
|
|
|
|
|
|
|
DIR is the root directory in which files are searched for,
|
|
|
|
|
recursively. If nil, the paths set in `quick-find-files-dirs'
|
|
|
|
|
will be used.
|
|
|
|
|
|
|
|
|
|
EXTENSION is the file extension to look for. If it is nil, then
|
|
|
|
|
all files in DIR will be listed. If DIR is nil, this argument
|
|
|
|
|
will be ignored.
|
|
|
|
|
|
|
|
|
|
IGNORED-PATHS will exclude all files matching at least one of
|
|
|
|
|
these paths."
|
2023-11-05 22:06:16 +00:00
|
|
|
|
(interactive "P")
|
2023-12-29 12:47:08 +00:00
|
|
|
|
(when arg
|
|
|
|
|
(setq dir (read-file-name "Root directory: "))
|
2023-12-29 15:16:14 +00:00
|
|
|
|
(setq extension (read-string "File extension (leave blank for all files): ")))
|
2023-12-29 12:47:08 +00:00
|
|
|
|
(find-file (funcall quick-find-files-completing-read
|
|
|
|
|
"Open file: "
|
2023-12-29 15:16:14 +00:00
|
|
|
|
(quick-find-files-list-files dir
|
|
|
|
|
extension
|
|
|
|
|
ignored-paths))))
|
2023-12-29 12:47:08 +00:00
|
|
|
|
|
2023-12-29 15:16:14 +00:00
|
|
|
|
; Provides ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2021-05-02 15:16:07 +00:00
|
|
|
|
|
2023-11-05 21:18:23 +00:00
|
|
|
|
(provide 'quick-find-files)
|
2021-05-02 15:16:07 +00:00
|
|
|
|
|
2023-11-05 21:18:23 +00:00
|
|
|
|
;;; quick-find-files.el ends here
|