2017-06-08 11:47:56 +02:00
|
|
|
;;; completion/ivy/autoload/ivy.el -*- lexical-binding: t; -*-
|
2017-02-13 04:54:12 -05:00
|
|
|
|
2017-08-21 21:54:04 +02:00
|
|
|
(defsubst +ivy--icon-for-mode (mode)
|
|
|
|
"Apply `all-the-icons-for-mode' on MODE but either return an icon or nil."
|
|
|
|
(let ((icon (all-the-icons-icon-for-mode mode)))
|
|
|
|
(unless (symbolp icon) icon)))
|
2017-02-24 20:03:08 -05:00
|
|
|
|
2017-02-13 04:54:12 -05:00
|
|
|
;;;###autoload
|
2017-08-21 21:54:04 +02:00
|
|
|
(defun +ivy-buffer-transformer (str)
|
|
|
|
(let* ((buf (get-buffer str))
|
|
|
|
(path (buffer-file-name buf))
|
|
|
|
(mode (buffer-local-value 'major-mode buf))
|
|
|
|
(faces
|
|
|
|
(with-current-buffer buf
|
|
|
|
(cond ((string-match-p "^ ?\\*" (buffer-name buf))
|
|
|
|
'font-lock-comment-face)
|
|
|
|
((buffer-modified-p buf)
|
|
|
|
'doom-modeline-buffer-modified)
|
|
|
|
(buffer-read-only
|
|
|
|
'error)))))
|
|
|
|
(propertize
|
|
|
|
(format "%-40s %s%-20s %s"
|
|
|
|
str
|
|
|
|
(if +ivy-buffer-icons
|
|
|
|
(concat (propertize " " 'display
|
|
|
|
(or (+ivy--icon-for-mode mode)
|
|
|
|
(+ivy--icon-for-mode (get mode 'derived-mode-parent))))
|
|
|
|
"\t")
|
|
|
|
"")
|
|
|
|
mode
|
|
|
|
(or (and path (abbreviate-file-name (file-name-directory (file-truename path))))
|
|
|
|
""))
|
|
|
|
'face faces)))
|
2017-02-13 04:54:12 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
2017-08-21 21:54:04 +02:00
|
|
|
(defun +ivy/switch-workspace-buffer (&optional arg)
|
|
|
|
"Switch to another buffer within the current workspace.
|
2017-07-08 13:54:44 +02:00
|
|
|
|
2017-08-21 21:54:04 +02:00
|
|
|
If ARG (universal argument), open selection in other-window."
|
2017-02-24 20:03:08 -05:00
|
|
|
(interactive "P")
|
2017-08-21 21:54:04 +02:00
|
|
|
(ivy-read "Switch to workspace buffer: "
|
|
|
|
(mapcar #'buffer-name (delq (current-buffer) (doom-buffer-list)))
|
|
|
|
:action (if arg
|
|
|
|
#'ivy--switch-buffer-other-window-action
|
|
|
|
#'ivy--switch-buffer-action)
|
2017-07-07 14:44:49 +02:00
|
|
|
:matcher #'ivy--switch-buffer-matcher
|
2017-02-21 00:43:15 -05:00
|
|
|
:keymap ivy-switch-buffer-map
|
2017-07-07 14:44:49 +02:00
|
|
|
:caller #'+ivy/switch-workspace-buffer))
|
2017-02-13 04:54:12 -05:00
|
|
|
|
2017-05-14 13:49:11 +02:00
|
|
|
(defun +ivy--tasks-candidates (tasks)
|
2017-05-10 06:13:14 +02:00
|
|
|
"Generate a list of task tags (specified by `+ivy-task-tags') for
|
|
|
|
`+ivy/tasks'."
|
2017-06-08 11:47:56 +02:00
|
|
|
(let* ((max-type-width
|
|
|
|
(cl-loop for task in +ivy-task-tags maximize (length (car task))))
|
|
|
|
(max-desc-width
|
|
|
|
(cl-loop for task in tasks maximize (length (cl-cdadr task))))
|
|
|
|
(max-width (max (- (frame-width) (1+ max-type-width) max-desc-width)
|
|
|
|
25)))
|
|
|
|
(cl-loop
|
|
|
|
with fmt = (format "%%-%ds %%-%ds%%s%%s:%%s" max-type-width max-width)
|
|
|
|
for alist in tasks
|
|
|
|
collect
|
|
|
|
(let-alist alist
|
|
|
|
(format fmt
|
|
|
|
(propertize .type 'face (cdr (assoc .type +ivy-task-tags)))
|
|
|
|
(substring .desc 0 (min max-desc-width (length .desc)))
|
|
|
|
(propertize " | " 'face 'font-lock-comment-face)
|
|
|
|
(propertize (abbreviate-file-name .file) 'face 'font-lock-keyword-face)
|
|
|
|
(propertize .line 'face 'font-lock-constant-face))))))
|
2017-05-14 13:49:11 +02:00
|
|
|
|
|
|
|
(defun +ivy--tasks (target)
|
2017-06-08 11:47:56 +02:00
|
|
|
(let* (case-fold-search
|
|
|
|
(task-tags (mapcar #'car +ivy-task-tags))
|
|
|
|
(cmd
|
|
|
|
(format "%s -H -S --no-heading -- %s %s"
|
|
|
|
(or (when-let (bin (executable-find "rg"))
|
|
|
|
(concat bin " --line-number"))
|
|
|
|
(when-let (bin (executable-find "ag"))
|
|
|
|
(concat bin " --numbers"))
|
|
|
|
(error "ripgrep & the_silver_searcher are unavailable"))
|
|
|
|
(shell-quote-argument
|
|
|
|
(concat "\\s("
|
|
|
|
(string-join task-tags "|")
|
|
|
|
")([\\s:]|\\([^)]+\\):?)"))
|
|
|
|
target)))
|
|
|
|
(save-match-data
|
|
|
|
(cl-loop with out = (shell-command-to-string cmd)
|
|
|
|
for x in (and out (split-string out "\n" t))
|
2017-08-14 23:37:17 -07:00
|
|
|
when (condition-case-unless-debug ex
|
|
|
|
(string-match
|
|
|
|
(concat "^\\([^:]+\\):\\([0-9]+\\):.+\\("
|
|
|
|
(string-join task-tags "\\|")
|
|
|
|
"\\):?\\s-*\\(.+\\)")
|
|
|
|
x)
|
|
|
|
(error
|
|
|
|
(message! (red "Error matching task in file: (%s) %s"
|
|
|
|
(error-message-string ex)
|
|
|
|
(car (split-string x ":"))))
|
|
|
|
nil))
|
2017-06-08 11:47:56 +02:00
|
|
|
collect `((type . ,(match-string 3 x))
|
|
|
|
(desc . ,(match-string 4 x))
|
|
|
|
(file . ,(match-string 1 x))
|
|
|
|
(line . ,(match-string 2 x)))))))
|
2017-05-10 06:13:14 +02:00
|
|
|
|
|
|
|
(defun +ivy--tasks-open-action (x)
|
|
|
|
"Jump to the file and line of the current task."
|
2017-05-14 13:49:11 +02:00
|
|
|
(let ((location (cadr (split-string x " | ")))
|
|
|
|
(type (car (split-string x " "))))
|
2017-06-25 02:04:50 +02:00
|
|
|
(cl-destructuring-bind (file line) (split-string location ":")
|
2017-05-14 13:49:11 +02:00
|
|
|
(with-ivy-window
|
|
|
|
(find-file (expand-file-name file (doom-project-root)))
|
|
|
|
(goto-char (point-min))
|
|
|
|
(forward-line (1- (string-to-number line)))
|
|
|
|
(search-forward type (line-end-position) t)
|
|
|
|
(backward-char (length type))
|
|
|
|
(recenter)))))
|
2017-05-10 06:13:14 +02:00
|
|
|
|
2017-02-13 04:54:12 -05:00
|
|
|
;;;###autoload
|
2017-05-10 06:13:14 +02:00
|
|
|
(defun +ivy/tasks (&optional arg)
|
|
|
|
"Search through all TODO/FIXME tags in the current project. If ARG, only
|
|
|
|
search current file. See `+ivy-task-tags' to customize what this searches for."
|
|
|
|
(interactive "P")
|
|
|
|
(ivy-read (format "Tasks (%s): "
|
|
|
|
(if arg
|
|
|
|
(concat "in: " (file-relative-name buffer-file-name))
|
|
|
|
"project"))
|
2017-05-14 13:49:11 +02:00
|
|
|
(+ivy--tasks-candidates
|
|
|
|
(+ivy--tasks (if arg buffer-file-name (doom-project-root))))
|
2017-05-10 06:13:14 +02:00
|
|
|
:action #'+ivy--tasks-open-action
|
|
|
|
:caller '+ivy/tasks))
|
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)
|
2017-05-10 05:28:50 +02:00
|
|
|
disable ivy's over-zealous parentheses quoting behavior (if i want literal
|
|
|
|
parentheses, I'll escape them myself).
|
|
|
|
|
|
|
|
NOTE This may need to be updated frequently, to meet changes upstream (in
|
|
|
|
counsel-rg)."
|
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)
|
2017-09-08 16:58:01 +02:00
|
|
|
(let ((default-directory counsel--git-dir)
|
2017-02-13 04:54:12 -05:00
|
|
|
(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-06-08 11:47:56 +02:00
|
|
|
(unless (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."
|
2017-05-14 17:40:33 +02:00
|
|
|
(when (string-match "\\`\\(.*?\\):\\([0-9]+\\):\\(.*\\)\\'" x)
|
|
|
|
(select-window
|
|
|
|
(with-ivy-window
|
|
|
|
(let ((file-name (match-string-no-properties 1 x))
|
|
|
|
(line-number (match-string-no-properties 2 x)))
|
2017-09-08 16:58:01 +02:00
|
|
|
(find-file-other-window (expand-file-name file-name counsel--git-dir))
|
2017-05-14 17:40:33 +02:00
|
|
|
(goto-char (point-min))
|
|
|
|
(forward-line (1- (string-to-number line-number)))
|
|
|
|
(re-search-forward (ivy--regex ivy-text t) (line-end-position) t)
|
|
|
|
(run-hooks 'counsel-grep-post-action-hook)
|
|
|
|
(selected-window))))))
|
2017-05-17 21:07:01 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +ivy-quit-and-resume ()
|
|
|
|
"Close the current popup window and resume ivy."
|
|
|
|
(interactive)
|
|
|
|
(when (doom-popup-p)
|
|
|
|
(doom/popup-close))
|
|
|
|
(ivy-resume))
|
2017-05-26 11:16:26 +02:00
|
|
|
|