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

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

[elpa] master 05a4e59 147/399: Do not use invalid :preselect as a regexp


From: Oleh Krehel
Subject: [elpa] master 05a4e59 147/399: Do not use invalid :preselect as a regexp
Date: Sat, 20 Jul 2019 14:57:12 -0400 (EDT)

branch: master
commit 05a4e59bf418f9a421b9446b143a7ad071eda6eb
Author: Basil L. Contovounesios <address@hidden>
Commit: Basil L. Contovounesios <address@hidden>

    Do not use invalid :preselect as a regexp
    
    * doc/ivy.org (Optional arguments for ivy-read):
    * ivy.el (ivy-read): Extend :preselect documentation.
    (ivy--preselect-index, ivy--recompute-index): Do not use preselect
    as a regexp if invalid.
    (ivy--legal-regex-p): Rename to...
    (ivy--regex-p): ...this.  Return nil regardless of the type of
    error.
    (ivy--regex-or-literal): Use it.
    
    Fixes #2002
---
 doc/ivy.org | 10 +++++++---
 ivy.el      | 33 +++++++++++++++------------------
 2 files changed, 22 insertions(+), 21 deletions(-)

diff --git a/doc/ivy.org b/doc/ivy.org
index f3b5a3b..6ea4bf4 100644
--- a/doc/ivy.org
+++ b/doc/ivy.org
@@ -1118,13 +1118,17 @@ features such as multi-actions, non-exiting actions, 
=ivy-occur= and
 - =history= ::
      Name of the symbol to store history. See =completing-read=.
 - =preselect= ::
-     When set to a string value, select the first candidate matching
-     this value.
+     Determines which one of the candidates to initially select.
 
      When set to an integer value, select the candidate with that
      index value.
 
-     Every time the input becomes empty, the item corresponding to to
+     When set to any other non-nil value, select the first candidate
+     matching this value.  Comparison is first done with =equal=.
+     If this fails, and when applicable, match =preselect= as a
+     regular expression.
+
+     Every time the input becomes empty, the item corresponding to
      =preselect= is selected.
 - =keymap= ::
      A keymap to be composed with =ivy-minibuffer-map=. This keymap
diff --git a/ivy.el b/ivy.el
index fb67ebd..c15e7fa 100644
--- a/ivy.el
+++ b/ivy.el
@@ -1808,8 +1808,13 @@ history.
 
 KEYMAP is composed with `ivy-minibuffer-map'.
 
-If PRESELECT is not nil, then select the corresponding candidate
-out of the ones that match the INITIAL-INPUT.
+PRESELECT, when non-nil, determines which one of the candidates
+matching INITIAL-INPUT to select initially.  An integer stands
+for the position of the desired candidate in the collection,
+counting from zero.  Otherwise, use the first occurrence of
+PRESELECT in the collection.  Comparison is first done with
+`equal'.  If that fails, and when applicable, match PRESELECT as
+a regular expression.
 
 DEF is for compatibility with `completing-read'.
 
@@ -2378,12 +2383,8 @@ Minibuffer bindings:
   (cond ((integerp preselect)
          preselect)
         ((cl-position preselect candidates :test #'equal))
-        ((stringp preselect)
-         (let ((re preselect))
-           (cl-position-if
-            (lambda (x)
-              (string-match-p re x))
-            candidates)))))
+        ((ivy--regex-p preselect)
+         (cl-position preselect candidates :test #'string-match-p))))
 
 ;;* Implementation
 ;;** Regex
@@ -2484,17 +2485,13 @@ When GREEDY is non-nil, join words in a greedy way."
                           (if greedy ".*" ".*?")))))
                     ivy--regex-hash)))))
 
-(defun ivy--legal-regex-p (str)
-  "Return t if STR is valid regular expression."
-  (condition-case nil
-      (progn
-        (string-match-p str "")
-        t)
-    (invalid-regexp nil)))
+(defun ivy--regex-p (object)
+  "Return OBJECT if it is a valid regular expression, else nil."
+  (ignore-errors (string-match-p object "") object))
 
 (defun ivy--regex-or-literal (str)
-  "If STR isn't a legal regex, escape it."
-  (if (ivy--legal-regex-p str) str (regexp-quote str)))
+  "If STR isn't a legal regexp, escape it."
+  (or (ivy--regex-p str) (regexp-quote str)))
 
 (defun ivy--split-negation (str)
   "Split STR into text before and after ! delimiter.
@@ -3264,7 +3261,7 @@ RE-STR is the regexp, CANDS are the current candidates."
                      (and (integerp preselect)
                           (= ivy--index preselect))
                      (equal current preselect)
-                     (and (stringp preselect)
+                     (and (ivy--regex-p preselect)
                           (stringp current)
                           (string-match-p preselect current))))
                ivy--old-cands



reply via email to

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