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) (* 1024 1024
(file-exists-p buffer-file-name) (assoc-default filename doom-large-file-size-alist
(ignore-errors #'string-match-p)))
(> (nth 7 (file-attributes buffer-file-name)) (setq doom-large-file-p size)))
(* 1024 1024
(assoc-default buffer-file-name doom-large-file-size-alist (defadvice! doom--optimize-for-large-files-a (&rest _)
#'string-match-p)))))) "Trigger `so-long-minor-mode' if the file is large."
(prog1 (apply orig-fn args) :after #'after-find-file
(if (memq major-mode doom-large-file-excluded-modes) (when (and doom-large-file-p buffer-file-name)
(setq doom-large-file-p nil) (if (memq major-mode doom-large-file-excluded-modes)
(when (fboundp 'so-long-minor-mode) ; in case the user disabled it (setq doom-large-file-p nil)
(so-long-minor-mode +1)) (when (fboundp 'so-long-minor-mode) ; in case the user disabled it
(message "Large file detected! Cutting a few corners to improve performance..."))) (so-long-minor-mode +1))
(apply orig-fn args))) (message "Large file detected! Cutting a few corners to improve performance..."))))
;; 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').