]> code.delx.au - gnu-emacs-elpa/blob - diff-hl-dired.el
047d0e89c465266094c37c87d4696d55431652df
[gnu-emacs-elpa] / diff-hl-dired.el
1 ;;; diff-hl-dired.el --- Highlight changed files in Dired -*- lexical-binding: t -*-
2
3 ;; This file is not part of GNU Emacs.
4
5 ;; This file is free software: you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation, either version 3 of the License, or
8 ;; (at your option) any later version.
9
10 ;; This file is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
14
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
17
18 ;;; Commentary:
19
20 ;; To enable in all Dired buffers, add this to your init file:
21 ;;
22 ;; (add-hook 'dired-mode-hook 'diff-hl-dired-mode)
23
24 ;;; Code:
25
26 (require 'diff-hl)
27
28 ;;;###autoload
29 (define-minor-mode diff-hl-dired-mode
30 "Toggle VC diff highlighting on the side of a Dired window."
31 :lighter ""
32 (if diff-hl-dired-mode
33 (progn
34 (diff-hl-maybe-define-bitmaps)
35 (set (make-local-variable 'diff-hl-dired-process-buffer) nil)
36 (add-hook 'dired-after-readin-hook 'diff-hl-dired-update nil t))
37 (remove-hook 'dired-after-readin-hook 'diff-hl-dired-update t)
38 (diff-hl-dired-clear)))
39
40 (defvar diff-hl-dired-process-buffer nil)
41
42 (defun diff-hl-dired-update ()
43 "Highlight the Dired buffer."
44 (let ((backend (ignore-errors (vc-responsible-backend default-directory)))
45 (def-dir default-directory)
46 (buffer (current-buffer))
47 dirs-alist files-alist)
48 (when backend
49 (diff-hl-dired-clear)
50 (if (buffer-live-p diff-hl-dired-process-buffer)
51 (let ((proc (get-buffer-process diff-hl-dired-process-buffer)))
52 (when proc (kill-process proc)))
53 (setq diff-hl-dired-process-buffer
54 (generate-new-buffer " *diff-hl-dired* tmp status")))
55 (with-current-buffer diff-hl-dired-process-buffer
56 (setq default-directory (expand-file-name def-dir))
57 (erase-buffer)
58 (vc-call-backend
59 backend 'dir-status def-dir
60 (lambda (entries &optional more-to-come)
61 (with-current-buffer buffer
62 (dolist (entry entries)
63 (cl-destructuring-bind (file state &rest) entry
64 (let ((type (plist-get
65 '(edited change added insert removed delete
66 unregistered unknown)
67 state)))
68 (if (string-match "\\`\\([^/]+\\)/" file)
69 (let* ((dir (match-string 1 file))
70 (value (cdr (assoc dir dirs-alist))))
71 (unless (eq value type)
72 (if (null value)
73 (push (cons dir type) dirs-alist)
74 (setcdr (assoc dir dirs-alist) 'change))))
75 (push (cons file type) files-alist)))))
76 ;; Process's finished, time to use the results.
77 (unless (get-buffer-process diff-hl-dired-process-buffer)
78 (diff-hl-dired-highlight-items (append dirs-alist
79 files-alist)))))
80 )))))
81
82 (defun diff-hl-dired-highlight-items (alist)
83 "Highlight ALIST containing (FILE . TYPE) elements."
84 (dolist (pair alist)
85 (let ((file (car pair))
86 (type (cdr pair)))
87 (save-excursion
88 (goto-char (point-min))
89 (when (and type (dired-goto-file-1
90 file (expand-file-name file) nil))
91 (let* ((diff-hl-fringe-bmp-function 'diff-hl-fringe-bmp-from-type)
92 (o (diff-hl-add-highlighting type 'single)))
93 (overlay-put o 'modification-hooks '(diff-hl-overlay-modified))
94 ))))))
95
96 (defalias 'diff-hl-dired-clear 'diff-hl-remove-overlays)
97
98 (provide 'diff-hl-dired)
99
100 ;;; diff-hl-dired.el ends here