]> code.delx.au - gnu-emacs-elpa/blob - diff-hl-dired.el
Initial implementation of diff-hl-dired-mode
[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 ;; FIXME: Raises "Stack overflow in equal" error in Emacs 24.3.50.2
21 ;; (at least) when changing directory.
22 ;;
23 ;; To enable in all Dired buffers, add this to your init file:
24 ;;
25 ;; (add-hook 'dired-mode-hook 'diff-hl-dired-mode)
26
27 ;;; Code:
28
29 (require 'diff-hl)
30
31 (define-minor-mode diff-hl-dired-mode
32 "Toggle VC diff fringe highlighting in a Dired buffer."
33 :lighter ""
34 (if diff-hl-dired-mode
35 (progn
36 (add-hook 'dired-after-readin-hook 'diff-hl-dired-update nil t)
37 (diff-hl-dired-update))
38 (remove-hook 'dired-after-readin-hook 'diff-hl-dired-update t)
39 (diff-hl-dired-clear)))
40
41 (defvar diff-hl-dired-process-buffer nil)
42
43 (defun diff-hl-dired-update ()
44 (let ((backend (cl-loop for file in (diff-hl-dired-files)
45 for backend = (vc-backend file)
46 thereis backend))
47 (def-dir default-directory)
48 (buffer (current-buffer)))
49 (diff-hl-dired-clear)
50 (unless (buffer-live-p diff-hl-dired-process-buffer)
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 state)))
65 (save-excursion
66 (goto-char (point-min))
67 (when (and type (dired-goto-file-1 file
68 (expand-file-name file)
69 (point-max)))
70 (diff-hl-add-highlighting type 'middle))))))))))))
71
72 (defalias 'diff-hl-dired-clear 'diff-hl-remove-overlays)
73
74 (defun diff-hl-dired-files ()
75 (cl-loop for file in (directory-files default-directory)
76 when (and (file-exists-p file)
77 (not (file-directory-p file)))
78 collect (expand-file-name file)))
79
80 (provide 'diff-hl-dired)