In Emacs by default there is a large file warning threshold. I would prefer to instruct Emacs never to open files of a certain type or size. These are files I would prefer always to open in an external application. I now have it set up that I can use the shortcut key C-RET to open a file in an external application. Sometimes however I accidentally press RET and Emacs prompts me as to whether I would like to open the file. My preferred response would be for Emacs to do nothing, not even to prompt me. Otherwise, I would prefer to always open the type in the default external app.
2 Answers
It sounds like you want this only for interactive use, i.e., a command.
Define your own substitute for command find-file
(or find-file-other-window
etc.).
Have you command check the file size, using functions file-attributes
and file-attribute-size
, and refuse to open the file if it is bigger than some threshold you set.
For example (untested):
(defvar my-find-file-max-size 100000
"Max size for `my-find-file'.")
(defun my-find-file (file &optional wildcards)
"`find-file`, unless FILE is > `my-find-file-max-size' bytes."
(interactive (find-file-read-args "Find file: " (confirm-nonexistent-file-or-buffer)))
(when (> (file-attribute-size (file-attributes file))) (error "File %s is too big" file))
(find-file file wildcards))
I am grateful to @Drew for also providing me with similar code for Emacs Dired. With it, clicking on a large file in Dired will result in a message about the file being too big to open.
(defun my-dired-find-file ()
"Like `dired-find-file', but raise error file is too big."
(interactive)
(let ((find-file-run-dired t)
(switch-to-buffer-preserve-window-point
(if dired-auto-revert-buffer
nil
switch-to-buffer-preserve-window-point))
(file (dired-get-file-for-visit)))
;; Check file size, and raise error if too big.
(when (> (file-attribute-size (file-attributes file))
large-file-warning-threshold)
(error "File %s is too big" file))
(find-file file)))
(define-key dired-mode-map [remap dired-find-file] 'my-dired-find-file)
The size of the file will depend here on the value of the variable large-file-warning-threshold
.