make project search functions more consistent with cp-find-file

Project search functions = `cp-grep', `cp-ag' `cp-rg'. The logic is
that these functions should look in the same files as `cp-find-file':
- Always ignore patterns from `.projectile' file
- No longer ignore patterns from `grep-find-ignored-files` and
`grep-find-ignored-directories'

For `cp-grep' this is not fully implemented because `grep' does not
care about `.gitignore' and `git-grep' cares only about that file.

Calling any of these three functions with a prefix arg gives full
control about the command's arguments, including ignored paths.
This commit is contained in:
Eric Danan 2017-12-01 21:24:10 +01:00
parent e4aa44419a
commit 162cdc2655

View file

@ -107,30 +107,6 @@ of `(ivy-thing-at-point)' by hitting \"M-n\" in the minibuffer."
(sexp :tag "Custom expression")) (sexp :tag "Custom expression"))
:group 'counsel-projectile) :group 'counsel-projectile)
(defcustom counsel-projectile-ag-use-gitignore-only t
"Non-nil if, for git projects, `counsel-projectile-ag' should
not ignore other files and directories than those specified in
the `.gitignore' file. This is the default behavior.
If nil, files and directories specified in the `.projectile' file
as well as variables `grep-find-ignored-files' and
`grep-find-ignored-directories' are also ignored for git
projects (as they systematically are for non-git projects)."
:type 'boolean
:group 'counsel-projectile)
(defcustom counsel-projectile-rg-use-gitignore-only t
"Non-nil if, for git projects, `counsel-projectile-rg' should
not ignore other files and directories than those specified in
the `.gitignore' file. This is the default behavior.
If nil, files and directories specified in the `.projectile' file
as well as variables `grep-find-ignored-files' and
`grep-find-ignored-directories' are also ignored for git
projects (as they systematically are for non-git projects)."
:type 'boolean
:group 'counsel-projectile)
(defcustom counsel-projectile-org-capture-templates nil (defcustom counsel-projectile-org-capture-templates nil
"Templates for the creation of new entries with `counsel-projectile-org-capture'. "Templates for the creation of new entries with `counsel-projectile-org-capture'.
@ -561,6 +537,9 @@ construct the command.")
(defvar counsel-projectile-grep-base-command nil) (defvar counsel-projectile-grep-base-command nil)
(defvar counsel-projectile-grep-options-history nil
"History for `counsel-projectile-grep' options.")
(defun counsel-projectile-grep-function (string) (defun counsel-projectile-grep-function (string)
"Grep in the current project for STRING." "Grep in the current project for STRING."
(if (< (length string) 3) (if (< (length string) 3)
@ -622,24 +601,25 @@ called with a prefix argument."
(counsel-git-grep (or current-prefix-arg options-or-cmd) (counsel-git-grep (or current-prefix-arg options-or-cmd)
counsel-projectile-grep-initial-input)) counsel-projectile-grep-initial-input))
(counsel-require-program (car (split-string counsel-projectile-grep-base-command))) (counsel-require-program (car (split-string counsel-projectile-grep-base-command)))
(let* ((options (if current-prefix-arg (let* ((ignored-files (mapconcat (lambda (i)
(read-string "options: ") (concat "--exclude="
options-or-cmd)) (shell-quote-argument i)
(ignored-files " "))
(cl-union (projectile-ignored-files-rel) grep-find-ignored-files)) (projectile-ignored-files-rel)
(ignored-dirs ""))
(cl-union (projectile-ignored-directories-rel) grep-find-ignored-directories)) (ignored-dirs (mapconcat (lambda (i)
(concat "--exclude-dir="
(shell-quote-argument i)
" "))
(projectile-ignored-directories-rel)
""))
(ignored (concat ignored-files ignored-dirs))
(options (options
(concat options " " (if current-prefix-arg
(mapconcat (lambda (i) (read-string (projectile-prepend-project-name "grep options: ")
(concat "--exclude=" (shell-quote-argument i))) ignored
ignored-files 'counsel-projectile-grep-options-history)
" ") (concat ignored options-or-cmd))))
" "
(mapconcat (lambda (i)
(concat "--exclude-dir=" (shell-quote-argument i)))
ignored-dirs
" "))))
(setq counsel-projectile-grep-command (setq counsel-projectile-grep-command
(format counsel-projectile-grep-base-command options)) (format counsel-projectile-grep-base-command options))
(ivy-set-prompt 'counsel-projectile-grep counsel-prompt-function) (ivy-set-prompt 'counsel-projectile-grep counsel-prompt-function)
@ -663,6 +643,9 @@ called with a prefix argument."
;;; counsel-projectile-ag ;;; counsel-projectile-ag
(defvar counsel-projectile-ag-options-history nil
"History for `counsel-projectile-ag' options.")
;;;###autoload ;;;###autoload
(defun counsel-projectile-ag (&optional options) (defun counsel-projectile-ag (&optional options)
"Run an ag search in the project. "Run an ag search in the project.
@ -672,22 +655,19 @@ be passed to ag. It is read from the minibuffer if the function
is called with a prefix argument." is called with a prefix argument."
(interactive) (interactive)
(if (projectile-project-p) (if (projectile-project-p)
(let* ((options (let* ((ignored (mapconcat (lambda (i)
(if current-prefix-arg (concat "--ignore "
(read-string "options: ") (shell-quote-argument i)
options)) " "))
(ignored (append (projectile-ignored-files-rel)
(unless (and (eq (projectile-project-vcs) 'git) (projectile-ignored-directories-rel))
counsel-projectile-ag-use-gitignore-only) ""))
(append
(cl-union (projectile-ignored-files-rel) grep-find-ignored-files)
(cl-union (projectile-ignored-directories-rel) grep-find-ignored-directories))))
(options (options
(concat options " " (if current-prefix-arg
(mapconcat (lambda (i) (read-string (projectile-prepend-project-name "ag options: ")
(concat "--ignore " (shell-quote-argument i)))
ignored ignored
" ")))) 'counsel-projectile-ag-options-history)
(concat ignored options))))
(counsel-ag (eval counsel-projectile-ag-initial-input) (counsel-ag (eval counsel-projectile-ag-initial-input)
(projectile-project-root) (projectile-project-root)
options options
@ -696,6 +676,9 @@ is called with a prefix argument."
;;; counsel-projectile-rg ;;; counsel-projectile-rg
(defvar counsel-projectile-rg-options-history nil
"History for `counsel-projectile-rg' options.")
;;;###autoload ;;;###autoload
(defun counsel-projectile-rg (&optional options) (defun counsel-projectile-rg (&optional options)
"Run an rg search in the project. "Run an rg search in the project.
@ -705,22 +688,19 @@ be passed to rg. It is read from the minibuffer if the function
is called with a prefix argument." is called with a prefix argument."
(interactive) (interactive)
(if (projectile-project-p) (if (projectile-project-p)
(let* ((options (let* ((ignored (mapconcat (lambda (i)
(if current-prefix-arg (concat "--glob "
(read-string "options: ") (shell-quote-argument (concat "!" i))
options)) " "))
(ignored (append (projectile-ignored-files-rel)
(unless (and (eq (projectile-project-vcs) 'git) (projectile-ignored-directories-rel))
counsel-projectile-rg-use-gitignore-only) ""))
(append
(cl-union (projectile-ignored-files-rel) grep-find-ignored-files)
(cl-union (projectile-ignored-directories-rel) grep-find-ignored-directories))))
(options (options
(concat options " " (if current-prefix-arg
(mapconcat (lambda (i) (read-string (projectile-prepend-project-name "rg options: ")
(concat "--glob " (shell-quote-argument (concat "!" i))))
ignored ignored
" ")))) 'counsel-projectile-rg-options-history)
(concat ignored options))))
(counsel-rg (eval counsel-projectile-rg-initial-input) (counsel-rg (eval counsel-projectile-rg-initial-input)
(projectile-project-root) (projectile-project-root)
options options