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,16 +385,19 @@ 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")
(projectile-maybe-invalidate-cache arg) (if (and (eq projectile-require-project-root 'prompt)
(let* ((project-files (projectile-current-project-files)) (not (projectile-project-p)))
(files (and dwim (projectile-select-files project-files)))) (counsel-projectile-find-file-action-switch-project)
(ivy-read (projectile-prepend-project-name "Find file: ") (projectile-maybe-invalidate-cache arg)
(or files project-files) (let* ((project-files (projectile-current-project-files))
:matcher counsel-projectile-find-file-matcher (files (and dwim (projectile-select-files project-files))))
:require-match t (ivy-read (projectile-prepend-project-name "Find file: ")
:sort counsel-projectile-sort-files (or files project-files)
:action counsel-projectile-find-file-action :matcher counsel-projectile-find-file-matcher
:caller 'counsel-projectile-find-file))) :require-match t
:sort counsel-projectile-sort-files
:action counsel-projectile-find-file-action
: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")
(projectile-maybe-invalidate-cache arg) (if (and (eq projectile-require-project-root 'prompt)
(ivy-read (projectile-prepend-project-name "Find dir: ") (not (projectile-project-p)))
(counsel-projectile--project-directories) (counsel-projectile-find-dir-action-switch-project)
:require-match t (projectile-maybe-invalidate-cache arg)
:sort counsel-projectile-sort-directories (ivy-read (projectile-prepend-project-name "Find dir: ")
:action counsel-projectile-find-dir-action (counsel-projectile--project-directories)
:caller 'counsel-projectile-find-dir)) :require-match t
:sort counsel-projectile-sort-directories
:action counsel-projectile-find-dir-action
: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,17 +579,20 @@ 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)
(ivy-read (projectile-prepend-project-name "Switch to buffer: ") (if (and (eq projectile-require-project-root 'prompt)
;; We use a collection function so that it is called each (not (projectile-project-p)))
;; time the `ivy-state' is reset. This is needed for the (counsel-projectile-switch-to-buffer-action-switch-project)
;; "kill buffer" action. (ivy-read (projectile-prepend-project-name "Switch to buffer: ")
#'counsel-projectile--project-buffers ;; We use a collection function so that it is called each
:matcher #'ivy--switch-buffer-matcher ;; time the `ivy-state' is reset. This is needed for the
:require-match t ;; "kill buffer" action.
:sort counsel-projectile-sort-buffers #'counsel-projectile--project-buffers
:action counsel-projectile-switch-to-buffer-action :matcher #'ivy--switch-buffer-matcher
:keymap counsel-projectile-switch-to-buffer-map :require-match t
:caller 'counsel-projectile-switch-to-buffer)) :sort counsel-projectile-sort-buffers
:action counsel-projectile-switch-to-buffer-action
:keymap counsel-projectile-switch-to-buffer-map
: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,43 +702,46 @@ 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-project-vcs) 'git) (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)
projectile-use-git-grep) projectile-use-git-grep)
(counsel-projectile-git-grep options-or-cmd) (counsel-projectile-git-grep options-or-cmd)
(let* ((path (let* ((path
(mapconcat 'shell-quote-argument (mapconcat 'shell-quote-argument
(projectile-normalise-paths (projectile-normalise-paths
(car (projectile-parse-dirconfig-file))) (car (projectile-parse-dirconfig-file)))
" ")) " "))
(ignored-files (ignored-files
(mapconcat (lambda (i) (mapconcat (lambda (i)
(concat "--exclude=" (shell-quote-argument i))) (concat "--exclude=" (shell-quote-argument i)))
(append (append
(projectile--globally-ignored-file-suffixes-glob) (projectile--globally-ignored-file-suffixes-glob)
(projectile-ignored-files-rel)) (projectile-ignored-files-rel))
" ")) " "))
(ignored-dirs (ignored-dirs
(mapconcat (lambda (i) (mapconcat (lambda (i)
(concat "--exclude-dir=" (shell-quote-argument i))) (concat "--exclude-dir=" (shell-quote-argument i)))
(projectile-ignored-directories-rel) (projectile-ignored-directories-rel)
" ")) " "))
(ignored (concat ignored-files " " ignored-dirs)) (ignored (concat ignored-files " " ignored-dirs))
(default-directory (projectile-project-root))) (default-directory (projectile-project-root)))
(counsel-require-program (counsel-require-program
(car (split-string counsel-projectile-grep-base-command))) (car (split-string counsel-projectile-grep-base-command)))
(setq counsel-projectile-grep-command (setq counsel-projectile-grep-command
(format counsel-projectile-grep-base-command ignored path)) (format counsel-projectile-grep-base-command ignored path))
(ivy-read (projectile-prepend-project-name "grep") (ivy-read (projectile-prepend-project-name "grep")
#'counsel-projectile-grep-function #'counsel-projectile-grep-function
:initial-input counsel-projectile-grep-initial-input :initial-input counsel-projectile-grep-initial-input
:dynamic-collection t :dynamic-collection t
:keymap counsel-ag-map :keymap counsel-ag-map
:history 'counsel-git-grep-history :history 'counsel-git-grep-history
:action counsel-projectile-grep-action :action counsel-projectile-grep-action
: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,20 +755,23 @@ 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)
(let* ((ivy--actions-list (copy-sequence ivy--actions-list)) (if (and (eq projectile-require-project-root 'prompt)
(path (not (projectile-project-p)))
(mapconcat 'shell-quote-argument (counsel-projectile-git-grep-action-switch-project)
(projectile-normalise-paths (let* ((ivy--actions-list (copy-sequence ivy--actions-list))
(car (projectile-parse-dirconfig-file))) (path
" ")) (mapconcat 'shell-quote-argument
(counsel-git-grep-cmd-default (projectile-normalise-paths
(concat (string-trim-right counsel-git-grep-cmd-default " \\.") (car (projectile-parse-dirconfig-file)))
" " path))) " "))
(ivy-add-actions (counsel-git-grep-cmd-default
'counsel-git-grep (concat (string-trim-right counsel-git-grep-cmd-default " \\.")
counsel-projectile-git-grep-extra-actions) " " path)))
(counsel-git-grep (or current-prefix-arg cmd) (ivy-add-actions
counsel-projectile-grep-initial-input))) 'counsel-git-grep
counsel-projectile-git-grep-extra-actions)
(counsel-git-grep (or current-prefix-arg cmd)
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,30 +815,33 @@ 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)
(let* ((ivy--actions-list (copy-sequence ivy--actions-list)) (if (and (eq projectile-require-project-root 'prompt)
(path (mapconcat 'shell-quote-argument (not (projectile-project-p)))
(projectile-normalise-paths (counsel-projectile-ag-action-switch-project)
(car (projectile-parse-dirconfig-file))) (let* ((ivy--actions-list (copy-sequence ivy--actions-list))
" ")) (path (mapconcat 'shell-quote-argument
(ignored (projectile-normalise-paths
(mapconcat (lambda (i) (car (projectile-parse-dirconfig-file)))
(concat "--ignore " (shell-quote-argument i))) " "))
(append (ignored
(projectile--globally-ignored-file-suffixes-glob) (mapconcat (lambda (i)
(projectile-ignored-files-rel) (concat "--ignore " (shell-quote-argument i)))
(projectile-ignored-directories-rel)) (append
" ")) (projectile--globally-ignored-file-suffixes-glob)
(counsel-ag-base-command (projectile-ignored-files-rel)
(format (string-trim-right counsel-ag-base-command " \\.") (projectile-ignored-directories-rel))
(concat ignored " %s " path)))) " "))
(ivy-add-actions (counsel-ag-base-command
'counsel-ag (format (string-trim-right counsel-ag-base-command " \\.")
counsel-projectile-ag-extra-actions) (concat ignored " %s " path))))
(counsel-ag (eval counsel-projectile-ag-initial-input) (ivy-add-actions
(projectile-project-root) 'counsel-ag
options counsel-projectile-ag-extra-actions)
(projectile-prepend-project-name (counsel-ag (eval counsel-projectile-ag-initial-input)
(car (split-string counsel-ag-base-command)))))) (projectile-project-root)
options
(projectile-prepend-project-name
(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,32 +885,35 @@ 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)
(let* ((ivy--actions-list (copy-sequence ivy--actions-list)) (if (and (eq projectile-require-project-root 'prompt)
(path (not (projectile-project-p)))
(mapconcat 'shell-quote-argument (counsel-projectile-rg-action-switch-project)
(or (projectile-normalise-paths (let* ((ivy--actions-list (copy-sequence ivy--actions-list))
(car (projectile-parse-dirconfig-file))) (path
'(".")) (mapconcat 'shell-quote-argument
" ")) (or (projectile-normalise-paths
(ignored (car (projectile-parse-dirconfig-file)))
(mapconcat (lambda (i) '("."))
(concat "--glob !" (shell-quote-argument i))) " "))
(append (ignored
(projectile--globally-ignored-file-suffixes-glob) (mapconcat (lambda (i)
(projectile-ignored-files-rel) (concat "--glob !" (shell-quote-argument i)))
(projectile-ignored-directories-rel)) (append
" ")) (projectile--globally-ignored-file-suffixes-glob)
(counsel-rg-base-command (projectile-ignored-files-rel)
(format (string-trim-right counsel-rg-base-command " \\.") (projectile-ignored-directories-rel))
(concat ignored " %s " path)))) " "))
(ivy-add-actions (counsel-rg-base-command
'counsel-ag (format (string-trim-right counsel-rg-base-command " \\.")
counsel-projectile-rg-extra-actions) (concat ignored " %s " path))))
(counsel-rg (eval counsel-projectile-rg-initial-input) (ivy-add-actions
(projectile-project-root) 'counsel-ag
options counsel-projectile-rg-extra-actions)
(projectile-prepend-project-name (counsel-rg (eval counsel-projectile-rg-initial-input)
(car (split-string counsel-rg-base-command)))))) (projectile-project-root)
options
(projectile-prepend-project-name
(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