1
0
mirror of https://github.com/m00natic/vlfi.git synced 2025-01-18 20:10:47 +00:00

Use temporary buffer for occur in case of modifications.

This commit is contained in:
Andrey Kotlarski 2013-12-03 01:50:03 +02:00
parent efae918a83
commit cb47e19128

45
vlf.el
View File

@ -101,13 +101,13 @@
(start (* (/ pos vlf-batch-size) vlf-batch-size))) (start (* (/ pos vlf-batch-size) vlf-batch-size)))
(goto-char (byte-to-position (- pos start))) (goto-char (byte-to-position (- pos start)))
(vlf-move-to-batch start))) (vlf-move-to-batch start)))
(kill-local-variable 'revert-buffer-function)
(when (or (not large-file-warning-threshold) (when (or (not large-file-warning-threshold)
(< vlf-file-size large-file-warning-threshold) (< vlf-file-size large-file-warning-threshold)
(y-or-n-p (format "Load whole file? (%s) " (y-or-n-p (format "Load whole file? (%s) "
(file-size-human-readable (file-size-human-readable
vlf-file-size)))) vlf-file-size))))
(remove-hook 'write-file-functions 'vlf-write t) (remove-hook 'write-file-functions 'vlf-write t)
(kill-local-variable 'revert-buffer-function)
(let ((pos (+ vlf-start-pos (position-bytes (point))))) (let ((pos (+ vlf-start-pos (position-bytes (point)))))
(erase-buffer) (erase-buffer)
(insert-file-contents buffer-file-name) (insert-file-contents buffer-file-name)
@ -472,7 +472,8 @@ bytes added to the end."
BATCH-STEP is amount of overlap between successive chunks." BATCH-STEP is amount of overlap between successive chunks."
(if (<= count 0) (if (<= count 0)
(error "Count must be positive")) (error "Count must be positive"))
(let* ((match-chunk-start vlf-start-pos) (let* ((case-fold-search t)
(match-chunk-start vlf-start-pos)
(match-chunk-end vlf-end-pos) (match-chunk-end vlf-end-pos)
(match-start-pos (+ vlf-start-pos (position-bytes (point)))) (match-start-pos (+ vlf-start-pos (position-bytes (point))))
(match-end-pos match-start-pos) (match-end-pos match-start-pos)
@ -707,21 +708,32 @@ Prematurely ending indexing will still show what's found so far."
(interactive (list (read-regexp "List lines matching regexp" (interactive (list (read-regexp "List lines matching regexp"
(if regexp-history (if regexp-history
(car regexp-history))))) (car regexp-history)))))
(let ((start-pos vlf-start-pos) (if (buffer-modified-p) ;use temporary buffer not to interfere with modifications
(end-pos vlf-end-pos) (let ((vlf-buffer (current-buffer))
(pos (point))) (file buffer-file-name)
(vlf-beginning-of-file) (batch-size vlf-batch-size))
(goto-char (point-min)) (with-temp-buffer
(set-buffer-modified-p nil) (setq buffer-file-name file)
(vlf-with-undo-disabled (set-buffer-modified-p nil)
(unwind-protect (vlf-build-occur regexp) (set (make-local-variable 'vlf-batch-size) batch-size)
(set-buffer-modified-p nil) (vlf-mode 1)
(vlf-move-to-chunk start-pos end-pos) (goto-char (point-min))
(goto-char pos))))) (vlf-with-undo-disabled
(vlf-build-occur regexp vlf-buffer))))
(let ((start-pos vlf-start-pos)
(end-pos vlf-end-pos)
(pos (point)))
(vlf-beginning-of-file)
(goto-char (point-min))
(vlf-with-undo-disabled
(unwind-protect (vlf-build-occur regexp (current-buffer))
(vlf-move-to-chunk start-pos end-pos)
(goto-char pos))))))
(defun vlf-build-occur (regexp) (defun vlf-build-occur (regexp vlf-buffer)
"Build occur style index for REGEXP." "Build occur style index for REGEXP."
(let ((line 1) (let ((case-fold-search t)
(line 1)
(last-match-line 0) (last-match-line 0)
(last-line-pos (point-min)) (last-line-pos (point-min))
(file buffer-file-name) (file buffer-file-name)
@ -751,7 +763,6 @@ Prematurely ending indexing will still show what's found so far."
last-line-pos (point)) last-line-pos (point))
(let* ((chunk-start vlf-start-pos) (let* ((chunk-start vlf-start-pos)
(chunk-end vlf-end-pos) (chunk-end vlf-end-pos)
(vlf-buffer (current-buffer))
(line-pos (line-beginning-position)) (line-pos (line-beginning-position))
(line-text (buffer-substring (line-text (buffer-substring
line-pos (line-end-position)))) line-pos (line-end-position))))
@ -812,7 +823,7 @@ Prematurely ending indexing will still show what's found so far."
(with-current-buffer occur-buffer (with-current-buffer occur-buffer
(goto-char (point-min)) (goto-char (point-min))
(insert (propertize (insert (propertize
(format "%d matches from %d lines for \"%s\" \ (format "%d matches in %d lines for \"%s\" \
in file: %s" total-matches line regexp file) in file: %s" total-matches line regexp file)
'face 'underline)) 'face 'underline))
(set-buffer-modified-p nil) (set-buffer-modified-p nil)