Skip to content

Commit fbc4b89

Browse files
committed
Minor counsel-git-grep cleanup
* counsel.el (counsel-git-grep-function): Simplify. (counsel--git-grep-file-and-line-number): Rename... (counsel--git-grep-file-and-line): ...to this, splitting long lines. (counsel--git-grep-go-to-location): Rename... (counsel--git-grep-visit): ...to this, incorporating more DRY. (counsel-git-grep-action, counsel-git-grep-action-other-window): Adapt accordingly (#3044).
1 parent cfd61b1 commit fbc4b89

File tree

1 file changed

+30
-35
lines changed

1 file changed

+30
-35
lines changed

counsel.el

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,49 +1429,44 @@ This function should set `ivy--old-re'."
14291429
"Grep in the current Git repository for STRING."
14301430
(or
14311431
(ivy-more-chars)
1432-
(progn
1433-
(counsel--async-command
1434-
(concat
1435-
(funcall counsel-git-grep-cmd-function string)
1436-
(if (ivy--case-fold-p string) " -i" "")))
1437-
nil)))
1432+
(ignore
1433+
(counsel--async-command
1434+
(concat
1435+
(funcall counsel-git-grep-cmd-function string)
1436+
(and (ivy--case-fold-p string) " -i"))))))
14381437

14391438
(defun counsel-git-grep-action (x)
14401439
"Go to occurrence X in current Git repository."
1441-
(let ((file-and-line-number (counsel--git-grep-file-and-line-number x)))
1442-
(when file-and-line-number
1443-
(find-file (expand-file-name
1444-
(car file-and-line-number)
1445-
(ivy-state-directory ivy-last)))
1446-
(counsel--git-grep-go-to-location (cdr file-and-line-number)))))
1440+
(counsel--git-grep-visit x))
14471441

14481442
(defun counsel-git-grep-action-other-window (x)
14491443
"Go to occurrence X in current Git repository in another window."
1450-
(let ((file-and-line-number (counsel--git-grep-file-and-line-number x)))
1451-
(when file-and-line-number
1452-
(find-file-other-window (expand-file-name
1453-
(car file-and-line-number)
1454-
(ivy-state-directory ivy-last)))
1455-
(counsel--git-grep-go-to-location (cdr file-and-line-number)))))
1456-
1457-
(defun counsel--git-grep-file-and-line-number (x)
1444+
(counsel--git-grep-visit x t))
1445+
1446+
(defun counsel--git-grep-file-and-line (x)
14581447
"Extract file name and line number from `counsel-git-grep' line X.
14591448
Return a pair (FILE . LINE) on success; nil otherwise."
1460-
(when (string-match "\\`\\(.*?\\):\\([0-9]+\\):\\(.*\\)\\'" x)
1461-
(cons (match-string-no-properties 1 x) (string-to-number (match-string-no-properties 2 x)))))
1462-
1463-
(defun counsel--git-grep-go-to-location (line-number)
1464-
"Go to LINE-NUMBER within current buffer."
1465-
(goto-char (point-min))
1466-
(forward-line (1- line-number))
1467-
(when (re-search-forward (ivy--regex ivy-text t) (line-end-position) t)
1468-
(when swiper-goto-start-of-match
1469-
(goto-char (match-beginning 0))))
1470-
(swiper--ensure-visible)
1471-
(run-hooks 'counsel-grep-post-action-hook)
1472-
(unless (eq ivy-exit 'done)
1473-
(swiper--cleanup)
1474-
(swiper--add-overlays (ivy--regex ivy-text))))
1449+
(and (string-match "\\`\\(.*?\\):\\([0-9]+\\):\\(.*\\)\\'" x)
1450+
(cons (match-string-no-properties 1 x)
1451+
(string-to-number (match-string-no-properties 2 x)))))
1452+
1453+
(defun counsel--git-grep-visit (cand &optional other-window)
1454+
"Visit `counsel-git-grep' CAND, optionally in OTHER-WINDOW."
1455+
(let ((file-and-line (counsel--git-grep-file-and-line cand)))
1456+
(when file-and-line
1457+
(funcall (if other-window #'find-file-other-window #'find-file)
1458+
(expand-file-name (car file-and-line)
1459+
(ivy-state-directory ivy-last)))
1460+
(goto-char (point-min))
1461+
(forward-line (1- (cdr file-and-line)))
1462+
(when (re-search-forward (ivy--regex ivy-text t) (line-end-position) t)
1463+
(when swiper-goto-start-of-match
1464+
(goto-char (match-beginning 0))))
1465+
(swiper--ensure-visible)
1466+
(run-hooks 'counsel-grep-post-action-hook)
1467+
(unless (eq ivy-exit 'done)
1468+
(swiper--cleanup)
1469+
(swiper--add-overlays (ivy--regex ivy-text))))))
14751470

14761471
(ivy-set-actions
14771472
'counsel-git-grep

0 commit comments

Comments
 (0)