From: Dmitry Gutov Date: Tue, 3 Sep 2013 16:14:20 +0000 (+0300) Subject: Add diff-hl-amend-mode X-Git-Url: https://code.delx.au/gnu-emacs-elpa/commitdiff_plain/20857ea5f2f8d348f93feecacaa5e0f7846d26c8 Add diff-hl-amend-mode Closes #11 --- diff --git a/diff-hl-amend.el b/diff-hl-amend.el new file mode 100644 index 000000000..f3a151b38 --- /dev/null +++ b/diff-hl-amend.el @@ -0,0 +1,63 @@ +;; Author: Dmitry Gutov +;; URL: https://github.com/dgutov/diff-hl + +;; This file is not part of GNU Emacs. + +;; This file 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. + +;; This file 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 . + +;;; 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: + +;;;###autoload +(define-minor-mode diff-hl-amend-mode + "Show changes against the second-last revision in `diff-hl-mode'. +Most useful with VCSes that support rewriting local commits, and +'amending' the most recent one in particular. +Currently only supports Git, Mercurial and Bazaar." + :lighter " Amend" + (if diff-hl-amend-mode + (progn + (if vc-mode + (diff-hl-amend-set) + (add-hook 'find-file-hook 'diff-hl-amend-setup nil t)) + (add-hook 'after-revert-hook 'diff-hl-amend-setup nil t)) + (remove-hook 'find-file-hook 'diff-hl-amend-setup 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))) diff --git a/diff-hl.el b/diff-hl.el index 2872e378c..506ae31ad 100644 --- a/diff-hl.el +++ b/diff-hl.el @@ -87,6 +87,9 @@ :group 'diff-hl :type 'boolean) +(defvar diff-hl-reference-revision nil + "Revision to diff against. nil means the most recent one.") + (defun diff-hl-define-bitmaps () (let* ((scale (if (and (boundp 'text-scale-mode-amount) (numberp text-scale-mode-amount)) @@ -141,11 +144,16 @@ ((or (eq state 'edited) (and (eq state 'up-to-date) ;; VC state is stale in after-revert-hook. - revert-buffer-in-progress-p)) + (or revert-buffer-in-progress-p + ;; Diffing against an older revision. + diff-hl-reference-revision))) (let* ((buf-name " *diff-hl* ") + diff-auto-refine-mode res) (diff-hl-with-diff-switches - (vc-call-backend backend 'diff (list file) nil nil buf-name)) + (vc-call-backend backend 'diff (list file) + diff-hl-reference-revision nil + buf-name)) (with-current-buffer buf-name (goto-char (point-min)) (unless (eobp) @@ -239,7 +247,7 @@ (vc-buffer-sync) (let* ((line (line-number-at-pos)) (buffer (current-buffer))) - (vc-diff-internal t (vc-deduce-fileset) nil nil t) + (vc-diff-internal t (vc-deduce-fileset) diff-hl-reference-revision nil t) (vc-exec-after `(if (< (line-number-at-pos (point-max)) 3) (with-current-buffer ,buffer (diff-hl-remove-overlays)) (diff-hl-diff-skip-to ,line) @@ -279,7 +287,8 @@ in the source file, or the last line of the hunk above it." (fileset (vc-deduce-fileset))) (unwind-protect (progn - (vc-diff-internal nil fileset nil nil nil diff-buffer) + (vc-diff-internal nil fileset diff-hl-reference-revision nil + nil diff-buffer) (vc-exec-after `(let (beg-line end-line) (when (eobp)