2017-02-19 18:41:37 -05:00
|
|
|
;;; completion/ivy/autoload/ivy.el
|
2017-02-13 04:54:12 -05:00
|
|
|
|
|
|
|
;; Show more information in ivy-switch-buffer; and only display
|
2017-02-21 00:43:15 -05:00
|
|
|
;; workgroup-relevant buffers.
|
2017-02-19 18:41:37 -05:00
|
|
|
(defun +ivy--get-buffers (&optional buffer-list)
|
2017-02-13 04:54:12 -05:00
|
|
|
(let ((min-name 5)
|
|
|
|
(min-mode 5)
|
|
|
|
(proot (doom-project-root)))
|
|
|
|
(mapcar
|
|
|
|
(lambda (b) (format (format "%%-%ds %%-%ds %%s" min-name min-mode)
|
|
|
|
(nth 0 b)
|
|
|
|
(nth 1 b)
|
|
|
|
(or (nth 2 b) "")))
|
|
|
|
(mapcar (lambda (b)
|
|
|
|
(with-current-buffer b
|
|
|
|
(let ((buffer-name (buffer-name b))
|
|
|
|
(mode-name (symbol-name major-mode)))
|
|
|
|
(when (> (length buffer-name) min-name)
|
|
|
|
(setq min-name (+ (length buffer-name) 15)))
|
|
|
|
(when (> (length mode-name) min-mode)
|
|
|
|
(setq min-mode (+ (length mode-name) 3)))
|
2017-02-21 00:43:15 -05:00
|
|
|
(list (concat
|
|
|
|
(propertize buffer-name
|
|
|
|
'face (cond ((string-match-p "^ ?\\*" buffer-name)
|
|
|
|
'font-lock-comment-face)
|
|
|
|
((not (string= proot (doom-project-root)))
|
|
|
|
'font-lock-keyword-face)
|
|
|
|
(buffer-read-only
|
|
|
|
'error)))
|
|
|
|
(when (and buffer-file-name (buffer-modified-p))
|
|
|
|
(propertize "[+]" 'face 'doom-modeline-buffer-modified)))
|
|
|
|
(propertize mode-name 'face 'font-lock-constant-face)
|
|
|
|
(when buffer-file-name
|
|
|
|
(abbreviate-file-name (file-name-directory buffer-file-name)))))))
|
|
|
|
(or buffer-list (doom-buffer-list))))))
|
2017-02-13 04:54:12 -05:00
|
|
|
|
|
|
|
(defun +ivy--select-buffer-action (buffer)
|
|
|
|
(ivy--switch-buffer-action
|
|
|
|
(s-chop-suffix
|
|
|
|
"[+]"
|
2017-04-17 02:30:54 -04:00
|
|
|
(substring buffer 0 (string-match-p (regexp-quote " ") buffer)))))
|
2017-02-13 04:54:12 -05:00
|
|
|
|
2017-02-24 20:03:08 -05:00
|
|
|
(defun +ivy--select-buffer-other-window-action (buffer)
|
|
|
|
(ivy--switch-buffer-other-window-action
|
|
|
|
(s-chop-suffix
|
|
|
|
"[+]"
|
2017-04-17 02:30:54 -04:00
|
|
|
(substring buffer 0 (string-match-p (regexp-quote " ") buffer)))))
|
2017-02-24 20:03:08 -05:00
|
|
|
|
2017-02-13 04:54:12 -05:00
|
|
|
;;;###autoload
|
2017-02-24 20:03:08 -05:00
|
|
|
(defun +ivy/switch-workspace-buffer (&optional other-window-p)
|
2017-02-21 00:43:15 -05:00
|
|
|
"Switch to an open buffer in the current workspace."
|
2017-02-24 20:03:08 -05:00
|
|
|
(interactive "P")
|
|
|
|
(+ivy/switch-buffer other-window-p t))
|
2017-02-13 04:54:12 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
2017-02-24 20:03:08 -05:00
|
|
|
(defun +ivy/switch-buffer (&optional other-window-p workspace-only-p)
|
2017-02-21 00:43:15 -05:00
|
|
|
"Switch to an open buffer in the global buffer list. If WORKSPACE-ONLY-P,
|
|
|
|
limit to buffers in the current workspace."
|
2017-02-24 20:03:08 -05:00
|
|
|
(interactive "P")
|
2017-02-21 00:43:15 -05:00
|
|
|
(ivy-read (format "%s buffers: " (if workspace-only-p "Workspace" "Global"))
|
|
|
|
(+ivy--get-buffers (unless workspace-only-p (buffer-list)))
|
2017-02-24 20:03:08 -05:00
|
|
|
:action (if other-window-p
|
|
|
|
'+ivy--select-buffer-other-window-action
|
|
|
|
'+ivy--select-buffer-action)
|
|
|
|
:matcher 'ivy--switch-buffer-matcher
|
2017-02-21 00:43:15 -05:00
|
|
|
:keymap ivy-switch-buffer-map
|
|
|
|
:caller '+ivy/switch-workspace-buffer))
|
2017-02-13 04:54:12 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +ivy/kill-ring ()
|
2017-05-06 16:41:34 +02:00
|
|
|
"Search through the kill ring with `ivy'."
|
2017-02-13 04:54:12 -05:00
|
|
|
(interactive)
|
2017-02-19 18:41:37 -05:00
|
|
|
(ivy-read "Kill ring:"
|
|
|
|
(cl-remove-if (lambda (it)
|
|
|
|
(or (< (length it) 3)
|
|
|
|
(string-match-p "\\`[\n[:blank:]]+\\'" it)))
|
|
|
|
(cl-remove-duplicates kill-ring :test 'equal))))
|
2017-02-13 04:54:12 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +ivy/tasks ()
|
2017-05-06 16:41:17 +02:00
|
|
|
"Search through all TODO/FIXME tags in the current project using
|
|
|
|
`counsel-rg'."
|
2017-02-13 04:54:12 -05:00
|
|
|
(interactive)
|
2017-05-06 16:41:17 +02:00
|
|
|
(counsel-rg "\\(TODO|FIXME\\)\\s" (doom-project-root) "--case-sensitive -w"))
|
2017-02-13 04:54:12 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +ivy*counsel-ag-function (string base-cmd extra-ag-args)
|
2017-05-06 16:41:17 +02:00
|
|
|
"Advice to 1) get rid of the character limit from `counsel-ag-function' and 2)
|
|
|
|
disable ivy's over-zealous parentheses quoting behavior, both of which
|
|
|
|
interferes with my custom :[ar]g ex command `+ivy:file-search'."
|
2017-02-13 04:54:12 -05:00
|
|
|
(when (null extra-ag-args)
|
|
|
|
(setq extra-ag-args ""))
|
2017-05-06 16:41:17 +02:00
|
|
|
(if (< (length string) 1) ;; #1
|
2017-02-13 04:54:12 -05:00
|
|
|
(counsel-more-chars 1)
|
|
|
|
(let ((default-directory counsel--git-grep-dir)
|
|
|
|
(regex (counsel-unquote-regex-parens
|
|
|
|
(setq ivy--old-re
|
2017-05-06 16:41:17 +02:00
|
|
|
(ivy--regex
|
|
|
|
(counsel-unquote-regex-parens string)))))) ;; #2
|
|
|
|
(let* ((args-end (string-match " -- " extra-ag-args))
|
|
|
|
(file (if args-end
|
|
|
|
(substring-no-properties extra-ag-args (+ args-end 3))
|
|
|
|
""))
|
|
|
|
(extra-ag-args (if args-end
|
|
|
|
(substring-no-properties extra-ag-args 0 args-end)
|
|
|
|
extra-ag-args))
|
|
|
|
(ag-cmd (format base-cmd
|
|
|
|
(concat extra-ag-args
|
|
|
|
" -- "
|
|
|
|
(shell-quote-argument regex)
|
|
|
|
file))))
|
2017-02-13 04:54:12 -05:00
|
|
|
(if (file-remote-p default-directory)
|
|
|
|
(split-string (shell-command-to-string ag-cmd) "\n" t)
|
|
|
|
(counsel--async-command ag-cmd)
|
|
|
|
nil)))))
|
|
|
|
|
|
|
|
;;;###autoload
|
2017-05-06 16:41:17 +02:00
|
|
|
(defun +ivy/wgrep-occur ()
|
|
|
|
"Invoke the search+replace wgrep buffer on the current ag/rg search results."
|
2017-02-13 04:54:12 -05:00
|
|
|
(interactive)
|
2017-05-06 16:41:17 +02:00
|
|
|
(if (not (window-minibuffer-p))
|
|
|
|
(user-error "No completion session is active")
|
|
|
|
(require 'wgrep)
|
|
|
|
(let* ((caller (ivy-state-caller ivy-last))
|
|
|
|
(occur-fn (plist-get ivy--occurs-list caller))
|
|
|
|
(buffer
|
|
|
|
(generate-new-buffer
|
|
|
|
(format "*ivy-occur%s \"%s\"*"
|
|
|
|
(if caller (concat " " (prin1-to-string caller)) "")
|
|
|
|
ivy-text))))
|
|
|
|
(with-current-buffer buffer
|
|
|
|
(let ((inhibit-read-only t))
|
|
|
|
(erase-buffer)
|
|
|
|
(funcall occur-fn))
|
|
|
|
(setf (ivy-state-text ivy-last) ivy-text)
|
|
|
|
(setq ivy-occur-last ivy-last)
|
|
|
|
(setq-local ivy--directory ivy--directory))
|
|
|
|
(ivy-exit-with-action
|
|
|
|
`(lambda (_)
|
|
|
|
(pop-to-buffer ,buffer)
|
|
|
|
(ivy-wgrep-change-to-wgrep-mode))))))
|
2017-02-13 04:54:12 -05:00
|
|
|
|
2017-02-13 21:11:54 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun +ivy-yas-prompt (prompt choices &optional display-fn)
|
|
|
|
(yas-completing-prompt prompt choices display-fn #'ivy-completing-read))
|
2017-05-10 05:20:54 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +ivy-git-grep-other-window-action (x)
|
|
|
|
"Opens the current candidate in another window."
|
|
|
|
(let (dest-window)
|
|
|
|
(cl-letf (((symbol-function 'find-file)
|
|
|
|
(lambda (filename)
|
|
|
|
(find-file-other-window filename)
|
|
|
|
(setq dest-window (selected-window)))))
|
|
|
|
(counsel-git-grep-action x)
|
|
|
|
(select-window dest-window))))
|