]> code.delx.au - gnu-emacs-elpa/blob - diff-hl-dired.el
diff-hl-dired: Always use dir-status-files
[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 ;;; Code:
27
28 (require 'diff-hl)
29 (require 'dired)
30
31 (defvar diff-hl-dired-process-buffer nil)
32
33 (defgroup diff-hl-dired nil
34 "VC diff highlighting on the side of a Dired window."
35 :group 'diff-hl)
36
37 (defface diff-hl-dired-insert
38 '((default :inherit diff-hl-insert))
39 "Face used to highlight added files.")
40
41 (defface diff-hl-dired-delete
42 '((default :inherit diff-hl-delete))
43 "Face used to highlight directories with deleted files.")
44
45 (defface diff-hl-dired-change
46 '((default :inherit diff-hl-change))
47 "Face used to highlight changed files.")
48
49 (defface diff-hl-dired-unknown
50 '((default :inherit dired-ignored))
51 "Face used to highlight unregistered files.")
52
53 (defface diff-hl-dired-ignored
54 '((default :inherit dired-ignored))
55 "Face used to highlight unregistered files.")
56
57 (defcustom diff-hl-dired-extra-indicators t
58 "Non-nil to indicate ignored files."
59 :group 'diff-hl
60 :type 'boolean)
61
62 ;;;###autoload
63 (define-minor-mode diff-hl-dired-mode
64 "Toggle VC diff highlighting on the side of a Dired window."
65 :lighter ""
66 (if diff-hl-dired-mode
67 (progn
68 (diff-hl-maybe-define-bitmaps)
69 (set (make-local-variable 'diff-hl-dired-process-buffer) nil)
70 (add-hook 'dired-after-readin-hook 'diff-hl-dired-update nil t))
71 (remove-hook 'dired-after-readin-hook 'diff-hl-dired-update t)
72 (diff-hl-dired-clear)))
73
74 (defun diff-hl-dired-update ()
75 "Highlight the Dired buffer."
76 (let ((backend (ignore-errors (vc-responsible-backend default-directory)))
77 (def-dir default-directory)
78 (buffer (current-buffer))
79 (contents (cl-loop for file in (directory-files default-directory)
80 unless (member file '("." ".." ".hg"))
81 collect file))
82 dirs-alist files-alist)
83 (when backend
84 (diff-hl-dired-clear)
85 (if (buffer-live-p diff-hl-dired-process-buffer)
86 (let ((proc (get-buffer-process diff-hl-dired-process-buffer)))
87 (when proc (kill-process proc)))
88 (setq diff-hl-dired-process-buffer
89 (generate-new-buffer " *diff-hl-dired* tmp status")))
90 (with-current-buffer diff-hl-dired-process-buffer
91 (setq default-directory (expand-file-name def-dir))
92 (erase-buffer)
93 (vc-call-backend
94 backend 'dir-status-files def-dir
95 (when diff-hl-dired-extra-indicators
96 contents)
97 nil
98 (lambda (entries &optional more-to-come)
99 (when (buffer-live-p buffer)
100 (with-current-buffer buffer
101 (dolist (entry entries)
102 (cl-destructuring-bind (file state &rest r) entry
103 ;; Work around http://debbugs.gnu.org/18605
104 (setq file (replace-regexp-in-string "\\` " "" file))
105 (let ((type (plist-get
106 '(edited change added insert removed delete
107 unregistered unknown ignored ignored)
108 state)))
109 (if (string-match "\\`\\([^/]+\\)/" file)
110 (let* ((dir (match-string 1 file))
111 (value (cdr (assoc dir dirs-alist))))
112 (unless (eq value type)
113 (cond
114 ((eq type 'up-to-date))
115 ((null value)
116 (push (cons dir type) dirs-alist))
117 ((not (eq type 'ignored))
118 (setcdr (assoc dir dirs-alist) 'change)))))
119 (push (cons file type) files-alist)))))
120 (unless more-to-come
121 (diff-hl-dired-highlight-items
122 (append dirs-alist files-alist))))))
123 )))))
124
125 (when (version< emacs-version "24.4.51.5")
126 ;; Work around http://debbugs.gnu.org/19386
127 (defadvice vc-git-dir-status-goto-stage (around
128 diff-hl-dired-skip-up-to-date
129 (stage files update-function)
130 activate)
131 (when (eq stage 'ls-files-up-to-date)
132 (setq stage 'diff-index))
133 ad-do-it))
134
135 (defun diff-hl-dired-highlight-items (alist)
136 "Highlight ALIST containing (FILE . TYPE) elements."
137 (dolist (pair alist)
138 (let ((file (car pair))
139 (type (cdr pair)))
140 (save-excursion
141 (goto-char (point-min))
142 (when (and type (dired-goto-file-1
143 file (expand-file-name file) nil))
144 (let* ((diff-hl-fringe-bmp-function 'diff-hl-fringe-bmp-from-type)
145 (diff-hl-fringe-face-function 'diff-hl-dired-face-from-type)
146 (o (diff-hl-add-highlighting type 'single)))
147 (overlay-put o 'modification-hooks '(diff-hl-overlay-modified))
148 ))))))
149
150 (defun diff-hl-dired-face-from-type (type _pos)
151 (intern (format "diff-hl-dired-%s" type)))
152
153 (defalias 'diff-hl-dired-clear 'diff-hl-remove-overlays)
154
155 (provide 'diff-hl-dired)
156
157 ;;; diff-hl-dired.el ends here