Handle new 'prompt value of projectile-require-project-root

Fixes #106
This commit is contained in:
Eric Danan 2018-09-22 16:52:59 +02:00
parent 3ee84f2717
commit 991b5587c3

View file

@ -368,7 +368,7 @@ 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 (_) (defun counsel-projectile-find-file-action-switch-project (&optional _)
"Switch project action for `counsel-projectile-find-file'." "Switch project action for `counsel-projectile-find-file'."
(counsel-projectile-switch-project 'counsel-projectile-switch-project-action-find-file)) (counsel-projectile-switch-project 'counsel-projectile-switch-project-action-find-file))
@ -385,6 +385,9 @@ on `counsel-find-file-ignore-regexp'."
With a prefix ARG, invalidate the cache first. If DWIM is With a prefix ARG, invalidate the cache first. If DWIM is
non-nil, use completion based on context." non-nil, use completion based on context."
(interactive "P") (interactive "P")
(if (and (eq projectile-require-project-root 'prompt)
(not (projectile-project-p)))
(counsel-projectile-find-file-action-switch-project)
(projectile-maybe-invalidate-cache arg) (projectile-maybe-invalidate-cache arg)
(let* ((project-files (projectile-current-project-files)) (let* ((project-files (projectile-current-project-files))
(files (and dwim (projectile-select-files project-files)))) (files (and dwim (projectile-select-files project-files))))
@ -394,7 +397,7 @@ non-nil, use completion based on context."
:require-match t :require-match t
:sort counsel-projectile-sort-files :sort counsel-projectile-sort-files
:action counsel-projectile-find-file-action :action counsel-projectile-find-file-action
:caller 'counsel-projectile-find-file))) :caller 'counsel-projectile-find-file))))
(ivy-set-display-transformer (ivy-set-display-transformer
'counsel-projectile-find-file 'counsel-projectile-find-file
@ -463,7 +466,7 @@ 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 (_) (defun counsel-projectile-find-dir-action-switch-project (&optional _)
"Switch project action for `counsel-projectile-find-dir'." "Switch project action for `counsel-projectile-find-dir'."
(counsel-projectile-switch-project 'counsel-projectile-switch-project-action-find-dir)) (counsel-projectile-switch-project 'counsel-projectile-switch-project-action-find-dir))
@ -477,13 +480,16 @@ The sorting function can be modified by adding an entry for
With a prefix ARG, invalidate the cache first." With a prefix ARG, invalidate the cache first."
(interactive "P") (interactive "P")
(if (and (eq projectile-require-project-root 'prompt)
(not (projectile-project-p)))
(counsel-projectile-find-dir-action-switch-project)
(projectile-maybe-invalidate-cache arg) (projectile-maybe-invalidate-cache arg)
(ivy-read (projectile-prepend-project-name "Find dir: ") (ivy-read (projectile-prepend-project-name "Find dir: ")
(counsel-projectile--project-directories) (counsel-projectile--project-directories)
:require-match t :require-match t
:sort counsel-projectile-sort-directories :sort counsel-projectile-sort-directories
:action counsel-projectile-find-dir-action :action counsel-projectile-find-dir-action
:caller 'counsel-projectile-find-dir)) :caller 'counsel-projectile-find-dir)))
(ivy-set-display-transformer (ivy-set-display-transformer
'counsel-projectile-find-dir 'counsel-projectile-find-dir
@ -558,7 +564,7 @@ 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 (_) (defun counsel-projectile-switch-to-buffer-action-switch-project (&optional _)
"Switch project action for `counsel-projectile-switch-to-buffer'." "Switch project action for `counsel-projectile-switch-to-buffer'."
(counsel-projectile-switch-project 'counsel-projectile-switch-project-action-switch-to-buffer)) (counsel-projectile-switch-project 'counsel-projectile-switch-project-action-switch-to-buffer))
@ -573,6 +579,9 @@ This simply applies the same transformer as in `ivy-switch-buffer', which is `iv
(defun counsel-projectile-switch-to-buffer () (defun counsel-projectile-switch-to-buffer ()
"Jump to a buffer in the current project." "Jump to a buffer in the current project."
(interactive) (interactive)
(if (and (eq projectile-require-project-root 'prompt)
(not (projectile-project-p)))
(counsel-projectile-switch-to-buffer-action-switch-project)
(ivy-read (projectile-prepend-project-name "Switch to buffer: ") (ivy-read (projectile-prepend-project-name "Switch to buffer: ")
;; We use a collection function so that it is called each ;; We use a collection function so that it is called each
;; time the `ivy-state' is reset. This is needed for the ;; time the `ivy-state' is reset. This is needed for the
@ -583,7 +592,7 @@ This simply applies the same transformer as in `ivy-switch-buffer', which is `iv
:sort counsel-projectile-sort-buffers :sort counsel-projectile-sort-buffers
:action counsel-projectile-switch-to-buffer-action :action counsel-projectile-switch-to-buffer-action
:keymap counsel-projectile-switch-to-buffer-map :keymap counsel-projectile-switch-to-buffer-map
:caller 'counsel-projectile-switch-to-buffer)) :caller 'counsel-projectile-switch-to-buffer)))
(ivy-set-display-transformer (ivy-set-display-transformer
'counsel-projectile-switch-to-buffer 'counsel-projectile-switch-to-buffer
@ -645,11 +654,11 @@ construct the command.")
(shell-quote-argument regex))) (shell-quote-argument regex)))
nil))) nil)))
(defun counsel-projectile-grep-action-switch-project (_) (defun counsel-projectile-grep-action-switch-project (&optional _)
"Switch project action for `counsel-projectile-grep'." "Switch project action for `counsel-projectile-grep'."
(counsel-projectile-switch-project 'counsel-projectile-switch-project-action-grep)) (counsel-projectile-switch-project 'counsel-projectile-switch-project-action-grep))
(defun counsel-projectile-git-grep-action-switch-project (_) (defun counsel-projectile-git-grep-action-switch-project (&optional _)
"Switch project action for `counsel-projectile-git-grep'." "Switch project action for `counsel-projectile-git-grep'."
(counsel-projectile-switch-project 'counsel-projectile-switch-project-action-git-grep)) (counsel-projectile-switch-project 'counsel-projectile-switch-project-action-git-grep))
@ -693,6 +702,9 @@ additional options to be passed to grep, or an alternative git
grep command. It is read from the minibuffer if the function is grep command. It is read from the minibuffer if the function is
called with a prefix argument." called with a prefix argument."
(interactive) (interactive)
(if (and (eq projectile-require-project-root 'prompt)
(not (projectile-project-p)))
(counsel-projectile-grep-action-switch-project)
(if (and (eq (projectile-project-vcs) 'git) (if (and (eq (projectile-project-vcs) 'git)
projectile-use-git-grep) projectile-use-git-grep)
(counsel-projectile-git-grep options-or-cmd) (counsel-projectile-git-grep options-or-cmd)
@ -729,7 +741,7 @@ called with a prefix argument."
:unwind (lambda () :unwind (lambda ()
(counsel-delete-process) (counsel-delete-process)
(swiper--cleanup)) (swiper--cleanup))
:caller 'counsel-projectile-grep)))) :caller 'counsel-projectile-grep)))))
(counsel-set-async-exit-code 'counsel-projectile-grep 1 "No matches found") (counsel-set-async-exit-code 'counsel-projectile-grep 1 "No matches found")
(ivy-set-occur 'counsel-projectile-grep 'counsel-projectile-grep-occur) (ivy-set-occur 'counsel-projectile-grep 'counsel-projectile-grep-occur)
@ -743,6 +755,9 @@ CMD, if non-nil, is a string containing an alternative git grep
command. It is read from the minibuffer if the function is called command. It is read from the minibuffer if the function is called
with a prefix argument." with a prefix argument."
(interactive) (interactive)
(if (and (eq projectile-require-project-root 'prompt)
(not (projectile-project-p)))
(counsel-projectile-git-grep-action-switch-project)
(let* ((ivy--actions-list (copy-sequence ivy--actions-list)) (let* ((ivy--actions-list (copy-sequence ivy--actions-list))
(path (path
(mapconcat 'shell-quote-argument (mapconcat 'shell-quote-argument
@ -756,7 +771,7 @@ with a prefix argument."
'counsel-git-grep 'counsel-git-grep
counsel-projectile-git-grep-extra-actions) counsel-projectile-git-grep-extra-actions)
(counsel-git-grep (or current-prefix-arg cmd) (counsel-git-grep (or current-prefix-arg cmd)
counsel-projectile-grep-initial-input))) counsel-projectile-grep-initial-input))))
;;* counsel-projectile-ag ;;* counsel-projectile-ag
@ -788,7 +803,7 @@ 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 (_) (defun counsel-projectile-ag-action-switch-project (&optional _)
"Switch project action for `counsel-projectile-ag'." "Switch project action for `counsel-projectile-ag'."
(counsel-projectile-switch-project 'counsel-projectile-switch-project-action-ag)) (counsel-projectile-switch-project 'counsel-projectile-switch-project-action-ag))
@ -800,6 +815,9 @@ OPTIONS, if non-nil, is a string containing additional options to
be passed to ag. It is read from the minibuffer if the function 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 (and (eq projectile-require-project-root 'prompt)
(not (projectile-project-p)))
(counsel-projectile-ag-action-switch-project)
(let* ((ivy--actions-list (copy-sequence ivy--actions-list)) (let* ((ivy--actions-list (copy-sequence ivy--actions-list))
(path (mapconcat 'shell-quote-argument (path (mapconcat 'shell-quote-argument
(projectile-normalise-paths (projectile-normalise-paths
@ -823,7 +841,7 @@ is called with a prefix argument."
(projectile-project-root) (projectile-project-root)
options options
(projectile-prepend-project-name (projectile-prepend-project-name
(car (split-string counsel-ag-base-command)))))) (car (split-string counsel-ag-base-command)))))))
;;* counsel-projectile-rg ;;* counsel-projectile-rg
@ -855,7 +873,7 @@ 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 (_) (defun counsel-projectile-rg-action-switch-project (&optional _)
"Switch project action for `counsel-projectile-rg'." "Switch project action for `counsel-projectile-rg'."
(counsel-projectile-switch-project 'counsel-projectile-switch-project-action-rg)) (counsel-projectile-switch-project 'counsel-projectile-switch-project-action-rg))
@ -867,6 +885,9 @@ OPTIONS, if non-nil, is a string containing additional options to
be passed to rg. It is read from the minibuffer if the function 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 (and (eq projectile-require-project-root 'prompt)
(not (projectile-project-p)))
(counsel-projectile-rg-action-switch-project)
(let* ((ivy--actions-list (copy-sequence ivy--actions-list)) (let* ((ivy--actions-list (copy-sequence ivy--actions-list))
(path (path
(mapconcat 'shell-quote-argument (mapconcat 'shell-quote-argument
@ -892,7 +913,7 @@ is called with a prefix argument."
(projectile-project-root) (projectile-project-root)
options options
(projectile-prepend-project-name (projectile-prepend-project-name
(car (split-string counsel-rg-base-command)))))) (car (split-string counsel-rg-base-command)))))))
;;* counsel-projectile-org-capture ;;* counsel-projectile-org-capture
@ -1047,7 +1068,7 @@ The format is the same as in `org-capture-templates-contexts'."
(defvar counsel-projectile--org-capture-templates-backup nil (defvar counsel-projectile--org-capture-templates-backup nil
"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 (&optional _)
"Switch project action for `counsel-projectile-org-capture'." "Switch project action for `counsel-projectile-org-capture'."
(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-action-org-capture)) (counsel-projectile-switch-project 'counsel-projectile-switch-project-action-org-capture))
@ -1125,13 +1146,17 @@ agenda files that do not belong to the current project.
Optional arguments ARG, KEYS, and RESTRICTION are as in Optional arguments ARG, KEYS, and RESTRICTION are as in
`org-agenda'." `org-agenda'."
(interactive "P") (interactive "P")
(if (and (eq projectile-require-project-root 'prompt)
(not (projectile-project-p)))
(counsel-projectile-switch-project
'counsel-projectile-switch-project-action-org-agenda)
(require 'org-agenda) (require 'org-agenda)
(let* ((root (projectile-project-root)) (let* ((root (projectile-project-root))
(org-agenda-files (org-agenda-files
(cl-remove-if-not (lambda (file) (cl-remove-if-not (lambda (file)
(string-prefix-p root file)) (string-prefix-p root file))
(org-agenda-files t 'ifmode)))) (org-agenda-files t 'ifmode))))
(org-agenda arg keys restriction))) (org-agenda arg keys restriction))))
;;* counsel-projectile-switch-project ;;* counsel-projectile-switch-project
@ -1500,7 +1525,7 @@ 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 (_) (defun counsel-projectile-action-switch-project (&optional _)
"Switch project action for `counsel-projectile'." "Switch project action for `counsel-projectile'."
(counsel-projectile-switch-project 'counsel-projectile-switch-project-action)) (counsel-projectile-switch-project 'counsel-projectile-switch-project-action))
@ -1518,8 +1543,9 @@ With a prefix ARG, invalidate the cache first.
If not inside a project, call `counsel-projectile-switch-project'." If not inside a project, call `counsel-projectile-switch-project'."
(interactive "P") (interactive "P")
(if (not (projectile-project-p)) (if (and (eq projectile-require-project-root 'prompt)
(counsel-projectile-switch-project) (not (projectile-project-p)))
(counsel-projectile-action-switch-project)
(projectile-maybe-invalidate-cache arg) (projectile-maybe-invalidate-cache arg)
(ivy-read (projectile-prepend-project-name "Load buffer or file: ") (ivy-read (projectile-prepend-project-name "Load buffer or file: ")
;; We use a collection function so that it is called each ;; We use a collection function so that it is called each