Archives

Creative Commons License
This blog is licensed under a Creative Commons License.

Omitting Git-ignored files in Emacs dired

| 3 Comments | No TrackBacks

Adding the following snippet to your .emacs file will cause Emacs’ dired mode to omit all files ignored by Git. This only works if you have dired-omit-mode on, which is ordinarily bound to Meta-o.

The code

Here is the code you would add to your .emacs file:

(add-hook ‘dired-load-hook #’(lambda nil (load “dired-x” t)))

(eval-after-load “dired-x”
  ‘(progn
     (defvar dired-omit-regexp-orig (symbol-function ‘dired-omit-regexp))

     (defun dired-omit-regexp ()
       (let ((file (expand-file-name “.git”))
             parent-dir)
         (while (and (not (file-exists-p file))
                     (progn
                       (setq parent-dir
                             (file-name-directory
                              (directory-file-name
                               (file-name-directory file))))
                       ;; Give up if we are already at the root dir.
                       (not (string= (file-name-directory file)
                                     parent-dir))))
           ;; Move up to the parent dir and try again.
           (setq file (expand-file-name “.git” parent-dir)))
         ;; If we found a change log in a parent, use that.
         (if (file-exists-p file)
             (let ((regexp (funcall dired-omit-regexp-orig)))
               (assert (stringp regexp))
               (concat
                regexp
                (if (> (length regexp) 0)
                    “\\|” “”)
                “\\(“
                (mapconcat
                 #’(lambda (str)
                     (concat “^”
                             (regexp-quote
                              (substring str 13
                                         (if (= ?/ (aref str (1- (length str))))
                                             (1- (length str))
                                           nil)))
                             “$”))
                 (split-string (shell-command-to-string
                                “git clean -d -x -n”)
                               “\n” t)
                 “\\|”)
                “\\)”))
           (funcall dired-omit-regexp-orig))))))

A note to fellow Emacs coders: I tried writing this as a piece of defadvice, rather than hijacking the definition of dired-omit-regexp, but for some reason it never called this function.

No TrackBacks

TrackBack URL: http://www.newartisans.com/mt/mt-tb.cgi/8

3 Comments

Did you call `(ad-activate ‘dired-omit-regexp)’? I always forget to do it.

Your posting software conveniently “fixed” all the quotes in your code to be pretty…

Ugh. I’ll have to find a way to stop it from doing that. Thanks.

About this Entry

This page contains a single entry by John Wiegley published on August 12, 2008 1:22 AM.

A new Ledger mailing list was the previous entry in this blog.

Too much good stuff is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Recent Comments

  • Curt Sampson: That there’s “no state” in Haskell is quite wrong; in read more
  • rv: Hi. I wanted to drop you a quick note to read more
  • John Wiegley: It’s here: http://ftp.newartisans.com/pub/python/modpython_gateway.py read more
  • Leon: The file “modpython_gateway.py” Is no longer available in the downloads read more
  • Kathy: Well, the article is really the sweetest on this laudable read more
  • mr.design: Hi John, I just started to read your GFTBU, it’s read more
  • yoman: “Barfin”? “Slurping”? “Slime” “Hunchentoot” ??? What in the T.F. world read more
  • John Wiegley: Something like this is slated for the next release of read more
  • womens health: According to me, Apple has implemented something called blocks, which read more
  • Bjorn Tipling: Why would you add instructions for installing an editor when read more
OpenID accepted here Learn more about OpenID
Powered by Movable Type 4.261