;; Author: Oleh Krehel <ohwoeowho@gmail.com>
;; URL: https://github.com/abo-abo/swiper
;; Version: 0.1.0
-;; Package-Requires: ((emacs "24.1") (swiper "0.3.0"))
+;; Package-Requires: ((emacs "24.1") (swiper "0.4.0"))
;; Keywords: completion, matching
;; This file is part of GNU Emacs.
(defun counsel-find-symbol ()
"Jump to the definition of the current symbol."
(interactive)
- (setq ivy--action 'counsel--find-symbol)
- (setq ivy-exit 'done)
- (exit-minibuffer))
-
-(defun counsel--find-symbol ()
- (let ((sym (read ivy--current)))
- (cond ((boundp sym)
- (find-variable sym))
- ((fboundp sym)
- (find-function sym))
- ((or (featurep sym)
- (locate-library
+ (ivy-set-action 'counsel--find-symbol)
+ (ivy-done))
+
+(defun counsel--find-symbol (x)
+ "Find symbol definition that corresponds to string X."
+ (let ((full-name (get-text-property 0 'full-name x)))
+ (if full-name
+ (find-library full-name)
+ (let ((sym (read x)))
+ (cond ((boundp sym)
+ (find-variable sym))
+ ((fboundp sym)
+ (find-function sym))
+ ((or (featurep sym)
+ (locate-library
+ (prin1-to-string sym)))
+ (find-library
(prin1-to-string sym)))
- (find-library (prin1-to-string sym)))
- (t
- (error "Couldn't fild definition of %s"
- sym)))))
+ (t
+ (error "Couldn't fild definition of %s"
+ sym)))))))
-(defun counsel-describe-variable (variable &optional buffer frame)
- "Forward to (`describe-variable' VARIABLE BUFFER FRAME)."
- (interactive
- (let ((v (variable-at-point))
- (enable-recursive-minibuffers t)
- (preselect (thing-at-point 'symbol))
- val)
- (setq ivy--action nil)
- (setq val (ivy-read
- (if (symbolp v)
- (format
- "Describe variable (default %s): " v)
- "Describe variable: ")
- (let (cands)
- (mapatoms
- (lambda (vv)
- (when (or (get vv 'variable-documentation)
- (and (boundp vv) (not (keywordp vv))))
- (push (symbol-name vv) cands))))
- cands)
- nil nil counsel-describe-map preselect
- nil t))
- (list (if (equal val "")
- v
- (intern val)))))
- (unless (eq ivy--action 'counsel--find-symbol)
- (describe-variable variable buffer frame)))
-
-(defun counsel-describe-function (function)
- "Forward to (`describe-function' FUNCTION) with ivy completion."
- (interactive
- (let ((fn (function-called-at-point))
- (enable-recursive-minibuffers t)
- (preselect (thing-at-point 'symbol))
- val)
- (setq ivy--action nil)
- (setq val (ivy-read (if fn
- (format "Describe function (default %s): " fn)
- "Describe function: ")
- (let (cands)
- (mapatoms
- (lambda (x)
- (when (fboundp x)
- (push (symbol-name x) cands))))
- cands)
- nil nil counsel-describe-map preselect
- nil t))
- (list (if (equal val "")
- fn (intern val)))))
- (unless (eq ivy--action 'counsel--find-symbol)
- (describe-function function)))
+(defvar counsel-describe-symbol-history nil
+ "History for `counsel-describe-variable' and `counsel-describe-function'.")
+
+(defun counsel-describe-variable ()
+ "Forward to `describe-variable'."
+ (interactive)
+ (let ((enable-recursive-minibuffers t))
+ (ivy-read
+ "Describe variable: "
+ (let (cands)
+ (mapatoms
+ (lambda (vv)
+ (when (or (get vv 'variable-documentation)
+ (and (boundp vv) (not (keywordp vv))))
+ (push (symbol-name vv) cands))))
+ cands)
+ :keymap counsel-describe-map
+ :preselect (thing-at-point 'symbol)
+ :history 'counsel-describe-symbol-history
+ :require-match t
+ :sort t
+ :action (lambda (x)
+ (describe-variable
+ (intern x))))))
+
+(defun counsel-describe-function ()
+ "Forward to `describe-function'."
+ (interactive)
+ (let ((enable-recursive-minibuffers t))
+ (ivy-read "Describe function: "
+ (let (cands)
+ (mapatoms
+ (lambda (x)
+ (when (fboundp x)
+ (push (symbol-name x) cands))))
+ cands)
+ :keymap counsel-describe-map
+ :preselect (thing-at-point 'symbol)
+ :history 'counsel-describe-symbol-history
+ :require-match t
+ :sort t
+ :action (lambda (x)
+ (describe-function
+ (intern x))))))
(defvar info-lookup-mode)
(declare-function info-lookup->completions "info-look")
(enable-recursive-minibuffers t)
(value (ivy-read
"Describe symbol: "
- (mapcar #'car completions))))
+ (mapcar #'car completions)
+ :sort t)))
(list value info-lookup-mode))))
(info-lookup 'symbol symbol mode))
"git ls-files --full-name --")
"\n"
t))
- (file (ivy-read "Find file: " cands)))
- (when file
- (find-file file))))
+ (action (lambda (x) (find-file x))))
+ (ivy-read "Find file: " cands
+ :action action)))
+
+(defvar counsel--git-grep-dir nil
+ "Store the base git directory.")
(defun counsel-git-grep-count (str)
"Quickly count the amount of git grep STR matches."
- (let ((out (shell-command-to-string
- (format "git grep -c '%s' | sed 's/.*:\\(.*\\)/\\1/g' | awk '{s+=$1} END {print s}'"
- (ivy--regex str)))))
+ (let* ((default-directory counsel--git-grep-dir)
+ (out (shell-command-to-string
+ (format "git grep -i -c '%s' | sed 's/.*:\\(.*\\)/\\1/g' | awk '{s+=$1} END {print s}'"
+ (ivy--regex str)))))
(string-to-number out)))
(defvar counsel--git-grep-count nil
(setq ivy--full-length counsel--git-grep-count)
(list ""
(format "%d chars more" (- 3 (length ivy-text)))))
- (let ((cmd (format "git --no-pager grep --full-name -n --no-color -i -e \"%s\""
- (ivy--regex string t)))
- res)
+ (let* ((default-directory counsel--git-grep-dir)
+ (cmd (format "git --no-pager grep --full-name -n --no-color -i -e \"%s\""
+ (ivy--regex string t)))
+ res)
(if (<= counsel--git-grep-count 20000)
(progn
(setq res (shell-command-to-string cmd))
(setq ivy--full-length nil))
- (setq res (shell-command-to-string (concat cmd " | head -n 5000")))
+ (setq res (shell-command-to-string (concat cmd " | head -n 2000")))
(setq ivy--full-length (counsel-git-grep-count ivy-text)))
(split-string res "\n" t))))
-(defun counsel-git-grep ()
+(defvar counsel-git-grep-map
+ (let ((map (make-sparse-keymap)))
+ (define-key map (kbd "C-l") 'counsel-git-grep-recenter)
+ map))
+
+(defun counsel-git-grep-recenter ()
+ (interactive)
+ (with-selected-window (ivy-state-window ivy-last)
+ (counsel-git-grep-action)
+ (recenter-top-bottom)))
+
+(defun counsel-git-grep-action (x)
+ (let ((lst (split-string x ":")))
+ (find-file (expand-file-name (car lst) counsel--git-grep-dir))
+ (goto-char (point-min))
+ (forward-line (1- (string-to-number (cadr lst))))
+ (unless (eq ivy-exit 'done)
+ (setq swiper--window (selected-window))
+ (swiper--cleanup)
+ (swiper--add-overlays (ivy--regex ivy-text)))))
+
+(defun counsel-git-grep (&optional initial-input)
"Grep for a string in the current git repository."
(interactive)
- (unwind-protect
- (let* ((counsel--git-grep-count (counsel-git-grep-count ""))
- (ivy--dynamic-function (when (> counsel--git-grep-count 20000)
- 'counsel-git-grep-function))
- (git-dir (locate-dominating-file
- default-directory ".git"))
- (ivy--persistent-action (lambda (x)
- (setq lst (split-string x ":"))
- (find-file (expand-file-name (car lst) git-dir))
- (goto-char (point-min))
- (forward-line (1- (string-to-number (cadr lst))))
- (setq swiper--window (selected-window))
- (swiper--cleanup)
- (swiper--add-overlays (ivy--regex ivy-text))))
- (val (ivy-read "pattern: " 'counsel-git-grep-function))
- lst)
- (when val
- (funcall ivy--persistent-action val)))
- (swiper--cleanup)))
+ (setq counsel--git-grep-dir
+ (locate-dominating-file default-directory ".git"))
+ (if (null counsel--git-grep-dir)
+ (error "Not in a git repository")
+ (setq counsel--git-grep-count (counsel-git-grep-count ""))
+ (ivy-read "pattern: " 'counsel-git-grep-function
+ :initial-input initial-input
+ :matcher #'counsel-git-grep-matcher
+ :dynamic-collection (when (> counsel--git-grep-count 20000)
+ 'counsel-git-grep-function)
+ :keymap counsel-git-grep-map
+ :action #'counsel-git-grep-action
+ :unwind #'swiper--cleanup)))
+
+(defun counsel-git-grep-matcher (x)
+ (ignore-errors
+ (when (string-match "^[^:]+:[^:]+:" x)
+ (setq x (substring x (match-end 0)))
+ (if (stringp ivy--old-re)
+ (string-match ivy--old-re x)
+ (let ((res t))
+ (dolist (re ivy--old-re)
+ (setq res
+ (and res
+ (ignore-errors
+ (if (cdr re)
+ (string-match (car re) x)
+ (not (string-match (car re) x)))))))
+ res)))))
(defun counsel-locate-function (str &rest _u)
(if (< (length str) 3)
(delete-region (car bnd) (cdr bnd)))
(insert res))))
+(defun counsel-directory-parent (dir)
+ "Return the directory parent of directory DIR."
+ (concat (file-name-nondirectory
+ (directory-file-name dir)) "/"))
+
+(defun counsel-string-compose (prefix str)
+ "Make PREFIX the display prefix of STR though text properties."
+ (let ((str (copy-sequence str)))
+ (put-text-property
+ 0 1 'display
+ (concat prefix (substring str 0 1))
+ str)
+ str))
+
+(defun counsel-load-library ()
+ "Load a selected the Emacs Lisp library.
+The libraries are offered from `load-path'."
+ (interactive)
+ (let ((dirs load-path)
+ (suffix (concat (regexp-opt '(".el" ".el.gz") t) "\\'"))
+ (cands (make-hash-table :test #'equal))
+ short-name
+ old-val
+ dir-parent
+ res)
+ (dolist (dir dirs)
+ (when (file-directory-p dir)
+ (dolist (file (file-name-all-completions "" dir))
+ (when (string-match suffix file)
+ (unless (string-match "pkg.elc?$" file)
+ (setq short-name (substring file 0 (match-beginning 0)))
+ (if (setq old-val (gethash short-name cands))
+ (progn
+ ;; assume going up directory once will resolve name clash
+ (setq dir-parent (counsel-directory-parent (cdr old-val)))
+ (puthash short-name
+ (cons
+ (counsel-string-compose dir-parent (car old-val))
+ (cdr old-val))
+ cands)
+ (setq dir-parent (counsel-directory-parent dir))
+ (puthash (concat dir-parent short-name)
+ (cons
+ (propertize
+ (counsel-string-compose
+ dir-parent short-name)
+ 'full-name (expand-file-name file dir))
+ dir)
+ cands))
+ (puthash short-name
+ (cons (propertize
+ short-name
+ 'full-name (expand-file-name file dir))
+ dir) cands)))))))
+ (maphash (lambda (_k v) (push (car v) res)) cands)
+ (ivy-read "Load library: " (nreverse res)
+ :action (lambda (x)
+ (load-library
+ (get-text-property 0 'full-name x)))
+ :keymap counsel-describe-map)))
+
(provide 'counsel)
;;; counsel.el ends here