]> code.delx.au - gnu-emacs-elpa/blobdiff - diff-hl.el
Introduce diff-hl-dired-mode-unless-remote
[gnu-emacs-elpa] / diff-hl.el
index 518a5d806927a1c659ee39cab8084d89f1422185..76b68157d3af88a3a3da9982d148962e99022a95 100644 (file)
@@ -1,19 +1,21 @@
 ;;; diff-hl.el --- Highlight uncommitted changes -*- lexical-binding: t -*-\r
 \r
+;; Copyright (C) 2012-2014  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.4.3\r
+;; Version:  1.6.0\r
 ;; Package-Requires: ((cl-lib "0.2"))\r
 \r
-;; This file is not part of GNU Emacs.\r
+;; This file is part of GNU Emacs.\r
 \r
-;; This file is free software: you can redistribute it and/or modify\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
-;; This file is distributed in the hope that it will be useful,\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
@@ -23,8 +25,9 @@
 \r
 ;;; Commentary:\r
 \r
-;; `diff-hl-mode' highlights uncommitted changes on the left fringe of the\r
-;; window, allows you to jump between the hunks and revert them selectively.\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
@@ -48,6 +51,7 @@
 \r
 ;;; Code:\r
 \r
+(require 'fringe)\r
 (require 'diff-mode)\r
 (require 'vc)\r
 (require 'vc-dir)\r
@@ -58,7 +62,7 @@
   (require 'face-remap))\r
 \r
 (defgroup diff-hl nil\r
-  "VC diff fringe highlighting"\r
+  "VC diff highlighting on the side of a window"\r
   :group 'vc)\r
 \r
 (defface diff-hl-insert\r
   "Face used to highlight changed lines."\r
   :group 'diff-hl)\r
 \r
+(defcustom diff-hl-command-prefix (kbd "C-x v")\r
+  "The prefix for all `diff-hl' commands."\r
+  :group 'diff-hl\r
+  :type 'string)\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
+(defcustom diff-hl-fringe-face-function 'diff-hl-fringe-face-from-type\r
+  "Function to choose the fringe face for a given change type\r
+  and position within a hunk.  Should accept two arguments."\r
+  :group 'diff-hl\r
+  :type '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
-                         (cl-plusp text-scale-mode-amount))\r
+                         (numberp text-scale-mode-amount))\r
                     (expt text-scale-mode-step text-scale-mode-amount)\r
                   1))\r
-         (h (round (* (frame-char-height) scale)))\r
+         (spacing (or (and (display-graphic-p) (default-value 'line-spacing)) 0))\r
+         (h (+ (ceiling (* (frame-char-height) scale))\r
+               (if (floatp spacing)\r
+                   (truncate (* (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
     (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
+    (define-fringe-bitmap 'diff-hl-bmp-single single h w 'top)\r
+    (define-fringe-bitmap 'diff-hl-bmp-i [3 3 0 3 3 3 3 3 3 3] nil 2 'center)\r
+    (let* ((w2 (* (/ w 2) 2))\r
+           ;; When fringes are disabled, it's easier to fix up the width,\r
+           ;; instead of doing nothing (#20).\r
+           (w2 (if (zerop w2) 2 w2))\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
+      )))\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 (cons 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 (concat "diff-hl-" (symbol-name type))))\r
-             (bmp-sym (intern (concat "diff-hl-bmp-" (symbol-name pos)))))\r
+      (let* ((face-sym (funcall diff-hl-fringe-face-function type pos))\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-face-from-type (type _pos)\r
+  (intern (format "diff-hl-%s" type)))\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
+  (cl-case type\r
+    (unknown 'question-mark)\r
+    (change 'exclamation-mark)\r
+    (ignored 'diff-hl-bmp-i)\r
+    (t (intern (format "diff-hl-bmp-%s" type)))))\r
+\r
+(defvar vc-svn-diff-switches)\r
+\r
 (defmacro diff-hl-with-diff-switches (body)\r
   `(let ((vc-git-diff-switches nil)\r
          (vc-hg-diff-switches nil)\r
          ((or (eq state 'edited)\r
               (and (eq state 'up-to-date)\r
                    ;; VC state is stale in after-revert-hook.\r
-                   revert-buffer-in-progress-p))\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) nil nil buf-name))\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
+                (ignore-errors\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
       (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
-            (forward-line len)\r
-            (setq current-line (+ line len))\r
-            (let ((h (make-overlay hunk-beg (1- (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
-              (if (= len 1)\r
-                  (overlay-put h 'before-string (diff-hl-fringe-spec type 'single))\r
-                (overlay-put h 'before-string (diff-hl-fringe-spec type 'top))\r
-                (overlay-put h 'line-prefix (diff-hl-fringe-spec type 'middle))\r
-                (overlay-put h 'after-string (diff-hl-fringe-spec type 'bottom)))\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 overlay."\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
   (vc-buffer-sync)\r
   (let* ((line (line-number-at-pos))\r
          (buffer (current-buffer)))\r
-    (vc-diff-internal t (vc-deduce-fileset) nil nil t)\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
@@ -262,7 +360,8 @@ in the source file, or the last line of the hunk above it."
         (fileset (vc-deduce-fileset)))\r
     (unwind-protect\r
         (progn\r
-          (vc-diff-internal nil fileset nil nil nil diff-buffer)\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
@@ -295,7 +394,7 @@ in the source file, or the last line of the hunk above it."
 \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)\r
+           when (overlay-get o 'diff-hl-hunk)\r
            return o))\r
 \r
 (defun diff-hl-next-hunk (&optional backward)\r
@@ -308,9 +407,7 @@ in the source file, or the last line of the hunk above it."
                                   (previous-overlay-change (point))\r
                                 (next-overlay-change (point))))\r
                    (let ((o (diff-hl-hunk-overlay-at (point))))\r
-                     (when (and o (if backward\r
-                                      (>= (overlay-end o) (point))\r
-                                    (<= (overlay-start o) (point))))\r
+                     (when (and o (= (overlay-start o) (point)))\r
                        (throw 'found (overlay-start o)))))))))\r
     (if pos\r
         (goto-char pos)\r
@@ -321,31 +418,50 @@ in the source file, or the last line of the hunk above it."
   (interactive)\r
   (diff-hl-next-hunk t))\r
 \r
+(define-prefix-command 'diff-hl-command-map)\r
+\r
+(let ((map diff-hl-command-map))\r
+  (define-key map "n" 'diff-hl-revert-hunk)\r
+  (define-key map "[" 'diff-hl-previous-hunk)\r
+  (define-key map "]" 'diff-hl-next-hunk)\r
+  map)\r
+\r
+;;;###autoload\r
 (define-minor-mode diff-hl-mode\r
-  "Toggle VC diff fringe highlighting."\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
+                        (,diff-hl-command-prefix . diff-hl-command-map))\r
   (if diff-hl-mode\r
       (progn\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
+        (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 (if vc-mode\r
+                      ;; Defer until the end of this hook, so that its\r
+                      ;; elements can modify the update behavior.\r
+                      'diff-hl-mode-on-hook\r
+                    ;; If we're only opening the file now,\r
+                    ;; `vc-find-file-hook' likely hasn't run yet, so\r
+                    ;; let's wait until the state information is\r
+                    ;; saved, in order not to fetch it twice.\r
+                    'find-file-hook)\r
+                  '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
+        ;; Magit does call `auto-revert-handler', but it usually\r
+        ;; doesn't do much, because `buffer-stale--default-function'\r
+        ;; doesn't care about changed VC state.\r
+        ;; https://github.com/magit/magit/issues/603\r
+        (add-hook 'magit-revert-buffer-hook 'diff-hl-update nil t)\r
+        (add-hook 'auto-revert-mode-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 'magit-revert-buffer-hook 'diff-hl-update t)\r
+    (remove-hook 'auto-revert-mode-hook 'diff-hl-update t)\r
     (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)\r
     (diff-hl-remove-overlays)))\r
 \r
@@ -359,8 +475,8 @@ in the source file, or the last line of the hunk above it."
                            (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
+      (scan diff-hl-command-map)\r
+      (smartrep-define-key diff-hl-mode-map diff-hl-command-prefix smart-keys))))\r
 \r
 (defun diff-hl-dir-update ()\r
   (dolist (pair (if (vc-dir-marked-files)\r