[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/el-search 24d0638 099/332: Rewrite the `change' and `ch
From: |
Stefan Monnier |
Subject: |
[elpa] externals/el-search 24d0638 099/332: Rewrite the `change' and `changed' patterns |
Date: |
Tue, 1 Dec 2020 15:48:20 -0500 (EST) |
branch: externals/el-search
commit 24d063888d7ce322d9dbe3f0b6febd9ed8eeccd6
Author: Michael Heerdegen <michael_heerdegen@web.de>
Commit: Michael Heerdegen <michael_heerdegen@web.de>
Rewrite the `change' and `changed' patterns
so that they don't rely on `diff-hl-mode's overlays. Instead use the
output of `diff-hl-changes' directly (and cache it).
Make both patterns accept an optional REVISION argument.
---
el-search.el | 89 ++++++++++++++++++++++++++++++++++++++++++++++--------------
1 file changed, 69 insertions(+), 20 deletions(-)
diff --git a/el-search.el b/el-search.el
index 9b2fe52..0c5543b 100644
--- a/el-search.el
+++ b/el-search.el
@@ -7,7 +7,7 @@
;; Created: 29 Jul 2015
;; Keywords: lisp
;; Compatibility: GNU Emacs 25
-;; Version: 0.2
+;; Version: 0.2.1
;; Package-Requires: ((emacs "25"))
@@ -908,25 +908,74 @@ the search pattern."
(point) ',property nil ,limit)
,limit))))))
-(el-search-defpattern change ()
- "Matches the object if it is part of a change.
-This is equivalent to (char-prop diff-hl-hunk).
-
-You need `diff-hl-mode' turned on, provided by the library
-\"diff-hl\" available in Gnu Elpa."
- (or (bound-and-true-p diff-hl-mode)
- (error "diff-hl-mode not enabled"))
- '(char-prop diff-hl-hunk))
-
-(el-search-defpattern changed ()
- "Matches the object if it contains a change.
-This is equivalent to (includes-prop diff-hl-hunk).
-
-You need `diff-hl-mode' turned on, provided by the library
-\"diff-hl\" available in Gnu Elpa."
- (or (bound-and-true-p diff-hl-mode)
- (error "diff-hl-mode not enabled"))
- '(includes-prop diff-hl-hunk))
+(defvar diff-hl-reference-revision)
+(declare-function diff-hl-changes "diff-hl")
+(defvar-local el-search--cached-changes nil)
+
+(defun el-search--changes-from-diff-hl (revision)
+ "Return a list of changed regions (as conses of positions) since REVISION.
+Use variable `el-search--cached-changes' for caching."
+ (if (and (consp el-search--cached-changes)
+ (equal (car el-search--cached-changes)
+ revision))
+ (cdr el-search--cached-changes)
+ (require 'diff-hl)
+ ;; `diff-hl-changes' returns line numbers. We must convert them into
positions.
+ (save-restriction
+ (widen)
+ (save-excursion
+ (let ((diff-hl-reference-revision revision)
+ (current-line-nbr 1) change-beg)
+ (goto-char 1)
+ (cdr (setq el-search--cached-changes
+ (cons revision
+ (delq nil (mapcar (pcase-lambda (`(,start-line
,nbr-lines ,kind))
+ (if (eq kind 'delete) nil
+ (forward-line (- start-line
current-line-nbr))
+ (setq change-beg (point))
+ (forward-line (1- nbr-lines))
+ (setq current-line-nbr (+
start-line nbr-lines -1))
+ (cons change-beg
(line-end-position))))
+ (diff-hl-changes)))))))))))
+
+(defun el-search--change-p (posn &optional revision)
+ ;; Non-nil when sexp after POSN is part of a change
+ (when (buffer-modified-p)
+ (error "Buffer is modified - please save"))
+ (save-restriction
+ (widen)
+ (let ((changes (el-search--changes-from-diff-hl revision))
+ (sexp-end (scan-sexps posn 1)))
+ (while (and changes (< (cdar changes) sexp-end))
+ (pop changes))
+ (and changes
+ (<= (caar changes) posn)))))
+
+(defun el-search--changed-p (posn &optional revision)
+ ;; Non-nil when sexp after POSN contains a change
+ (when (buffer-modified-p)
+ (error "Buffer is modified - please save"))
+ (save-restriction
+ (widen)
+ (let ((changes (el-search--changes-from-diff-hl revision)))
+ (while (and changes (<= (cdar changes) posn))
+ (pop changes))
+ (and changes
+ (< (caar changes) (scan-sexps posn 1))))))
+
+(el-search-defpattern change (&optional revision)
+ "Matches the object if its text is part of a file change.
+
+Requires library \"diff-hl\". REVISION defaults to the file's
+repository's HEAD commit."
+ `(guard (el-search--change-p (point) ,revision)))
+
+(el-search-defpattern changed (&optional revision)
+ "Matches the object if its text contains a file change.
+
+Requires library \"diff-hl\". REVISION defaults to the file's
+repository's HEAD commit."
+ `(guard (el-search--changed-p (point) ,revision)))
;;;; Highlighting
- [elpa] externals/el-search c70b003 164/332: Use stream-x.el, (continued)
- [elpa] externals/el-search c70b003 164/332: Use stream-x.el, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search a4004b3 167/332: Make `el-search-reset-search' modify its argument, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search 2871f22 178/332: Recover from search head pointing to a killed buffer, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search cf5b5a9 177/332: Display an x/y-style match count in the echo area, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search ca6b9f2 183/332: Add a PROMPT arg to `el-search--read-pattern-for-interactive', Stefan Monnier, 2020/12/01
- [elpa] externals/el-search d2b8f84 184/332: New command `el-search-count-matches', Stefan Monnier, 2020/12/01
- [elpa] externals/el-search 468745a 185/332: Show match count for `el-search-query-replace', Stefan Monnier, 2020/12/01
- [elpa] externals/el-search c80df81 186/332: Fix resuming search in a modified buffer, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search 0b93a25 188/332: * el-search/el-search.el: Minor tweak., Stefan Monnier, 2020/12/01
- [elpa] externals/el-search 145ce47 102/332: Update copyright, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search 24d0638 099/332: Rewrite the `change' and `changed' patterns,
Stefan Monnier <=
- [elpa] externals/el-search da31900 146/332: Update some copyrights, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search 94b79e5 149/332: Add new pattern type `de-morgan', Stefan Monnier, 2020/12/01
- [elpa] externals/el-search 3b73e0e 154/332: Extend heuristic matching to top-level sexps, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search 0cff5d2 153/332: Recompile search pattern when resuming searches, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search 51a539a 158/332: Implement multi-buffer query-replace, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search 56d9f20 165/332: Add some useful commands for *El Occur* buffers, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search 40217c0 174/332: Fix replacing matches with nil, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search 1bfe7c0 110/332: Simplify `el-search--transform-nontrivial-lpat', Stefan Monnier, 2020/12/01
- [elpa] externals/el-search 6ff7aa4 116/332: Work around Emacs bug#24542, Stefan Monnier, 2020/12/01
- [elpa] externals/el-search 817c902 123/332: Document change-revision transformer function; make a defcustom, Stefan Monnier, 2020/12/01