[Python-Dev] Emacs users: hg-tools-grep
barry at python.org
Sat Dec 8 22:51:06 CET 2012
Hark fellow Emacsers. All you unenlightened heathens can stop reading now.
A few years ago, my colleague Jono Lange wrote probably the best little chunk
of Emacs lisp ever. `M-x bzr-tools-grep` lets you easily search a Bazaar
repository for a case-sensitive string, providing you with a nice *grep*
buffer which you can scroll through. When you find a code sample you want to
look at, C-c C-c visits the file and plops you right at the matching line.
You *only* grep through files under version control, so you get to ignore
generated files, and compilation artifacts, etc.
Of course, this doesn't help you for working on the Python code base, because
Mercurial. I finally whipped up this straight up rip of Jono's code to work
with hg. I'm actually embarrassed to put a copyright on this thing, and would
happily just donate it to Jono, drop it in Python's Misc directory, or slip it
like a lump of coal into the xmas stocking of whoever wants to "maintain" it
for the next 20 years.
But anyway, it's already proven enormously helpful to me, so here it is.
P.S. Who wants to abuse Jono and Matthew's copyright again and provide a
<shudder> git version?
;; Copyright (c) 2012 Barry A. Warsaw
;; Permission is hereby granted, free of charge, to any person obtaining
;; a copy of this software and associated documentation files (the
;; "Software"), to deal in the Software without restriction, including
;; without limitation the rights to use, copy, modify, merge, publish,
;; distribute, sublicense, and/or sell copies of the Software, and to
;; permit persons to whom the Software is furnished to do so, subject to
;; the following conditions:
;; The above copyright notice and this permission notice shall be
;; included in all copies or substantial portions of the Software.
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
;; LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
;; OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
;; WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
;; This code is based on bzr-tools.el
;; Copyright (c) 2008-2009 Jonathan Lange, Matthew Lefkowitz
"hg locate --print0 | xargs -0 grep -In %s"
"The command used for grepping files using hg. See `hg-tools-grep'.")
;; Run 'code' at the root of the branch which dirname is in.
(defmacro hg-tools-at-branch-root (dirname &rest code)
`(let ((default-directory (locate-dominating-file (expand-file-name ,dirname) ".hg"))) , at code))
(defun hg-tools-grep (expression dirname)
"Search a branch for `expression'. If there's a C-u prefix, prompt for `dirname'."
(let* ((string (read-string "Search for: "))
(dir (if (null current-prefix-arg)
(read-directory-name (format "Search for %s in: " string)))))
(list string dir)))
(grep-find (format hg-tools-grep-command (shell-quote-argument expression)))))
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 836 bytes
Desc: not available
More information about the Python-Dev