Skip to content

Commit

Permalink
Partial preview: Improve detection of binary files (Fix #952)
Browse files Browse the repository at this point in the history
  • Loading branch information
minad committed Feb 24, 2024
1 parent 66f7c20 commit 9d96faf
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions consult.el
Original file line number Diff line number Diff line change
Expand Up @@ -1283,29 +1283,34 @@ ORIG is the original function, HOOKS the arguments."
;; file-attributes may throw permission denied error
(attrs (ignore-errors (file-attributes name)))
(size (file-attribute-size attrs)))
(with-current-buffer (if (>= size consult-preview-partial-size)
(generate-new-buffer (format "consult-partial-preview-%s" name))
(find-file-noselect name 'nowarn))
(when (>= size consult-preview-partial-size)
(setq buffer-read-only t)
(with-silent-modifications
(insert-file-contents name nil 0 consult-preview-partial-chunk)
(goto-char (point-max))
(insert "\nFile truncated. End of partial preview.\n"))
(goto-char (point-min))
;; Auto detect major mode and hope for the best, given the file which
;; is only previewed partially.
(set-auto-mode)
(font-lock-mode 1))
(when (bound-and-true-p so-long-detected-p)
(kill-buffer)
(error "File `%s' with long lines not previewed" name))
(when (or (eq major-mode 'hexl-mode)
(and (eq major-mode 'fundamental-mode)
(save-excursion (search-forward "\0" nil 'noerror))))
(kill-buffer)
(error "Binary file `%s' not previewed" name))
(current-buffer))))
(let* ((partial (>= size consult-preview-partial-size))
(buffer (if partial
(generate-new-buffer (format "consult-partial-preview-%s" name))
(find-file-noselect name 'nowarn)))
(success nil))
(unwind-protect
(with-current-buffer buffer
(when partial
(with-silent-modifications
(setq buffer-read-only t)
(insert-file-contents name nil 0 consult-preview-partial-chunk)
(goto-char (point-max))
(insert "\nFile truncated. End of partial preview.\n")
(goto-char (point-min))))
(when (or (eq major-mode 'hexl-mode)
(and (or partial (eq major-mode 'fundamental-mode))
(save-excursion (search-forward "\0" nil 'noerror))))
(error "Binary file `%s' not previewed" (file-name-nondirectory name)))
(when partial
;; Auto detect major mode and hope for the best, given the file which
;; is only previewed partially.
(set-auto-mode)
(font-lock-mode 1))
(when (bound-and-true-p so-long-detected-p)
(error "File `%s' with long lines not previewed" (file-name-nondirectory name)))
(setq success (current-buffer)))
(unless success
(kill-buffer buffer))))))

(defun consult--find-file-temporarily (name)
"Open file NAME temporarily for preview."
Expand Down

0 comments on commit 9d96faf

Please sign in to comment.