1
0
mirror of https://github.com/m00natic/vlfi.git synced 2025-01-18 12:05:31 +00:00

Fixes to hexl-mode integration.

This commit is contained in:
Andrey Kotlarski 2014-08-17 22:27:36 +03:00
parent 274c5ab903
commit 86223ed46c
4 changed files with 81 additions and 66 deletions

View File

@ -125,11 +125,17 @@ bytes added to the end."
(let* ((modified (buffer-modified-p))
(start (max 0 start))
(end (min end vlf-file-size))
(hexl (derived-mode-p 'hexl-mode))
(restore-hexl nil)
(edit-end (if modified
(+ vlf-start-pos
(length (encode-coding-region
(point-min) (point-max)
buffer-file-coding-system t)))
(progn
(when hexl
(hexl-mode-exit)
(setq restore-hexl t))
(+ vlf-start-pos
(length (encode-coding-region
(point-min) (point-max)
buffer-file-coding-system t))))
vlf-end-pos)))
(cond
((or (< edit-end start) (< end vlf-start-pos)
@ -137,13 +143,22 @@ bytes added to the end."
(when (or (not modified)
(y-or-n-p "Chunk modified, are you sure? ")) ;full chunk renewal
(set-buffer-modified-p nil)
(vlf-move-to-chunk-2 start end)))
(let ((shifts (vlf-move-to-chunk-2 start end)))
(if restore-hexl (hexl-mode))
shifts)))
((and (= start vlf-start-pos) (= end edit-end))
(or modified (vlf-move-to-chunk-2 start end)))
(unless modified
(let ((shifts (vlf-move-to-chunk-2 start end)))
(if restore-hexl (hexl-mode))
shifts)))
((or (and (<= start vlf-start-pos) (<= edit-end end))
(not modified)
(y-or-n-p "Chunk modified, are you sure? "))
(run-hooks 'vlf-before-chunk-update)
(when (and hexl (not restore-hexl))
(if (consp buffer-undo-list)
(setq buffer-undo-list nil))
(hexl-mode-exit))
(let ((shift-start 0)
(shift-end 0))
(let ((pos (+ (position-bytes (point)) vlf-start-pos))
@ -202,8 +217,11 @@ bytes added to the end."
(setq vlf-start-pos start))
(set-buffer-modified-p modified)
(set-visited-file-modtime)
(if hexl (hexl-mode))
(run-hooks 'vlf-after-chunk-update)
(cons shift-start shift-end))))))
(cons shift-start shift-end)))
(restore-hexl (hexl-mode)
nil))))
(defun vlf-move-to-chunk-2 (start end)
"Unconditionally move to chunk enclosed by START END bytes.
@ -217,11 +235,14 @@ bytes added to the end."
(let ((inhibit-read-only t)
(pos (position-bytes (point))))
(vlf-with-undo-disabled
(erase-buffer)
(setq shifts (vlf-insert-file-contents vlf-start-pos
vlf-end-pos t t)
vlf-start-pos (- vlf-start-pos (car shifts))
vlf-end-pos (+ vlf-end-pos (cdr shifts)))
(let ((hexl (derived-mode-p 'hexl-mode)))
(if hexl (hexl-mode-exit t))
(erase-buffer)
(setq shifts (vlf-insert-file-contents vlf-start-pos
vlf-end-pos t t)
vlf-start-pos (- vlf-start-pos (car shifts))
vlf-end-pos (+ vlf-end-pos (cdr shifts)))
(if hexl (hexl-mode)))
(goto-char (or (byte-to-position (+ pos (car shifts)))
(point-max)))))
(set-buffer-modified-p nil)

View File

@ -124,14 +124,19 @@ Prematurely ending indexing will still show what's found so far."
(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))
(batch-size vlf-batch-size)
(is-hexl (derived-mode-p 'hexl-mode)))
(with-temp-buffer
(setq buffer-file-name file)
(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)
(goto-char (point-min))
(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)))
@ -170,6 +175,8 @@ Prematurely ending indexing will still show what's found so far."
(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
(progn
(while (not end-of-file)

View File

@ -37,37 +37,43 @@ If changing size of chunk, shift remaining file content."
(or (verify-visited-file-modtime (current-buffer))
(y-or-n-p "File has changed since visited or saved.\
Save anyway? ")))
(widen)
(run-hook-with-args 'vlf-before-batch-functions 'write)
(if (zerop vlf-file-size) ;new file
(progn (write-region nil nil buffer-file-name vlf-start-pos t)
(setq vlf-file-size (vlf-get-file-size
buffer-file-truename)
vlf-end-pos vlf-file-size)
(vlf-update-buffer-name))
(widen)
(let* ((region-length (length (encode-coding-region
(point-min) (point-max)
buffer-file-coding-system t)))
(size-change (- vlf-end-pos vlf-start-pos
region-length)))
(if (zerop size-change)
(write-region nil nil buffer-file-name vlf-start-pos t)
(let ((tramp-verbose (if (boundp 'tramp-verbose)
(min tramp-verbose 2)))
(pos (point))
(font-lock font-lock-mode))
(font-lock-mode 0)
(if (< 0 size-change)
(vlf-file-shift-back size-change)
(vlf-file-shift-forward (- size-change)))
(if font-lock (font-lock-mode 1))
(vlf-move-to-chunk-2 vlf-start-pos
(if (< (- vlf-end-pos vlf-start-pos)
vlf-batch-size)
(+ vlf-start-pos vlf-batch-size)
vlf-end-pos))
(vlf-update-buffer-name)
(goto-char pos)))))
(let ((hexl (derived-mode-p 'hexl-mode)))
(when hexl
(if (consp buffer-undo-list)
(setq buffer-undo-list nil))
(hexl-mode-exit))
(if (zerop vlf-file-size) ;new file
(progn (write-region nil nil buffer-file-name vlf-start-pos t)
(setq vlf-file-size (vlf-get-file-size
buffer-file-truename)
vlf-end-pos vlf-file-size)
(vlf-update-buffer-name))
(let* ((region-length (length (encode-coding-region
(point-min) (point-max)
buffer-file-coding-system t)))
(size-change (- vlf-end-pos vlf-start-pos
region-length)))
(if (zerop size-change)
(write-region nil nil buffer-file-name vlf-start-pos t)
(let ((tramp-verbose (if (boundp 'tramp-verbose)
(min tramp-verbose 2)))
(pos (point))
(font-lock font-lock-mode))
(font-lock-mode 0)
(if (< 0 size-change)
(vlf-file-shift-back size-change)
(vlf-file-shift-forward (- size-change)))
(if font-lock (font-lock-mode 1))
(vlf-move-to-chunk-2 vlf-start-pos
(if (< (- vlf-end-pos vlf-start-pos)
vlf-batch-size)
(+ vlf-start-pos vlf-batch-size)
vlf-end-pos))
(vlf-update-buffer-name)
(goto-char pos)))))
(if hexl (hexl-mode)))
(run-hook-with-args 'vlf-after-batch-functions 'write))
t)

23
vlf.el
View File

@ -206,27 +206,8 @@ When prefix argument is negative
(goto-char (point-max)))
ad-do-it))
;; hexl mode integration
(defun vlf-hexl-disable (&optional operation)
"Temporarily disable `hexl-mode' for OPERATION."
(when (and (derived-mode-p 'hexl-mode)
(or (not operation)
(eq operation 'write)))
(if (consp buffer-undo-list)
(setq buffer-undo-list nil))
(hexl-mode-exit)
(set (make-local-variable 'vlf-restore-hexl-mode) operation)))
(defun vlf-hexl-enable (&optional _operation)
"Re-enable `hexl-mode' if active before _OPERATION."
(when (boundp 'vlf-restore-hexl-mode)
(hexl-mode)
(kill-local-variable 'vlf-restore-hexl-mode)))
(add-hook 'vlf-before-batch-functions 'vlf-hexl-disable)
(add-hook 'vlf-after-batch-functions 'vlf-hexl-enable)
(add-hook 'vlf-before-chunk-update 'vlf-hexl-disable)
(add-hook 'vlf-after-chunk-update 'vlf-hexl-enable)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; hexl mode integration
(eval-after-load "hexl"
'(progn