]> code.delx.au - gnu-emacs-elpa/blob - diff-hl-dired.el
Bump the version
[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-2015 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 dirs-alist files-alist)
86 (when backend
87 (diff-hl-dired-clear)
88 (if (buffer-live-p diff-hl-dired-process-buffer)
89 (let ((proc (get-buffer-process diff-hl-dired-process-buffer)))
90 (when proc (kill-process proc)))
91 (setq diff-hl-dired-process-buffer
92 (generate-new-buffer " *diff-hl-dired* tmp status")))
93 (with-current-buffer diff-hl-dired-process-buffer
94 (setq default-directory (expand-file-name def-dir))
95 (erase-buffer)
96 (diff-hl-dired-status-files
97 backend def-dir
98 (when diff-hl-dired-extra-indicators
99 (cl-loop for file in (directory-files def-dir)
100 unless (member file '("." ".." ".hg"))
101 collect file))
102 (lambda (entries &optional more-to-come)
103 (when (buffer-live-p buffer)
104 (with-current-buffer buffer
105 (dolist (entry entries)
106 (cl-destructuring-bind (file state &rest r) entry
107 ;; Work around http://debbugs.gnu.org/18605
108 (setq file (replace-regexp-in-string "\\` " "" file))
109 (let ((type (plist-get
110 '(edited change added insert removed delete
111 unregistered unknown ignored ignored)
112 state)))
113 (if (string-match "\\`\\([^/]+\\)/" file)
114 (let* ((dir (match-string 1 file))
115 (value (cdr (assoc dir dirs-alist))))
116 (unless (eq value type)
117 (cond
118 ((eq type 'up-to-date))
119 ((null value)
120 (push (cons dir type) dirs-alist))
121 ((not (eq type 'ignored))
122 (setcdr (assoc dir dirs-alist) 'change)))))
123 (push (cons file type) files-alist)))))
124 (unless more-to-come
125 (diff-hl-dired-highlight-items
126 (append dirs-alist files-alist))))))
127 )))))
128
129 (defun diff-hl-dired-status-files (backend dir files update-function)
130 "Using version control BACKEND, return list of (FILE STATE EXTRA) entries
131 for DIR containing FILES. Call UPDATE-FUNCTION as entries are added."
132 (if (version< "25" emacs-version)
133 (vc-call-backend backend 'dir-status-files dir files update-function)
134 (vc-call-backend backend 'dir-status-files dir files nil update-function)))
135
136 (when (version< emacs-version "24.4.51.5")
137 ;; Work around http://debbugs.gnu.org/19386
138 (defadvice vc-git-dir-status-goto-stage (around
139 diff-hl-dired-skip-up-to-date
140 (stage files update-function)
141 activate)
142 (when (eq stage 'ls-files-up-to-date)
143 (setq stage 'diff-index))
144 ad-do-it))
145
146 (defun diff-hl-dired-highlight-items (alist)
147 "Highlight ALIST containing (FILE . TYPE) elements."
148 (dolist (pair alist)
149 (let ((file (car pair))
150 (type (cdr pair)))
151 (save-excursion
152 (goto-char (point-min))
153 (when (and type (dired-goto-file-1
154 file (expand-file-name file) nil))
155 (let* ((diff-hl-fringe-bmp-function 'diff-hl-fringe-bmp-from-type)
156 (diff-hl-fringe-face-function 'diff-hl-dired-face-from-type)
157 (o (diff-hl-add-highlighting type 'single)))
158 (overlay-put o 'modification-hooks '(diff-hl-overlay-modified))
159 ))))))
160
161 (defun diff-hl-dired-face-from-type (type _pos)
162 (intern (format "diff-hl-dired-%s" type)))
163
164 (defalias 'diff-hl-dired-clear 'diff-hl-remove-overlays)
165
166 ;;;###autoload
167 (defun diff-hl-dired-mode-unless-remote ()
168 (unless (file-remote-p default-directory)
169 (diff-hl-dired-mode)))
170
171 (provide 'diff-hl-dired)
172
173 ;;; diff-hl-dired.el ends here