Fix code review issues: use with-current-buffer, fix documentation, improve property detection

- Use with-current-buffer instead of set-buffer in tp-forward and tp-backward
- Fix tp-search documentation to correctly describe return type as list of (START END VALUE)
- Improve property detection in tp-search to handle falsy property values (0, nil, empty string)

Co-authored-by: Kinneyzhang <38454496+Kinneyzhang@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2025-12-15 07:38:20 +00:00
parent 11e6201bc1
commit ad6ec58305

38
tp.el
View File

@ -1511,11 +1511,11 @@ or nil if not found.
Uses `tp-search-forward' internally." Uses `tp-search-forward' internally."
(let ((count (or n 1)) (let ((count (or n 1))
(result nil)) (result nil)
(when object (buf (or object (current-buffer))))
(set-buffer object)) (with-current-buffer buf
(dotimes (_ count) (dotimes (_ count)
(setq result (tp-search-forward property value))) (setq result (tp-search-forward property value))))
result)) result))
(defun tp-backward (property &optional value object n) (defun tp-backward (property &optional value object n)
@ -1530,11 +1530,11 @@ or nil if not found.
Uses `tp-search-backward' internally." Uses `tp-search-backward' internally."
(let ((count (or n 1)) (let ((count (or n 1))
(result nil)) (result nil)
(when object (buf (or object (current-buffer))))
(set-buffer object)) (with-current-buffer buf
(dotimes (_ count) (dotimes (_ count)
(setq result (tp-search-backward property value))) (setq result (tp-search-backward property value))))
result)) result))
(defun tp-forward-do (function property &optional value object n) (defun tp-forward-do (function property &optional value object n)
@ -1586,8 +1586,8 @@ This function supports two calling conventions:
2. Entire string: 2. Entire string:
(tp-search STRING PROPERTY &optional VALUE) (tp-search STRING PROPERTY &optional VALUE)
Returns a list of prop-match objects for all matching regions. Returns a list of (START END VALUE) lists for all matching regions.
Each prop-match object has beginning, end, and value information." Each element contains the start position, end position, and property value."
(cond (cond
;; Entire string form: (tp-search string property &optional value) ;; Entire string form: (tp-search string property &optional value)
((stringp start-or-string) ((stringp start-or-string)
@ -1598,8 +1598,10 @@ Each prop-match object has beginning, end, and value information."
(pos 0) (pos 0)
(len (length str))) (len (length str)))
(while (< pos len) (while (< pos len)
(let ((prop-val (get-text-property pos property str))) (let* ((props (text-properties-at pos str))
(if (and prop-val (has-prop (plist-member props property))
(prop-val (plist-get props property)))
(if (and has-prop
(or (null value) (or (null value)
(equal prop-val value))) (equal prop-val value)))
;; Find the extent of this property ;; Find the extent of this property
@ -1621,8 +1623,10 @@ Each prop-match object has beginning, end, and value information."
(if (stringp obj) (if (stringp obj)
;; String object ;; String object
(while (< pos end) (while (< pos end)
(let ((prop-val (get-text-property pos property obj))) (let* ((props (text-properties-at pos obj))
(if (and prop-val (has-prop (plist-member props property))
(prop-val (plist-get props property)))
(if (and has-prop
(or (null value) (or (null value)
(equal prop-val value))) (equal prop-val value)))
(let ((next-change (or (next-single-property-change pos property obj end) end))) (let ((next-change (or (next-single-property-change pos property obj end) end)))
@ -1632,8 +1636,10 @@ Each prop-match object has beginning, end, and value information."
;; Buffer object ;; Buffer object
(with-current-buffer obj (with-current-buffer obj
(while (< pos end) (while (< pos end)
(let ((prop-val (get-text-property pos property))) (let* ((props (text-properties-at pos))
(if (and prop-val (has-prop (plist-member props property))
(prop-val (plist-get props property)))
(if (and has-prop
(or (null value) (or (null value)
(equal prop-val value))) (equal prop-val value)))
(let ((next-change (or (next-single-property-change pos property nil end) end))) (let ((next-change (or (next-single-property-change pos property nil end) end)))