home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / functions / add-hook.el next >
Encoding:
Text File  |  1990-07-22  |  2.6 KB  |  66 lines

  1. ;From ark1!uakari.primate.wisc.edu!brutus.cs.uiuc.edu!liberte Thu Apr 19 12:34:17 EDT 1990
  2. ;Article 1772 of comp.emacs:
  3. ;Path: ark1!uakari.primate.wisc.edu!brutus.cs.uiuc.edu!liberte
  4. ;>From: liberte@cs.uiuc.edu (Daniel LaLiberte)
  5. ;Newsgroups: comp.emacs
  6. ;Subject: Re: text-mode-hook
  7. ;Message-ID: <LIBERTE.90Apr15174338@cassius.cs.uiuc.edu>
  8. ;Date: 15 Apr 90 22:41:10 GMT
  9. ;References: <sa7sr8K00hMN1MQG0R@cs.cmu.edu> <PD.90Apr10155710@rutland.ixi.uucp>
  10. ;    <WEINER.90Apr11233748@novavax.UUCP>
  11. ;Sender: news@brutus.cs.uiuc.edu
  12. ;Organization: University of Illinois, Urbana-Champaign, Dept CS
  13. ;Lines: 50
  14. ;In-reply-to: weiner@novavax.UUCP's message of 12 Apr 90 03:37:48 GMT
  15. ;
  16. ;
  17. ;Here is another, more general way to solve the multiple hook problem.
  18. ;I may not have covered every case, but compared to Bob Weiner's,
  19. ;this one tests whether the hook-var already contains the hook-function;
  20. ;it also allows the hook-var to initially contain either a single function
  21. ;or a list of functions, as run-hooks supports.
  22. ;
  23. ;Now that I look at it again, I'm not even sure that it works correctly.
  24. ;But I thought I would toss it out for what it's worth.
  25.  
  26. (defun add-hook (hook-var hook-function)
  27.   "Prepend hook-function to hook-var's value, if it is not already an element.
  28. hook-var's value may be a single function or a list of functions."
  29.   (if (boundp hook-var)
  30.       (let ((value (symbol-value hook-var)))
  31.     (if (and (listp value) (not (eq (car value) 'lambda)))
  32.         (and (not (memq hook-function value))
  33.          (set hook-var
  34.               (cons hook-function value)))
  35.       (and (not (eq hook-function value))
  36.            (set hook-var
  37.             (list hook-function value)))))
  38.     (set hook-var hook-function)
  39.     ))
  40.  
  41. ;---------
  42. ;For comparison, here is the definition of run-hooks in subr.el: 
  43. ;
  44. ;(defun run-hooks (&rest hooklist)
  45. ;  "Takes hook names and runs each one in turn.  Major mode functions use this.
  46. ;Each argument should be a symbol, a hook variable.
  47. ;These symbols are processed in the order specified.
  48. ;If a hook symbol has a non-nil value, that value may be a function
  49. ;or a list of functions to be called to run the hook.
  50. ;If the value is a function, it is called with no arguments.
  51. ;If it is a list, the elements are called, in order, with no arguments."
  52. ;  (while hooklist
  53. ;    (let ((sym (car hooklist)))
  54. ;      (and (boundp sym)
  55. ;       (symbol-value sym)
  56. ;       (let ((value (symbol-value sym)))
  57. ;         (if (and (listp value) (not (eq (car value) 'lambda)))
  58. ;         (mapcar 'funcall value)
  59. ;           (funcall value)))))
  60. ;    (setq hooklist (cdr hooklist))))
  61. ;
  62. ;Dan LaLiberte
  63. ;uiucdcs!liberte
  64. ;liberte@cs.uiuc.edu
  65. ;liberte%a.cs.uiuc.edu@uiucvmd.bitnet
  66.