[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master 6ab3ebe 30/42: Stop duplicating code in diff-hl-changes
From: |
Dmitry Gutov |
Subject: |
[elpa] master 6ab3ebe 30/42: Stop duplicating code in diff-hl-changes |
Date: |
Wed, 09 Sep 2015 19:03:43 +0000 |
branch: master
commit 6ab3ebe6a05795f4ae31b71dbb75dede5775ece8
Author: PythonNut <address@hidden>
Commit: PythonNut <address@hidden>
Stop duplicating code in diff-hl-changes
---
diff-hl-flydiff.el | 77 +++++++++++++++++++---------------------------------
diff-hl.el | 33 +++++++++++++---------
2 files changed, 47 insertions(+), 63 deletions(-)
diff --git a/diff-hl-flydiff.el b/diff-hl-flydiff.el
index 1c58a52..6215ab7 100644
--- a/diff-hl-flydiff.el
+++ b/diff-hl-flydiff.el
@@ -108,17 +108,16 @@ the user should be returned."
(delete-file filename)))))))
filename))
-(defun diff-hl-flydiff-buffer-with-head ()
+(defun diff-hl-flydiff-buffer-with-head (file &optional backend)
"View the differences between BUFFER and its associated file.
This requires the external program `diff' to be in your `exec-path'."
(interactive)
(vc-ensure-vc-buffer)
(with-current-buffer (get-buffer (current-buffer))
- (let* ((file buffer-file-name)
- (temporary-file-directory
- (if (file-directory-p "/dev/shm/")
- "/dev/shm/"
- temporary-file-directory))
+ (let* ((temporary-file-directory
+ (if (file-directory-p "/dev/shm/")
+ "/dev/shm/"
+ temporary-file-directory))
(rev (diff-hl-flydiff-create-revision file
(diff-hl-flydiff/working-revision file))))
(diff-no-select rev (current-buffer) "-U 0" 'noasync
@@ -132,47 +131,18 @@ This requires the external program `diff' to be in your
`exec-path'."
(not (buffer-modified-p))))
(funcall old-fun)))
-(defun diff-hl-flydiff/changes (&rest args)
- (let* ((file buffer-file-name)
- (backend (vc-backend file)))
- (when backend
- (let ((state (vc-state file backend)))
- (cond
- ((or
- (buffer-modified-p)
- (eq state 'edited)
- (and (eq state 'up-to-date)
- ;; VC state is stale in after-revert-hook.
- (or revert-buffer-in-progress-p
- ;; Diffing against an older revision.
- diff-hl-reference-revision)))
- (let (diff-auto-refine-mode res)
- (with-current-buffer (diff-hl-flydiff-buffer-with-head)
- (goto-char (point-min))
- (unless (eobp)
- (ignore-errors
- (diff-beginning-of-hunk t))
- (while (looking-at diff-hunk-header-re-unified)
- (let ((line (string-to-number (match-string 3)))
- (len (let ((m (match-string 4)))
- (if m (string-to-number m) 1)))
- (beg (point)))
- (diff-end-of-hunk)
- (let* ((inserts (diff-count-matches "^\\+" beg (point)))
- (deletes (diff-count-matches "^-" beg (point)))
- (type (cond ((zerop deletes) 'insert)
- ((zerop inserts) 'delete)
- (t 'change))))
- (when (eq type 'delete)
- (setq len 1)
- (cl-incf line))
- (push (list line len type) res))))))
- (setq diff-hl-flydiff-modified-tick (buffer-modified-tick))
- (nreverse res)))
- ((eq state 'added)
- `((1 ,(line-number-at-pos (point-max)) insert)))
- ((eq state 'removed)
- `((1 ,(line-number-at-pos (point-max)) delete))))))))
+(defun diff-hl-flydiff/modified-p (state)
+ (or
+ (buffer-modified-p)
+ (eq state 'edited)
+ (and (eq state 'up-to-date)
+ ;; VC state is stale in after-revert-hook.
+ (or revert-buffer-in-progress-p
+ ;; Diffing against an older revision.
+ diff-hl-reference-revision))))
+
+(defun diff-hl-flydiff/update-modified-tick (&rest args)
+ (setq diff-hl-flydiff-modified-tick (buffer-modified-tick)))
;;;###autoload
(define-minor-mode diff-hl-flydiff-mode
@@ -182,16 +152,25 @@ This requires the external program `diff' to be in your
`exec-path'."
(if diff-hl-flydiff-mode
(progn
(advice-add 'diff-hl-update :around #'diff-hl-flydiff/update)
- (advice-add 'diff-hl-changes :override #'diff-hl-flydiff/changes)
(advice-add 'diff-hl-overlay-modified :override #'ignore)
+ (advice-add 'diff-hl-modified-p :override
+ #'diff-hl-flydiff/modified-p)
+ (advice-add 'diff-hl-changes-buffer :override
+ #'diff-hl-flydiff-buffer-with-head)
+ (advice-add 'diff-hl-change :after
+ #'diff-hl-flydiff/update-modified-tick)
+
(setq diff-hl-flydiff-timer
(run-with-idle-timer 0.3 t #'diff-hl-update t)))
(advice-remove 'diff-hl-update #'diff-hl-flydiff/update)
- (advice-remove 'diff-hl-changes #'diff-hl-flydiff/changes)
(advice-remove 'diff-hl-overlay-modified #'ignore)
+ (advice-remove 'diff-hl-modified-p #'diff-hl-flydiff/modified-p)
+ (advice-remove 'diff-hl-changes-buffer #'diff-hl-flydiff-buffer-with-head)
+ (advice-remove 'diff-hl-change #'diff-hl-flydiff/update-modified-tick)
+
(cancel-timer diff-hl-flydiff-timer)))
(provide 'diff-hl-flydiff)
diff --git a/diff-hl.el b/diff-hl.el
index 17106c1..1677798 100644
--- a/diff-hl.el
+++ b/diff-hl.el
@@ -215,26 +215,31 @@
(vc-disable-async-diff t))
,body))
+(defun diff-hl-modified-p (state)
+ (or (eq state 'edited)
+ (and (eq state 'up-to-date)
+ ;; VC state is stale in after-revert-hook.
+ (or revert-buffer-in-progress-p
+ ;; Diffing against an older revision.
+ diff-hl-reference-revision))))
+
+(defun diff-hl-changes-buffer (file backend)
+ (let ((buf-name " *diff-hl* "))
+ (diff-hl-with-diff-switches
+ (vc-call-backend backend 'diff (list file)
+ diff-hl-reference-revision nil
+ buf-name))
+ buf-name))
+
(defun diff-hl-changes ()
(let* ((file buffer-file-name)
(backend (vc-backend file)))
(when backend
(let ((state (vc-state file backend)))
(cond
- ((or (eq state 'edited)
- (and (eq state 'up-to-date)
- ;; VC state is stale in after-revert-hook.
- (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)
- diff-hl-reference-revision nil
- buf-name))
- (with-current-buffer buf-name
+ ((diff-hl-modified-p state)
+ (let* (diff-auto-refine-mode res)
+ (with-current-buffer (diff-hl-changes-buffer file backend)
(goto-char (point-min))
(unless (eobp)
(ignore-errors
- [elpa] master c79dc5b 13/42: Allow diffing on-the-fly, (continued)
- [elpa] master c79dc5b 13/42: Allow diffing on-the-fly, Dmitry Gutov, 2015/09/09
- [elpa] master 3e64ed5 22/42: #'diff-hl-flydiff/overlay-modified → #'ignored, Dmitry Gutov, 2015/09/09
- [elpa] master e0f8fe1 23/42: Require nadvice at toplevel, Dmitry Gutov, 2015/09/09
- [elpa] master 34d2802 24/42: No need to remove diff-hl-edit from hooks, Dmitry Gutov, 2015/09/09
- [elpa] master 45ac71d 18/42: Revert "Allow diffing on-the-fly", Dmitry Gutov, 2015/09/09
- [elpa] master 66e521a 19/42: Add diff-hl-flydiff as a separate file, Dmitry Gutov, 2015/09/09
- [elpa] master 4bd5166 28/42: Update copyright year(s) for diff-hl-flydiff, Dmitry Gutov, 2015/09/09
- [elpa] master 3fa2b54 25/42: Prefer using /dev/shm for temporary files, Dmitry Gutov, 2015/09/09
- [elpa] master 3ab3aae 29/42: Fix *another* usage of "ignored", Dmitry Gutov, 2015/09/09
- [elpa] master 2992efd 31/42: Simplify diff-hl-flydiff/modified-p advice, Dmitry Gutov, 2015/09/09
- [elpa] master 6ab3ebe 30/42: Stop duplicating code in diff-hl-changes,
Dmitry Gutov <=
- [elpa] master 2209370 27/42: Ensure working revisions are never stale, Dmitry Gutov, 2015/09/09
- [elpa] master 15aa2b8 26/42: Fix usage of "ignored", Dmitry Gutov, 2015/09/09
- [elpa] master 2d40373 34/42: Update diff-hl-flydiff/vc-git-mode-line-string as per Emacs master, Dmitry Gutov, 2015/09/09
- [elpa] master a16b225 38/42: One fewer version check, Dmitry Gutov, 2015/09/09
- [elpa] master f300548 33/42: Merge pull request #50 from PythonNut/master, Dmitry Gutov, 2015/09/09
- [elpa] master 7428f4a 37/42: Reduce warnings during byte-compilation; don't error b-c in <24.4, Dmitry Gutov, 2015/09/09
- [elpa] master 418dbc7 32/42: Update docs to reflect new diff-hl-flydiff-mode, Dmitry Gutov, 2015/09/09
- [elpa] master 1558064 35/42: Reword the recent README changes, Dmitry Gutov, 2015/09/09
- [elpa] master 8d313db 36/42: Use --strip-trailing-cr, Dmitry Gutov, 2015/09/09
- [elpa] master 5068da4 39/42: Reindent, Dmitry Gutov, 2015/09/09