emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] master 1fad831 10/22: Change `ivy-read' to a cl-defun


From: Oleh Krehel
Subject: [elpa] master 1fad831 10/22: Change `ivy-read' to a cl-defun
Date: Wed, 22 Apr 2015 19:51:49 +0000

branch: master
commit 1fad83142ed5512548b0d9e7a927da4145437591
Author: Oleh Krehel <address@hidden>
Commit: Oleh Krehel <address@hidden>

    Change `ivy-read' to a cl-defun
    
    * ivy.el (ivy-read): All args but PROMPT and COLLECTION are now keys.
    The existing basic calls to `ivy-read' should still work, the others
    need to be updated. It's best to try to use `ivy-completing-read' if
    possible, since it conforms to the `completing-read' arguments.
    New arguments: REQUIRE-MATCH and HISTORY. If HISTORY is nil, `ivy-history'
    is used.
    
    (ivy--sorted-files): Don't try to extend the collection.
    (ivy-completing-read): Update.
    
    * swiper.el (swiper--ivy): Update.
    
    * counsel.el (counsel-describe-symbol-history): New defvar.
    (counsel-describe-variable): Update the call, require match, use
    `counsel-describe-symbol-history'.
    (counsel-describe-function): Update the call, require match, use
    `counsel-describe-symbol-history'.
    
    * ivy-test.el: Update tests, since zero and one length input doesn't
      return immediately any more.
    
    Re #46
---
 counsel.el  |   26 ++++++++------
 ivy-test.el |    6 ---
 ivy.el      |  115 +++++++++++++++++++++++++++++------------------------------
 swiper.el   |    9 ++---
 4 files changed, 76 insertions(+), 80 deletions(-)

diff --git a/counsel.el b/counsel.el
index 5a2ac72..a99bda8 100644
--- a/counsel.el
+++ b/counsel.el
@@ -66,6 +66,9 @@
            (error "Couldn't fild definition of %s"
                   sym)))))
 
+(defvar counsel-describe-symbol-history nil
+  "History for `counsel-describe-variable' and `counsel-describe-function'.")
+
 (defun counsel-describe-variable (variable &optional buffer frame)
   "Forward to (`describe-variable' VARIABLE BUFFER FRAME)."
   (interactive
@@ -75,10 +78,7 @@
          val)
      (setq ivy--action nil)
      (setq val (ivy-read
-                (if (symbolp v)
-                    (format
-                     "Describe variable (default %s): " v)
-                  "Describe variable: ")
+                "Describe variable: "
                 (let (cands)
                   (mapatoms
                    (lambda (vv)
@@ -86,8 +86,11 @@
                                (and (boundp vv) (not (keywordp vv))))
                        (push (symbol-name vv) cands))))
                   cands)
-                nil nil counsel-describe-map preselect
-                nil t))
+                :keymap counsel-describe-map
+                :preselect preselect
+                :history 'counsel-describe-symbol-history
+                :require-match t
+                :sort t))
      (list (if (equal val "")
                v
              (intern val)))))
@@ -102,17 +105,18 @@
          (preselect (thing-at-point 'symbol))
          val)
      (setq ivy--action nil)
-     (setq val (ivy-read (if fn
-                             (format "Describe function (default %s): " fn)
-                           "Describe function: ")
+     (setq val (ivy-read "Describe function: "
                          (let (cands)
                            (mapatoms
                             (lambda (x)
                               (when (fboundp x)
                                 (push (symbol-name x) cands))))
                            cands)
-                         nil nil counsel-describe-map preselect
-                         nil t))
+                         :keymap counsel-describe-map
+                         :preselect preselect
+                         :history 'counsel-describe-symbol-history
+                         :require-match t
+                         :sort t))
      (list (if (equal val "")
                fn (intern val)))))
   (unless (eq ivy--action 'counsel--find-symbol)
diff --git a/ivy-test.el b/ivy-test.el
index 957f824..dfc1985 100644
--- a/ivy-test.el
+++ b/ivy-test.el
@@ -45,12 +45,6 @@
 
 (ert-deftest ivy-read ()
   (should (equal
-           (ivy-read "pattern: " nil)
-           nil))
-  (should (equal
-           (ivy-read "pattern: " '("42"))
-           "42"))
-  (should (equal
            (ivy-with '(ivy-read "pattern: " '("blue" "yellow"))
                      "C-m")
            "blue"))
diff --git a/ivy.el b/ivy.el
index 5ac652c..8619f3e 100644
--- a/ivy.el
+++ b/ivy.el
@@ -375,17 +375,12 @@ Directories come first."
         (setq seq (cl-sort seq sort-fn)))
       (dolist (dir ivy-extra-directories)
         (push dir seq))
-      (cl-case (length seq)
-        (0
-         '("" ""))
-        (1
-         (cons "" seq))
-        (t
-         seq)))))
+      seq)))
 
 ;;** Entry Point
-(defun ivy-read (prompt collection
-                 &optional predicate initial-input keymap preselect update-fn 
sort)
+(cl-defun ivy-read (prompt collection
+                           &key predicate require-match initial-input
+                           history preselect keymap update-fn sort)
   "Read a string in the minibuffer, with completion.
 
 PROMPT is a string to prompt with; normally it ends in a colon
@@ -406,6 +401,7 @@ UPDATE-FN is called each time the current candidate(s) is 
changed.
 
 When SORT is t, refer to `ivy-sort-functions-alist' for sorting."
   (setq ivy--directory nil)
+  (setq ivy-require-match require-match)
   (setq ivy-window (selected-window))
   (let (coll sort-fn)
     (cond ((eq collection 'Info-read-node-name-1)
@@ -421,7 +417,7 @@ When SORT is t, refer to `ivy-sort-functions-alist' for 
sorting."
            (setq coll
                  (ivy--sorted-files default-directory))
            (when initial-input
-             (unless (or ivy-require-match
+             (unless (or require-match
                          (equal initial-input default-directory))
                (setq coll (cons initial-input coll)))
              (setq initial-input nil)))
@@ -443,57 +439,54 @@ When SORT is t, refer to `ivy-sort-functions-alist' for 
sorting."
                  (<= (length coll) ivy-sort-max-size))
             (setq coll (cl-sort (copy-sequence coll) sort-fn)))))
     (when preselect
-      (unless (or ivy-require-match
+      (unless (or require-match
                   (all-completions preselect collection))
         (setq coll (cons preselect coll))))
-    (cl-case (length coll)
-      (0 nil)
-      (1 (car coll))
-      (t
-       (setq ivy--index (or
-                         (and preselect
-                              (ivy--preselect-index
-                               coll initial-input preselect))
-                         0))
-       (setq ivy--old-re nil)
-       (setq ivy--old-cands nil)
-       (setq ivy-text "")
-       (setq ivy--all-candidates coll)
-       (setq ivy--update-fn update-fn)
-       (setq ivy-exit nil)
-       (setq ivy--default (or (thing-at-point 'symbol) ""))
-       (setq ivy--prompt
-             (cond ((string-match "%.*d" prompt)
-                    prompt)
-                   ((string-match "%.*d" ivy-count-format)
-                    (concat ivy-count-format prompt))
-                   (ivy--directory
-                    prompt)
-                   (t
-                    nil)))
-       (setq ivy--action nil)
-       (prog1
-           (unwind-protect
-                (minibuffer-with-setup-hook
-                    #'ivy--minibuffer-setup
-                  (let ((res (read-from-minibuffer
-                              prompt
-                              initial-input
-                              (make-composed-keymap keymap ivy-minibuffer-map)
-                              nil
-                              'ivy-history)))
-                    (when (eq ivy-exit 'done)
-                      (pop ivy-history)
-                      (setq ivy-history
-                            (cons ivy-text (delete ivy-text ivy-history)))
-                      res)))
-             (remove-hook 'post-command-hook #'ivy--exhibit))
-         (when ivy--action
-           (funcall ivy--action)))))))
+    (setq ivy--index (or
+                      (and preselect
+                           (ivy--preselect-index
+                            coll initial-input preselect))
+                      0))
+    (setq ivy--old-re nil)
+    (setq ivy--old-cands nil)
+    (setq ivy-text "")
+    (setq ivy--all-candidates coll)
+    (setq ivy--update-fn update-fn)
+    (setq ivy-exit nil)
+    (setq ivy--default (or (thing-at-point 'symbol) ""))
+    (setq ivy--prompt
+          (cond ((string-match "%.*d" prompt)
+                 prompt)
+                ((string-match "%.*d" ivy-count-format)
+                 (concat ivy-count-format prompt))
+                (ivy--directory
+                 prompt)
+                (t
+                 nil)))
+    (setq ivy--action nil)
+    (prog1
+        (unwind-protect
+             (minibuffer-with-setup-hook
+                 #'ivy--minibuffer-setup
+               (let* ((hist (or history 'ivy-history))
+                      (res (read-from-minibuffer
+                            prompt
+                            initial-input
+                            (make-composed-keymap keymap ivy-minibuffer-map)
+                            nil
+                            hist)))
+                 (when (eq ivy-exit 'done)
+                   (set hist (cons ivy-text
+                                   (delete ivy-text
+                                           (cdr (symbol-value hist)))))
+                   res)))
+          (remove-hook 'post-command-hook #'ivy--exhibit))
+      (when ivy--action
+        (funcall ivy--action)))))
 
 (defun ivy-completing-read (prompt collection
                             &optional predicate require-match initial-input
-                              _history def _inherit-input-method)
+                              history def _inherit-input-method)
   "Read a string in the minibuffer, with completion.
 
 This is an interface that conforms to `completing-read', so that
@@ -512,8 +505,14 @@ _INHERIT-INPUT-METHOD is ignored for now.
 The history, defaults and input-method arguments are ignored for now."
   (when (listp def)
     (setq def (car def)))
-  (setq ivy-require-match require-match)
-  (ivy-read prompt collection predicate initial-input nil def nil t))
+  (ivy-read prompt collection
+            :predicate predicate
+            :require-match require-match
+            :initial-input initial-input
+            :preselect def
+            :history history
+            :keymap nil
+            :sort t))
 
 ;;;###autoload
 (define-minor-mode ivy-mode
diff --git a/swiper.el b/swiper.el
index f123c73..083c22c 100644
--- a/swiper.el
+++ b/swiper.el
@@ -186,11 +186,10 @@ When non-nil, INITIAL-INPUT is the initial search 
pattern."
                     (replace-regexp-in-string
                      "%s" "pattern: " swiper--format-spec)
                     candidates
-                    nil
-                    initial-input
-                    swiper-map
-                    preselect
-                    #'swiper--update-input-ivy))
+                    :initial-input initial-input
+                    :keymap swiper-map
+                    :preselect preselect
+                    :update-fn #'swiper--update-input-ivy))
       (swiper--cleanup)
       (if (null ivy-exit)
           (goto-char swiper--opoint)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]