help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to reject visiting a file


From: Óscar Fuentes
Subject: Re: How to reject visiting a file
Date: Fri, 12 Dec 2014 17:35:30 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux)

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>>> (defun sm-prevent-visiting-files (filename &rest _)
>>>   (if (string-match "nasty-file-name" filename)
>>>       (error "Bad! Bad file name! Bad!")))
>>> (advice-add 'find-file-noselect :before #'sm-prevent-visiting-files)
>> That was my first idea, but it was discarded because I'm not sure about
>> its coverage.  I don't know the method a given package can use to
>> visit/load/read a file.
>
> find-file-noselect is the "single entry" used normally to visit a file
> in a normal file buffer, no matter how this is triggered (find-file,
> find-file-other-window, clinking on an error in a *compile* buffer, ...)
> so it should have excellent coverage.
>
> This advice doesn't prevent Emacs from accessing the file (e.g. you can
> still use insert-file-contents to read the file and put it in some
> buffer and you can still use M-x insert-file as well), but it would be
> *very* weird for a package to use such an approach over just calling
> find-file-noselect.
>
> I'll let you judge if it's good enough for your use case.

I wish to stop Emacs from loading pdf files unless a given feature is
present. IMO pdf-tools [1] is an usable Emacs PDF viewer but, when it is
absent, Emacs defaults to doc-view (with terrible consecuences) or to
visiting the pdf file as if it were a regular one (and you end with a
garbage-filled buffer.)

Advising find-file-noselect would prevent the later, but I was not so
sure about the former case. A quick experiment shows that it works. Now
only remains my dislike for advising as the only reason for using Eli's
suggestion.

1. https://github.com/politza/pdf-tools




reply via email to

[Prev in Thread] Current Thread [Next in Thread]