+ivy/tasks: ag as a fallback

This commit is contained in:
Henrik Lissner 2017-05-20 18:39:07 +02:00
parent a4bacca812
commit 5c34732266

View file

@ -71,46 +71,49 @@ limit to buffers in the current workspace."
"Generate a list of task tags (specified by `+ivy-task-tags') for "Generate a list of task tags (specified by `+ivy-task-tags') for
`+ivy/tasks'." `+ivy/tasks'."
(let* ((max-type-width (seq-max (mapcar #'length (mapcar #'car +ivy-task-tags)))) (let* ((max-type-width (seq-max (mapcar #'length (mapcar #'car +ivy-task-tags))))
(max-desc-width (seq-max (mapcar #'length (mapcar #'cadr tasks)))) (max-desc-width (seq-max (mapcar #'length (mapcar #'cl-cdadr tasks))))
(max-width (max 25 (min (- (window-width) (+ max-type-width 1)) (max-width (max 25 (min (- (frame-width) (+ max-type-width 1))
(seq-max (mapcar #'length (mapcar #'cadr tasks)))))) max-desc-width)))
(fmt (format "%%-%ds %%-%ds%%s%%s:%%s" max-type-width max-width))) (fmt (format "%%-%ds %%-%ds%%s%%s:%%s" max-type-width max-desc-width))
(mapcar (lambda (task) lines)
(let ((file (nth 2 task)) (dolist (alist tasks (nreverse lines))
(line (nth 3 task)) (let-alist alist
(type (nth 0 task)) (push (format fmt
(desc (nth 1 task))) (propertize .type 'face (cdr (assoc .type +ivy-task-tags)))
(format fmt (substring .desc 0 (min max-desc-width (length .desc)))
(propertize type 'face (cdr (assoc type +ivy-task-tags)))
(substring desc 0 (min max-width (length desc)))
(propertize " | " 'face 'font-lock-comment-face) (propertize " | " 'face 'font-lock-comment-face)
(propertize (abbreviate-file-name file) 'face 'font-lock-keyword-face) (propertize (abbreviate-file-name .file) 'face 'font-lock-keyword-face)
(propertize line 'face 'font-lock-constant-face)))) (propertize .line 'face 'font-lock-constant-face))
tasks))) lines)))))
(defun +ivy--tasks (target) (defun +ivy--tasks (target)
(let (case-fold-search) (let (case-fold-search)
(delq
nil
(mapcar (lambda (x) (mapcar (lambda (x)
(save-match-data (save-match-data
(string-match (concat "^\\([^:]+\\):\\([0-9]+\\):.+\\(" (when (string-match (concat "^\\([^:]+\\):\\([0-9]+\\):.+\\("
(string-join (mapcar #'car +ivy-task-tags) "\\|") (string-join (mapcar #'car +ivy-task-tags) "\\|")
"\\):?\\s-*\\(.+\\)") "\\):?\\s-*\\(.+\\)")
x) x)
(let ((file (match-string 1 x)) `((type . ,(match-string 3 x))
(line (match-string 2 x)) (desc . ,(match-string 4 x))
(type (match-string 3 x)) (file . ,(match-string 1 x))
(desc (match-string 4 x))) (line . ,(match-string 2 x))))))
(list type desc file line)))) (let ((command (or (let ((bin (executable-find "rg")))
(split-string (and bin (concat bin " --line-number")))
(shell-command-to-string (let ((bin (executable-find "ag")))
(format "rg -H -S --no-heading --line-number %s %s" (and bin (concat bin " --numbers")))
(concat " -- " (error "Neither ripgrep or the_silver_searcher is available")))
(args (concat " -- "
(shell-quote-argument (shell-quote-argument
(concat "\\s(" (concat "\\s("
(string-join (mapcar #'car +ivy-task-tags) "|") (string-join (mapcar #'car +ivy-task-tags) "|")
")([\\s:]|\\([^)]+\\):?)"))) ")([\\s:]|\\([^)]+\\):?)")))))
target)) (when-let (out (shell-command-to-string
"\n" t)))) (format "%s -H -S --no-heading %s %s"
command args target)))
(split-string out "\n" t)))))))
(defun +ivy--tasks-open-action (x) (defun +ivy--tasks-open-action (x)
"Jump to the file and line of the current task." "Jump to the file and line of the current task."