From 44cad762d23930e849a24fdf3bdb43506f1943f6 Mon Sep 17 00:00:00 2001 From: Vineet Kumar Date: Sat, 23 May 2026 21:33:18 -0400 Subject: deduplicate the hl-* functions --- gptel-tools-hl.el | 58 ++++++++++++++++++++++++++----------------------------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/gptel-tools-hl.el b/gptel-tools-hl.el index 45d44fe..fd08260 100644 --- a/gptel-tools-hl.el +++ b/gptel-tools-hl.el @@ -57,50 +57,46 @@ Precisely: https://raw.githubusercontent.com/can1357/oh-my-pi/85003ca/packages/c (defun hl-hash (line) (elt hl-bigrams (% (sxhash line) 647))) -(defun hl-file-read (file) - "Return FILE contents in hashline format." +(defun hl-file-lines (file &optional beg end) + "Return FILE contents as a list of strings. +With optional BEG and END (inclusive, 1-based), return the lines between that range." (with-temp-buffer (insert-file-contents-literally file) (let ((lines (string-lines (buffer-string)))) - (string-join - (cl-mapcar (lambda (line i) - (format "%d%2s|%s" i (hl-hash line) line)) - lines - (number-sequence 1 (length lines))) - "\n")))) + (if beg + (cl-subseq lines (1- beg) end) + lines)))) + +(defun hl-format-lines (lines &optional start include-content?) + "Format LINES in hashline format. +START is 1-based (default 1). +If INCLUDE-CONTENT is non-nil, append `|LINE' after each hash." + (string-join + (cl-mapcar (lambda (line i) + (if include-content? + (format "%d%2s|%s" i (hl-hash line) line) + (format "%d%2s" i (hl-hash line)))) + lines + (number-sequence (or start 1) (+ (or start 1) (1- (length lines))))) + "\n")) + +(defun hl-file-read (file) + "Return FILE contents in hashline format." + (hl-format-lines (hl-file-lines file) nil t)) (defun hl-file-read-range (file beg end) "Return FILE contents in hashline format from lines BEG to END." - (with-temp-buffer - (insert-file-contents-literally file) - (let ((lines (cl-subseq (string-lines (buffer-string)) - (1- beg) end))) - (string-join - (cl-mapcar (lambda (line i) - (format "%d%2s|%s" i (hl-hash line) line)) - lines - (number-sequence beg (+ beg (1- (length lines))))) - "\n")))) + (hl-format-lines (hl-file-lines file beg end) beg t)) ;; FIXME might be redundant with =hl-file-line-hash= if I'm only ;; verifying the hash of the anchors and telling LLM to re-read the ;; file when they don't match (defun hl-file-read-hashes (file) "Return FILE contents in hashline format without content." - (with-temp-buffer - (insert-file-contents-literally file) - (let ((lines (string-lines (buffer-string)))) - (string-join - (cl-mapcar (lambda (line i) - (format "%d%2s" i (hl-hash line))) - lines - (number-sequence 1 (length lines))) - "\n")))) + (hl-format-lines (hl-file-lines file))) (defun hl-file-line-hash (file n) "Return the hash of the Nth line in FILE." - (with-temp-buffer - (insert-file-contents-literally file) - (let ((lines (string-lines (buffer-string)))) - (hl-hash (elt lines (1- n)))))) + (let ((lines (hl-file-lines file))) + (hl-hash (elt lines (1- n))))) -- cgit v1.2.3