Skip to content

Instantly share code, notes, and snippets.

@narendraj9
Last active February 7, 2019 18:41
Show Gist options
  • Save narendraj9/d49bbbc51a83aaa9b8fb07d5d82a0025 to your computer and use it in GitHub Desktop.
Save narendraj9/d49bbbc51a83aaa9b8fb07d5d82a0025 to your computer and use it in GitHub Desktop.
Search magit-blame committers
;;; -- Magit blame swiper results
(require 'magit-blame)
;;; -- Magit blame swiper results
(require 'dash)
(defun magit-blame--current-committer ()
(when (and (boundp 'magit-blame-read-only-mode)
magit-blame-read-only-mode)
(->> (overlays-at (point))
(-keep (lambda (ov)
(when (and (= (line-beginning-position) (overlay-start ov)))
(assoc-default "committer"
(overlay-get ov 'magit-blame-revinfo)))))
(apply #'concat)
((lambda (s) (when (not (string-empty-p s)) s))))))
(defun magit-blame--swiper-line (orig-line)
(if-let ((c (magit-blame--current-committer)))
(format (format "%%s %%%ds"
(max 0 (- (window-width) (length orig-line) 15)))
orig-line c)
orig-line))
(defun magit-blame--swiper-update-input-ivy ()
"Called when `ivy' input is updated."
(with-ivy-window
(swiper--cleanup)
(when (> (length (ivy-state-current ivy-last)) 0)
(let* ((regexp-or-regexps (funcall ivy--regex-function ivy-text))
(regexps
(if (listp regexp-or-regexps)
(mapcar #'car (cl-remove-if-not #'cdr regexp-or-regexps))
(list regexp-or-regexps))))
(dolist (re regexps)
(let* ((re (replace-regexp-in-string
" " "\t"
re))
(str (get-text-property 0 'swiper-line-number (ivy-state-current ivy-last)))
(num (if (string-match "^[0-9]+" str)
(string-to-number (match-string 0 str))
0)))
(unless (memq this-command '(ivy-yank-word
ivy-yank-symbol
ivy-yank-char
scroll-other-window))
(when (cl-plusp num)
(unless (if swiper--current-line
(eq swiper--current-line num)
(eq (line-number-at-pos) num))
(goto-char swiper--point-min)
(if swiper-use-visual-line
(line-move (1- num))
(forward-line (1- num))))
(if (and (equal ivy-text "")
(>= swiper--opoint (line-beginning-position))
(<= swiper--opoint (line-end-position)))
(goto-char swiper--opoint)
(if (eq swiper--current-line num)
(when swiper--current-match-start
(goto-char swiper--current-match-start))
(setq swiper--current-line num))
(when (re-search-forward re (line-end-position) t)
(setq swiper--current-match-start (match-beginning 0)))
;; If committer's name matches override
;; `swiper--current-match-start'.
(when (and (magit-blame--current-committer)
(string-match re (magit-blame--current-committer)))
(setq swiper--current-match-start (line-beginning-position))))
(isearch-range-invisible (line-beginning-position)
(line-end-position))
(unless (and (>= (point) (window-start))
(<= (point) (window-end (ivy-state-window ivy-last) t)))
(recenter))
(setq swiper--current-window-start (window-start))))
(swiper--add-overlays
re
(max (window-start) swiper--point-min)
(min (window-end (selected-window) t) swiper--point-max))))))))
(add-function :filter-return
(symbol-function #'swiper--line)
#'magit-blame--swiper-line)
(add-function :override
(symbol-function #'swiper--update-input-ivy)
#'magit-blame--swiper-update-input-ivy)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment