Re: Improving add-hook of two functions

tpeplt <[email protected]> Mon, 06 Jul 2026 11:39:05 -0400
Newsgroups gmane.emacs.help
Message-ID <[email protected]>
Heime <[email protected]> writes:

> I would like to improve the selection mechanism and
> performance of the following function.
>

Until someone suggests an alternate mechanism for what you
want, you can simplify your code by replacing your ‘cond’
expressions with ‘cl-case’ (formerly, ‘case’):

>          (hka   (cond
>                   ((eq actm 'add)            #'add-hook)
>                   ((eq actm 'remove)         #'remove-hook)
>                   ((eq actm 'add-regexp)     #'add-hook)
>                   ((eq actm 'remove-regexp)  #'remove-hook)
>                   ((eq actm 'add-outline)    #'add-hook)
>                   ((eq actm 'remove-outline) #'remove-hook)
>                   (t (error "%s: ACTM %s is invalid"
>                        "kronaire-hooks" actm)))) )

Using ‘cl-case’ can simplify this binding expression to:

   (hka (cl-case actm
          ((add add-regexp add-outline)          #'add-hook)
          ((remove remove-regexp remove-outline) #'remove-hook)
          (otherwise (error "%s: ACTM %s is invalid"
                            "kronaire-hooks" actm))))

Likewise,

>       (cond
>          ((or (eq actm 'add) (eq actm 'remove))
>              (funcall hka mdhook #'kronaire-regexp)
>              (funcall hka mdhook #'outline-minor-mode))
>          ((or (eq actm 'add-regexp) (eq actm 'remove-regexp))
>              (funcall hka mdhook #'kronaire-regexp))
>          ((or (eq actm 'add-outline) (eq actm 'remove-outline))
>              (funcall hka mdhook #'outline-minor-mode))))))

simplifies to:

   (cl-case actm
     ((add remove)
      (funcall hka mdhook #'kronaire-regexp)
      (funcall hka mdhook #'outline-minor-mode))

     ((add-regexp remove-regexp)
      (funcall hka mdhook #'kronaire-regexp))

     ((add-outline remove-outline)
      (funcall hka mdhook #'outline-minor-mode)))

You should not expect that these changes will improve
performance significantly.  Evaluate the following
expression in Emacs to read more about ‘cl-case’:

   (info "(cl) Conditionals")

-- 
The lyf so short, the craft so long to lerne.
- Geoffrey Chaucer, The Parliament of Birds.