Optimize large-file detection

Two less file ops when opening new files. Particularly helpful for
remote files.
This commit is contained in:
Henrik Lissner 2020-10-18 17:12:39 -04:00
parent 6778c1239c
commit 9cdb0b85d3
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -31,30 +31,30 @@ These thresholds are in MB, and is used by `doom--optimize-for-large-files-a'.")
;; ;;
;;; File handling ;;; File handling
(defadvice! doom--optimize-for-large-files-a (orig-fn &rest args) (defadvice! doom--prepare-for-large-files-a (size _ filename &rest _)
"Set `doom-large-file-p' if the file is too large. "Sets `doom-large-file-p' if the file is considered large.
Uses `doom-large-file-size-alist' to determine when a file is too large. When Uses `doom-large-file-size-alist' to determine when a file is too large. When
`doom-large-file-p' is set, other plugins can detect this and reduce their `doom-large-file-p' is set, other plugins can detect this and reduce their
runtime costs (or disable themselves) to ensure the buffer is as fast as runtime costs (or disable themselves) to ensure the buffer is as fast as
possible." possible."
:around #'after-find-file :before #'abort-if-file-too-large
(if (setq doom-large-file-p (and (numberp size)
(and buffer-file-name (> size
(not doom-large-file-p)
(file-exists-p buffer-file-name)
(ignore-errors
(> (nth 7 (file-attributes buffer-file-name))
(* 1024 1024 (* 1024 1024
(assoc-default buffer-file-name doom-large-file-size-alist (assoc-default filename doom-large-file-size-alist
#'string-match-p)))))) #'string-match-p)))
(prog1 (apply orig-fn args) (setq doom-large-file-p size)))
(defadvice! doom--optimize-for-large-files-a (&rest _)
"Trigger `so-long-minor-mode' if the file is large."
:after #'after-find-file
(when (and doom-large-file-p buffer-file-name)
(if (memq major-mode doom-large-file-excluded-modes) (if (memq major-mode doom-large-file-excluded-modes)
(setq doom-large-file-p nil) (setq doom-large-file-p nil)
(when (fboundp 'so-long-minor-mode) ; in case the user disabled it (when (fboundp 'so-long-minor-mode) ; in case the user disabled it
(so-long-minor-mode +1)) (so-long-minor-mode +1))
(message "Large file detected! Cutting a few corners to improve performance..."))) (message "Large file detected! Cutting a few corners to improve performance..."))))
(apply orig-fn args)))
;; Resolve symlinks when opening files, so that any operations are conducted ;; Resolve symlinks when opening files, so that any operations are conducted
;; from the file's true directory (like `find-file'). ;; from the file's true directory (like `find-file').