]> code.delx.au - gnu-emacs-elpa/blobdiff - packages/vlf/vlf-occur.el
* packages/vlf: Version 1.6. Automatically tune batch size to
[gnu-emacs-elpa] / packages / vlf / vlf-occur.el
index 2dac4a423b37b92cb97a4bc6bc2448a1f49299b8..a41f448933e3fb4b760ffca6462731a659803859 100644 (file)
@@ -124,7 +124,7 @@ EVENT may hold details of the invocation."
                             pos-relative)))
           (cond (current-prefix-arg
                  (setq vlf-buffer (vlf file t))
-                 (or not-hexl (hexl-mode))
+                 (or not-hexl (vlf-tune-hexlify))
                  (switch-to-buffer occur-buffer))
                 ((not (buffer-live-p vlf-buffer))
                  (unless (catch 'found
@@ -137,72 +137,109 @@ EVENT may hold details of the invocation."
                                   (setq vlf-buffer buf)
                                   (throw 'found t))))
                    (setq vlf-buffer (vlf file t))
-                   (or not-hexl (hexl-mode)))
+                   (or not-hexl (vlf-tune-hexlify)))
                  (switch-to-buffer occur-buffer)
                  (setq vlf-occur-vlf-buffer vlf-buffer)))
           (pop-to-buffer vlf-buffer)
           (vlf-move-to-chunk chunk-start chunk-end)
           (goto-char match-pos)))))
 
+(defun vlf-occur-other-buffer (regexp)
+  "Make whole file occur style index for REGEXP branching to new buffer.
+Prematurely ending indexing will still show what's found so far."
+  (let ((vlf-buffer (current-buffer))
+        (file buffer-file-name)
+        (batch-size vlf-batch-size)
+        (is-hexl (derived-mode-p 'hexl-mode))
+        (insert-bps vlf-tune-insert-bps)
+        (encode-bps vlf-tune-encode-bps)
+        (hexl-bps vlf-tune-hexl-bps)
+        (dehexlify-bps vlf-tune-dehexlify-bps))
+    (with-temp-buffer
+      (setq buffer-file-name file
+            buffer-file-truename file
+            buffer-undo-list t)
+      (set-buffer-modified-p nil)
+      (set (make-local-variable 'vlf-batch-size) batch-size)
+      (when vlf-tune-enabled
+        (setq vlf-tune-insert-bps insert-bps
+              vlf-tune-encode-bps encode-bps)
+        (if is-hexl
+            (progn (setq vlf-tune-hexl-bps hexl-bps
+                         vlf-tune-dehexlify-bps dehexlify-bps)
+                   (vlf-tune-batch '(:hexl :dehexlify :insert :encode)))
+          (vlf-tune-batch '(:insert :encode))))
+      (vlf-mode 1)
+      (if is-hexl (vlf-tune-hexlify))
+      (goto-char (point-min))
+      (vlf-with-undo-disabled
+       (vlf-build-occur regexp vlf-buffer))
+      (when vlf-tune-enabled
+        (setq insert-bps vlf-tune-insert-bps
+              encode-bps vlf-tune-encode-bps)
+        (if is-hexl
+            (setq insert-bps vlf-tune-insert-bps
+                  encode-bps vlf-tune-encode-bps))))
+    (when vlf-tune-enabled              ;merge back tune measurements
+      (setq vlf-tune-insert-bps insert-bps
+            vlf-tune-encode-bps encode-bps)
+      (if is-hexl
+          (setq vlf-tune-insert-bps insert-bps
+                vlf-tune-encode-bps encode-bps)))))
+
 (defun vlf-occur (regexp)
   "Make whole file occur style index for REGEXP.
 Prematurely ending indexing will still show what's found so far."
   (interactive (list (read-regexp "List lines matching regexp"
                                   (if regexp-history
                                       (car regexp-history)))))
-  (if (buffer-modified-p) ;use temporary buffer not to interfere with modifications
-      (let ((vlf-buffer (current-buffer))
-            (file buffer-file-name)
-            (batch-size vlf-batch-size)
-            (is-hexl (derived-mode-p 'hexl-mode)))
-        (with-temp-buffer
-          (setq buffer-file-name file
-                buffer-file-truename file
-                buffer-undo-list t)
-          (set-buffer-modified-p nil)
-          (set (make-local-variable 'vlf-batch-size) batch-size)
-          (vlf-mode 1)
-          (if is-hexl
-              (hexl-mode))
-          (run-hook-with-args 'vlf-before-batch-functions 'occur)
-          (goto-char (point-min))
-          (vlf-with-undo-disabled
-           (vlf-build-occur regexp vlf-buffer))
-          (run-hook-with-args 'vlf-after-batch-functions 'occur)))
-    (run-hook-with-args 'vlf-before-batch-functions 'occur)
+  (run-hook-with-args 'vlf-before-batch-functions 'occur)
+  (if (or (buffer-modified-p)
+          (< vlf-batch-size vlf-start-pos))
+      (vlf-occur-other-buffer regexp)
     (let ((start-pos vlf-start-pos)
           (end-pos vlf-end-pos)
-          (pos (point)))
+          (pos (point))
+          (batch-size vlf-batch-size)
+          (is-hexl (derived-mode-p 'hexl-mode)))
+      (vlf-tune-batch (if (derived-mode-p 'hexl-mode)
+                          '(:hexl :dehexlify :insert :encode)
+                        '(:insert :encode)))
       (vlf-with-undo-disabled
-       (vlf-beginning-of-file)
+       (vlf-move-to-batch 0)
        (goto-char (point-min))
        (unwind-protect (vlf-build-occur regexp (current-buffer))
          (vlf-move-to-chunk start-pos end-pos)
-         (goto-char pos))))
-    (run-hook-with-args 'vlf-after-batch-functions 'occur)))
+         (if is-hexl (vlf-tune-hexlify))
+         (goto-char pos)
+         (setq vlf-batch-size batch-size)))))
+  (run-hook-with-args 'vlf-after-batch-functions 'occur))
 
 (defun vlf-build-occur (regexp vlf-buffer)
   "Build occur style index for REGEXP over VLF-BUFFER."
-  (let ((tramp-verbose (if (boundp 'tramp-verbose)
-                           (min tramp-verbose 2)))
-        (case-fold-search t)
-        (line 1)
-        (last-match-line 0)
-        (last-line-pos (point-min))
-        (total-matches 0)
-        (match-end-pos (+ vlf-start-pos (position-bytes (point))))
-        (occur-buffer (generate-new-buffer
-                       (concat "*VLF-occur " (file-name-nondirectory
-                                              buffer-file-name)
-                               "*")))
-        (line-regexp (concat "\\(?5:[\n\C-m]\\)\\|\\(?10:"
-                             regexp "\\)"))
-        (batch-step (/ vlf-batch-size 8))
-        (is-hexl (derived-mode-p 'hexl-mode))
-        (end-of-file nil)
-        (reporter (make-progress-reporter
-                   (concat "Building index for " regexp "...")
-                   vlf-start-pos vlf-file-size)))
+  (let* ((tramp-verbose (if (boundp 'tramp-verbose)
+                            (min tramp-verbose 2)))
+         (case-fold-search t)
+         (line 1)
+         (last-match-line 0)
+         (last-line-pos (point-min))
+         (total-matches 0)
+         (match-end-pos (+ vlf-start-pos (position-bytes (point))))
+         (occur-buffer (generate-new-buffer
+                        (concat "*VLF-occur " (file-name-nondirectory
+                                               buffer-file-name)
+                                "*")))
+         (line-regexp (concat "\\(?5:[\n\C-m]\\)\\|\\(?10:"
+                              regexp "\\)"))
+         (batch-step (min 1024 (/ vlf-batch-size 8)))
+         (is-hexl (derived-mode-p 'hexl-mode))
+         (end-of-file nil)
+         (time (float-time))
+         (tune-types (if is-hexl '(:hexl :dehexlify :insert :encode)
+                       '(:insert :encode)))
+         (reporter (make-progress-reporter
+                    (concat "Building index for " regexp "...")
+                    vlf-start-pos vlf-file-size)))
     (with-current-buffer occur-buffer
       (setq buffer-undo-list t))
     (unwind-protect
@@ -255,6 +292,7 @@ Prematurely ending indexing will still show what's found so far."
                                          total-matches))))))))
               (setq end-of-file (= vlf-end-pos vlf-file-size))
               (unless end-of-file
+                (vlf-tune-batch tune-types)
                 (let ((batch-move (- vlf-end-pos batch-step)))
                   (vlf-move-to-batch (if (or is-hexl
                                              (< match-end-pos
@@ -274,7 +312,8 @@ Prematurely ending indexing will still show what's found so far."
       (set-buffer-modified-p nil)
       (if (zerop total-matches)
           (progn (kill-buffer occur-buffer)
-                 (message "No matches for \"%s\"" regexp))
+                 (message "No matches for \"%s\" (%f secs)"
+                          regexp (- (float-time) time)))
         (let ((file buffer-file-name)
               (dir default-directory))
           (with-current-buffer occur-buffer
@@ -292,10 +331,12 @@ in file: %s" total-matches line regexp file)
                   vlf-occur-regexp regexp
                   vlf-occur-hexl is-hexl
                   vlf-occur-lines line)))
-        (display-buffer occur-buffer)))))
-
+        (display-buffer occur-buffer)
+        (message "Occur finished for \"%s\" (%f secs)"
+                 regexp (- (float-time) time))))))
 
-;; save, load vlf-occur data
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;; save, load vlf-occur data
 
 (defun vlf-occur-save (file)
   "Serialize `vlf-occur' results to FILE which can later be reloaded."