1 ;;; diff-hl-dired.el --- Highlight changed files in Dired -*- lexical-binding: t -*-
3 ;; Copyright (C) 2012-2014 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software: you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;; To enable in all Dired buffers, add this to your init file:
24 ;; (add-hook 'dired-mode-hook 'diff-hl-dired-mode)
28 ;; (add-hook 'dired-mode-hook 'diff-hl-dired-mode-unless-remote)
30 ;; to do it only in local Dired buffers.
37 (defvar diff-hl-dired-process-buffer nil)
39 (defgroup diff-hl-dired nil
40 "VC diff highlighting on the side of a Dired window."
43 (defface diff-hl-dired-insert
44 '((default :inherit diff-hl-insert))
45 "Face used to highlight added files.")
47 (defface diff-hl-dired-delete
48 '((default :inherit diff-hl-delete))
49 "Face used to highlight directories with deleted files.")
51 (defface diff-hl-dired-change
52 '((default :inherit diff-hl-change))
53 "Face used to highlight changed files.")
55 (defface diff-hl-dired-unknown
56 '((default :inherit dired-ignored))
57 "Face used to highlight unregistered files.")
59 (defface diff-hl-dired-ignored
60 '((default :inherit dired-ignored))
61 "Face used to highlight unregistered files.")
63 (defcustom diff-hl-dired-extra-indicators t
64 "Non-nil to indicate ignored files."
69 (define-minor-mode diff-hl-dired-mode
70 "Toggle VC diff highlighting on the side of a Dired window."
72 (if diff-hl-dired-mode
74 (diff-hl-maybe-define-bitmaps)
75 (set (make-local-variable 'diff-hl-dired-process-buffer) nil)
76 (add-hook 'dired-after-readin-hook 'diff-hl-dired-update nil t))
77 (remove-hook 'dired-after-readin-hook 'diff-hl-dired-update t)
78 (diff-hl-dired-clear)))
80 (defun diff-hl-dired-update ()
81 "Highlight the Dired buffer."
82 (let ((backend (ignore-errors (vc-responsible-backend default-directory)))
83 (def-dir default-directory)
84 (buffer (current-buffer))
85 (contents (cl-loop for file in (directory-files default-directory)
86 unless (member file '("." ".." ".hg"))
88 dirs-alist files-alist)
91 (if (buffer-live-p diff-hl-dired-process-buffer)
92 (let ((proc (get-buffer-process diff-hl-dired-process-buffer)))
93 (when proc (kill-process proc)))
94 (setq diff-hl-dired-process-buffer
95 (generate-new-buffer " *diff-hl-dired* tmp status")))
96 (with-current-buffer diff-hl-dired-process-buffer
97 (setq default-directory (expand-file-name def-dir))
100 backend 'dir-status-files def-dir
101 (when diff-hl-dired-extra-indicators
104 (lambda (entries &optional more-to-come)
105 (when (buffer-live-p buffer)
106 (with-current-buffer buffer
107 (dolist (entry entries)
108 (cl-destructuring-bind (file state &rest r) entry
109 ;; Work around http://debbugs.gnu.org/18605
110 (setq file (replace-regexp-in-string "\\` " "" file))
111 (let ((type (plist-get
112 '(edited change added insert removed delete
113 unregistered unknown ignored ignored)
115 (if (string-match "\\`\\([^/]+\\)/" file)
116 (let* ((dir (match-string 1 file))
117 (value (cdr (assoc dir dirs-alist))))
118 (unless (eq value type)
120 ((eq type 'up-to-date))
122 (push (cons dir type) dirs-alist))
123 ((not (eq type 'ignored))
124 (setcdr (assoc dir dirs-alist) 'change)))))
125 (push (cons file type) files-alist)))))
127 (diff-hl-dired-highlight-items
128 (append dirs-alist files-alist))))))
131 (when (version< emacs-version "24.4.51.5")
132 ;; Work around http://debbugs.gnu.org/19386
133 (defadvice vc-git-dir-status-goto-stage (around
134 diff-hl-dired-skip-up-to-date
135 (stage files update-function)
137 (when (eq stage 'ls-files-up-to-date)
138 (setq stage 'diff-index))
141 (defun diff-hl-dired-highlight-items (alist)
142 "Highlight ALIST containing (FILE . TYPE) elements."
144 (let ((file (car pair))
147 (goto-char (point-min))
148 (when (and type (dired-goto-file-1
149 file (expand-file-name file) nil))
150 (let* ((diff-hl-fringe-bmp-function 'diff-hl-fringe-bmp-from-type)
151 (diff-hl-fringe-face-function 'diff-hl-dired-face-from-type)
152 (o (diff-hl-add-highlighting type 'single)))
153 (overlay-put o 'modification-hooks '(diff-hl-overlay-modified))
156 (defun diff-hl-dired-face-from-type (type _pos)
157 (intern (format "diff-hl-dired-%s" type)))
159 (defalias 'diff-hl-dired-clear 'diff-hl-remove-overlays)
161 (defun diff-hl-dired-mode-unless-remote ()
162 (unless (file-remote-p default-directory)
163 (diff-hl-dired-mode)))
165 (provide 'diff-hl-dired)
167 ;;; diff-hl-dired.el ends here