Skip to content

Commit

Permalink
New dotfile variable dotspacemacs-search-tools
Browse files Browse the repository at this point in the history
Default list is `("ag" "pt" "ack" "grep")
Resolve syl20bnr#1158
  • Loading branch information
syl20bnr committed Apr 15, 2015
1 parent 8743535 commit 9f853dc
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 33 deletions.
4 changes: 4 additions & 0 deletions core/core-dotspacemacs.el
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ it reaches the top or bottom of the screen.")
declared in a layer which is not a member of
`dotspacemacs-configuration-layers'")

(defvar dotspacemacs-search-tools '("ag" "pt" "ack" "grep")
"List of search tool executable names. Spacemacs uses the first installed
tool of the list. Supported tools are `ag', `pt', `ack' and `grep'.")

(defvar dotspacemacs-default-package-repository 'melpa-stable
"The default package repository used if no explicit repository has been
specified with an installed package.
Expand Down
3 changes: 3 additions & 0 deletions core/templates/.spacemacs.template
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ before layers configuration."
dotspacemacs-smartparens-strict-mode nil
;; If non nil advises quit functions to keep server open when quitting.
dotspacemacs-persistent-server nil
;; List of search tool executable names. Spacemacs uses the first installed
;; tool of the list. Supported tools are `ag', `pt', `ack' and `grep'.
dotspacemacs-search-tools '("ag" "pt" "ack" "grep")
;; The default package repository used if no explicit repository has been
;; specified with an installed package.
;; Not used for now.
Expand Down
14 changes: 11 additions & 3 deletions doc/DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -1652,13 +1652,20 @@ Key Binding | Description
**Note** `ag` and `pt` are optimized to be used in a source control repository
but they can be used in an arbitrary directory as well.

By default Spacemacs will look for the first installed tool in the list
`("ag", "pt", "ack", "grep")`. This list can be changed in the dotfile with
the variable `dotspacemacs-search-tools`.

**Beware** if you use `pt`, [TCL parser tools][tcl] also install a command line
tool called `pt`.

#### Searching in an arbitrary directory

To use these utilities in one or several arbitrary directories:

Key Binding | Description
--------------------------|---------------------------------------------
<kbd>SPC s /</kbd> | execute the first found utility in this order `pt`, `ag`, `ack` and `grep`
<kbd>SPC s /</kbd> | execute the first found utility (by default `ag`, `pt`, `ack` and `grep`)
<kbd>SPC s a</kbd> | `ag`
<kbd>SPC s g</kbd> | `grep`
<kbd>SPC s k</kbd> | `ack`
Expand All @@ -1676,7 +1683,7 @@ To use these utilities in a project using `projectile`:

Key Binding | Description
--------------------------|---------------------------------------------
<kbd>SPC /</kbd> | execute the first found utility in this order `pt`, `ag`, `ack` and `grep`
<kbd>SPC /</kbd> | execute the first found utility (by default `ag`, `pt`, `ack` and `grep`)
<kbd>SPC p s a</kbd> | `ag`
<kbd>SPC p s g</kbd> | `grep`
<kbd>SPC p s k</kbd> | `ack`
Expand Down Expand Up @@ -2222,7 +2229,7 @@ encountered in the file tree.

`Helm` is used whenever it is possible.

To search in a project see [project searching](#project-searching).
To search in a project see [project searching](#searching-in-a-project).

`projectile` commands start with <kbd>p</kbd>:

Expand Down Expand Up @@ -2495,6 +2502,7 @@ developers to elisp hackers!
[sp]: https://github.com/Fuco1/smartparens
[ag]: https://github.com/ggreer/the_silver_searcher
[pt]: https://github.com/monochromegane/the_platinum_searcher
[tcl]: https://core.tcl.tk/tcllib/doc/trunk/embedded/www/tcllib/files/apps/pt.html
[flycheck]: https://github.com/flycheck
[yasnippet]: https://github.com/capitaomorte/yasnippet
[expand-region]: https://github.com/magnars/expand-region.el
Expand Down
85 changes: 55 additions & 30 deletions spacemacs/packages.el
Original file line number Diff line number Diff line change
Expand Up @@ -1269,21 +1269,33 @@ which require an initialization must be listed explicitly in the list.")
(call-interactively 'helm-do-ag))
(message "error: helm-ag not found.")))

(defun spacemacs/helm-do-search-dwim (&optional arg)
"Execute the first found search tool.
Search for a search tool in the following order pt > ag > ack > grep.
If ARG is non nil then the search order is changed to ack > grep (ag and pt are
ignored)."
(defun spacemacs//helm-do-search-find-tool (tools)
"Create a cond form given a TOOLS string list and evaluate it."
(eval `(cond
,@(mapcar (lambda (x)
`((executable-find ,x)
',(let ((func (intern
(format "spacemacs/helm-do-%s"
x))))
(if (fboundp func)
func
(intern (format "helm-do-%s" x))))))
tools)
(t 'helm-do-grep))))

(defun spacemacs/helm-smart-do-search (&optional arg)
"Execute the first found search tool in `dotspacemacs-search-tools'.
Search for a search tool in the order provided by `dotspacemacs-search-tools'
If ARG is non nil then `ag' and `pt' and ignored."
(interactive "P")
(call-interactively
(if arg
(cond (((executable-find "ack") 'spacemacs/helm-do-ack)
(t 'helm-do-grep)))
(cond ((executable-find "pt") 'spacemacs/helm-do-pt)
((executable-find "ag") 'helm-do-ag)
((executable-find "ack") 'spacemacs/helm-do-ack)
(t 'helm-do-grep)))))
(let ((tools
(if arg
(delq nil (mapcar (lambda (x)
(and (not (member x '("ag" "pt"))) x))
dotspacemacs-search-tools))
dotspacemacs-search-tools)))
(call-interactively (spacemacs//helm-do-search-find-tool tools))))

;; use helm by default for M-x
(unless (configuration-layer/package-usedp 'smex)
Expand All @@ -1302,7 +1314,7 @@ ignored)."
"ry" 'helm-show-kill-ring
"rr" 'helm-register
"rm" 'helm-all-mark-rings
"s/" 'spacemacs/helm-do-search-dwim
"s/" 'spacemacs/helm-smart-do-search
"sa" 'helm-do-ag
"sg" 'helm-do-grep
"sk" 'spacemacs/helm-do-ack
Expand Down Expand Up @@ -1566,24 +1578,37 @@ ARG non nil means that the editing style is `vim'."
(call-interactively 'helm-projectile-ag))
(message "error: helm-ag not found.")))

(defun spacemacs/helm-projectile-search-dwim (&optional arg)
"Execute the first found search tool.
Search for a search tool in the following order pt > ag > ack > grep.
If ARG is non nil then the search order is changed to ack > grep (ag and pt are
ignored)."
(defun spacemacs//helm-projectile-do-search-find-tool (tools)
"Create a cond form given a TOOLS string list and evaluate it."
(eval `(cond
,@(mapcar (lambda (x)
`((executable-find ,x)
',(let ((func (intern
(format "spacemacs/helm-projectile-%s"
x))))
(if (fboundp func)
func
(intern (format "helm-projectile-%s" x))))))
tools)
(t 'helm-do-grep))))

(defun spacemacs/helm-projectile-smart-do-search (&optional arg)
"Execute the first found search tool in `dotspacemacs-search-tools'.
Search for a search tool in the order provided by `dotspacemacs-search-tools'
If ARG is non nil then `ag' and `pt' and ignored."
(interactive "P")
(call-interactively
(if arg
(cond (((executable-find "ack") 'helm-projectile-ack)
(t 'helm-projectile-grep)))
(cond ((executable-find "pt") 'spacemacs/helm-projectile-pt)
((executable-find "ag") 'helm-projectile-ag)
((executable-find "ack") 'spacemacs/helm-projectile-ack)
(t 'helm-projectile-grep)))))
(let ((tools
(if arg
(delq nil (mapcar (lambda (x)
(and (not (member x '("ag" "pt"))) x))
dotspacemacs-search-tools))
dotspacemacs-search-tools)))
(call-interactively (spacemacs//helm-projectile-do-search-find-tool
tools))))

(evil-leader/set-key
"/" 'spacemacs/helm-projectile-search-dwim
"/" 'spacemacs/helm-projectile-smart-do-search
"pb" 'helm-projectile-switch-to-buffer
"pd" 'helm-projectile-find-dir
"pe" 'helm-projectile-recentf
Expand Down

0 comments on commit 9f853dc

Please sign in to comment.