12 Aug 2008 johnw   » (Master)

Omitting Git-ignored files in Emacs dired

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.

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

(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.

Syndicated 2008-08-12 07:22:26 from johnw@newartisans.com

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!