[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master 61c2b12 125/348: counsel-git-grep now supports custom per-
From: |
Oleh Krehel |
Subject: |
[elpa] master 61c2b12 125/348: counsel-git-grep now supports custom per-project commands |
Date: |
Sat, 8 Apr 2017 11:03:41 -0400 (EDT) |
branch: master
commit 61c2b123cdb8d85019c349eb056d2c3915ed7fb1
Author: Oleh Krehel <address@hidden>
Commit: Oleh Krehel <address@hidden>
counsel-git-grep now supports custom per-project commands
* ivy.el (ivy-recompute-index-swiper): Return 0 on error.
* counsel.el (counsel-git-grep-projects-alist): New defvar.
(counsel-git-grep): When used with "C-u", and inside
`counsel-git-grep-projects-alist', use the corresponding command
instead. Additionally, `counsel-git-grep-proj-function' is used.
(counsel-git-grep-proj-function): New defun.
For a possible implementation of the custom git-grep command, see:
https://github.com/abo-abo/oremacs/commit/c7effdb94749dc600b1204ea7a9db319ebdb0f00
Fixes #616
---
counsel.el | 78 +++++++++++++++++++++++++++++++++++++++++---------------------
ivy.el | 44 ++++++++++++++++++-----------------
2 files changed, 75 insertions(+), 47 deletions(-)
diff --git a/counsel.el b/counsel.el
index 2b08da6..b42550e 100644
--- a/counsel.el
+++ b/counsel.el
@@ -979,6 +979,11 @@ Describe the selected candidate."
str))
str)
+(defvar counsel-git-grep-projects-alist nil
+ "An alist of project directory to \"git-grep\" command.
+Allows to automatically use a custom \"git-grep\" command for all
+files in a project.")
+
;;;###autoload
(defun counsel-git-grep (&optional cmd initial-input)
"Grep for a string in the current git repository.
@@ -987,32 +992,53 @@ When CMD is non-nil, prompt for a specific \"git grep\"
command.
INITIAL-INPUT can be given as the initial minibuffer input."
(interactive "P")
(ivy-set-prompt 'counsel-git-grep counsel-prompt-function)
- (cond
- ((stringp cmd)
- (setq counsel-git-grep-cmd cmd))
- (cmd
- (setq counsel-git-grep-cmd
- (ivy-read "cmd: " counsel-git-grep-cmd-history
- :history 'counsel-git-grep-cmd-history
- :re-builder #'ivy--regex))
- (setq counsel-git-grep-cmd-history
- (delete-dups counsel-git-grep-cmd-history)))
- (t
- (setq counsel-git-grep-cmd counsel-git-grep-cmd-default)))
- (setq counsel--git-grep-dir
- (locate-dominating-file default-directory ".git"))
- (if (null counsel--git-grep-dir)
- (error "Not in a git repository")
- (setq counsel--git-grep-count (counsel--gg-count "" t))
- (ivy-read "git grep" 'counsel-git-grep-function
- :initial-input initial-input
- :matcher #'counsel-git-grep-matcher
- :dynamic-collection (> counsel--git-grep-count 20000)
- :keymap counsel-git-grep-map
- :action #'counsel-git-grep-action
- :unwind #'swiper--cleanup
- :history 'counsel-git-grep-history
- :caller 'counsel-git-grep)))
+ (let ((dd (expand-file-name default-directory))
+ proj)
+ (cond
+ ((stringp cmd)
+ (setq counsel-git-grep-cmd cmd))
+ (cmd
+ (if (setq proj
+ (cl-find-if
+ (lambda (x)
+ (string-match (car x) dd))
+ counsel-git-grep-projects-alist))
+ (setq counsel-git-grep-cmd (cdr proj))
+ (setq counsel-git-grep-cmd
+ (ivy-read "cmd: " counsel-git-grep-cmd-history
+ :history 'counsel-git-grep-cmd-history
+ :re-builder #'ivy--regex))
+ (setq counsel-git-grep-cmd-history
+ (delete-dups counsel-git-grep-cmd-history))))
+ (t
+ (setq counsel-git-grep-cmd counsel-git-grep-cmd-default)))
+ (setq counsel--git-grep-dir
+ (if proj
+ (car proj)
+ (locate-dominating-file default-directory ".git")))
+ (if (null counsel--git-grep-dir)
+ (error "Not in a git repository")
+ (unless proj
+ (setq counsel--git-grep-count (counsel--gg-count "" t)))
+ (ivy-read "git grep: " (if proj
+ 'counsel-git-grep-proj-function
+ 'counsel-git-grep-function)
+ :initial-input initial-input
+ :matcher #'counsel-git-grep-matcher
+ :dynamic-collection (or proj (> counsel--git-grep-count 20000))
+ :keymap counsel-git-grep-map
+ :action #'counsel-git-grep-action
+ :unwind #'swiper--cleanup
+ :history 'counsel-git-grep-history
+ :caller 'counsel-git-grep))))
+
+(defun counsel-git-grep-proj-function (str)
+ (if (< (length str) 3)
+ (counsel-more-chars 3)
+ (let ((regex (setq ivy--old-re
+ (ivy--regex str t))))
+ (counsel--async-command (format counsel-git-grep-cmd regex))
+ nil)))
(defun counsel-git-grep-switch-cmd ()
"Set `counsel-git-grep-cmd' to a different value."
diff --git a/ivy.el b/ivy.el
index aa433d4..8034739 100644
--- a/ivy.el
+++ b/ivy.el
@@ -2444,27 +2444,29 @@ Prefer first \"^*NAME\", then \"^NAME\"."
ivy--index)))))
(defun ivy-recompute-index-swiper (_re-str cands)
- (let ((tail (nthcdr ivy--index ivy--old-cands))
- idx)
- (if (and tail ivy--old-cands (not (equal "^" ivy--old-re)))
- (progn
- (while (and tail (null idx))
- ;; Compare with eq to handle equal duplicates in cands
- (setq idx (cl-position (pop tail) cands)))
- (or
- idx
- (1- (length cands))))
- (if ivy--old-cands
- ivy--index
- ;; already in ivy-state-buffer
- (let ((n (line-number-at-pos))
- (res 0)
- (i 0))
- (dolist (c cands)
- (when (eq n (read (get-text-property 0 'swiper-line-number c)))
- (setq res i))
- (cl-incf i))
- res)))))
+ (condition-case nil
+ (let ((tail (nthcdr ivy--index ivy--old-cands))
+ idx)
+ (if (and tail ivy--old-cands (not (equal "^" ivy--old-re)))
+ (progn
+ (while (and tail (null idx))
+ ;; Compare with eq to handle equal duplicates in cands
+ (setq idx (cl-position (pop tail) cands)))
+ (or
+ idx
+ (1- (length cands))))
+ (if ivy--old-cands
+ ivy--index
+ ;; already in ivy-state-buffer
+ (let ((n (line-number-at-pos))
+ (res 0)
+ (i 0))
+ (dolist (c cands)
+ (when (eq n (read (get-text-property 0 'swiper-line-number c)))
+ (setq res i))
+ (cl-incf i))
+ res))))
+ (error 0)))
(defun ivy-recompute-index-swiper-async (_re-str cands)
(if (null ivy--old-cands)
- [elpa] master f27a1f0 124/348: swiper.el (swiper-all-function): Add a ">0 cands -> 0 cands" fix, (continued)
- [elpa] master f27a1f0 124/348: swiper.el (swiper-all-function): Add a ">0 cands -> 0 cands" fix, Oleh Krehel, 2017/04/08
- [elpa] master b3ca5e3 119/348: Add counsel commands for jumping to files, Oleh Krehel, 2017/04/08
- [elpa] master 46a5346 121/348: counsel.el (counsel-file-jump): Skip ".git/", Oleh Krehel, 2017/04/08
- [elpa] master 3c20b2c 126/348: counsel.el (counsel-git-grep): Fix prompt, Oleh Krehel, 2017/04/08
- [elpa] master fe92b1b 133/348: ivy.el: Fix the "C-m" not working for last commit, Oleh Krehel, 2017/04/08
- [elpa] master 230ae1a 132/348: Implement unique index for alist completion, Oleh Krehel, 2017/04/08
- [elpa] master 413bf7f 135/348: ivy.el: Prevent region expanding to prompt, Oleh Krehel, 2017/04/08
- [elpa] master b49ed2f 140/348: ivy.el (ivy--reset-state): Add stringp preselect guard, Oleh Krehel, 2017/04/08
- [elpa] master a2ed614 138/348: ivy.el (ivy-occur-mode-map): Make "RET" switch window, Oleh Krehel, 2017/04/08
- [elpa] master 80db2cc 142/348: ivy-test.el (ivy-read-preselect): New test, Oleh Krehel, 2017/04/08
- [elpa] master 61c2b12 125/348: counsel-git-grep now supports custom per-project commands,
Oleh Krehel <=
- [elpa] master 434ccce 150/348: swiper.el (swiper-font-lock-exclude): Add bookmark-bmenu-mode, Oleh Krehel, 2017/04/08
- [elpa] master 242ae95 148/348: ivy.el (ivy--reset-state): Fix read-only collection member, Oleh Krehel, 2017/04/08
- [elpa] master a99c125 161/348: counsel.el (counsel-grep): Specify :re-builder, Oleh Krehel, 2017/04/08
- [elpa] master 4e6145c 168/348: counsel.el (counsel-yank-pop-separator): New defcustom, Oleh Krehel, 2017/04/08
- [elpa] master b73264e 163/348: counsel.el (counsel-git-log-cmd): New defvar, Oleh Krehel, 2017/04/08
- [elpa] master 7457d1e 169/348: Add leading zeros to the hex representation to counsel-unicode-char, Oleh Krehel, 2017/04/08
- [elpa] master 8983f9d 167/348: counsel.el (counsel-recentf): Turn on recentf, Oleh Krehel, 2017/04/08
- [elpa] master 6961df6 166/348: doc/ivy.org: Replace swiper with counsel (MELPA), Oleh Krehel, 2017/04/08
- [elpa] master 3656dfe 171/348: ivy.el (ivy-exit-with-action): Remove with-ivy-window, Oleh Krehel, 2017/04/08
- [elpa] master 4c3ecb4 157/348: Restore window position after using swiper., Oleh Krehel, 2017/04/08