Skip to content

Commit

Permalink
Preselect current file when showing list of files
Browse files Browse the repository at this point in the history
  • Loading branch information
neojski committed Jan 13, 2018
1 parent 6da6d70 commit c643a5c
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions counsel.el
Expand Up @@ -1661,6 +1661,11 @@ Does not list the currently checked out one."
:type 'boolean
:group 'ivy)

(defcustom counsel-preselect-current-file nil
"When non-nil, preselect current file in list of candidates."
:type 'boolean
:group 'ivy)

(defcustom counsel-find-file-ignore-regexp nil
"A regexp of files to ignore while in `counsel-find-file'.
These files are un-ignored if `ivy-text' matches them. The
Expand Down Expand Up @@ -1711,6 +1716,17 @@ Skip some dotfiles unless `ivy-text' requires them."
(find-file (expand-file-name x ivy--directory)))
(find-file (expand-file-name x ivy--directory)))))

(defun counsel--preselect-file ()
"Chooses the file to preselect in find-file like completions depending on customizations"
(or
(when counsel-find-file-at-point
(require 'ffap)
(let ((f (ffap-guesser)))
(when f (expand-file-name f))))
(and counsel-preselect-current-file
buffer-file-name
(file-name-nondirectory buffer-file-name))))

;;;###autoload
(defun counsel-find-file (&optional initial-input)
"Forward to `find-file'.
Expand All @@ -1720,11 +1736,7 @@ When INITIAL-INPUT is non-nil, use it in the minibuffer during completion."
:matcher #'counsel--find-file-matcher
:initial-input initial-input
:action #'counsel-find-file-action
:preselect (when counsel-find-file-at-point
(require 'ffap)
(let ((f (ffap-guesser)))
(when (and f (not (ffap-url-p f)))
(expand-file-name f))))
:preselect (counsel--preselect-file)
:require-match 'confirm-after-completion
:history 'file-name-history
:keymap counsel-find-file-map
Expand Down Expand Up @@ -2145,10 +2157,7 @@ INITIAL-DIRECTORY, if non-nil, is used as the root directory for search."
:action (lambda (x)
(with-ivy-window
(find-file (expand-file-name x ivy--directory))))
:preselect (when counsel-find-file-at-point
(require 'ffap)
(let ((f (ffap-guesser)))
(when f (expand-file-name f))))
:preselect (counsel--preselect-file)
:require-match 'confirm-after-completion
:history 'file-name-history
:keymap counsel-find-file-map
Expand Down

0 comments on commit c643a5c

Please sign in to comment.