;; Author: Oleh Krehel <ohwoeowho@gmail.com>
;; URL: https://github.com/abo-abo/swiper
-;; Version: 0.2.3
;; Package-Requires: ((emacs "24.1"))
;; Keywords: matching
;; re-building it into a regex.
;; So "for example" is transformed into "\\(for\\).*\\(example\\)".
-(require 'cl-lib)
-
;;; Code:
(require 'cl-lib)
ivy--current ivy--directory))))))
(ivy--cd dir)
(ivy--exhibit))
- ((string-match "^/\\([^/]+?\\):\\(?:\\(.*\\)@\\)?" ivy-text)
+ ((string-match "\\`/\\([^/]+?\\):\\(?:\\(.*\\)@\\)?" ivy-text)
(let ((method (match-string 1 ivy-text))
(user (match-string 2 ivy-text))
res)
(when user
(dolist (x res)
(setcar x user)))
- (setq res (cl-delete-duplicates res :test 'equal))
+ (setq res (cl-delete-duplicates res :test #'equal))
(let ((host (ivy-read "Find File: "
(mapcar #'ivy-build-tramp-name res))))
(when host
"Complete the minibuffer text as much as possible.
If the text hasn't changed as a result, forward to `ivy-alt-done'."
(interactive)
- (if (and (eq (ivy-state-collection ivy-last) 'read-file-name-internal)
- (string-match "^/" ivy-text))
+ (if (and (eq (ivy-state-collection ivy-last) #'read-file-name-internal)
+ (string-match "\\`/" ivy-text))
(let ((default-directory ivy--directory))
(minibuffer-complete)
(setq ivy-text (ivy--input))
(mapcar (lambda (str) (substring str (string-match postfix str)))
ivy--old-cands))))
(cond ((eq new t) nil)
- ((string= new ivy-text) nil)
+ ((string= new ivy-text) nil)
(new
(delete-region (minibuffer-prompt-end) (point-max))
(setcar (last parts) new)
"Exit the minibuffer with the current input."
(interactive)
(delete-minibuffer-contents)
- (insert ivy-text)
+ (insert (setq ivy--current ivy-text))
(setq ivy-exit 'done)
(exit-minibuffer))
(ivy--exhibit)
(when (ivy-state-action ivy-last)
(with-selected-window (ivy-state-window ivy-last)
- (funcall (ivy-state-action ivy-last)))))
+ (funcall (ivy-state-action ivy-last) ivy--current))))
(defun ivy-previous-line-and-call (&optional arg)
"Move cursor vertically down ARG candidates.
(ivy--exhibit)
(when (ivy-state-action ivy-last)
(with-selected-window (ivy-state-window ivy-last)
- (funcall (ivy-state-action ivy-last)))))
+ (funcall (ivy-state-action ivy-last) ivy--current))))
(defun ivy-previous-history-element (arg)
"Forward to `previous-history-element' with ARG."
(setq seq (delete "./" (delete "../" seq)))
(when (eq (setq sort-fn (cdr (assoc 'read-file-name-internal
ivy-sort-functions-alist)))
- 'ivy-sort-file-function-default)
+ #'ivy-sort-file-function-default)
(setq seq (mapcar (lambda (x)
- (propertize x 'dirp (string-match-p "/$" x)))
+ (propertize x 'dirp (string-match-p "/\\'" x)))
seq)))
(when sort-fn
(setq seq (cl-sort seq sort-fn)))
When SORT is t, refer to `ivy-sort-functions-alist' for sorting.
-ACTION is a lambda to call after a result was selected.
+ACTION is a lambda to call after a result was selected. It should
+take a single argument, usually a string.
UNWIND is a lambda to call before exiting.
(mapcar (lambda (x) (format "(%s)" x))
(cl-delete-duplicates
(all-completions "(" collection predicate)
- :test 'equal)))
+ :test #'equal)))
(setq coll (all-completions "" collection predicate))))
((eq collection 'read-file-name-internal)
(setq ivy--directory default-directory)
(setq coll (cl-sort (copy-sequence coll) sort-fn))))))
(when preselect
(unless (or require-match
- (cl-find-if `(lambda (x)
- (string-match ,(format "^%s" preselect) x))
- coll))
+ (let ((re (format "\\`%s" preselect)))
+ (cl-find-if (lambda (x) (string-match re x))
+ coll)))
(setq coll (cons preselect coll))))
(setq ivy--index (or
(and dynamic-collection
(when (setq unwind (ivy-state-unwind ivy-last))
(funcall unwind)))
(when (setq action (ivy-state-action ivy-last))
- (funcall action)))))
+ (funcall action ivy--current)))))
(defun ivy-completing-read (prompt collection
&optional predicate require-match initial-input
(ivy-read prompt collection
:predicate predicate
:require-match require-match
- :initial-input initial-input
+ :initial-input (if (consp initial-input)
+ (car initial-input)
+ initial-input)
:preselect (if (listp def) (car def) def)
:history history
:keymap nil
(lambda (x)
(string-match initial-input x))
candidates)))
- (or (cl-position preselect candidates :test 'equal)
+ (or (cl-position preselect candidates :test #'equal)
(cl-position-if
(lambda (x)
(string-match (regexp-quote preselect) x))
;;* Implementation
;;** Regex
(defvar ivy--regex-hash
- (make-hash-table :test 'equal)
+ (make-hash-table :test #'equal)
"Store pre-computed regex.")
(defun ivy--split (str)
(setq ivy--subexps (length subs))
(mapconcat
(lambda (x)
- (if (string-match "^\\\\(.*\\\\)$" x)
+ (if (string-match "\\`\\\\(.*\\\\)\\'" x)
x
(format "\\(%s\\)" x)))
subs
(let ((all (mapconcat #'identity subs "\\|")))
(mapconcat
(lambda (x)
- (if (string-match "^\\\\(.*\\\\)$" x)
+ (if (string-match "\\`\\\\(.*\\\\)\\'" x)
x
(format "\\(%s\\)" x)))
(make-list len all)
counsel-find-symbol))
(setq ivy--prompt-extra ""))
(let (head tail)
- (if (string-match "\\(.*\\): $" ivy--prompt)
+ (if (string-match "\\(.*\\): \\'" ivy--prompt)
(progn
(setq head (match-string 1 ivy--prompt))
(setq tail ": "))
(setq ivy--all-candidates (funcall store ivy-text))))
(ivy--insert-minibuffer (ivy--format ivy--all-candidates))))
(cond (ivy--directory
- (if (string-match "/$" ivy-text)
+ (if (string-match "/\\'" ivy-text)
(if (member ivy-text ivy--all-candidates)
(ivy--cd (expand-file-name ivy-text ivy--directory))
- (when (string-match "//$" ivy-text)
+ (when (string-match "//\\'" ivy-text)
(ivy--cd "/")))
- (if (string-match "~$" ivy-text)
+ (if (string-match "~\\'" ivy-text)
(ivy--cd (expand-file-name "~/")))))
((eq (ivy-state-collection ivy-last) 'internal-complete-buffer)
- (when (or (and (string-match "^ " ivy-text)
- (not (string-match "^ " ivy--old-text)))
- (and (string-match "^ " ivy--old-text)
- (not (string-match "^ " ivy-text))))
+ (when (or (and (string-match "\\` " ivy-text)
+ (not (string-match "\\` " ivy--old-text)))
+ (and (string-match "\\` " ivy--old-text)
+ (not (string-match "\\` " ivy-text))))
(setq ivy--all-candidates
(if (and (> (length ivy-text) 0)
(eq (aref ivy-text 0)
(not (string-match "\\\\" ivy--old-re))
(not (equal ivy--old-re ""))
(memq (cl-search
- (if (string-match "\\\\)$" ivy--old-re)
+ (if (string-match "\\\\)\\'" ivy--old-re)
(substring ivy--old-re 0 -2)
ivy--old-re)
- re) '(0 2)))
+ re)
+ '(0 2)))
(ignore-errors
(cl-remove-if-not
(lambda (x) (string-match re x))
(if (cdr re)
#'cl-remove-if-not
#'cl-remove-if)
- `(lambda (x) (string-match ,(car re) x))
+ (let ((re (car re)))
+ (lambda (x) (string-match re x)))
res))))
res))))
(tail (nthcdr ivy--index ivy--old-cands))
(or (setq ivy--index
(or
(cl-position re cands
- :test 'equal)
+ :test #'equal)
(and ivy--directory
(cl-position
(concat re "/") cands
- :test 'equal))))))
+ :test #'equal))))))
(while (and tail (null idx))
;; Compare with eq to handle equal duplicates in cands
(setq idx (cl-position (pop tail) cands)))
(when (and (string= name "") (not (equal ivy--old-re "")))
(setq ivy--index
(or (cl-position (ivy-state-preselect ivy-last)
- cands :test 'equal)
+ cands :test #'equal)
ivy--index)))
(setq ivy--old-re (if cands re ""))
(setq ivy--old-cands cands)))
(index (min ivy--index half-height (1- (length cands)))))
(when ivy--directory
(setq cands (mapcar (lambda (x)
- (if (string-match-p "/$" x)
+ (if (string-match-p "/\\'" x)
(propertize x 'face 'ivy-subdir)
x))
cands)))
(and virtual
(ivy--virtual-buffers)))))
-(defun ivy--switch-buffer-action ()
- "Finalizer for `ivy-switch-buffer'."
- (if (zerop (length ivy--current))
+(defun ivy--switch-buffer-action (buffer)
+ "Switch to BUFFER.
+BUFFER may be a string or nil."
+ (if (zerop (length buffer))
(switch-to-buffer
ivy-text nil 'force-same-window)
- (let ((virtual (assoc ivy--current ivy--virtual-buffers)))
+ (let ((virtual (assoc buffer ivy--virtual-buffers)))
(if virtual
(find-file (cdr virtual))
(switch-to-buffer
- ivy--current nil 'force-same-window)))))
+ buffer nil 'force-same-window)))))
(defun ivy-switch-buffer ()
"Switch to another buffer."
(if (not ivy-mode)
(call-interactively 'switch-to-buffer)
(ivy-read "Switch to buffer: " 'internal-complete-buffer
- :action #'ivy--switch-buffer-action
- :preselect (buffer-name (other-buffer (current-buffer))))))
+ :preselect (buffer-name (other-buffer (current-buffer)))
+ :action #'ivy--switch-buffer-action)))
(provide 'ivy)