branch: master commit 9970ae60b60dca532b9aed2ec56773786d9519f0 Author: Hong Xu <h...@topbug.net> Commit: Oleh Krehel <ohwoeo...@gmail.com>
counsel.el (counsel-read-directory-name): Add This is a partial replacement of `read-directory-name', except that DEFAULT-DIRNAME does not have any effect. This should be able to mitigate the issue mentioned in #1646 to allow more flexibility regarding customized key bindings. Fixes #2277 --- counsel.el | 31 ++++++++++++++++++++++--------- ivy-test.el | 8 ++++++++ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/counsel.el b/counsel.el index 8f92523..a6fe736 100644 --- a/counsel.el +++ b/counsel.el @@ -2540,9 +2540,9 @@ FZF-PROMPT, if non-nil, is passed as `ivy-read' prompt argument." (let ((fzf-basename (car (split-string counsel-fzf-cmd)))) (list nil (when current-prefix-arg - (read-directory-name (concat - fzf-basename - " in directory: ")))))) + (counsel-read-directory-name (concat + fzf-basename + " in directory: ")))))) (counsel-require-program counsel-fzf-cmd) (setq counsel--fzf-dir (or initial-directory @@ -2651,7 +2651,7 @@ INITIAL-DIRECTORY, if non-nil, is used as the root directory for search." (interactive (list nil (when current-prefix-arg - (read-directory-name "From directory: ")))) + (counsel-read-directory-name "From directory: ")))) (counsel-require-program find-program) (let ((default-directory (or initial-directory default-directory))) (ivy-read "Find file: " @@ -2685,7 +2685,7 @@ INITIAL-DIRECTORY, if non-nil, is used as the root directory for search." (interactive (list nil (when current-prefix-arg - (read-directory-name "From directory: ")))) + (counsel-read-directory-name "From directory: ")))) (counsel-require-program find-program) (let ((default-directory (or initial-directory default-directory))) (ivy-read "Find directory: " @@ -2813,9 +2813,9 @@ CALLER is passed to `ivy-read'." (when current-prefix-arg (setq initial-directory (or initial-directory - (read-directory-name (concat - (car (split-string counsel-ag-command)) - " in directory: ")))) + (counsel-read-directory-name (concat + (car (split-string counsel-ag-command)) + " in directory: ")))) (setq extra-ag-args (or extra-ag-args (read-from-minibuffer (format @@ -2842,12 +2842,25 @@ CALLER is passed to `ivy-read'." :grep-p t :exit-codes '(1 "No matches found")) +(defun counsel-read-directory-name (prompt) + "Read a directory name from user, a (partial) replacement of `read-directory-name'." + (let ((counsel--find-file-predicate #'file-directory-p)) + (ivy-read prompt + #'read-file-name-internal + :matcher #'counsel--find-file-matcher + :history 'file-name-history + :keymap counsel-find-file-map + :caller 'counsel-read-directory-name))) + +(ivy-configure 'counsel-read-directory-name + :display-transformer-fn #'ivy-read-file-transformer) + (defun counsel-cd () "Change the directory for the currently running Ivy grep-like command. Works for `counsel-git-grep', `counsel-ag', etc." (interactive) (let ((input ivy-text) - (new-dir (read-directory-name "cd: "))) + (new-dir (counsel-read-directory-name "cd: "))) (ivy-quit-and-run (funcall (ivy-state-caller ivy-last) input new-dir)))) diff --git a/ivy-test.el b/ivy-test.el index 9a645fd..c15cf2f 100644 --- a/ivy-test.el +++ b/ivy-test.el @@ -975,6 +975,14 @@ will bring the behavior in line with the newer Emacsen." "DEL C-M-j" :dir "/tmp")))) +(ert-deftest ivy-counsel-read-directory-name () + (should + (equal (expand-file-name "/tmp/") + (ivy-with + '(counsel-read-directory-name "cd: ") + "RET" + :dir "/tmp/")))) + (ert-deftest ivy-partial-files () (when (file-exists-p "/tmp/ivy-partial-test") (delete-directory "/tmp/ivy-partial-test" t))