]> code.delx.au - gnu-emacs-elpa/blob - diff-hl-dired.el
Add and use fringe faces specific to 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 ;; Copyright (C) 2012-2013 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
30 (defvar diff-hl-dired-process-buffer nil)
31
32 (defgroup diff-hl-dired nil
33 "VC diff highlighting on the side of a Dired window."
34 :group 'diff-hl)
35
36 (defface diff-hl-dired-insert
37 '((default :inherit diff-hl-insert))
38 "Face used to highlight added files.")
39
40 (defface diff-hl-dired-delete
41 '((default :inherit diff-hl-delete))
42 "Face used to highlight directories with deleted files.")
43
44 (defface diff-hl-dired-change
45 '((default :inherit diff-hl-change))
46 "Face used to highlight changed files.")
47
48 (defface diff-hl-dired-unknown
49 '((default :inherit diff-header))
50 "Face used to highlight unregistered files.")
51
52 ;;;###autoload
53 (define-minor-mode diff-hl-dired-mode
54 "Toggle VC diff highlighting on the side of a Dired window."
55 :lighter ""
56 (if diff-hl-dired-mode
57 (progn
58 (diff-hl-maybe-define-bitmaps)
59 (set (make-local-variable 'diff-hl-dired-process-buffer) nil)
60 (add-hook 'dired-after-readin-hook 'diff-hl-dired-update nil t))
61 (remove-hook 'dired-after-readin-hook 'diff-hl-dired-update t)
62 (diff-hl-dired-clear)))
63
64 (defun diff-hl-dired-update ()
65 "Highlight the Dired buffer."
66 (let ((backend (ignore-errors (vc-responsible-backend default-directory)))
67 (def-dir default-directory)
68 (buffer (current-buffer))
69 dirs-alist files-alist)
70 (when backend
71 (diff-hl-dired-clear)
72 (if (buffer-live-p diff-hl-dired-process-buffer)
73 (let ((proc (get-buffer-process diff-hl-dired-process-buffer)))
74 (when proc (kill-process proc)))
75 (setq diff-hl-dired-process-buffer
76 (generate-new-buffer " *diff-hl-dired* tmp status")))
77 (with-current-buffer diff-hl-dired-process-buffer
78 (setq default-directory (expand-file-name def-dir))
79 (erase-buffer)
80 (vc-call-backend
81 backend 'dir-status def-dir
82 (lambda (entries &optional _more-to-come)
83 (with-current-buffer buffer
84 (dolist (entry entries)
85 (cl-destructuring-bind (file state &rest) entry
86 (let ((type (plist-get
87 '(edited change added insert removed delete
88 unregistered unknown)
89 state)))
90 (if (string-match "\\`\\([^/]+\\)/" file)
91 (let* ((dir (match-string 1 file))
92 (value (cdr (assoc dir dirs-alist))))
93 (unless (eq value type)
94 (if (null value)
95 (push (cons dir type) dirs-alist)
96 (setcdr (assoc dir dirs-alist) 'change))))
97 (push (cons file type) files-alist)))))
98 ;; Process's finished, time to use the results.
99 (unless (get-buffer-process diff-hl-dired-process-buffer)
100 (diff-hl-dired-highlight-items (append dirs-alist
101 files-alist)))))
102 )))))
103
104 (defun diff-hl-dired-highlight-items (alist)
105 "Highlight ALIST containing (FILE . TYPE) elements."
106 (dolist (pair alist)
107 (let ((file (car pair))
108 (type (cdr pair)))
109 (save-excursion
110 (goto-char (point-min))
111 (when (and type (dired-goto-file-1
112 file (expand-file-name file) nil))
113 (let* ((diff-hl-fringe-bmp-function 'diff-hl-fringe-bmp-from-type)
114 (diff-hl-fringe-face-function 'diff-hl-dired-face-from-type)
115 (o (diff-hl-add-highlighting type 'single)))
116 (overlay-put o 'modification-hooks '(diff-hl-overlay-modified))
117 ))))))
118
119 (defun diff-hl-dired-face-from-type (type _pos)
120 (intern (format "diff-hl-dired-%s" type)))
121
122 (defalias 'diff-hl-dired-clear 'diff-hl-remove-overlays)
123
124 (provide 'diff-hl-dired)
125
126 ;;; diff-hl-dired.el ends here