]> code.delx.au - gnu-emacs-elpa/commitdiff
Add 'packages/diff-hl/' from commit '80abec80ea5c060470a34e2d2ed593f1bb9dc5eb'
authorDmitry Gutov <dgutov@yandex.ru>
Sun, 10 Nov 2013 10:01:40 +0000 (12:01 +0200)
committerDmitry Gutov <dgutov@yandex.ru>
Sun, 10 Nov 2013 10:01:40 +0000 (12:01 +0200)
git-subtree-dir: packages/diff-hl
git-subtree-mainline: 34e3d94eb31cd286c429f12c2a318f7a79dcec4b
git-subtree-split: 80abec80ea5c060470a34e2d2ed593f1bb9dc5eb

packages/diff-hl/.elpaignore [new file with mode: 0644]
packages/diff-hl/README.md [new file with mode: 0644]
packages/diff-hl/diff-hl-amend.el [new file with mode: 0644]
packages/diff-hl/diff-hl-dired.el [new file with mode: 0644]
packages/diff-hl/diff-hl-margin.el [new file with mode: 0644]
packages/diff-hl/diff-hl.el [new file with mode: 0644]
packages/diff-hl/screenshot-dired.png [new file with mode: 0644]
packages/diff-hl/screenshot-margin.png [new file with mode: 0644]
packages/diff-hl/screenshot.png [new file with mode: 0644]

diff --git a/packages/diff-hl/.elpaignore b/packages/diff-hl/.elpaignore
new file mode 100644 (file)
index 0000000..fc72b33
--- /dev/null
@@ -0,0 +1,2 @@
+README.md
+screenshot*
diff --git a/packages/diff-hl/README.md b/packages/diff-hl/README.md
new file mode 100644 (file)
index 0000000..b01e8bd
--- /dev/null
@@ -0,0 +1,83 @@
+About\r
+=====\r
+\r
+`diff-hl-mode` highlights uncommitted changes on the left side of the window,\r
+allows you to jump between and revert them selectively.\r
+\r
+For the usage instructions and the list of commands, see the Commentary section\r
+inside the file.\r
+\r
+Tested with Git, Mercurial, Bazaar and SVN. May work with other VC backends, too.\r
+\r
+The package also contains auxiliary modes:\r
+\r
+* `diff-hl-dired-mode` provides similar functionality in Dired.\r
+* `diff-hl-margin-mode` changes the highlighting function to\r
+  use the margin instead of the fringe.\r
+* `diff-hl-amend-mode` shifts the reference revision back by one.\r
+\r
+Check out the Commentary section in each respective file for the usage\r
+instructions.\r
+\r
+Screenshots\r
+=====\r
+\r
+diff-hl-mode\r
+-----\r
+Top window: a buffer in this minor mode, bottom window: the corresponding diff.\r
+\r
+![screenie](screenshot.png)\r
+\r
+diff-hl-dired-mode\r
+-----\r
+\r
+![screenie](screenshot-dired.png)\r
+\r
+diff-hl-margin-mode\r
+-----\r
+\r
+![screenie](screenshot-margin.png)\r
+\r
+Requirements\r
+=====\r
+\r
+Emacs 24+. On OS X, Emacs 24.3 or higher is recommended.\r
+\r
+Notes\r
+=====\r
+\r
+* Since it uses the corresponding VC diff command, it's only accurate when the\r
+  buffer is in saved state. Highlighting changes "on the fly" might be better,\r
+  maybe we can do something similar to `highlight-markup-buffers` with a hidden\r
+  buffer containing the unmodified copy.\r
+\r
+* We conflict with other modes when they put indicators on the fringe,\r
+  such as [Flycheck](https://github.com/flycheck/flycheck). This is\r
+  rarely a significant problem, since if you're using such a mode,\r
+  you'd usually want to fix all errors and warnings before continuing,\r
+  and then the conflicting indicators go away.\r
+\r
+* There's no fringe when Emacs is running in the console, but the navigation and\r
+  revert commands still work. Consider turning `diff-hl-margin-mode` on.\r
+\r
+* Frame-local and buffer-local values of `line-spacing` are not supported.\r
+\r
+* [git-gutter](https://github.com/syohex/emacs-git-gutter) provides interactive\r
+  commands to show/hide/toggle margin indicators for the same information, and\r
+  allows you to customize how the indicators look.\r
+\r
+Integration\r
+=====\r
+\r
+If you're using some package other than `vc` to commit changes, it might\r
+not run `vc-checkin-hook` after commits. In that case, you'll need to\r
+either add `diff-hl-update` to the hook it does run, or advise some\r
+function that's called in the buffer after its state has changed.\r
+\r
+psvn\r
+-----\r
+\r
+```lisp\r
+(defadvice svn-status-update-modeline (after svn-update-diff-hl activate)\r
+  (diff-hl-update))\r
+```\r
diff --git a/packages/diff-hl/diff-hl-amend.el b/packages/diff-hl/diff-hl-amend.el
new file mode 100644 (file)
index 0000000..defb3f4
--- /dev/null
@@ -0,0 +1,68 @@
+;; Copyright (C) 2012-2013  Free Software Foundation, Inc.
+
+;; Author:   Dmitry Gutov <dgutov@yandex.ru>
+;; URL:      https://github.com/dgutov/diff-hl
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; Toggle in the current buffer with `M-x diff-hl-amend-mode'.
+;; Toggle in all buffers with `M-x global-diff-hl-amend-mode'.
+
+;;; Code:
+
+(require 'diff-hl)
+
+;;;###autoload
+(define-minor-mode diff-hl-amend-mode
+  "Show changes against the second-last revision in `diff-hl-mode'.
+Most useful with backends that support rewriting local commits,
+and most importantly, 'amending' the most recent one.
+Currently only supports Git, Mercurial and Bazaar."
+  :lighter " Amend"
+  (if diff-hl-amend-mode
+      (progn
+        (diff-hl-amend-setup)
+        (add-hook 'after-revert-hook 'diff-hl-amend-setup nil t))
+    (remove-hook 'after-revert-hook 'diff-hl-amend-setup t)
+    (setq-local diff-hl-reference-revision nil))
+  (when diff-hl-mode
+    (diff-hl-update)))
+
+(defun diff-hl-amend-setup ()
+  (let ((backend (vc-backend buffer-file-name)))
+    (when backend
+      (setq-local diff-hl-reference-revision
+                  (cl-case backend
+                    (Git
+                     "HEAD^")
+                    (Hg
+                     "-2")
+                    (Bzr
+                     "revno:-2"))))))
+
+;;;###autoload
+(define-globalized-minor-mode global-diff-hl-amend-mode diff-hl-amend-mode
+  turn-on-diff-hl-amend-mode)
+
+(defun turn-on-diff-hl-amend-mode ()
+  "Turn on `diff-hl-amend-mode' in a buffer if appropriate."
+  (and buffer-file-name (diff-hl-amend-mode 1)))
+
+(provide 'diff-hl-amend)
+
+;;; diff-hl-amend.el ends here
diff --git a/packages/diff-hl/diff-hl-dired.el b/packages/diff-hl/diff-hl-dired.el
new file mode 100644 (file)
index 0000000..a31a7a6
--- /dev/null
@@ -0,0 +1,102 @@
+;;; diff-hl-dired.el --- Highlight changed files in Dired -*- lexical-binding: t -*-
+
+;; Copyright (C) 2012-2013  Free Software Foundation, Inc.
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; To enable in all Dired buffers, add this to your init file:
+;;
+;; (add-hook 'dired-mode-hook 'diff-hl-dired-mode)
+
+;;; Code:
+
+(require 'diff-hl)
+
+;;;###autoload
+(define-minor-mode diff-hl-dired-mode
+  "Toggle VC diff highlighting on the side of a Dired window."
+  :lighter ""
+  (if diff-hl-dired-mode
+      (progn
+        (diff-hl-maybe-define-bitmaps)
+        (set (make-local-variable 'diff-hl-dired-process-buffer) nil)
+        (add-hook 'dired-after-readin-hook 'diff-hl-dired-update nil t))
+    (remove-hook 'dired-after-readin-hook 'diff-hl-dired-update t)
+    (diff-hl-dired-clear)))
+
+(defvar diff-hl-dired-process-buffer nil)
+
+(defun diff-hl-dired-update ()
+  "Highlight the Dired buffer."
+  (let ((backend (ignore-errors (vc-responsible-backend default-directory)))
+        (def-dir default-directory)
+        (buffer (current-buffer))
+        dirs-alist files-alist)
+    (when backend
+      (diff-hl-dired-clear)
+      (if (buffer-live-p diff-hl-dired-process-buffer)
+          (let ((proc (get-buffer-process diff-hl-dired-process-buffer)))
+            (when proc (kill-process proc)))
+        (setq diff-hl-dired-process-buffer
+              (generate-new-buffer " *diff-hl-dired* tmp status")))
+      (with-current-buffer diff-hl-dired-process-buffer
+        (setq default-directory (expand-file-name def-dir))
+        (erase-buffer)
+        (vc-call-backend
+         backend 'dir-status def-dir
+         (lambda (entries &optional more-to-come)
+           (with-current-buffer buffer
+             (dolist (entry entries)
+               (cl-destructuring-bind (file state &rest) entry
+                 (let ((type (plist-get
+                              '(edited change added insert removed delete
+                                unregistered unknown)
+                              state)))
+                   (if (string-match "\\`\\([^/]+\\)/" file)
+                       (let* ((dir (match-string 1 file))
+                              (value (cdr (assoc dir dirs-alist))))
+                         (unless (eq value type)
+                           (if (null value)
+                               (push (cons dir type) dirs-alist)
+                             (setcdr (assoc dir dirs-alist) 'change))))
+                     (push (cons file type) files-alist)))))
+             ;; Process's finished, time to use the results.
+             (unless (get-buffer-process diff-hl-dired-process-buffer)
+               (diff-hl-dired-highlight-items (append dirs-alist
+                                                      files-alist)))))
+         )))))
+
+(defun diff-hl-dired-highlight-items (alist)
+  "Highlight ALIST containing (FILE . TYPE) elements."
+  (dolist (pair alist)
+    (let ((file (car pair))
+          (type (cdr pair)))
+      (save-excursion
+        (goto-char (point-min))
+        (when (and type (dired-goto-file-1
+                         file (expand-file-name file) nil))
+          (let* ((diff-hl-fringe-bmp-function 'diff-hl-fringe-bmp-from-type)
+                 (o (diff-hl-add-highlighting type 'single)))
+            (overlay-put o 'modification-hooks '(diff-hl-overlay-modified))
+            ))))))
+
+(defalias 'diff-hl-dired-clear 'diff-hl-remove-overlays)
+
+(provide 'diff-hl-dired)
+
+;;; diff-hl-dired.el ends here
diff --git a/packages/diff-hl/diff-hl-margin.el b/packages/diff-hl/diff-hl-margin.el
new file mode 100644 (file)
index 0000000..f805aab
--- /dev/null
@@ -0,0 +1,98 @@
+;;; diff-hl-margin.el --- Highlight buffer changes on margins -*- lexical-binding: t -*-
+
+;; Copyright (C) 2012-2013  Free Software Foundation, Inc.
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; This is a global mode, it modified `diff-hl-mode' to use the margin
+;; instead of the fringe. To toggle, type `M-x diff-hl-margin-mode'.
+;;
+;; Compared to the default behavior, this makes `diff-hl-mode'
+;; indicators show up even when Emacs is running in a terminal.
+;;
+;; On the flip side, the indicators look simpler, and they are
+;; incompatible with `linum-mode' or any other mode that uses the
+;; margin.
+;;
+;; You might want to enable it conditionally in your init file
+;; depending on whether Emacs is running in graphical mode:
+;;
+;; (unless (window-system) (diff-hl-margin-mode))
+
+(require 'diff-hl)
+(require 'diff-hl-dired)
+
+(defvar diff-hl-margin-old-highlight-function nil)
+
+(defgroup diff-hl-margin nil
+  "Highlight buffer changes on margin"
+  :group 'diff-hl)
+
+(defcustom diff-hl-margin-side 'left
+  "Which margin to use for indicators."
+  :type '(choice (const left)
+                 (const right))
+  :set (lambda (var value)
+         (let ((on diff-hl-margin-mode))
+           (when on (diff-hl-margin-mode -1))
+           (set-default var value)
+           (when on (diff-hl-margin-mode 1)))))
+
+;;;###autoload
+(define-minor-mode diff-hl-margin-mode
+  "Toggle displaying `diff-hl-mode' highlights on the margin."
+  :lighter "" :global t
+  (let ((width-var (intern (format "%s-margin-width" diff-hl-margin-side))))
+    (if diff-hl-margin-mode
+        (progn
+          (setq diff-hl-margin-old-highlight-function diff-hl-highlight-function
+                diff-hl-highlight-function 'diff-hl-highlight-on-margin)
+          (set-default width-var 1))
+      (setq diff-hl-highlight-function diff-hl-margin-old-highlight-function
+            diff-hl-margin-old-highlight-function nil)
+      (set-default width-var 0)))
+  (dolist (buffer (buffer-list))
+    (with-current-buffer buffer
+      (cond
+       (diff-hl-mode
+        (diff-hl-update))
+       (diff-hl-dired-mode
+        (diff-hl-dired-update)))))
+  (walk-windows (lambda (win) (set-window-buffer win (window-buffer win)))))
+
+(defvar diff-hl-margin-spec-cache
+  (loop for (type . char) in '((insert . "+") (delete . "-")
+                               (change . "|") (unknown . "?"))
+        nconc
+        (loop for side in '(left right)
+              collect
+              (cons (cons type side)
+                    (propertize
+                     " " 'display
+                     `((margin ,(intern (format "%s-margin" side)))
+                       ,(propertize char 'face
+                                    (intern (format "diff-hl-%s" type)))))))))
+
+(defun diff-hl-highlight-on-margin (ovl type _shape)
+  (let ((spec (cdr (assoc (cons type diff-hl-margin-side)
+                          diff-hl-margin-spec-cache))))
+    (overlay-put ovl 'before-string spec)))
+
+(provide 'diff-hl-margin)
+
+;;; diff-hl-margin.el ends here
diff --git a/packages/diff-hl/diff-hl.el b/packages/diff-hl/diff-hl.el
new file mode 100644 (file)
index 0000000..41df9b9
--- /dev/null
@@ -0,0 +1,480 @@
+;;; diff-hl.el --- Highlight uncommitted changes -*- lexical-binding: t -*-\r
+\r
+;; Copyright (C) 2012-2013  Free Software Foundation, Inc.\r
+\r
+;; Author:   Dmitry Gutov <dgutov@yandex.ru>\r
+;; URL:      https://github.com/dgutov/diff-hl\r
+;; Keywords: vc, diff\r
+;; Version:  1.5.1\r
+;; Package-Requires: ((cl-lib "0.2"))\r
+\r
+;; This file is part of GNU Emacs.\r
+\r
+;; GNU Emacs is free software: you can redistribute it and/or modify\r
+;; it under the terms of the GNU General Public License as published by\r
+;; the Free Software Foundation, either version 3 of the License, or\r
+;; (at your option) any later version.\r
+\r
+;; GNU Emacs is distributed in the hope that it will be useful,\r
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+;; GNU General Public License for more details.\r
+\r
+;; You should have received a copy of the GNU General Public License\r
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.\r
+\r
+;;; Commentary:\r
+\r
+;; `diff-hl-mode' highlights uncommitted changes on the left side of\r
+;; the window (using the fringe, by default), allows you to jump\r
+;; between the hunks and revert them selectively.\r
+\r
+;; Provided commands:\r
+;;\r
+;; `diff-hl-diff-goto-hunk'  C-x v =\r
+;; `diff-hl-revert-hunk'     C-x v n\r
+;; `diff-hl-previous-hunk'   C-x v [\r
+;; `diff-hl-next-hunk'       C-x v ]\r
+;;\r
+;; The mode takes advantage of `smartrep' if it is installed.\r
+\r
+;; Add either of the following to your init file.\r
+;;\r
+;; To use it in all buffers:\r
+;;\r
+;; (global-diff-hl-mode)\r
+;;\r
+;; Only in `prog-mode' buffers, with `vc-dir' integration:\r
+;;\r
+;; (add-hook 'prog-mode-hook 'turn-on-diff-hl-mode)\r
+;; (add-hook 'vc-dir-mode-hook 'turn-on-diff-hl-mode)\r
+\r
+;;; Code:\r
+\r
+(require 'diff-mode)\r
+(require 'vc)\r
+(require 'vc-dir)\r
+(eval-when-compile\r
+  (require 'cl-lib)\r
+  (require 'vc-git)\r
+  (require 'vc-hg)\r
+  (require 'face-remap))\r
+\r
+(defgroup diff-hl nil\r
+  "VC diff highlighting on the side of a window"\r
+  :group 'vc)\r
+\r
+(defface diff-hl-insert\r
+  '((default :inherit diff-added)\r
+    (((class color)) :foreground "green4"))\r
+  "Face used to highlight inserted lines."\r
+  :group 'diff-hl)\r
+\r
+(defface diff-hl-delete\r
+  '((default :inherit diff-removed)\r
+    (((class color)) :foreground "red3"))\r
+  "Face used to highlight deleted lines."\r
+  :group 'diff-hl)\r
+\r
+(defface diff-hl-change\r
+  '((default :foreground "blue3")\r
+    (((class color) (min-colors 88) (background light))\r
+     :background "#ddddff")\r
+    (((class color) (min-colors 88) (background dark))\r
+     :background "#333355"))\r
+  "Face used to highlight changed lines."\r
+  :group 'diff-hl)\r
+\r
+(defface diff-hl-unknown\r
+  '((default :inherit diff-header))\r
+  "Face used to highlight unregistered files.")\r
+\r
+(defcustom diff-hl-draw-borders t\r
+  "Non-nil to draw borders around fringe indicators."\r
+  :group 'diff-hl\r
+  :type 'boolean)\r
+\r
+(defcustom diff-hl-highlight-function 'diff-hl-highlight-on-fringe\r
+  "Function to highlight the current line. Its arguments are\r
+  overlay, change type and position within a hunk."\r
+  :group 'diff-hl\r
+  :type 'function)\r
+\r
+(defcustom diff-hl-fringe-bmp-function 'diff-hl-fringe-bmp-from-pos\r
+  "Function to choose the fringe bitmap for a given change type\r
+  and position within a hunk.  Should accept two arguments."\r
+  :group 'diff-hl\r
+  :type '(choice (const diff-hl-fringe-bmp-from-pos)\r
+                 (const diff-hl-fringe-bmp-from-type)\r
+                 function))\r
+\r
+(defvar diff-hl-reference-revision nil\r
+  "Revision to diff against.  nil means the most recent one.")\r
+\r
+(defun diff-hl-define-bitmaps ()\r
+  (let* ((scale (if (and (boundp 'text-scale-mode-amount)\r
+                         (numberp text-scale-mode-amount))\r
+                    (expt text-scale-mode-step text-scale-mode-amount)\r
+                  1))\r
+         (spacing (or (default-value 'line-spacing) 0))\r
+         (h (round (+ (* (frame-char-height) scale)\r
+                      (if (floatp spacing)\r
+                          (* (frame-char-height) spacing)\r
+                        spacing))))\r
+         (w (frame-parameter nil 'left-fringe))\r
+         (middle (make-vector h (expt 2 (1- w))))\r
+         (ones (1- (expt 2 w)))\r
+         (top (copy-sequence middle))\r
+         (bottom (copy-sequence middle))\r
+         (single (copy-sequence middle)))\r
+    (aset top 0 ones)\r
+    (aset bottom (1- h) ones)\r
+    (aset single 0 ones)\r
+    (aset single (1- h) ones)\r
+    (define-fringe-bitmap 'diff-hl-bmp-top top h w 'top)\r
+    (define-fringe-bitmap 'diff-hl-bmp-middle middle h w 'center)\r
+    (define-fringe-bitmap 'diff-hl-bmp-bottom bottom h w 'bottom)\r
+    (define-fringe-bitmap 'diff-hl-bmp-single single h w 'top)\r
+    (let* ((w2 (* (/ w 2) 2))\r
+           (delete-row (- (expt 2 (1- w2)) 2))\r
+           (middle-pos (1- (/ w2 2)))\r
+           (middle-bit (expt 2 middle-pos))\r
+           (insert-bmp (make-vector w2 (* 3 middle-bit))))\r
+      (define-fringe-bitmap 'diff-hl-bmp-delete (make-vector 2 delete-row) w2 w2)\r
+      (aset insert-bmp 0 0)\r
+      (aset insert-bmp middle-pos delete-row)\r
+      (aset insert-bmp (1+ middle-pos) delete-row)\r
+      (aset insert-bmp (1- w2) 0)\r
+      (define-fringe-bitmap 'diff-hl-bmp-insert insert-bmp w2 w2)\r
+      (define-fringe-bitmap 'diff-hl-bmp-change (make-vector\r
+                                                 w2 (* 3 middle-bit)) w2 w2))))\r
+\r
+(defun diff-hl-maybe-define-bitmaps ()\r
+  (when (window-system) ;; No fringes in the console.\r
+    (unless (fringe-bitmap-p 'diff-hl-bmp-empty)\r
+      (diff-hl-define-bitmaps)\r
+      (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center))))\r
+\r
+(defvar diff-hl-spec-cache (make-hash-table :test 'equal))\r
+\r
+(defun diff-hl-fringe-spec (type pos)\r
+  (let* ((key (list type pos diff-hl-fringe-bmp-function))\r
+         (val (gethash key diff-hl-spec-cache)))\r
+    (unless val\r
+      (let* ((face-sym (intern (format "diff-hl-%s" type)))\r
+             (bmp-sym (funcall diff-hl-fringe-bmp-function type pos)))\r
+        (setq val (propertize " " 'display `((left-fringe ,bmp-sym ,face-sym))))\r
+        (puthash key val diff-hl-spec-cache)))\r
+    val))\r
+\r
+(defun diff-hl-fringe-bmp-from-pos (type pos)\r
+  (intern (format "diff-hl-bmp-%s" pos)))\r
+\r
+(defun diff-hl-fringe-bmp-from-type (type pos)\r
+  (if (eq type 'unknown)\r
+      'question-mark\r
+    (intern (format "diff-hl-bmp-%s" type))))\r
+\r
+(defmacro diff-hl-with-diff-switches (body)\r
+  `(let ((vc-git-diff-switches nil)\r
+         (vc-hg-diff-switches nil)\r
+         (vc-svn-diff-switches nil)\r
+         (vc-diff-switches '("-U0"))\r
+         (vc-disable-async-diff t))\r
+     ,body))\r
+\r
+(defun diff-hl-changes ()\r
+  (let* ((file buffer-file-name)\r
+         (backend (vc-backend file)))\r
+    (when backend\r
+      (let ((state (vc-state file backend)))\r
+        (cond\r
+         ((or (eq state 'edited)\r
+              (and (eq state 'up-to-date)\r
+                   ;; VC state is stale in after-revert-hook.\r
+                   (or revert-buffer-in-progress-p\r
+                       ;; Diffing against an older revision.\r
+                       diff-hl-reference-revision)))\r
+          (let* ((buf-name " *diff-hl* ")\r
+                 diff-auto-refine-mode\r
+                 res)\r
+            (diff-hl-with-diff-switches\r
+             (vc-call-backend backend 'diff (list file)\r
+                              diff-hl-reference-revision nil\r
+                              buf-name))\r
+            (with-current-buffer buf-name\r
+              (goto-char (point-min))\r
+              (unless (eobp)\r
+                (diff-beginning-of-hunk t)\r
+                (while (looking-at diff-hunk-header-re-unified)\r
+                  (let ((line (string-to-number (match-string 3)))\r
+                        (len (let ((m (match-string 4)))\r
+                               (if m (string-to-number m) 1)))\r
+                        (beg (point)))\r
+                    (diff-end-of-hunk)\r
+                    (let* ((inserts (diff-count-matches "^\\+" beg (point)))\r
+                           (deletes (diff-count-matches "^-" beg (point)))\r
+                           (type (cond ((zerop deletes) 'insert)\r
+                                       ((zerop inserts) 'delete)\r
+                                       (t 'change))))\r
+                      (when (eq type 'delete)\r
+                        (setq len 1)\r
+                        (cl-incf line))\r
+                      (push (list line len type) res))))))\r
+            (nreverse res)))\r
+         ((eq state 'added)\r
+          `((1 ,(line-number-at-pos (point-max)) insert)))\r
+         ((eq state 'removed)\r
+          `((1 ,(line-number-at-pos (point-max)) delete))))))))\r
+\r
+(defun diff-hl-update ()\r
+  (let ((changes (diff-hl-changes))\r
+        (current-line 1))\r
+    (diff-hl-remove-overlays)\r
+    (save-excursion\r
+      (goto-char (point-min))\r
+      (dolist (c changes)\r
+        (cl-destructuring-bind (line len type) c\r
+          (forward-line (- line current-line))\r
+          (setq current-line line)\r
+          (let ((hunk-beg (point)))\r
+            (while (cl-plusp len)\r
+              (diff-hl-add-highlighting\r
+               type\r
+               (cond\r
+                ((not diff-hl-draw-borders) 'empty)\r
+                ((and (= len 1) (= line current-line)) 'single)\r
+                ((= len 1) 'bottom)\r
+                ((= line current-line) 'top)\r
+                (t 'middle)))\r
+              (forward-line 1)\r
+              (cl-incf current-line)\r
+              (cl-decf len))\r
+            (let ((h (make-overlay hunk-beg (point)))\r
+                  (hook '(diff-hl-overlay-modified)))\r
+              (overlay-put h 'diff-hl t)\r
+              (overlay-put h 'diff-hl-hunk t)\r
+              (overlay-put h 'modification-hooks hook)\r
+              (overlay-put h 'insert-in-front-hooks hook)\r
+              (overlay-put h 'insert-behind-hooks hook))))))))\r
+\r
+(defun diff-hl-add-highlighting (type shape)\r
+  (let ((o (make-overlay (point) (point))))\r
+    (overlay-put o 'diff-hl t)\r
+    (funcall diff-hl-highlight-function o type shape)\r
+    o))\r
+\r
+(defun diff-hl-highlight-on-fringe (ovl type shape)\r
+  (overlay-put ovl 'before-string (diff-hl-fringe-spec type shape)))\r
+\r
+(defun diff-hl-remove-overlays ()\r
+  (dolist (o (overlays-in (point-min) (point-max)))\r
+    (when (overlay-get o 'diff-hl) (delete-overlay o))))\r
+\r
+(defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)\r
+  "Delete the hunk overlay and all our line overlays inside it."\r
+  (unless after-p\r
+    (when (overlay-buffer ov)\r
+      (save-restriction\r
+        (narrow-to-region (overlay-start ov) (overlay-end ov))\r
+        (diff-hl-remove-overlays))\r
+      (delete-overlay ov))))\r
+\r
+(defvar diff-hl-timer nil)\r
+\r
+(defun diff-hl-edit (_beg _end _len)\r
+  "DTRT when we've `undo'-ne the buffer into unmodified state."\r
+  (when undo-in-progress\r
+    (when diff-hl-timer\r
+      (cancel-timer diff-hl-timer))\r
+    (setq diff-hl-timer\r
+          (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))\r
+\r
+(defun diff-hl-after-undo (buffer)\r
+  (with-current-buffer buffer\r
+    (unless (buffer-modified-p)\r
+      (diff-hl-update))))\r
+\r
+(defun diff-hl-diff-goto-hunk ()\r
+  "Run VC diff command and go to the line corresponding to the current."\r
+  (interactive)\r
+  (vc-buffer-sync)\r
+  (let* ((line (line-number-at-pos))\r
+         (buffer (current-buffer)))\r
+    (vc-diff-internal t (vc-deduce-fileset) diff-hl-reference-revision nil t)\r
+    (vc-exec-after `(if (< (line-number-at-pos (point-max)) 3)\r
+                        (with-current-buffer ,buffer (diff-hl-remove-overlays))\r
+                      (diff-hl-diff-skip-to ,line)\r
+                      (setq vc-sentinel-movepoint (point))))))\r
+\r
+(defun diff-hl-diff-skip-to (line)\r
+  "In `diff-mode', skip to the hunk and line corresponding to LINE\r
+in the source file, or the last line of the hunk above it."\r
+  (diff-hunk-next)\r
+  (let (found)\r
+    (while (and (looking-at diff-hunk-header-re-unified) (not found))\r
+      (let ((hunk-line (string-to-number (match-string 3)))\r
+            (len (let ((m (match-string 4)))\r
+                   (if m (string-to-number m) 1))))\r
+        (if (> line (+ hunk-line len))\r
+            (diff-hunk-next)\r
+          (setq found t)\r
+          (if (< line hunk-line)\r
+              ;; Retreat to the previous hunk.\r
+              (forward-line -1)\r
+            (let ((to-go (1+ (- line hunk-line))))\r
+              (while (cl-plusp to-go)\r
+                (forward-line 1)\r
+                (unless (looking-at "^-")\r
+                  (cl-decf to-go))))))))))\r
+\r
+(defun diff-hl-revert-hunk ()\r
+  "Revert the diff hunk with changes at or above the point."\r
+  (interactive)\r
+  (vc-buffer-sync)\r
+  (let ((diff-buffer (generate-new-buffer-name "*diff-hl*"))\r
+        (buffer (current-buffer))\r
+        (line (save-excursion\r
+                (unless (diff-hl-hunk-overlay-at (point))\r
+                  (diff-hl-previous-hunk))\r
+                (line-number-at-pos)))\r
+        (fileset (vc-deduce-fileset)))\r
+    (unwind-protect\r
+        (progn\r
+          (vc-diff-internal nil fileset diff-hl-reference-revision nil\r
+                            nil diff-buffer)\r
+          (vc-exec-after\r
+           `(let (beg-line end-line)\r
+              (when (eobp)\r
+                (with-current-buffer ,buffer (diff-hl-remove-overlays))\r
+                (error "Buffer is up-to-date"))\r
+              (diff-hl-diff-skip-to ,line)\r
+              (save-excursion\r
+                (while (looking-at "[-+]") (forward-line 1))\r
+                (setq end-line (line-number-at-pos (point)))\r
+                (unless (eobp) (diff-split-hunk)))\r
+              (unless (looking-at "[-+]") (forward-line -1))\r
+              (while (looking-at "[-+]") (forward-line -1))\r
+              (setq beg-line (line-number-at-pos (point)))\r
+              (unless (looking-at "@")\r
+                (forward-line 1)\r
+                (diff-split-hunk))\r
+              (let ((wbh (window-body-height)))\r
+                (if (>= wbh (- end-line beg-line))\r
+                    (recenter (/ (+ wbh (- beg-line end-line) 2) 2))\r
+                  (recenter 1)))\r
+              (unless (yes-or-no-p (format "Revert current hunk in %s?"\r
+                                           ,(cl-caadr fileset)))\r
+                (error "Revert canceled"))\r
+              (let ((diff-advance-after-apply-hunk nil))\r
+                (diff-apply-hunk t))\r
+              (with-current-buffer ,buffer\r
+                (save-buffer))\r
+              (message "Hunk reverted"))))\r
+      (quit-windows-on diff-buffer))))\r
+\r
+(defun diff-hl-hunk-overlay-at (pos)\r
+  (cl-loop for o in (overlays-in pos (1+ pos))\r
+           when (overlay-get o 'diff-hl-hunk)\r
+           return o))\r
+\r
+(defun diff-hl-next-hunk (&optional backward)\r
+  "Go to the beginning of the next hunk in the current buffer."\r
+  (interactive)\r
+  (let ((pos (save-excursion\r
+               (catch 'found\r
+                 (while (not (if backward (bobp) (eobp)))\r
+                   (goto-char (if backward\r
+                                  (previous-overlay-change (point))\r
+                                (next-overlay-change (point))))\r
+                   (let ((o (diff-hl-hunk-overlay-at (point))))\r
+                     (when (and o (= (overlay-start o) (point)))\r
+                       (throw 'found (overlay-start o)))))))))\r
+    (if pos\r
+        (goto-char pos)\r
+      (error "No further hunks found"))))\r
+\r
+(defun diff-hl-previous-hunk ()\r
+  "Go to the beginning of the previous hunk in the current buffer."\r
+  (interactive)\r
+  (diff-hl-next-hunk t))\r
+\r
+;;;###autoload\r
+(define-minor-mode diff-hl-mode\r
+  "Toggle VC diff highlighting."\r
+  :lighter "" :keymap `(([remap vc-diff] . diff-hl-diff-goto-hunk)\r
+                        (,(kbd "C-x v n") . diff-hl-revert-hunk)\r
+                        (,(kbd "C-x v [") . diff-hl-previous-hunk)\r
+                        (,(kbd "C-x v ]") . diff-hl-next-hunk))\r
+  (if diff-hl-mode\r
+      (progn\r
+        (diff-hl-maybe-define-bitmaps)\r
+        (add-hook 'after-save-hook 'diff-hl-update nil t)\r
+        (add-hook 'after-change-functions 'diff-hl-edit nil t)\r
+        (if vc-mode\r
+            (diff-hl-update)\r
+          (add-hook 'find-file-hook 'diff-hl-update t t))\r
+        (add-hook 'vc-checkin-hook 'diff-hl-update nil t)\r
+        (add-hook 'after-revert-hook 'diff-hl-update nil t)\r
+        (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))\r
+    (remove-hook 'after-save-hook 'diff-hl-update t)\r
+    (remove-hook 'after-change-functions 'diff-hl-edit t)\r
+    (remove-hook 'find-file-hook 'diff-hl-update t)\r
+    (remove-hook 'vc-checkin-hook 'diff-hl-update t)\r
+    (remove-hook 'after-revert-hook 'diff-hl-update t)\r
+    (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)\r
+    (diff-hl-remove-overlays)))\r
+\r
+(when (require 'smartrep nil t)\r
+  (let (smart-keys)\r
+    (cl-labels ((scan (map)\r
+                      (map-keymap\r
+                       (lambda (event binding)\r
+                         (if (consp binding)\r
+                             (scan binding)\r
+                           (when (characterp event)\r
+                             (push (cons (string event) binding) smart-keys))))\r
+                       map)))\r
+      (scan diff-hl-mode-map)\r
+      (smartrep-define-key diff-hl-mode-map "C-x v" smart-keys))))\r
+\r
+(defun diff-hl-dir-update ()\r
+  (dolist (pair (if (vc-dir-marked-files)\r
+                    (vc-dir-marked-only-files-and-states)\r
+                  (vc-dir-child-files-and-states)))\r
+    (when (eq 'up-to-date (cdr pair))\r
+      (let ((buffer (find-buffer-visiting (car pair))))\r
+        (when buffer\r
+          (with-current-buffer buffer\r
+            (diff-hl-remove-overlays)))))))\r
+\r
+(define-minor-mode diff-hl-dir-mode\r
+  "Toggle `diff-hl-mode' integration in a `vc-dir-mode' buffer."\r
+  :lighter ""\r
+  (if diff-hl-dir-mode\r
+      (add-hook 'vc-checkin-hook 'diff-hl-dir-update t t)\r
+    (remove-hook 'vc-checkin-hook 'diff-hl-dir-update t)))\r
+\r
+;;;###autoload\r
+(defun turn-on-diff-hl-mode ()\r
+  "Turn on `diff-hl-mode' or `diff-hl-dir-mode' in a buffer if appropriate."\r
+  (cond\r
+   (buffer-file-name\r
+    (diff-hl-mode 1))\r
+   ((eq major-mode 'vc-dir-mode)\r
+    (diff-hl-dir-mode 1))))\r
+\r
+;;;###autoload\r
+(define-globalized-minor-mode global-diff-hl-mode diff-hl-mode\r
+  turn-on-diff-hl-mode :after-hook (diff-hl-global-mode-change))\r
+\r
+(defun diff-hl-global-mode-change ()\r
+  (unless global-diff-hl-mode\r
+    (dolist (buf (buffer-list))\r
+      (with-current-buffer buf\r
+        (when diff-hl-dir-mode\r
+          (diff-hl-dir-mode -1))))))\r
+\r
+(provide 'diff-hl)\r
+\r
+;;; diff-hl.el ends here\r
diff --git a/packages/diff-hl/screenshot-dired.png b/packages/diff-hl/screenshot-dired.png
new file mode 100644 (file)
index 0000000..97df41b
Binary files /dev/null and b/packages/diff-hl/screenshot-dired.png differ
diff --git a/packages/diff-hl/screenshot-margin.png b/packages/diff-hl/screenshot-margin.png
new file mode 100644 (file)
index 0000000..6698a29
Binary files /dev/null and b/packages/diff-hl/screenshot-margin.png differ
diff --git a/packages/diff-hl/screenshot.png b/packages/diff-hl/screenshot.png
new file mode 100644 (file)
index 0000000..5f99db7
Binary files /dev/null and b/packages/diff-hl/screenshot.png differ