unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How to reject visiting a file
@ 2014-12-12  1:46 Óscar Fuentes
  2014-12-12  8:01 ` Eli Zaretskii
  0 siblings, 1 reply; 7+ messages in thread
From: Óscar Fuentes @ 2014-12-12  1:46 UTC (permalink / raw)
  To: help-gnu-emacs

Is there a way to error-out when Emacs tries to visit certain file
before the file is loaded? I'll like to use a hook that receives the
file name and be able to throw an error from there that aborts the
operation.




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to reject visiting a file
  2014-12-12  1:46 How to reject visiting a file Óscar Fuentes
@ 2014-12-12  8:01 ` Eli Zaretskii
  2014-12-12 15:19   ` Óscar Fuentes
  0 siblings, 1 reply; 7+ messages in thread
From: Eli Zaretskii @ 2014-12-12  8:01 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Óscar Fuentes <ofv@wanadoo.es>
> Date: Fri, 12 Dec 2014 02:46:23 +0100
> 
> Is there a way to error-out when Emacs tries to visit certain file
> before the file is loaded? I'll like to use a hook that receives the
> file name and be able to throw an error from there that aborts the
> operation.

You could define a file-handler for these files.




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to reject visiting a file
  2014-12-12  8:01 ` Eli Zaretskii
@ 2014-12-12 15:19   ` Óscar Fuentes
  2014-12-12 15:31     ` Stefan Monnier
  0 siblings, 1 reply; 7+ messages in thread
From: Óscar Fuentes @ 2014-12-12 15:19 UTC (permalink / raw)
  To: help-gnu-emacs

Eli Zaretskii <eliz@gnu.org> writes:

>> Is there a way to error-out when Emacs tries to visit certain file
>> before the file is loaded? I'll like to use a hook that receives the
>> file name and be able to throw an error from there that aborts the
>> operation.
>
> You could define a file-handler for these files.

Yes, that looks like a solution.

For the record, the relevant info is in the Elisp manual:

    Making Certain File Names “Magic”

Thanks.




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to reject visiting a file
  2014-12-12 15:19   ` Óscar Fuentes
@ 2014-12-12 15:31     ` Stefan Monnier
  2014-12-12 15:47       ` Óscar Fuentes
  0 siblings, 1 reply; 7+ messages in thread
From: Stefan Monnier @ 2014-12-12 15:31 UTC (permalink / raw)
  To: help-gnu-emacs

>>> Is there a way to error-out when Emacs tries to visit certain file
>>> before the file is loaded? I'll like to use a hook that receives the
>>> file name and be able to throw an error from there that aborts the
>>> operation.
>> You could define a file-handler for these files.
> Yes, that looks like a solution.
> For the record, the relevant info is in the Elisp manual:
>     Making Certain File Names “Magic”

I think that's going to be pretty heavyweight.
A simpler approach is something like

    (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)


-- Stefan




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to reject visiting a file
  2014-12-12 15:31     ` Stefan Monnier
@ 2014-12-12 15:47       ` Óscar Fuentes
  2014-12-12 15:56         ` Stefan Monnier
  0 siblings, 1 reply; 7+ messages in thread
From: Óscar Fuentes @ 2014-12-12 15:47 UTC (permalink / raw)
  To: help-gnu-emacs

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

>>>> Is there a way to error-out when Emacs tries to visit certain file
>>>> before the file is loaded? I'll like to use a hook that receives the
>>>> file name and be able to throw an error from there that aborts the
>>>> operation.
>>> You could define a file-handler for these files.
>> Yes, that looks like a solution.
>> For the record, the relevant info is in the Elisp manual:
>>     Making Certain File Names “Magic”
>
> I think that's going to be pretty heavyweight.

I hope not. The function will inspect the file name (possibly the
primitive too) and either throw an error or delegate to
file-name-non-special.

> A simpler approach is something like
>
>     (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.




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to reject visiting a file
  2014-12-12 15:47       ` Óscar Fuentes
@ 2014-12-12 15:56         ` Stefan Monnier
  2014-12-12 16:35           ` Óscar Fuentes
  0 siblings, 1 reply; 7+ messages in thread
From: Stefan Monnier @ 2014-12-12 15:56 UTC (permalink / raw)
  To: help-gnu-emacs

>> (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.


        Stefan




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to reject visiting a file
  2014-12-12 15:56         ` Stefan Monnier
@ 2014-12-12 16:35           ` Óscar Fuentes
  0 siblings, 0 replies; 7+ messages in thread
From: Óscar Fuentes @ 2014-12-12 16:35 UTC (permalink / raw)
  To: help-gnu-emacs

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




^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-12-12 16:35 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-12-12  1:46 How to reject visiting a file Óscar Fuentes
2014-12-12  8:01 ` Eli Zaretskii
2014-12-12 15:19   ` Óscar Fuentes
2014-12-12 15:31     ` Stefan Monnier
2014-12-12 15:47       ` Óscar Fuentes
2014-12-12 15:56         ` Stefan Monnier
2014-12-12 16:35           ` Óscar Fuentes

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).