When I use the when keyword (or expression) in emacs lisp

I am learning how to extend my local GNU emacs software by learning emacs lisp. In one of the source codes that I came across, I saw "when." I think this is a management structure, but I'm not sure. I tried googling "when a keyword / expression in emacs lisp" (and other similar permutations of this kind). I even checked gnu.org. I only found source codes containing "when", but no description of how and when to use "when." Can someone tell me how and in what suitable situations should I use when when in management structures, etc. In emacs lisp? Thanks in advance.

+4
source share
2 answers

Enter Ch f when RET and you will see the documentation:

when - Lisp macro in subr.el

(when COND BODY...)

When COND gives non nil , eval BODY forms sequentially and returns the value of the latter, or nil if none exist.

You can see how this is implemented if you macro-expand it:

 ELISP> (macroexpand '(when cond body1 body2 body3)) (if cond (progn body1 body2 body3)) 

You should use when instead of if in case you don't have an else clause. It looks better, and it gives the reader a hint that there is no β€œyet” sentence. If you have an else clause but no then clause, you can write unless .

+14
source

Many people follow a usage agreement when and if you don’t tell human readers that the return value of sexp doesn't matter - it’s important that any side effects occur.

+2
source

All Articles