Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!rutgers!mit-eddie!uw-beaver!tikal!sigma!uw-nsr!uw-warp!tom From: tom@uw-warp.UUCP (F. Thomas May) Newsgroups: comp.emacs Subject: Re: dired-mode-hook exists - here is the way to setq it Message-ID: <260@uw-warp.UUCP> Date: Sat, 22-Aug-87 17:11:36 EDT Article-I.D.: uw-warp.260 Posted: Sat Aug 22 17:11:36 1987 Date-Received: Thu, 27-Aug-87 06:16:03 EDT References: <431@morgoth.UUCP> Organization: The Warp, Seattle, WA Lines: 59 Summary: Here is a function to set hooks In-reply-to: dmb@morgoth.UUCP's message of 18 Aug 87 17:13:43 GMT In article <431@morgoth.UUCP> dmb@morgoth.UUCP (David M. Brown) writes: > (setq dired-mode-hook > (function > (lambda nil > (define-key dired-mode-map "1" 'dired-find-file)))) > > Dave: If you copy this style it will work. Just don't ask me why, okay? Things get more complicated when using multiple hooks as supported by run-hooks. To make things a bit simpler, I wrote a function which allows a new hook function to be added to a hook variable. Here it is: (defun add-hook (hook-var hook-fun) "Two arguments, HOOK-VAR and HOOK-FUN. Adds HOOK-FUN to the list of hooks in HOOK-VAR if it is not already present. add-hook is very tolerant; HOOK-VAR need not be previously defined, its value doesn't have to be a list, lamda expressions are cool, etc." (or (boundp hook-var) (set hook-var nil)) (let ((hook-var-val (symbol-value hook-var))) (or (listp hook-var-val) (setq hook-var-val (cons hook-var-val nil))) (if (eq (car hook-var-val) 'lambda) (setq hook-var-val (cons hook-var-val nil))) (or (spud-member hook-fun hook-var-val) (set hook-var (nreverse (cons hook-fun (reverse hook-var-val))))))) ;; non-recursive definition of member (used by add-hook) (defun spud-member (elt list) "Returns non-nil if ELT is an element of LIST. Comparison done with EQUAL. The value is actually the tail of LIST whose car is ELT." (while (and list (not (equal elt (car list)))) (setq list (cdr list))) list) Using add-hook, the original example becomes (add-hook 'dired-mode-hook (function (lambda nil (define-key dired-mode-map "1" 'dired-find-file)))) which is nearly identical to the original. The benefit comes when a second hook is added to dired-mode-hook: (add-hook 'dired-mode-hook 'some-cool-function) Now both the lambda thing and some-cool-function will be invoked, in that order, when dired is used. add-hook even diagnoses the case in which setq was used previously to set a hook and fixes things up for the multiple hook case. It also won't add the same hook more than once. -- Tom May uw-nsr!uw-warp!tom@beaver.cs.washington.edu uw-beaver!uw-nsr!uw-warp!tom