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.
30 (defun vlf-re-search (regexp count backward batch-step)
31 "Search for REGEXP COUNT number of times forward or BACKWARD.
32 BATCH-STEP is amount of overlap between successive chunks."
34 (error "Count must be positive"))
35 (let* ((case-fold-search t)
36 (match-chunk-start vlf-start-pos)
37 (match-chunk-end vlf-end-pos)
38 (match-start-pos (+ vlf-start-pos (position-bytes (point))))
39 (match-end-pos match-start-pos)
41 (reporter (make-progress-reporter
42 (concat "Searching for " regexp "...")
44 (- vlf-file-size vlf-end-pos)
47 (vlf-with-undo-disabled
51 (while (not (zerop to-find))
52 (cond ((re-search-backward regexp nil t)
53 (setq to-find (1- to-find)
54 match-chunk-start vlf-start-pos
55 match-chunk-end vlf-end-pos
56 match-start-pos (+ vlf-start-pos
59 match-end-pos (+ vlf-start-pos
62 ((zerop vlf-start-pos)
63 (throw 'end-of-file nil))
64 (t (let ((batch-move (- vlf-start-pos
68 (if (< match-start-pos batch-move)
69 (- match-start-pos vlf-batch-size)
71 (goto-char (if (< match-start-pos
78 (progress-reporter-update
79 reporter (- vlf-file-size
81 (while (not (zerop to-find))
82 (cond ((re-search-forward regexp nil t)
83 (setq to-find (1- to-find)
84 match-chunk-start vlf-start-pos
85 match-chunk-end vlf-end-pos
86 match-start-pos (+ vlf-start-pos
89 match-end-pos (+ vlf-start-pos
92 ((= vlf-end-pos vlf-file-size)
93 (throw 'end-of-file nil))
94 (t (let ((batch-move (- vlf-end-pos batch-step)))
96 (if (< batch-move match-end-pos)
99 (goto-char (if (< vlf-start-pos match-end-pos)
100 (or (byte-to-position
105 (progress-reporter-update reporter
107 (progress-reporter-done reporter))
108 (set-buffer-modified-p nil)
110 (vlf-goto-match match-chunk-start match-chunk-end
111 match-end-pos match-start-pos
113 (vlf-goto-match match-chunk-start match-chunk-end
114 match-start-pos match-end-pos
117 (defun vlf-goto-match (match-chunk-start match-chunk-end
121 "Move to MATCH-CHUNK-START MATCH-CHUNK-END surrounding \
122 MATCH-POS-START and MATCH-POS-END.
123 According to COUNT and left TO-FIND, show if search has been
124 successful. Return nil if nothing found."
125 (if (= count to-find)
126 (progn (vlf-move-to-chunk match-chunk-start match-chunk-end)
127 (goto-char (or (byte-to-position (- match-pos-start
130 (message "Not found")
132 (let ((success (zerop to-find)))
134 (vlf-update-buffer-name)
135 (vlf-move-to-chunk match-chunk-start match-chunk-end))
136 (let* ((match-end (or (byte-to-position (- match-pos-end
139 (overlay (make-overlay (byte-to-position
143 (overlay-put overlay 'face 'match)
145 (goto-char match-end)
146 (message "Moved to the %d match which is last"
148 (unwind-protect (sit-for 3)
149 (delete-overlay overlay))
152 (defun vlf-re-search-forward (regexp count)
153 "Search forward for REGEXP prefix COUNT number of times.
154 Search is performed chunk by chunk in `vlf-batch-size' memory."
155 (interactive (if (vlf-no-modifications)
156 (list (read-regexp "Search whole file"
158 (car regexp-history)))
159 (or current-prefix-arg 1))))
160 (vlf-re-search regexp count nil (/ vlf-batch-size 8)))
162 (defun vlf-re-search-backward (regexp count)
163 "Search backward for REGEXP prefix COUNT number of times.
164 Search is performed chunk by chunk in `vlf-batch-size' memory."
165 (interactive (if (vlf-no-modifications)
166 (list (read-regexp "Search whole file backward"
168 (car regexp-history)))
169 (or current-prefix-arg 1))))
170 (vlf-re-search regexp count t (/ vlf-batch-size 8)))
172 (defun vlf-goto-line (n)
173 "Go to line N. If N is negative, count from the end of file."
174 (interactive (if (vlf-no-modifications)
175 (list (read-number "Go to line: "))))
176 (let ((start-pos vlf-start-pos)
177 (end-pos vlf-end-pos)
182 (progn (vlf-beginning-of-file)
183 (goto-char (point-min))
184 (setq success (vlf-re-search "[\n\C-m]" (1- n)
187 (goto-char (point-max))
188 (setq success (vlf-re-search "[\n\C-m]" (- n) t 0)))
190 (message "Onto line %s" n)
191 (vlf-move-to-chunk start-pos end-pos)
194 (provide 'vlf-search)
196 ;;; vlf-search.el ends here