2

Background

I see the question Best way to disable code:

I think something like the following would be nice:

(off
  (global-set-key "\C-x" 'do-stuff)
  (defun do-stuff () ...))

I understand I can define off myself, but isn’t there a standard way to do this?

and the corresponding answer by gigiair:

(defun off (&rest args)
  "Do nothing and return nil."
  nil)

But I think this solution has 2 disadvantages considering that every function will evaluate its arguments and return something, for example (which is about "return something"):

(custom-set-variables
  (off '(display-time-mode   t)) ; ==> (), which is non-well-formed
  '(blink-cursor-mode nil))

Using the off function will lead to a non-well-formed s-expression (which is not expected by custom-set-variables).


Q

The key may be to define something that returns nothing, like:

(defmacro erase-code (&rest pieces-of-code)
  ...)

which will make the last example expand to:

(custom-set-variables
  '(blink-cursor-mode nil))

In other words, it leaves nothing.


Maybe the ultimate solution is not about macro, i don't know.

Essentially, I am asking: Is there anything that can return nothing or leave nothing in place in Common Lisp or Emacs Lisp?

coredump
  • 37,664
  • 5
  • 43
  • 77
shynur
  • 334
  • 10
  • "It would be nice to use an S-expression-based approach to comment some code." – shynur Mar 18 '23 at 11:37
  • Looks like `serapeum:comment` effectively returns nothing: https://github.com/ruricolist/serapeum/blob/master/REFERENCE.md#comment-body-body it `(declare (ignore body))`. – Ehvince Mar 19 '23 at 12:10

1 Answers1

1

You can't define a macro which expands to nothing, since CL (and I think elisp) does not have what were sometimes called 'splicing macros'. But you can very easily write a macro which expands to some fixed expression, for instance nil:

(defmacro off (&body forms)
  (declare (ignore forms))
  nil)

This is Common Lisp: the equivalent for elisp should be similar. So then something like

(defun foo (x)
  (off
    (when x
      (explode-world)))
  x)

will not explode the world, and indeed there is no reference to explode-world in the macroexpanded code.

ignis volens
  • 7,040
  • 2
  • 12