Evalling the code in try-foo.el below loads then unloads foo.el. It
gets an error
void-function foo-message
where I hoped unload-feature might have purged that `foo-message' from
`after-change-functions'.
I suppose looking in all buffers is more work for unload-feature, but
would be a good protection against bad things happening later. I expect
some of the standard hooks like `after-change-functions' are used
buffer-local most of the time.
If instead it's an intentional omission (to save work) then the elisp
manual and the docstring could note it so that modes or packages using
buffer-local hook settings can take steps to undo.
> When `unload-feature' looks in hooks for functions that it's going to
> unload, it doesn't seem to look in buffer-local values, other than for
> the current buffer.
[...]
> I suppose looking in all buffers is more work for unload-feature, but
> would be a good protection against bad things happening later.
Yes, unload-feature should look at buffer-local values, too.
For the moment being, a package like foo should define a function (foo
in the function name matches the feature name you're unloading):
(defun foo-unload-function ()
;; do whatever is necessary; in foo's case:
(with-current-buffer (get-buffer "foo-bufer")
(remove-hook 'after-change-functions 'foo-message t))
;; continue standard unloading
nil)
that will be automatically called upon unloading foo.el; in most
cases, it should return nil to allow unload-feature to continue the
normal unloading process.
Juanma
You're right that some hooks are used buffer-locally most of the time,
but it's also true that in many cases they are used from a major mode,
i.e., in your case, if foo.el defined a foo-mode and set
`after-change-functions' locally in foo-mode buffers, unload-feature
would do OK (with the current trunk, not any released Emacsen).
As it is, foo.el is doing something non-standard, and unload-feature
cannot try to revert by itself every non-standard thing packages do.
That's why FEATURE-unload-function exists. So, in this case, the right
fix would be adding this function to foo.el:
(defun foo-unload-function ()
"Unload foo.el."
(ignore-errors
(with-current-buffer (get-buffer "foo-buffer")
(remove-hook 'after-change-functions 'foo-message t)))
;; continue standard unloading
nil)
That said, there are improvements that could be made to unload-feature
(for example, trying to automatically deactivate minor modes being
undefined), but I'm not sure that looking in every buffer-local
variable of every live buffer is a sensible thing to do. I'm closing
this one because it is not really a bug. We can open a wishlist bug
for unload-feature is you want.
Juanma
No, just the hooks. If it makes sense to remove unloaded funcs from
hook global values then surely the same rationale applies to remove them
from the buffer-local values too.
Or conversely, it's undesirable to leave behind an unbound func in a
hook, and the same undesirability as to a buffer-local value as a global
value.
> Or conversely, it's undesirable to leave behind an unbound func in a
> hook, and the same undesirability as to a buffer-local value as a global
> value.
But the usual case is that these buffer-local values are set via major
modes also defined in the same package, and so they are automatically
removed when the major modes are disabled (i.e., when the buffers are
switched to other major modes). The only case where a buffer-local
value is left behind is when the package's code sets it in
non-standard ways, and in this case, it's the package responsability
to define a FEATURE-unload-function to undo the changes.
The philosophy behind unload-feature is: we try to automatically undo
the easy/standard things, and give the package the opportunity to undo
the hard/unstandard things itself. And I think it's the right
approach.
Juanma
1) If your reasoning about hooks being added via modes were correct, you
wouldn't have to remove even the global hook additions. If it's faulty
(which is probably the case), both global and local hooks need to be
managed, as Kevin said.
2) The `unload-feature' docstring says it undoes "any additions that the
library has made to hook variables", but that's apparently not what's
really happening, so if things stay as they are, the doc string should
be corrected.
3) Are local hook additions really such a "hard/unstandard" thing to
undo?
Štěpán
> 1) If your reasoning about hooks being added via modes were correct, you
> wouldn't have to remove even the global hook additions.
Why? Global additions are not removed when the buffer's major mode is
switched. Local variables, including local values of hooks, are.
> If it's faulty
> (which is probably the case), both global and local hooks need to be
> managed, as Kevin said.
I'm more of the opinion that both should be un-managed. A look at the
sources is enough to see that hook removal is currently ugly and
ad-hoc, and ugly too.
> 2) The `unload-feature' docstring says it undoes "any additions that the
> library has made to hook variables", but that's apparently not what's
> really happening, so if things stay as they are, the doc string should
> be corrected.
Yes. The docstring for unload-feature has always promised more than it
could reasonably accomplish. Yours is only one example.
> 3) Are local hook additions really such a "hard/unstandard" thing to
> undo?
Local hook additions aren't. And they are correctly treated right now.
What we are discussing is local hooks in buffers whose major mode
wasn't also defined in the same feature being unloaded. For example,
things like
;;; my-mode.el
(define-derived-mode my-mode ...)
(defun my-mode-this () ...)
(defun my-mode-that () ...)
(defun my-mode-hook () ...)
(with-current-buffer (get-buffer "some poor unsuspecting buffer"))
;;; do not set the buffer major mode to my-mode, but
(add-hook 'some-hook 'my-mode-hook nil t))
(provide 'my-mode)
;;;; end of my-mode.el
and that kind of thing is unfrequent enough that the better fix is
(defun my-mode-unload-function ()
(ignore-errors
(with-current-buffer (get-buffer "some poor unsuspecting buffer")
(remove-hook 'some-hook 'my-mode-hook t)))
nil)
Both Kevin and you seem to think that unload-feature should do
everything and that having to define FEATURE-unload-function is a bug
or something like that. It is not. I'm all for making unload-feature
smarter, as long as it does not trample on the programmer's ability to
do. I can perfectly well imagine unloading a package but setting a
hook with an autoloading function from that same package.
Juanma
> On Fri, Jul 15, 2011 at 10:52, Štěpán Němec <ste...@gmail.com> wrote:
>
>> 1) If your reasoning about hooks being added via modes were correct, you
>> wouldn't have to remove even the global hook additions.
>
> Why? Global additions are not removed when the buffer's major mode is
> switched. Local variables, including local values of hooks, are.
Note I omitted the "major" part, i.e., it's not uncommon for minor modes
to make global hook additions. Sorry if that's not really related to the
problem at hand.
>> If it's faulty
>> (which is probably the case), both global and local hooks need to be
>> managed, as Kevin said.
>
> I'm more of the opinion that both should be un-managed. A look at the
> sources is enough to see that hook removal is currently ugly and
> ad-hoc, and ugly too.
Fine with me, as long as the documentation reflects this.
>> 2) The `unload-feature' docstring says it undoes "any additions that the
>> library has made to hook variables", but that's apparently not what's
>> really happening, so if things stay as they are, the doc string should
>> be corrected.
>
> Yes. The docstring for unload-feature has always promised more than it
> could reasonably accomplish. Yours is only one example.
Please do update it then.
[...]
> Both Kevin and you seem to think that unload-feature should do
> everything and that having to define FEATURE-unload-function is a bug
> or something like that. It is not. I'm all for making unload-feature
> smarter, as long as it does not trample on the programmer's ability to
> do. I can perfectly well imagine unloading a package but setting a
> hook with an autoloading function from that same package.
Right... I do know about unload functions and use them where
appropriate. The important thing is that the documentation needs to
describe what actually happens, so whatever you decide to do about this,
please update the documentation (which, as you confirmed, needs to be
done anyway).
Štěpán
> Note I omitted the "major" part, i.e., it's not uncommon for minor modes
> to make global hook additions. Sorry if that's not really related to the
> problem at hand.
Currently, minor modes are not automatically turned off; packages that
define minor modes *need* a FEATURE-unload-function. See allout.el,
hi-lock.el, hl-line.el, linum.el, etc. Turning off the minor mode
should remove these hooks.
> The important thing is that the documentation needs to
> describe what actually happens, so whatever you decide to do about this,
> please update the documentation (which, as you confirmed, needs to be
> done anyway).
I agree that the documentation should better reflect what
unload-feature actually does, but I won't be the one writing it. I
suck at that.
Juanma
Agreed. Someone would have to try it out to see if it can be
done efficiently.
Stefan
Reopened for that, or failing that then for clarifying the docstring.
I imagine there's not normally many hooks or many buffers, or many
buffer-local variables, whichever one of those was the efficient way to
scan ... and unload-feature isn't used very much anyway.
(Actually the way unload-feature seems not much used and not getting
much attention from packages makes me wonder if it's worth bothering.
But if unload-feature did a little more then it would increase its
usefulness, perhaps to the point where it would be used more :-)