Skip to content

Commit

Permalink
more file/grep tools based on git
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen Bin committed Dec 7, 2015
1 parent 3dac921 commit 7500c10
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
7 changes: 4 additions & 3 deletions lisp/init-evil.el
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@
"ip" 'find-file-in-project
"kk" 'find-file-in-project-by-selected
"fd" 'find-directory-in-project-by-selected
"tm" 'get-term
"trm" 'get-term
"tff" 'toggle-frame-fullscreen
"tfm" 'toggle-frame-maximized
;; "ci" 'evilnc-comment-or-uncomment-lines
Expand All @@ -376,6 +376,8 @@
"yy" 'browse-kill-ring
"gf" 'counsel-git-find-file
"gl" 'counsel-git-grep-yank-line
"gg" 'counsel-git-grep ; quickest grep should be easy to press
"gm" 'counsel-git-find-my-file
"rjs" 'run-js
"rmz" 'run-mozilla
"rpy" 'run-python
Expand Down Expand Up @@ -405,7 +407,7 @@
"." 'evil-ex
;; @see https://github.com/pidu/git-timemachine
;; p: previous; n: next; w:hash; W:complete hash; g:nth version; q:quit
"gm" 'git-timemachine-toggle
"tmt" 'git-timemachine-toggle
;; toggle overview, @see http://emacs.wordpress.com/2007/01/16/quick-and-dirty-code-folding/
"ov" 'my-overview-of-current-buffer
"or" 'open-readme-in-git-root-directory
Expand All @@ -424,7 +426,6 @@
"sj" 'w3m-search-js-api-mdn
"sa" 'w3m-java-search
"sh" 'w3mext-hacker-search ; code search in all engines with firefox
"gg" 'counsel-git-grep ; quickest grep should be easy to press
"qq" 'my-grep
"gss" 'git-gutter:set-start-revision
"gsh" 'git-gutter-reset-to-head-parent
Expand Down
35 changes: 29 additions & 6 deletions lisp/init-git.el
Original file line number Diff line number Diff line change
Expand Up @@ -210,19 +210,42 @@ If OPEN-ANOTHER-WINDOW is not nil, results are displayed in new window."
"file"
open-another-window)))

(defun counsel-git-grep-yank-line ()
"Grep in the current git repository and yank the line."
(interactive)
(defun counsel-git-grep-yank-line (&optional insert-line)
"Grep in the current git repository and yank the line.
If INSERT-LINE is not nil, insert the line grepped"
(interactive "P")
(let (fn)
(setq fn (lambda (open-another-window val)
(let ((lst (split-string val ":")))
(kill-new (nth 2 lst))
(setq fn (lambda (unused-param val)
(let ((lst (split-string val ":")) text-line)
;; the actual text line could contain ":"
(setq text-line (replace-regexp-in-string (format "^%s:%s:" (car lst) (nth 1 lst)) "" val))
;; trim the text line
(setq text-line (replace-regexp-in-string (rx (* (any " \t\n")) eos) "" text-line))
(kill-new text-line)
(if insert-line (insert text-line))
(message "line from %s:%s => kill-ring" (car lst) (nth 1 lst)))))

(counsel-git-grep-or-find-api fn
"git --no-pager grep --full-name -n --no-color -i -e \"%s\""
"grep"
nil)))

(defvar counsel-my-name-regex ""
"My name used by `counsel-git-find-my-file', support regex like '[Tt]om [Cc]hen'.")

(defun counsel-git-find-my-file (&optional open-another-window)
"Find file in the current git repository.
If OPEN-ANOTHER-WINDOW is not nil, results are displayed in new window."
(interactive "P")
(let (fn)
(setq fn (lambda (open-another-window val)
(funcall (if open-another-window 'find-file-other-window 'find-file) val)))
(counsel-git-grep-or-find-api fn
(concat "git log --pretty=format: --name-only --since=\"6 months ago\" --author=\""
counsel-my-name-regex
"\" | grep \"%s\" | sort | uniq")
"file"
open-another-window)))
;; }}

(provide 'init-git)
Expand Down

0 comments on commit 7500c10

Please sign in to comment.