forked from yadex205/consult-ag
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsult-ugrep.el
84 lines (70 loc) · 3.38 KB
/
consult-ugrep.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
;;; consult-ugrep.el --- Ugrep integration using Consult -*- lexical-binding: t; -*-
;; Copyright (C) 2022 Joachim Nielandt
;; Author: Joachim Nielandt <[email protected]>
;; Homepage: https://github.com/joachimnielandt/consult-ugrep
;; Package-Requires: ((emacs "27.1") (consult "0.16"))
;; SPDX-License-Identifier: MIT
;; Version: 0.1.2
;; This file is not part of GNU Emacs.
;;; Commentary:
;;; This is a quick modification of the consult-ag package. All props go to yadex205.
;; consult-ugrep provides interfaces for using `ugrep`.
;; To use this, turn on `consult-ugrep` in your init-file or interactively.
(eval-when-compile ;; as mentioned in consult-ag - required for byte-compile
(require 'cl-lib)
(require 'subr-x))
(require 'consult)
;; define binary ugrep interaction
(setq ugrep-bin "ugrep")
(setq ugrep-args "--line-buffered --color=never --ignore-case --bool --files --exclude-dir=.git --line-number --column-number -I -r")
(setq ugrep-command (concat ugrep-bin " " ugrep-args))
(defun consult-ugrep--builder (input)
"Build command line given INPUT."
(pcase-let* ((cmd (split-string-and-unquote ugrep-command))
(`(,arg . ,opts) (consult--command-split input)))
`(,@cmd ,@opts ,arg ".")))
(defun consult-ugrep--format (line)
"Parse LINE into candidate text."
(when (string-match "^\\([^:]+\\):\\([0-9]+\\):\\([0-9]+\\):\\(.*\\)$" line)
(let* ((filename (match-string 1 line))
(row (match-string 2 line))
(column (match-string 3 line))
(body (match-string 4 line))
(candidate (format "%s:%s:%s:%s"
(propertize filename 'face 'consult-file)
(propertize row 'face 'consult-line-number)
(propertize column 'face 'consult-line-number) body)))
(propertize candidate 'filename filename 'row row 'column column))))
(defun consult-ugrep--grep-position (cand &optional find-file)
"Return the candidate position marker for CAND.
FIND-FILE is the file open function, defaulting to `find-file`."
(when cand
(let ((file (get-text-property 0 'filename cand))
(row (string-to-number (get-text-property 0 'row cand)))
(column (- (string-to-number (get-text-property 0 'column cand)) 1)))
(consult--position-marker (funcall (or find-file #'find-file) file) row column))))
(defun consult-ugrep--grep-state ()
"Not documented."
(let ((open (consult--temporary-files))
(jump (consult--jump-state)))
(lambda (action cand)
(unless cand
(funcall open nil))
(funcall jump action (consult-ugrep--grep-position cand open)))))
;;;###autoload
(defun consult-ugrep (&optional target initial)
"Consult ugrep for query in TARGET file(s) with INITIAL input."
(interactive)
(let* ((prompt-dir (consult--directory-prompt "Consult ugrep: " target))
(default-directory (cdr prompt-dir)))
(consult--read (consult--async-command #'consult-ugrep--builder
(consult--async-map #'consult-ugrep--format))
:prompt (car prompt-dir)
:lookup #'consult--lookup-member
:state (consult-ugrep--grep-state)
:initial (consult--async-split-initial initial)
:require-match t
:category 'file
:sort nil)))
(provide 'consult-ugrep)
;;; consult-ugrep.el ends here