all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* What do 'hooks' do  and how do they do it?
@ 2009-08-01 17:17 William Case
  2009-08-01 17:39 ` Drew Adams
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: William Case @ 2009-08-01 17:17 UTC (permalink / raw
  To: Emacs Help List

Hi;

This is not an urgent question, it is more in the way of a request for
an explanation by anyone who has the time and inclination to do a little
teaching.

I know at a high level what a 'hook' is and how to use it an elisp
statement.  And, I have seen hooks used in other programs like SELinux.

But I am curious about what is going on at the kernel level with a
'hook'.  If someone can give me a brief overview in relatively plain
language, I would appreciate it.

e.g. some of the kind of questions that spring to mind.
Is it a process that is added to the task structure waiting to be
called?
How is it woken up?  And what kind of events might wake it?  etc.

As I said, an overview would be helpful.  If there is some detail that I
want clarified, I can dig deeper once I have a general idea of what is
going on. 

-- 
Regards Bill
Fedora 11, Gnome 2.26.3
Evo.2.26.3, Emacs 22.3.1





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

* RE: What do 'hooks' do  and how do they do it?
  2009-08-01 17:17 What do 'hooks' do and how do they do it? William Case
@ 2009-08-01 17:39 ` Drew Adams
  2009-08-01 18:00 ` Dirk-Jan C. Binnema
  2009-08-07 15:17 ` Thien-Thi Nguyen
  2 siblings, 0 replies; 7+ messages in thread
From: Drew Adams @ 2009-08-01 17:39 UTC (permalink / raw
  To: 'William Case', 'Emacs Help List'

> I know at a high level what a 'hook' is and how to use it an elisp
> statement.  And, I have seen hooks used in other programs 
> like SELinux.
> 
> But I am curious about what is going on at the kernel level with a
> 'hook'.  If someone can give me a brief overview in relatively plain
> language, I would appreciate it.
> 
> e.g. some of the kind of questions that spring to mind.
> Is it a process that is added to the task structure waiting to be
> called?
> How is it woken up?  And what kind of events might wake it?  etc.
> 
> As I said, an overview would be helpful.  If there is some 
> detail that I
> want clarified, I can dig deeper once I have a general idea of what is
> going on. 

See the Emacs manual, node `Hooks'.
See the Elisp manual, node `Hooks'.

A hook is a variable whose value is a list of functions. Typically, code invokes
the functions in such a list, one by one. Such invocation is done using a
function such as `run-mode-hooks' or `run-hooks'.

A typical example is the code for a major mode, such as `emacs-lisp-mode'. The
last thing such code does, when you turn on the mode, is to call
`run-mode-hooks' to run the hook provided specifically for that mode, e.g.
`emacs-lisp-mode-hook'. That is, it tries to invoke each of the functions on the
list `emacs-lisp-mode-hook', in turn. If there are no functions on the hook,
then nothing is done.

Because you can change the value of a hook variable, to include different
functions, you can in effect modify the behavior of the code that runs the hook.
If, for example, you add your own function `foo' to `emacs-lisp-mode-hook', then
whenever Emacs-Lisp mode is entered, `foo' will be invoked. In effect, you have
extended the definition of function `emacs-lisp-mode' so that it also does
`foo', after doing what it normally does.

Hooks are thus a way to allow easy code extension at predefined places.





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

* Re: What do 'hooks' do  and how do they do it?
  2009-08-01 17:17 What do 'hooks' do and how do they do it? William Case
  2009-08-01 17:39 ` Drew Adams
@ 2009-08-01 18:00 ` Dirk-Jan C. Binnema
  2009-08-01 20:43   ` William Case
  2009-08-07 15:17 ` Thien-Thi Nguyen
  2 siblings, 1 reply; 7+ messages in thread
From: Dirk-Jan C. Binnema @ 2009-08-01 18:00 UTC (permalink / raw
  To: William Case; +Cc: Emacs Help List


Hi Bill,

>>>>> "Bill" == William Case <billlinux@rogers.com> writes:

    Bill> Hi;
    Bill> This is not an urgent question, it is more in the way of a request for
    Bill> an explanation by anyone who has the time and inclination to do a little
    Bill> teaching.

    Bill> I know at a high level what a 'hook' is and how to use it an elisp
    Bill> statement.  And, I have seen hooks used in other programs like SELinux.

    Bill> But I am curious about what is going on at the kernel level with a
    Bill> 'hook'.  If someone can give me a brief overview in relatively plain
    Bill> language, I would appreciate it.

    Bill> e.g. some of the kind of questions that spring to mind.
    Bill> Is it a process that is added to the task structure waiting to be
    Bill> called?
    Bill> How is it woken up?  And what kind of events might wake it?  etc.

I think Drew Adams gave a very clear answer about the Emacs implementation,
but let me add that the concept of a 'hook' is simply a programming technique:
the ability to set some function to be run whenever a particular event
happens.

The concept is in use in many places (such as SE-Linux), but how it's
implemented is quite different. In Emacs-Lisp, the hooks are simply Lisp
functions to be called -- no kernel involved (well...).

See: http://en.wikipedia.org/wiki/Hooking

Best wishes,
Dirk.

-- 
Dirk-Jan C. Binnema                  Helsinki, Finland
e:djcb@djcbsoftware.nl           w:www.djcbsoftware.nl
pgp: D09C E664 897D 7D39 5047 A178 E96A C7A1 017D DA3C




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

* Re: What do 'hooks' do  and how do they do it?
  2009-08-01 18:00 ` Dirk-Jan C. Binnema
@ 2009-08-01 20:43   ` William Case
  2009-08-01 22:05     ` Drew Adams
  0 siblings, 1 reply; 7+ messages in thread
From: William Case @ 2009-08-01 20:43 UTC (permalink / raw
  To: djcb; +Cc: Emacs Help List

Thanks Dirk;

On Sat, 2009-08-01 at 21:00 +0300, Dirk-Jan C.Binnema wrote:
> Hi Bill,

> I think Drew Adams gave a very clear answer about the Emacs implementation,

Yes he did.  However, I tried to point out in my original post that I
was aware of how to implement a 'hook' in emacs.

> but let me add that the concept of a 'hook' is simply a programming technique:
> the ability to set some function to be run whenever a particular event
> happens.

I was curious about the programming technique that was used -- or more
specifically -- how the hook function was setup so that the hook
automagically responded to a program's hook event.


> The concept is in use in many places (such as SE-Linux), but how it's
> implemented is quite different. In Emacs-Lisp, the hooks are simply Lisp
> functions to be called -- no kernel involved (well...).
> 
> See: http://en.wikipedia.org/wiki/Hooking

The wikipedia site you suggest above covers just about everything I
wanted to know.  I don't know how I missed it.

-- 
Regards Bill
Fedora 11, Gnome 2.26.3
Evo.2.26.3, Emacs 22.3.1





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

* RE: What do 'hooks' do  and how do they do it?
  2009-08-01 20:43   ` William Case
@ 2009-08-01 22:05     ` Drew Adams
  2009-08-01 22:44       ` William Case
  0 siblings, 1 reply; 7+ messages in thread
From: Drew Adams @ 2009-08-01 22:05 UTC (permalink / raw
  To: 'William Case', djcb; +Cc: 'Emacs Help List'

> > I think Drew Adams gave a very clear answer about the Emacs 
> > implementation,
> 
> Yes he did.  However, I tried to point out in my original post that I
> was aware of how to implement a 'hook' in emacs.

Well, I did not explain how to implement a hook. I tried to explain what a hook
is: "a way to allow easy code extension at predefined places".

I explained that when a hook is run in Emacs, the calling code just calls a
function (e.g. `run-hooks') that obtains the functions to invoke from a
variable's value. That's what a hook _is_, for Emacs; that's not how to
implement a hook.

The general point is that (a) the call (when, why, etc.) is predefined, but (b)
what to call is determined dynamically (from the value of a variable, in the
case of Emacs).

In particular, in Emacs, hooking has _nothing to do with events_. Invocation of
a hook function in Emacs is _not_ event-driven.

> > but let me add that the concept of a 'hook' is simply a 
> > programming technique: the ability to set some function
> > to be run whenever a particular event happens.

Nope. (Unless the code that runs the hook variable happens to examine events to
determine whether to call `run-hooks'.)

The call to invoke the hook functions is static. The functions to invoke are
determined dynamically by the value of a variable. The hook is the static part;
what's hooked is the dynamic part - think of hooking a fish, if you like.

> I was curious about the programming technique that was used -- or more
> specifically -- how the hook function was setup so that the hook
> automagically responded to a program's hook event.

There is nothing automagical here, and there is no hook event.

> > The concept is in use in many places (such as SE-Linux), 
> > but how it's implemented is quite different. In Emacs-Lisp,
> > the hooks are simply Lisp functions to be called -- no kernel
> > involved (well...).

No. The "hooks" are the hook variables plus the calls to `run-hooks' that use
those variables, both of which are essentially static code. The values of the
hook variables are dynamic, and they determine which functions (the "hook
functions") get called. The functions called by the hooks are not the hooks; the
variables are the hooks.

More generally, a "hook" is a predefined call to something to be determined
outside the static calling code. It is, well, a "hook" on which you can hang any
code you want executed.

Hooks are static things; dynamic things are hung on the hooks. Think coat hooks
(stable, static) and coats that might be hung there (movable, dynamic,
changeable set).

The hanging typically takes place at a different time from the definition of the
code that has the hook (the building of the coat rack). That's the point:
decouple where to do something from what actually gets done there. Those are
determined at different times: code writing vs runtime.

Connecting (a) the actual functions to be called to (b) the hook that is made
available for calling them is, in effect, a form of dynamic or late binding. In
the case of Emacs, this is simply the binding of a variable to its value.

This is akin to (funcall foo): the (functional) value of variable `foo' is
obtained at runtime, and it is called as a function. (funcall foo) is a kind of
"hook", on which is hung the value of `foo' at runtime. (But in Emacs we
generally reserve "hook" for a hook variable. Nevertheless, the overall concept
here is the same.)

Note that in environments that are much less dynamic than Emacs and Lisp, hooks
can have greater importance: they are sometimes the _only_ effective way for
user code to be plugged into the main (binary) code.

In the early days, giving users a way to branch their own calls into the main
code was sometimes called a "user exit". (A hook that was hidden was sometimes
called a "back door".) "User exit" pretty much sums up this claustrophobic state
of affairs: it was the only way to escape from the program to do something extra
or different.

> > See: http://en.wikipedia.org/wiki/Hooking
> 
> The wikipedia site you suggest above covers just about everything I
> wanted to know.  I don't know how I missed it.

Most of that page does not particularly apply to Emacs hooks, I'm afraid. The
general idea applies, but most of the hook mechanisms described on that page
have little relation to Emacs hooks.

What is most helpful on that page wrt Emacs hooks are the two sentences that
constitute the (entire) section "Emacs":

  "Hooks are an important mechanism for customization of Emacs.
  A hook is a Lisp variable which holds a list of functions,
  to be called on some well-defined occasion. (This is called
  running the hook.)"

The "well-defined occasion" and the hook variable together constitute the
stable, "hook" part. The functions on the list at any given time are the
dynamic, "hung" part.

The best explanation of Emacs hooks and hooking remains the Emacs doc (which is
why I pointed you to it, and Wikipedia does likewise).

HTH.





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

* RE: What do 'hooks' do  and how do they do it?
  2009-08-01 22:05     ` Drew Adams
@ 2009-08-01 22:44       ` William Case
  0 siblings, 0 replies; 7+ messages in thread
From: William Case @ 2009-08-01 22:44 UTC (permalink / raw
  To: Drew Adams; +Cc: 'Emacs Help List', djcb

Sorry Drew;

I wish I had calibrated my response to Dirk a bit better.

On Sat, 2009-08-01 at 15:05 -0700, Drew Adams wrote:
> > > I think Drew Adams gave a very clear answer about the Emacs 
> > > implementation,
> > 
> > Yes he did.  However, I tried to point out in my original post that I
> > was aware of how to implement a 'hook' in emacs.
> 
> Well, I did not explain how to implement a hook. I tried to explain what a hook
> is: "a way to allow easy code extension at predefined places".
> 
> I explained that when a hook is run in Emacs, the calling code just calls a
> function (e.g. `run-hooks') that obtains the functions to invoke from a
> variable's value. That's what a hook _is_, for Emacs; that's not how to
> implement a hook.
> 

On rereading your first post, the answer I wanted was, in fact, there.

"A typical example is the code for a major mode, such as
`emacs-lisp-mode'. The last thing such code does, when you turn on the
mode, is to call `run-mode-hooks' to run the hook provided specifically
for that mode, e.g. `emacs-lisp-mode-hook'. That is, it tries to invoke
each of the functions on the list `emacs-lisp-mode-hook', in turn. If
there are no functions on the hook, then nothing is done."


> The general point is that (a) the call (when, why, etc.) is predefined, but (b)
> what to call is determined dynamically (from the value of a variable, in the
> case of Emacs).
> 
> In particular, in Emacs, hooking has _nothing to do with events_. Invocation of
> a hook function in Emacs is _not_ event-driven.
> 
> > > but let me add that the concept of a 'hook' is simply a 
> > > programming technique: the ability to set some function
> > > to be run whenever a particular event happens.
> 
> Nope. (Unless the code that runs the hook variable happens to examine events to
> determine whether to call `run-hooks'.)

The event, so to speak, is turning on the mode. 

> 
> The call to invoke the hook functions is static. The functions to invoke are
> determined dynamically by the value of a variable. The hook is the static part;
> what's hooked is the dynamic part - think of hooking a fish, if you like.
> 
> > I was curious about the programming technique that was used -- or more
> > specifically -- how the hook function was setup so that the hook
> > automagically responded to a program's hook event.
> 
> There is nothing automagical here, and there is no hook event.

Yes, I have tended to over use the term automagical since I first
discovered the word.  Almost always I use it in reference to correcting
my understanding of things that seem automagical.  To me, the
automagical has no place in computing.
> 
> > > The concept is in use in many places (such as SE-Linux), 
> > > but how it's implemented is quite different. In Emacs-Lisp,
> > > the hooks are simply Lisp functions to be called -- no kernel
> > > involved (well...).
> 
> No. The "hooks" are the hook variables plus the calls to `run-hooks' that use
> those variables, both of which are essentially static code. The values of the
> hook variables are dynamic, and they determine which functions (the "hook
> functions") get called. The functions called by the hooks are not the hooks; the
> variables are the hooks.
> 
> More generally, a "hook" is a predefined call to something to be determined
> outside the static calling code. It is, well, a "hook" on which you can hang any
> code you want executed.
> 
> Hooks are static things; dynamic things are hung on the hooks. Think coat hooks
> (stable, static) and coats that might be hung there (movable, dynamic,
> changeable set).
> 
> The hanging typically takes place at a different time from the definition of the
> code that has the hook (the building of the coat rack). That's the point:
> decouple where to do something from what actually gets done there. Those are
> determined at different times: code writing vs runtime.
> 
> Connecting (a) the actual functions to be called to (b) the hook that is made
> available for calling them is, in effect, a form of dynamic or late binding. In
> the case of Emacs, this is simply the binding of a variable to its value.
> 
> This is akin to (funcall foo): the (functional) value of variable `foo' is
> obtained at runtime, and it is called as a function. (funcall foo) is a kind of
> "hook", on which is hung the value of `foo' at runtime. (But in Emacs we
> generally reserve "hook" for a hook variable. Nevertheless, the overall concept
> here is the same.)
> 
> Note that in environments that are much less dynamic than Emacs and Lisp, hooks
> can have greater importance: they are sometimes the _only_ effective way for
> user code to be plugged into the main (binary) code.
> 
> In the early days, giving users a way to branch their own calls into the main
> code was sometimes called a "user exit". (A hook that was hidden was sometimes
> called a "back door".) "User exit" pretty much sums up this claustrophobic state
> of affairs: it was the only way to escape from the program to do something extra
> or different.
> 
I will keep the entirety of this reponse, in my personal emacs
explanations file.

> > > See: http://en.wikipedia.org/wiki/Hooking
> > 
> > The wikipedia site you suggest above covers just about everything I
> > wanted to know.  I don't know how I missed it.
> 
> Most of that page does not particularly apply to Emacs hooks, I'm afraid. The
> general idea applies, but most of the hook mechanisms described on that page
> have little relation to Emacs hooks.
> 

OK.  I'll keep that in mind.

> What is most helpful on that page wrt Emacs hooks are the two sentences that
> constitute the (entire) section "Emacs":
> 
>   "Hooks are an important mechanism for customization of Emacs.
>   A hook is a Lisp variable which holds a list of functions,
>   to be called on some well-defined occasion. (This is called
>   running the hook.)"
> 
> The "well-defined occasion" and the hook variable together constitute the
> stable, "hook" part. The functions on the list at any given time are the
> dynamic, "hung" part.
> 
> The best explanation of Emacs hooks and hooking remains the Emacs doc (which is
> why I pointed you to it, and Wikipedia does likewise).HTH.

All that I can say in my own defence, is that I had somewhat of a
misconception of what hooks were and therefore I DID have good reason to
ask further what hooks were.

-- 
Regards Bill
Fedora 11, Gnome 2.26.3
Evo.2.26.3, Emacs 22.3.1





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

* Re: What do 'hooks' do  and how do they do it?
  2009-08-01 17:17 What do 'hooks' do and how do they do it? William Case
  2009-08-01 17:39 ` Drew Adams
  2009-08-01 18:00 ` Dirk-Jan C. Binnema
@ 2009-08-07 15:17 ` Thien-Thi Nguyen
  2 siblings, 0 replies; 7+ messages in thread
From: Thien-Thi Nguyen @ 2009-08-07 15:17 UTC (permalink / raw
  To: William Case; +Cc: Emacs Help List

() William Case <billlinux@rogers.com>
() Sat, 01 Aug 2009 17:17:22 +0000

   How is it woken up?

A hook is a courtesy of the program's Author, a place
where humility triumphs over and yet also encompasses
cleverness, an invitation from the past to the Author
in the present.  A good hook is like a nice joke that
builds merriment in the listener, because imagination
is what wakes it and brooks its growth.

thi




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

end of thread, other threads:[~2009-08-07 15:17 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-08-01 17:17 What do 'hooks' do and how do they do it? William Case
2009-08-01 17:39 ` Drew Adams
2009-08-01 18:00 ` Dirk-Jan C. Binnema
2009-08-01 20:43   ` William Case
2009-08-01 22:05     ` Drew Adams
2009-08-01 22:44       ` William Case
2009-08-07 15:17 ` Thien-Thi Nguyen

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.