- (or is-hexl
- (while (and (< (- end start) n)
- (< n (- vlf-file-size start)))
- (erase-buffer)
- (vlf-tune-insert-file-contents-literally start end)
- (goto-char (point-min))
- (while (re-search-forward "[\n\C-m]" nil t)
- (setq n (1- n)))
- (vlf-verify-size)
- (vlf-tune-batch '(:raw))
- (setq start end
- end (min vlf-file-size
- (+ start vlf-batch-size)))
- (progress-reporter-update reporter start)))
- (when (< n (- vlf-file-size end))
- (vlf-tune-batch (if is-hexl
- '(:hexl :dehexlify :insert :encode)
- '(:insert :encode)))
- (vlf-move-to-chunk-2 start (+ start vlf-batch-size))
- (goto-char (point-min))
- (setq success (vlf-re-search "[\n\C-m]" n nil 0
- reporter time)))))
- (let ((start (max 0 (- vlf-file-size vlf-batch-size)))
- (end vlf-file-size)
- (reporter (make-progress-reporter
- (concat "Searching for line -"
- (number-to-string n) "...")
- 0 vlf-file-size))
- (inhibit-read-only t))
- (setq n (- n))
- (vlf-with-undo-disabled
- (or is-hexl
- (while (and (< (- end start) n) (< n end))
- (erase-buffer)
- (vlf-tune-insert-file-contents-literally start end)
- (goto-char (point-max))
- (while (re-search-backward "[\n\C-m]" nil t)
- (setq n (1- n)))
- (vlf-tune-batch '(:raw))
- (setq end start
- start (max 0 (- end vlf-batch-size)))
- (progress-reporter-update reporter
- (- vlf-file-size end))))
- (when (< n end)
- (vlf-tune-batch (if is-hexl
- '(:hexl :dehexlify :insert :encode)
- '(:insert :encode)))
- (vlf-move-to-chunk-2 (- end vlf-batch-size) end)
- (goto-char (point-max))
- (setq success (vlf-re-search "[\n\C-m]" n t 0
- reporter time))))))
- (if font-lock (font-lock-mode 1))
- (unless success
- (vlf-with-undo-disabled
- (vlf-move-to-chunk-2 start-pos end-pos))
- (vlf-update-buffer-name)
- (goto-char pos)
- (setq vlf-batch-size batch-size)
- (message "Unable to find line"))
- (run-hook-with-args 'vlf-after-batch-functions 'goto-line))))
+ ;; (let ((start (max 0 (- vlf-file-size vlf-batch-size))))
+ ;; (while (and (< (- end start) n) (< n end))
+ ;; (erase-buffer)
+ ;; (vlf-tune-insert-file-contents-literally start end)
+ ;; (goto-char (point-max))
+ ;; (while (re-search-backward "[\n\C-m]" nil t)
+ ;; (setq n (1- n)))
+ ;; (vlf-tune-batch '(:raw))
+ ;; (setq end start
+ ;; start (max 0 (- end vlf-batch-size)))
+ ;; (progress-reporter-update reporter
+ ;; (- vlf-file-size end))))
+ (when (< n end)
+ (vlf-tune-batch '(:insert :encode))
+ (vlf-move-to-chunk (- end vlf-batch-size) end)
+ (goto-char (point-max))
+ (setq success (vlf-re-search "[\n\C-m]" n t
+ reporter time))))))
+ (if font-lock (font-lock-mode 1))
+ (unless success
+ (vlf-with-undo-disabled
+ (vlf-move-to-chunk start-pos end-pos))
+ (goto-char pos)
+ (setq vlf-batch-size batch-size)
+ (message "Unable to find line"))
+ (run-hook-with-args 'vlf-after-batch-functions 'goto-line)))))
+
+(defun vlf-goto-line-hexl (n)
+ "Go to line N. If N is negative, count from the end of file.
+Assume `hexl-mode' is active."
+ (vlf-tune-load '(:hexl :raw))
+ (if (< n 0)
+ (let ((hidden-bytes (+ vlf-file-size (* n hexl-bits))))
+ (setq hidden-bytes (- hidden-bytes (mod hidden-bytes
+ vlf-batch-size)))
+ (vlf-move-to-batch hidden-bytes)
+ (goto-char (point-max))
+ (forward-line (+ (round (- vlf-file-size
+ (min vlf-file-size
+ (+ hidden-bytes
+ vlf-batch-size)))
+ hexl-bits)
+ n)))
+ (let ((hidden-bytes (1- (* n hexl-bits))))
+ (setq hidden-bytes (- hidden-bytes (mod hidden-bytes
+ vlf-batch-size)))
+ (vlf-move-to-batch hidden-bytes)
+ (goto-char (point-min))
+ (forward-line (- n 1 (/ hidden-bytes hexl-bits))))))
+
+(defun vlf-query-replace (regexp to-string &optional delimited backward)
+ "Query replace over whole file matching REGEXP with TO-STRING.
+Third arg DELIMITED (prefix arg if interactive), if non-nil, replace
+only matches surrounded by word boundaries. A negative prefix arg means
+replace BACKWARD."
+ (interactive (let ((common (query-replace-read-args
+ (concat "Query replace over whole file"
+ (if current-prefix-arg
+ (if (eq current-prefix-arg '-)
+ " backward"
+ " word")
+ "")
+ " regexp")
+ t)))
+ (list (nth 0 common) (nth 1 common) (nth 2 common)
+ (nth 3 common))))
+ (let ((not-automatic t))
+ (while (vlf-re-search regexp 1 backward)
+ (cond (not-automatic
+ (query-replace-regexp regexp to-string delimited
+ nil nil backward)
+ (if (eq 'automatic (lookup-key query-replace-map
+ (vector last-input-event)))
+ (setq not-automatic nil)))
+ (backward (while (re-search-backward regexp nil t)
+ (replace-match to-string)))
+ (t (while (re-search-forward regexp nil t)
+ (replace-match to-string))))
+ (if (buffer-modified-p)
+ (save-buffer)))))