1 ;;; vlf-search.el --- Search functionality for VLF -*- lexical-binding: t -*-
3 ;; Copyright (C) 2014 Free Software Foundation, Inc.
5 ;; Keywords: large files, search
6 ;; Author: Andrey Kotlarski <m00naticus@gmail.com>
7 ;; URL: https://github.com/m00natic/vlfi
9 ;; This file is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; This file is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
25 ;; This package provides search utilities for dealing with large files
26 ;; in constant memory.
32 (defun vlf-re-search (regexp count backward batch-step)
33 "Search for REGEXP COUNT number of times forward or BACKWARD.
34 BATCH-STEP is amount of overlap between successive chunks."
36 (error "Count must be positive"))
37 (let* ((tramp-verbose (min 2 tramp-verbose))
39 (match-chunk-start vlf-start-pos)
40 (match-chunk-end vlf-end-pos)
41 (match-start-pos (+ vlf-start-pos (position-bytes (point))))
42 (match-end-pos match-start-pos)
44 (font-lock font-lock-mode)
45 (reporter (make-progress-reporter
46 (concat "Searching for " regexp "...")
48 (- vlf-file-size vlf-end-pos)
52 (vlf-with-undo-disabled
56 (while (not (zerop to-find))
57 (cond ((re-search-backward regexp nil t)
58 (setq to-find (1- to-find)
59 match-chunk-start vlf-start-pos
60 match-chunk-end vlf-end-pos
61 match-start-pos (+ vlf-start-pos
64 match-end-pos (+ vlf-start-pos
67 ((zerop vlf-start-pos)
68 (throw 'end-of-file nil))
69 (t (let ((batch-move (- vlf-start-pos
73 (if (< match-start-pos batch-move)
74 (- match-start-pos vlf-batch-size)
76 (goto-char (if (< match-start-pos
83 (progress-reporter-update
84 reporter (- vlf-file-size
86 (while (not (zerop to-find))
87 (cond ((re-search-forward regexp nil t)
88 (setq to-find (1- to-find)
89 match-chunk-start vlf-start-pos
90 match-chunk-end vlf-end-pos
91 match-start-pos (+ vlf-start-pos
94 match-end-pos (+ vlf-start-pos
97 ((= vlf-end-pos vlf-file-size)
98 (throw 'end-of-file nil))
99 (t (let ((batch-move (- vlf-end-pos batch-step)))
101 (if (< batch-move match-end-pos)
104 (goto-char (if (< vlf-start-pos match-end-pos)
105 (or (byte-to-position
110 (progress-reporter-update reporter
112 (progress-reporter-done reporter))
113 (set-buffer-modified-p nil)
114 (if font-lock (font-lock-mode 1))
116 (vlf-goto-match match-chunk-start match-chunk-end
117 match-end-pos match-start-pos
119 (vlf-goto-match match-chunk-start match-chunk-end
120 match-start-pos match-end-pos
123 (defun vlf-goto-match (match-chunk-start match-chunk-end
127 "Move to MATCH-CHUNK-START MATCH-CHUNK-END surrounding \
128 MATCH-POS-START and MATCH-POS-END.
129 According to COUNT and left TO-FIND, show if search has been
130 successful. Return nil if nothing found."
131 (if (= count to-find)
132 (progn (vlf-move-to-chunk match-chunk-start match-chunk-end)
133 (goto-char (or (byte-to-position (- match-pos-start
136 (message "Not found")
138 (let ((success (zerop to-find)))
140 (vlf-update-buffer-name)
141 (vlf-move-to-chunk match-chunk-start match-chunk-end))
142 (let* ((match-end (or (byte-to-position (- match-pos-end
145 (overlay (make-overlay (byte-to-position
149 (overlay-put overlay 'face 'match)
151 (goto-char match-end)
152 (message "Moved to the %d match which is last"
154 (unwind-protect (sit-for 3)
155 (delete-overlay overlay))
158 (defun vlf-re-search-forward (regexp count)
159 "Search forward for REGEXP prefix COUNT number of times.
160 Search is performed chunk by chunk in `vlf-batch-size' memory."
161 (interactive (if (vlf-no-modifications)
162 (list (read-regexp "Search whole file"
164 (car regexp-history)))
165 (or current-prefix-arg 1))))
166 (vlf-re-search regexp count nil (/ vlf-batch-size 8)))
168 (defun vlf-re-search-backward (regexp count)
169 "Search backward for REGEXP prefix COUNT number of times.
170 Search is performed chunk by chunk in `vlf-batch-size' memory."
171 (interactive (if (vlf-no-modifications)
172 (list (read-regexp "Search whole file backward"
174 (car regexp-history)))
175 (or current-prefix-arg 1))))
176 (vlf-re-search regexp count t (/ vlf-batch-size 8)))
178 (defun vlf-goto-line (n)
179 "Go to line N. If N is negative, count from the end of file."
180 (interactive (if (vlf-no-modifications)
181 (list (read-number "Go to line: "))))
183 (let ((tramp-verbose (min 2 tramp-verbose))
184 (start-pos vlf-start-pos)
185 (end-pos vlf-end-pos)
187 (font-lock font-lock-mode)
193 (end (min vlf-batch-size vlf-file-size))
194 (reporter (make-progress-reporter
195 (concat "Searching for line "
196 (number-to-string n) "...")
198 (inhibit-read-only t))
200 (vlf-with-undo-disabled
201 (while (and (< (- end start) n)
202 (< n (- vlf-file-size start)))
204 (insert-file-contents-literally buffer-file-name
206 (goto-char (point-min))
207 (while (re-search-forward "[\n\C-m]" nil t)
211 end (min vlf-file-size
212 (+ start vlf-batch-size)))
213 (progress-reporter-update reporter start))
214 (when (< n (- vlf-file-size end))
215 (vlf-move-to-chunk-2 start end)
216 (goto-char (point-min))
217 (setq success (vlf-re-search "[\n\C-m]" n nil 0)))))
218 (let ((start (max 0 (- vlf-file-size vlf-batch-size)))
220 (reporter (make-progress-reporter
221 (concat "Searching for line -"
222 (number-to-string n) "...")
224 (inhibit-read-only t))
226 (vlf-with-undo-disabled
227 (while (and (< (- end start) n) (< n end))
229 (insert-file-contents-literally buffer-file-name nil
231 (goto-char (point-max))
232 (while (re-search-backward "[\n\C-m]" nil t)
235 start (max 0 (- end vlf-batch-size)))
236 (progress-reporter-update reporter
237 (- vlf-file-size end)))
239 (vlf-move-to-chunk-2 start end)
240 (goto-char (point-max))
241 (setq success (vlf-re-search "[\n\C-m]" n t 0))))))
242 (if font-lock (font-lock-mode 1))
244 (vlf-with-undo-disabled
245 (vlf-move-to-chunk-2 start-pos end-pos))
247 (message "Unable to find line")))))
249 (provide 'vlf-search)
251 ;;; vlf-search.el ends here