-(defcustom counsel-find-file-at-point nil
- "When non-nil, add file-at-point to the list of candidates."
- :type 'boolean
- :group 'ivy)
-
-(declare-function ffap-guesser "ffap")
-
-(defvar counsel-find-file-map (make-sparse-keymap))
-
-;;;###autoload
-(defun counsel-find-file ()
- "Forward to `find-file'."
- (interactive)
- (ivy-read "Find file: " 'read-file-name-internal
- :matcher #'counsel--find-file-matcher
- :action
- (lambda (x)
- (with-ivy-window
- (find-file (expand-file-name x ivy--directory))))
- :preselect (when counsel-find-file-at-point
- (require 'ffap)
- (ffap-guesser))
- :require-match 'confirm-after-completion
- :history 'file-name-history
- :keymap counsel-find-file-map))
-
-(defcustom counsel-find-file-ignore-regexp nil
- "A regexp of files to ignore while in `counsel-find-file'.
-These files are un-ignored if `ivy-text' matches them.
-The common way to show all files is to start `ivy-text' with a dot.
-Possible value: \"\\(?:\\`[#.]\\)\\|\\(?:[#~]\\'\\)\"."
- :group 'ivy)
-
-(defun counsel--find-file-matcher (regexp candidates)
- "Return REGEXP-matching CANDIDATES.
-Skip some dotfiles unless `ivy-text' requires them."
- (let ((res (cl-remove-if-not
- (lambda (x)
- (string-match regexp x))
- candidates)))
- (if (or (null counsel-find-file-ignore-regexp)
- (string-match counsel-find-file-ignore-regexp ivy-text))
- res
- (cl-remove-if
- (lambda (x)
- (string-match counsel-find-file-ignore-regexp x))
- res))))
-
-(defun counsel-git-grep-matcher (regexp candidates)
- (or (and (equal regexp ivy--old-re)
- ivy--old-cands)
- (prog1
- (setq ivy--old-cands
- (cl-remove-if-not
- (lambda (x)
- (ignore-errors
- (when (string-match "^[^:]+:[^:]+:" x)
- (setq x (substring x (match-end 0)))
- (if (stringp regexp)
- (string-match regexp x)
- (let ((res t))
- (dolist (re regexp)
- (setq res
- (and res
- (ignore-errors
- (if (cdr re)
- (string-match (car re) x)
- (not (string-match (car re) x)))))))
- res)))))
- candidates))
- (setq ivy--old-re regexp))))
-
-(defvar counsel--async-time nil
- "Store the time when a new process was started.
-Or the time of the last minibuffer update.")
-
-(defun counsel--async-command (cmd)
- (let* ((counsel--process " *counsel*")
- (proc (get-process counsel--process))
- (buff (get-buffer counsel--process)))
- (when proc
- (delete-process proc))
- (when buff
- (kill-buffer buff))
- (setq proc (start-process-shell-command
- counsel--process
- counsel--process
- cmd))
- (setq counsel--async-time (current-time))
- (set-process-sentinel proc #'counsel--async-sentinel)
- (set-process-filter proc #'counsel--async-filter)))
-
-(defun counsel--async-sentinel (process event)
- (if (string= event "finished\n")
- (progn
- (with-current-buffer (process-buffer process)
- (setq ivy--all-candidates
- (ivy--sort-maybe
- (split-string (buffer-string) "\n" t)))
- (setq ivy--old-cands ivy--all-candidates))
- (ivy--exhibit))
- (if (string= event "exited abnormally with code 1\n")
- (progn
- (setq ivy--all-candidates '("Error"))
- (setq ivy--old-cands ivy--all-candidates)
- (ivy--exhibit)))))
-
-(defun counsel--async-filter (process str)
- "Receive from PROCESS the output STR.
-Update the minibuffer with the amount of lines collected every
-0.5 seconds since the last update."
- (with-current-buffer (process-buffer process)
- (insert str))
- (let (size)
- (when (time-less-p
- ;; 0.5s
- '(0 0 500000 0)
- (time-since counsel--async-time))
- (with-current-buffer (process-buffer process)
- (goto-char (point-min))
- (setq size (- (buffer-size) (forward-line (buffer-size)))))
- (ivy--insert-minibuffer
- (format "\ncollected: %d" size))
- (setq counsel--async-time (current-time)))))
-
-(defun counsel-locate-action-extern (x)
- "Use xdg-open shell command on X."
- (call-process shell-file-name nil
- nil nil
- shell-command-switch
- (format "%s %s"
- (if (eq system-type 'darwin)
- "open"
- "xdg-open")
- (shell-quote-argument x))))
-
-(declare-function dired-jump "dired-x")
-(defun counsel-locate-action-dired (x)
- "Use `dired-jump' on X."
- (dired-jump nil x))
-
-(defvar counsel-locate-history nil
- "History for `counsel-locate'.")
-
-(defcustom counsel-locate-options (if (eq system-type 'darwin)
- '("-i")
- '("-i" "--regex"))
- "Command line options for `locate`."
- :group 'ivy
- :type '(repeat string))
-
-(ivy-set-actions
- 'counsel-locate
- '(("x" counsel-locate-action-extern "xdg-open")
- ("d" counsel-locate-action-dired "dired")))
-
-(defun counsel-unquote-regex-parens (str)
- (replace-regexp-in-string
- "\\\\)" ")"
- (replace-regexp-in-string
- "\\\\(" "("
- str)))
-
-(defun counsel-locate-function (str &rest _u)
- (if (< (length str) 3)
- (counsel-more-chars 3)
- (counsel--async-command
- (format "locate %s '%s'"
- (mapconcat #'identity counsel-locate-options " ")
- (counsel-unquote-regex-parens
- (ivy--regex str))))
- '("" "working...")))
-
-(defun counsel-delete-process ()
- (let ((process (get-process " *counsel*")))
- (when process
- (delete-process process))))
-
-;;;###autoload
-(defun counsel-locate ()
- "Call locate shell command."
- (interactive)
- (ivy-read "Locate: " #'counsel-locate-function
- :dynamic-collection t
- :history 'counsel-locate-history
- :action (lambda (file)
- (with-ivy-window
- (when file
- (find-file file))))
- :unwind #'counsel-delete-process))
-
-(defun counsel--generic (completion-fn)
- "Complete thing at point with COMPLETION-FN."
- (let* ((bnd (bounds-of-thing-at-point 'symbol))
- (str (if bnd
- (buffer-substring-no-properties
- (car bnd) (cdr bnd))
- ""))
- (candidates (funcall completion-fn str))
- (ivy-height 7)
- (res (ivy-read (format "pattern (%s): " str)
- candidates)))
- (when (stringp res)
- (when bnd
- (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))
-
-;;;###autoload
-(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)))
-