[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master ea79f0e 135/272: New API function ivy-set-display-transfor
From: |
Oleh Krehel |
Subject: |
[elpa] master ea79f0e 135/272: New API function ivy-set-display-transformer |
Date: |
Mon, 25 Apr 2016 10:13:21 +0000 |
branch: master
commit ea79f0e487ef444f0d01b7498fcb25de6f5d0d3a
Author: Oleh Krehel <address@hidden>
Commit: Oleh Krehel <address@hidden>
New API function ivy-set-display-transformer
* ivy.el (ivy--display-transformers-list): New defvar.
(ivy-set-display-transformer): New defun. Keys are :caller, values are
str->str lambda.
(ivy-state): New field display-transformer-fn.
(ivy-read): Set :display-transformer-fn.
(ivy--format): Apply :display-transformer-fn to each displayed
candidate, in the context of ivy-window.
* counsel.el (ivy-set-display-transformer): Set for `counsel-M-x'.
(counsel-M-x-transformer): Promote from `counsel--M-x-transformer'.
Now takes a string instead of a cons cell.
(counsel-M-x): No more messing with `ivy-format-function'.
Re #399
---
counsel.el | 31 +++++++++++--------------------
ivy.el | 35 +++++++++++++++++++++++++++++------
2 files changed, 40 insertions(+), 26 deletions(-)
diff --git a/counsel.el b/counsel.el
index e6a2bad..fc69a1e 100644
--- a/counsel.el
+++ b/counsel.el
@@ -445,19 +445,18 @@ Update the minibuffer with the amount of lines collected
every
'counsel-M-x
'(("d" counsel--find-symbol "definition")))
-(defun counsel--M-x-transformer (cand-pair)
- "Add a binding to CAND-PAIR cdr if the car is bound in the current window.
-CAND-PAIR is (command-name . extra-info)."
- (let* ((command-name (car cand-pair))
- (extra-info (cdr cand-pair))
- (binding (substitute-command-keys (format "\\[%s]" command-name))))
+(ivy-set-display-transformer
+ 'counsel-M-x
+ 'counsel-M-x-transformer)
+
+(defun counsel-M-x-transformer (cmd)
+ "Return CMD appended with the corresponding binding in the current window."
+ (let ((binding (substitute-command-keys (format "\\[%s]" cmd))))
(setq binding (replace-regexp-in-string "C-x 6" "<f2>" binding))
(if (string-match "^M-x" binding)
- cand-pair
- (cons command-name
- (if extra-info
- (format " %s (%s)" extra-info (propertize binding 'face
'font-lock-keyword-face))
- (format " (%s)" (propertize binding 'face
'font-lock-keyword-face)))))))
+ cmd
+ (format "%s (%s)"
+ cmd (propertize binding 'face 'font-lock-keyword-face)))))
(defvar smex-initialized-p)
(defvar smex-ido-cache)
@@ -488,14 +487,7 @@ Optional INITIAL-INPUT is the initial input in the
minibuffer."
(unless initial-input
(setq initial-input (cdr (assoc this-command
ivy-initial-inputs-alist))))
- (let* ((store ivy-format-function)
- (ivy-format-function
- (lambda (cand-pairs)
- (funcall
- store
- (with-ivy-window
- (mapcar #'counsel--M-x-transformer cand-pairs)))))
- (cands obarray)
+ (let* ((cands obarray)
(pred 'commandp)
(sort t))
(when (require 'smex nil 'noerror)
@@ -515,7 +507,6 @@ Optional INITIAL-INPUT is the initial input in the
minibuffer."
(when (featurep 'smex)
(smex-rank (intern cmd)))
(let ((prefix-arg current-prefix-arg)
- (ivy-format-function store)
(this-command (intern cmd)))
(command-execute (intern cmd) 'record)))
:sort sort
diff --git a/ivy.el b/ivy.el
index 5e59717..bff2454 100644
--- a/ivy.el
+++ b/ivy.el
@@ -168,6 +168,21 @@ Only \"./\" and \"../\" apply here. They appear in reverse
order."
(setq ivy--actions-list
(plist-put ivy--actions-list cmd actions)))
+(defvar ivy--display-transformers-list nil
+ "A list of str->str transformers per command.")
+
+(defun ivy-set-display-transformer (cmd transformer)
+ "Set CMD a displayed candidate TRANSFORMER.
+
+It's a lambda that takes a string one of the candidates in the
+collection and returns a string for display, the same candidate
+plus some extra information.
+
+This lambda is called only on the `ivy-height' candidates that
+are about to be displayed, not on the whole collection."
+ (setq ivy--display-transformers-list
+ (plist-put ivy--display-transformers-list cmd transformer)))
+
(defvar ivy--sources-list nil
"A list of extra sources per command.")
@@ -269,6 +284,8 @@ Example:
matcher
;; When this is non-nil, call it for each input change to get new candidates
dynamic-collection
+ ;; A lambda that transforms candidates only for display
+ display-transformer-fn
caller)
(defvar ivy-last (make-ivy-state)
@@ -1241,7 +1258,8 @@ customizations apply to the current completion session."
(list (car source) (funcall (car source)))
ivy--extra-candidates))))))
(setq ivy--extra-candidates '((original-source)))))
- (let ((recursive-ivy-last (and (active-minibuffer-window) ivy-last)))
+ (let ((recursive-ivy-last (and (active-minibuffer-window) ivy-last))
+ (transformer-fn (plist-get ivy--display-transformers-list caller)))
(setq ivy-last
(make-ivy-state
:prompt prompt
@@ -1261,6 +1279,7 @@ customizations apply to the current completion session."
:re-builder re-builder
:matcher matcher
:dynamic-collection dynamic-collection
+ :display-transformer-fn transformer-fn
:caller caller))
(ivy--reset-state ivy-last)
(prog1
@@ -2380,7 +2399,8 @@ CANDS is a list of strings."
(end (min (+ start (1- ivy-height)) ivy--length))
(start (max 0 (min start (- end (1- ivy-height)))))
(cands (cl-subseq cands start end))
- (index (- ivy--index start)))
+ (index (- ivy--index start))
+ transformer-fn)
(cond (ivy--directory
(setq cands (mapcar (lambda (x)
(if (string-match-p "/\\'" x)
@@ -2397,11 +2417,14 @@ CANDS is a list of strings."
x)))
cands))))
(setq ivy--current (copy-sequence (nth index cands)))
+ (when (setq transformer-fn (ivy-state-display-transformer-fn ivy-last))
+ (with-ivy-window
+ (setq cands (mapcar transformer-fn cands))))
(let* ((ivy--index index)
- (cand-pairs (mapcar
- (lambda (cand)
- (cons (ivy--format-minibuffer-line cand) nil))
cands))
- (res (concat "\n" (funcall ivy-format-function cand-pairs))))
+ (cands (mapcar
+ (lambda (cand)
+ (cons (ivy--format-minibuffer-line cand) nil)) cands))
+ (res (concat "\n" (funcall ivy-format-function cands))))
(put-text-property 0 (length res) 'read-only nil res)
res))))
- [elpa] master 7f6d39f 193/272: counsel.el (counsel-linux-app-action-file): New action on "M-o f", (continued)
- [elpa] master 7f6d39f 193/272: counsel.el (counsel-linux-app-action-file): New action on "M-o f", Oleh Krehel, 2016/04/25
- [elpa] master ac4bedf 175/272: counsel.el (counsel-find-file): Bind "M-o f" to find-file-other-window, Oleh Krehel, 2016/04/25
- [elpa] master 41e2e41 246/272: Clean-up previous change, Oleh Krehel, 2016/04/25
- [elpa] master 90a09fa 086/272: doc/ivy.org: CUSTOM_ID should not end in "?", Oleh Krehel, 2016/04/25
- [elpa] master 6254e88 244/272: Allow a sexp node in ivy-views, Oleh Krehel, 2016/04/25
- [elpa] master e224847 179/272: ivy.el (ivy--reset-state): Fix prompt / require-match issue, Oleh Krehel, 2016/04/25
- [elpa] master 5229ebe 173/272: counsel.el (counsel-prompt-function-dir): Fix format, Oleh Krehel, 2016/04/25
- [elpa] master 8de2930 221/272: ivy.el: Improve "M-n", Oleh Krehel, 2016/04/25
- [elpa] master 6823518 196/272: swiper.el (swiper--candidates): Always remove '(field), Oleh Krehel, 2016/04/25
- [elpa] master d45ee54 258/272: Make the slash more magic during file name completion, Oleh Krehel, 2016/04/25
- [elpa] master ea79f0e 135/272: New API function ivy-set-display-transformer,
Oleh Krehel <=
- [elpa] master b2d2b82 209/272: counsel.el (counsel-grep-action): Improve cleanup, Oleh Krehel, 2016/04/25
- [elpa] master 8c4f595 233/272: Fix remapping commit c9b6e0, Oleh Krehel, 2016/04/25
- [elpa] master 6fae6e0 259/272: counsel.el (counsel-grep-or-swiper): Use swiper when narrowed, Oleh Krehel, 2016/04/25
- [elpa] master 1b4e819 218/272: ivy.el (ivy-next-history-element): Simplify, Oleh Krehel, 2016/04/25
- [elpa] master b5a9dc5 255/272: ivy.el (ivy--reset-state): file-directory-p needs a string, Oleh Krehel, 2016/04/25
- [elpa] master 6fc36b5 210/272: option to add newline after ivy-read prompt, Oleh Krehel, 2016/04/25
- [elpa] master 9899547 235/272: ivy.el (ivy-action): New face, Oleh Krehel, 2016/04/25
- [elpa] master 63b96a1 250/272: Fix counsel-grep-last-line bug, Oleh Krehel, 2016/04/25
- [elpa] master a891df3 264/272: swiper.el (swiper--multi-candidates): Add check, Oleh Krehel, 2016/04/25
- [elpa] master 5ce2623 223/272: counsel.el (counsel-git): Add "j" action, Oleh Krehel, 2016/04/25