cp-switch-project: Allow setting the default action

This commit is contained in:
Eric Danan 2018-09-20 22:54:54 +02:00
parent 46ca1a7cd9
commit 3ee84f2717

View file

@ -298,7 +298,7 @@ It is also possible to use a custom matcher. It must be a function taking two a
"find file manually") "find file manually")
("k" counsel-projectile-find-file-action-delete ("k" counsel-projectile-find-file-action-delete
"delete") "delete")
("p" (lambda (_) (counsel-projectile-switch-project)) ("p" counsel-projectile-find-file-action-switch-project
"switch project")) "switch project"))
'counsel-projectile) 'counsel-projectile)
@ -368,6 +368,10 @@ on `counsel-find-file-ignore-regexp'."
"Delete FILE." "Delete FILE."
(counsel-find-file-delete (projectile-expand-root file))) (counsel-find-file-delete (projectile-expand-root file)))
(defun counsel-projectile-find-file-action-switch-project (_)
"Switch project action for `counsel-projectile-find-file'."
(counsel-projectile-switch-project 'counsel-projectile-switch-project-action-find-file))
(defun counsel-projectile-find-file-transformer (str) (defun counsel-projectile-find-file-transformer (str)
"Transform non-visited file names with `ivy-virtual' face." "Transform non-visited file names with `ivy-virtual' face."
(if (not (get-file-buffer (projectile-expand-root str))) (if (not (get-file-buffer (projectile-expand-root str)))
@ -428,7 +432,7 @@ The sorting function can be modified by adding an entry for
"open as root") "open as root")
("m" counsel-projectile-find-file-action-find-file-manually ("m" counsel-projectile-find-file-action-find-file-manually
"find file manually") "find file manually")
("p" (lambda (_) (counsel-projectile-switch-project)) ("p" counsel-projectile-find-dir-action-switch-project
"switch project")) "switch project"))
'counsel-projectile) 'counsel-projectile)
@ -459,6 +463,10 @@ The sorting function can be modified by adding an entry for
(counsel-find-file-as-root (projectile-expand-root dir)) (counsel-find-file-as-root (projectile-expand-root dir))
(run-hooks 'projectile-find-dir-hook)) (run-hooks 'projectile-find-dir-hook))
(defun counsel-projectile-find-dir-action-switch-project (_)
"Switch project action for `counsel-projectile-find-dir'."
(counsel-projectile-switch-project 'counsel-projectile-switch-project-action-find-dir))
(defun counsel-projectile-find-dir-transformer (str) (defun counsel-projectile-find-dir-transformer (str)
"Transform candidates with `ivy-subdir' face." "Transform candidates with `ivy-subdir' face."
(propertize str 'face 'ivy-subdir)) (propertize str 'face 'ivy-subdir))
@ -511,7 +519,7 @@ candidates list of `counsel-projectile-switch-to-buffer' and
"kill") "kill")
("m" counsel-projectile-switch-to-buffer-action-find-file-manually ("m" counsel-projectile-switch-to-buffer-action-find-file-manually
"find file manually") "find file manually")
("p" (lambda (_) (counsel-projectile-switch-project)) ("p" counsel-projectile-switch-to-buffer-action-switch-project
"switch project")) "switch project"))
'counsel-projectile) 'counsel-projectile)
@ -550,6 +558,10 @@ names as in `ivy--buffer-list', and remove current buffer if
(projectile-project-root)))) (projectile-project-root))))
(counsel-find-file))) (counsel-find-file)))
(defun counsel-projectile-switch-to-buffer-action-switch-project (_)
"Switch project action for `counsel-projectile-switch-to-buffer'."
(counsel-projectile-switch-project 'counsel-projectile-switch-project-action-switch-to-buffer))
(defun counsel-projectile-switch-to-buffer-transformer (str) (defun counsel-projectile-switch-to-buffer-transformer (str)
"Transform candidate STR when switching project buffers. "Transform candidate STR when switching project buffers.
@ -599,12 +611,12 @@ of `(ivy-thing-at-point)' by hitting \"M-n\" in the minibuffer."
'(1 '(1
("o" counsel-git-grep-action ("o" counsel-git-grep-action
"default") "default")
("p" (lambda (_) (counsel-projectile-switch-project)) ("p" counsel-projectile-grep-action-switch-project
"switch project")) "switch project"))
'counsel-projectile) 'counsel-projectile)
(defcustom counsel-projectile-git-grep-extra-actions (defcustom counsel-projectile-git-grep-extra-actions
'(("p" (lambda (_) (counsel-projectile-switch-project)) '(("p" counsel-projectile-git-grep-action-switch-project
"switch project")) "switch project"))
"List of extra actions to add to "List of extra actions to add to
`counsel-projectile-git-grep' (in addition to the actions of `counsel-projectile-git-grep' (in addition to the actions of
@ -633,6 +645,14 @@ construct the command.")
(shell-quote-argument regex))) (shell-quote-argument regex)))
nil))) nil)))
(defun counsel-projectile-grep-action-switch-project (_)
"Switch project action for `counsel-projectile-grep'."
(counsel-projectile-switch-project 'counsel-projectile-switch-project-action-grep))
(defun counsel-projectile-git-grep-action-switch-project (_)
"Switch project action for `counsel-projectile-git-grep'."
(counsel-projectile-switch-project 'counsel-projectile-switch-project-action-git-grep))
(defun counsel-projectile-grep-transformer (str) (defun counsel-projectile-grep-transformer (str)
"Higlight file and line number in STR, first removing the "Higlight file and line number in STR, first removing the
\"./\" prefix from the filename." \"./\" prefix from the filename."
@ -756,7 +776,7 @@ of `(ivy-thing-at-point)' by hitting \"M-n\" in the minibuffer."
:group 'counsel-projectile) :group 'counsel-projectile)
(defcustom counsel-projectile-ag-extra-actions (defcustom counsel-projectile-ag-extra-actions
'(("p" (lambda (_) (counsel-projectile-switch-project)) '(("p" counsel-projectile-ag-action-switch-project
"switch project")) "switch project"))
"List of extra actions to add to "List of extra actions to add to
`counsel-projectile-ag' (in addition to the actions of `counsel-projectile-ag' (in addition to the actions of
@ -768,6 +788,10 @@ of `(ivy-thing-at-point)' by hitting \"M-n\" in the minibuffer."
(string :tag " name"))) (string :tag " name")))
:group 'counsel-projectile) :group 'counsel-projectile)
(defun counsel-projectile-ag-action-switch-project (_)
"Switch project action for `counsel-projectile-ag'."
(counsel-projectile-switch-project 'counsel-projectile-switch-project-action-ag))
;;;###autoload ;;;###autoload
(defun counsel-projectile-ag (&optional options) (defun counsel-projectile-ag (&optional options)
"Search the current project with ag. "Search the current project with ag.
@ -819,7 +843,7 @@ of `(ivy-thing-at-point)' by hitting \"M-n\" in the minibuffer."
:group 'counsel-projectile) :group 'counsel-projectile)
(defcustom counsel-projectile-rg-extra-actions (defcustom counsel-projectile-rg-extra-actions
'(("p" (lambda (_) (counsel-projectile-switch-project)) '(("p" counsel-projectile-rg-action-switch-project
"switch project")) "switch project"))
"List of extra actions to add to "List of extra actions to add to
`counsel-projectile-rg' (in addition to the actions of `counsel-projectile-rg' (in addition to the actions of
@ -831,6 +855,10 @@ of `(ivy-thing-at-point)' by hitting \"M-n\" in the minibuffer."
(string :tag " name"))) (string :tag " name")))
:group 'counsel-projectile) :group 'counsel-projectile)
(defun counsel-projectile-rg-action-switch-project (_)
"Switch project action for `counsel-projectile-rg'."
(counsel-projectile-switch-project 'counsel-projectile-switch-project-action-rg))
;;;###autoload ;;;###autoload
(defun counsel-projectile-rg (&optional options) (defun counsel-projectile-rg (&optional options)
"Search the current project with rg. "Search the current project with rg.
@ -1020,11 +1048,9 @@ The format is the same as in `org-capture-templates-contexts'."
"Stores a backup of `org-capture-templates'.") "Stores a backup of `org-capture-templates'.")
(defun counsel-projectile-org-capture-action-switch-project (_) (defun counsel-projectile-org-capture-action-switch-project (_)
"Reset `org-capture-templates' from "Switch project action for `counsel-projectile-org-capture'."
`counsel-projectile--org-capture-templates-backup' and call
`counsel-projectile-switch-project'."
(setq org-capture-templates counsel-projectile--org-capture-templates-backup) (setq org-capture-templates counsel-projectile--org-capture-templates-backup)
(counsel-projectile-switch-project)) (counsel-projectile-switch-project 'counsel-projectile-switch-project-action-org-capture))
;;;###autoload ;;;###autoload
(defun counsel-projectile-org-capture (&optional from-buffer) (defun counsel-projectile-org-capture (&optional from-buffer)
@ -1330,8 +1356,12 @@ action."
(counsel-projectile-switch-project-by-name project))) (counsel-projectile-switch-project-by-name project)))
;;;###autoload ;;;###autoload
(defun counsel-projectile-switch-project () (defun counsel-projectile-switch-project (&optional default-action)
"Switch project." "Switch project.
Optional argument DEFAULT-ACTION is the key, function, name, or
index in the list `counsel-projectile-switch-project-action' (1
for the first action, etc) of the action to set as default."
(interactive) (interactive)
(ivy-read (projectile-prepend-project-name "Switch to project: ") (ivy-read (projectile-prepend-project-name "Switch to project: ")
(if counsel-projectile-remove-current-project (if counsel-projectile-remove-current-project
@ -1339,7 +1369,14 @@ action."
projectile-known-projects) projectile-known-projects)
:preselect (and (projectile-project-p) :preselect (and (projectile-project-p)
(abbreviate-file-name (projectile-project-root))) (abbreviate-file-name (projectile-project-root)))
:action counsel-projectile-switch-project-action :action (or (and default-action
(listp counsel-projectile-switch-project-action)
(integerp (car counsel-projectile-switch-project-action))
(cons (counsel-projectile--action-index
default-action
counsel-projectile-switch-project-action)
(cdr counsel-projectile-switch-project-action)))
counsel-projectile-switch-project-action)
:require-match t :require-match t
:sort counsel-projectile-sort-projects :sort counsel-projectile-sort-projects
:caller 'counsel-projectile-switch-project)) :caller 'counsel-projectile-switch-project))
@ -1361,7 +1398,7 @@ action."
"open file as root") "open file as root")
("m" counsel-projectile-action-find-file-manually ("m" counsel-projectile-action-find-file-manually
"find file manually") "find file manually")
("p" (lambda (_) (counsel-projectile-switch-project)) ("p" counsel-projectile-action-switch-project
"switch project")) "switch project"))
'counsel-projectile) 'counsel-projectile)
@ -1463,6 +1500,10 @@ directory of file named NAME."
(message "This action does not apply to buffers.") (message "This action does not apply to buffers.")
(counsel-projectile-find-file-action-root name))) (counsel-projectile-find-file-action-root name)))
(defun counsel-projectile-action-switch-project (_)
"Switch project action for `counsel-projectile'."
(counsel-projectile-switch-project 'counsel-projectile-switch-project-action))
(defun counsel-projectile-transformer (str) (defun counsel-projectile-transformer (str)
"Fontifies modified, file-visiting buffers as well as non-visited files." "Fontifies modified, file-visiting buffers as well as non-visited files."
(if (member str counsel-projectile--buffers) (if (member str counsel-projectile--buffers)