]> code.delx.au - gnu-emacs-elpa/blob - diff-hl-dired.el
c9097ee90aa34dac982348a4d6daec27e368a840
[gnu-emacs-elpa] / diff-hl-dired.el
1 ;;; diff-hl-dired.el --- Highlight changed files in Dired -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2012-2014 Free Software Foundation, Inc.
4
5 ;; This file is part of GNU Emacs.
6
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.
11
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.
16
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/>.
19
20 ;;; Commentary:
21
22 ;; To enable in all Dired buffers, add this to your init file:
23 ;;
24 ;; (add-hook 'dired-mode-hook 'diff-hl-dired-mode)
25 ;;
26 ;; or
27 ;;
28 ;; (add-hook 'dired-mode-hook 'diff-hl-dired-mode-unless-remote)
29 ;;
30 ;; to do it only in local Dired buffers.
31
32 ;;; Code:
33
34 (require 'diff-hl)
35 (require 'dired)
36
37 (defvar diff-hl-dired-process-buffer nil)
38
39 (defgroup diff-hl-dired nil
40 "VC diff highlighting on the side of a Dired window."
41 :group 'diff-hl)
42
43 (defface diff-hl-dired-insert
44 '((default :inherit diff-hl-insert))
45 "Face used to highlight added files.")
46
47 (defface diff-hl-dired-delete
48 '((default :inherit diff-hl-delete))
49 "Face used to highlight directories with deleted files.")
50
51 (defface diff-hl-dired-change
52 '((default :inherit diff-hl-change))
53 "Face used to highlight changed files.")
54
55 (defface diff-hl-dired-unknown
56 '((default :inherit dired-ignored))
57 "Face used to highlight unregistered files.")
58
59 (defface diff-hl-dired-ignored
60 '((default :inherit dired-ignored))
61 "Face used to highlight unregistered files.")
62
63 (defcustom diff-hl-dired-extra-indicators t
64 "Non-nil to indicate ignored files."
65 :group 'diff-hl
66 :type 'boolean)
67
68 ;;;###autoload
69 (define-minor-mode diff-hl-dired-mode
70 "Toggle VC diff highlighting on the side of a Dired window."
71 :lighter ""
72 (if diff-hl-dired-mode
73 (progn
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)))
79
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"))
87 collect file))
88 dirs-alist files-alist)
89 (when backend
90 (diff-hl-dired-clear)
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))
98 (erase-buffer)
99 (vc-call-backend
100 backend 'dir-status-files def-dir
101 (when diff-hl-dired-extra-indicators
102 contents)
103 nil
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)
114 state)))
115 (if (string-match "\\`\\([^/]+\\)/" file)
116 (let* ((dir (match-string 1 file))
117 (value (cdr (assoc dir dirs-alist))))
118 (unless (eq value type)
119 (cond
120 ((eq type 'up-to-date))
121 ((null value)
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)))))
126 (unless more-to-come
127 (diff-hl-dired-highlight-items
128 (append dirs-alist files-alist))))))
129 )))))
130
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)
136 activate)
137 (when (eq stage 'ls-files-up-to-date)
138 (setq stage 'diff-index))
139 ad-do-it))
140
141 (defun diff-hl-dired-highlight-items (alist)
142 "Highlight ALIST containing (FILE . TYPE) elements."
143 (dolist (pair alist)
144 (let ((file (car pair))
145 (type (cdr pair)))
146 (save-excursion
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))
154 ))))))
155
156 (defun diff-hl-dired-face-from-type (type _pos)
157 (intern (format "diff-hl-dired-%s" type)))
158
159 (defalias 'diff-hl-dired-clear 'diff-hl-remove-overlays)
160
161 (defun diff-hl-dired-mode-unless-remote ()
162 (unless (file-remote-p default-directory)
163 (diff-hl-dired-mode)))
164
165 (provide 'diff-hl-dired)
166
167 ;;; diff-hl-dired.el ends here