]> code.delx.au - gnu-emacs-elpa/blob - diff-hl-dired.el
Highlight unregistered files in diff-hl-dired
[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 (define-minor-mode diff-hl-dired-mode
29 "Toggle VC diff fringe highlighting in a Dired buffer."
30 :lighter ""
31 (if diff-hl-dired-mode
32 (progn
33 (set (make-local-variable 'diff-hl-dired-process-buffer) nil)
34 (add-hook 'dired-after-readin-hook 'diff-hl-dired-update nil t))
35 (remove-hook 'dired-after-readin-hook 'diff-hl-dired-update t)
36 (diff-hl-dired-clear)))
37
38 (defvar diff-hl-dired-process-buffer nil)
39
40 (defun diff-hl-dired-update ()
41 "Highlight the Dired buffer."
42 (let ((backend (ignore-errors (vc-responsible-backend default-directory)))
43 (def-dir default-directory)
44 (buffer (current-buffer))
45 dirs-alist files-alist)
46 (when backend
47 (diff-hl-dired-clear)
48 (if (buffer-live-p diff-hl-dired-process-buffer)
49 (let ((proc (get-buffer-process diff-hl-dired-process-buffer)))
50 (when proc (kill-process proc)))
51 (setq diff-hl-dired-process-buffer
52 (generate-new-buffer " *diff-hl-dired* tmp status")))
53 (with-current-buffer diff-hl-dired-process-buffer
54 (setq default-directory (expand-file-name def-dir))
55 (erase-buffer)
56 (vc-call-backend
57 backend 'dir-status def-dir
58 (lambda (entries &optional more-to-come)
59 (with-current-buffer buffer
60 (dolist (entry entries)
61 (cl-destructuring-bind (file state &rest) entry
62 (let ((type (plist-get
63 '(edited change added insert removed delete
64 unregistered unknown)
65 state)))
66 (if (string-match "\\`\\([^/]+\\)/" file)
67 (let* ((dir (match-string 1 file))
68 (value (cdr (assoc dir dirs-alist))))
69 (unless (eq value type)
70 (if (null value)
71 (push (cons dir type) dirs-alist)
72 (setcdr (assoc dir dirs-alist) 'change))))
73 (push (cons file type) files-alist))
74 ;; Process's finished, time to use the results.
75 (unless (get-buffer-process diff-hl-dired-process-buffer)
76 (diff-hl-dired-highlight-items (append dirs-alist
77 files-alist))))))))
78 )))))
79
80 (defun diff-hl-dired-highlight-items (alist)
81 "Highlight ALIST containing (FILE . TYPE) elements."
82 (dolist (pair alist)
83 (let ((file (car pair))
84 (type (cdr pair)))
85 (save-excursion
86 (goto-char (point-min))
87 (when (and type (dired-goto-file-1
88 file (expand-file-name file) nil))
89 (forward-line 0)
90 (diff-hl-add-highlighting type 'middle))))))
91
92 (defalias 'diff-hl-dired-clear 'diff-hl-remove-overlays)
93
94 (provide 'diff-hl-dired)
95
96 ;;; diff-hl-dired.el ends here