Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] A customization mechanism for ivy-height #1531

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 25 additions & 8 deletions counsel.el
Expand Up @@ -256,7 +256,6 @@ Update the minibuffer with the amount of lines collected every
(car bnd)
(cdr bnd))
""))
(ivy-height 7)
(pred (and (eq (char-before (car bnd)) ?\()
#'fboundp))
symbol-names)
Expand All @@ -269,10 +268,13 @@ Update the minibuffer with the amount of lines collected every
(push (symbol-name x) symbol-names))))
(setq symbol-names (all-completions str obarray pred)))
(ivy-read "Symbol name: " symbol-names
:caller 'counsel-el
:predicate pred
:initial-input str
:action #'ivy-completion-in-region-action)))

(add-to-list 'ivy-height-alist '(counsel-el . 7))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring of add-to-list says

This is handy to add some elements to configuration variables,
but please do not abuse it in Elisp code, where you are usually
better off using ‘push’ or ‘cl-pushnew’.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's precisely configuration stuff.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mookid While you're not wrong, and it shouldn't make a difference here, "configuration" generally refers to user customisations, i.e. init file code. In particular, add-to-list can misbehave under lexical scope/extent.


;;** `counsel-cl'
(declare-function slime-symbol-start-pos "ext:slime")
(declare-function slime-symbol-end-pos "ext:slime")
Expand Down Expand Up @@ -331,14 +333,16 @@ Update the minibuffer with the amount of lines collected every
(str (buffer-substring-no-properties
(car bnd) (cdr bnd)))
(candidates (funcall completion-fn str))
(ivy-height 7)
(res (ivy-read (format "pattern (%s): " str)
candidates)))
candidates
:caller 'counsel--generic)))
(when (stringp res)
(when bnd
(delete-region (car bnd) (cdr bnd)))
(insert res))))

(add-to-list 'ivy-height-alist '(counsel--generic . 7))

;;;###autoload
(defun counsel-clj ()
"Clojure completion at point."
Expand Down Expand Up @@ -1573,14 +1577,15 @@ Does not list the currently checked out one."
(let ((counsel-async-split-string-re counsel-git-log-split-string-re)
(counsel-async-ignore-re "^[ \n]*$")
(counsel-yank-pop-truncate-radius 5)
(ivy-format-function #'counsel--yank-pop-format-function)
(ivy-height 4))
(ivy-format-function #'counsel--yank-pop-format-function))
(ivy-read "Grep log: " #'counsel-git-log-function
:dynamic-collection t
:action #'counsel-git-log-action
:unwind #'counsel-delete-process
:caller 'counsel-git-log)))

(add-to-list 'ivy-height-alist '(counsel-git-log . 4))

;;* File
;;** `counsel-find-file'
(defvar counsel-find-file-map
Expand Down Expand Up @@ -3212,6 +3217,11 @@ A is the left hand side, B the right hand side."
:group 'ivy
:type 'integer)

(make-obsolete-variable
'counsel-yank-pop-height
"use `ivy-height-alist' instead."
"<2018-04-14 Fri>") ;; TODO add version

(defun counsel--yank-pop-format-function (cand-pairs)
"Transform CAND-PAIRS into a string for `counsel-yank-pop'."
(ivy--format-function-generic
Expand Down Expand Up @@ -3328,7 +3338,6 @@ Note: Duplicate elements of `kill-ring' are always deleted."
;; Do not specify `*' to allow browsing `kill-ring' in read-only buffers
(interactive "P")
(let ((ivy-format-function #'counsel--yank-pop-format-function)
(ivy-height counsel-yank-pop-height)
(kills (counsel--yank-pop-kills)))
(unless kills
(error "Kill ring is empty or blank"))
Expand All @@ -3351,6 +3360,8 @@ Note: Duplicate elements of `kill-ring' are always deleted."
:action #'counsel-yank-pop-action
:caller 'counsel-yank-pop)))

(add-to-list 'ivy-height-alist '(counsel-yank-pop . 5))

(ivy-set-actions
'counsel-yank-pop
'(("d" counsel-yank-pop-action-remove "delete")
Expand All @@ -3362,12 +3373,16 @@ Note: Duplicate elements of `kill-ring' are always deleted."
:group 'ivy
:type 'integer)

(make-obsolete-variable
'counsel-evil-registers
"use `ivy-height-alist' instead."
"<2018-04-14 Fri>") ;; TODO add version

(defun counsel-evil-registers ()
"Ivy replacement for `evil-show-registers'."
(interactive)
(if (fboundp 'evil-register-list)
(let ((ivy-format-function #'counsel--yank-pop-format-function)
(ivy-height counsel-evil-registers-height))
(let ((ivy-format-function #'counsel--yank-pop-format-function))
(ivy-read "evil-registers: "
(cl-loop for (key . val) in (evil-register-list)
collect (format "[%c]: %s" key (if (stringp val) val "")))
Expand All @@ -3376,6 +3391,8 @@ Note: Duplicate elements of `kill-ring' are always deleted."
:caller 'counsel-evil-registers))
(user-error "Required feature `evil' not installed.")))

(add-to-list 'ivy-height-alist '(counsel-evil-registers . 5))

(defun counsel-evil-registers-action (s)
"Paste contents of S, trimming the register part.

Expand Down
19 changes: 17 additions & 2 deletions ivy.el
Expand Up @@ -132,7 +132,9 @@
(setcdr (assoc load-file-name custom-current-group-alist) 'ivy)

(defcustom ivy-height 10
"Number of lines for the minibuffer window."
"Number of lines for the minibuffer window.

See also `ivy-height-alist'."
:type 'integer)

(defcustom ivy-count-format "%-4d "
Expand Down Expand Up @@ -227,6 +229,13 @@ Examples of properties include associated `:cleanup' functions.")
"An alist of handlers to replace `completing-read' in `ivy-mode'."
:type '(alist :key-type function :value-type function))

(defcustom ivy-height-alist nil
"An alist to customize `ivy-height'.

It is a list of (CALLER . HEIGHT). CALLER is a caller of
`ivy-read' and HEIGHT is the number of lines displayed."
:type '(alist :key-type function :value-type integer))

(defvar ivy-completing-read-ignore-handlers-depth -1
"Used to avoid infinite recursion.

Expand Down Expand Up @@ -1700,7 +1709,12 @@ customizations apply to the current completion session."
(unless (window-minibuffer-p)
(or ivy-display-function
(cdr (or (assq caller ivy-display-functions-alist)
(assq t ivy-display-functions-alist)))))))
(assq t ivy-display-functions-alist))))))
(height
(if caller
(let ((entry (assoc caller ivy-height-alist)))
(if entry (cdr entry) ivy-height))
ivy-height)))
(setq ivy-last
(make-ivy-state
:prompt prompt
Expand Down Expand Up @@ -1733,6 +1747,7 @@ customizations apply to the current completion session."
(let* ((hist (or history 'ivy-history))
(minibuffer-completion-table collection)
(minibuffer-completion-predicate predicate)
(ivy-height height)
(resize-mini-windows
(cond
((display-graphic-p) nil)
Expand Down