1

I'm using Emacs, and I have a macro that's like a switch for regexes, and runs an expression with variables bound to match results.

The indentation isn't what I'd like but I can't find if there's a way to tell the macro to indent sub-expressions.

E.g. currently usage looks like:

(regex-case "some-string"
  ("name +([a-z]+) +(.+)" (arg1 arg2)
                          (format t "~A / ~A~%" arg1 arg2))
  ("A*" ()
        (format t "all A~%")))

But I'd like the parts that run to be indented more like functions, i.e.:

(regex-case "some-string"
  ("name +([a-z]+) +(.+)" (arg1 arg2)
    (format t "~A / ~A~%" arg1 arg2))
  ("A*" ()
    (format t "all A~%")))

Is there a way to do this?

I figure a next-best thing would be to have the regex and the variables in a list, and then it naturally becomes:

(regex-case "some-string"
  (("name +([a-z]+) +(.+)" (arg1 arg2))
   (format t "~A / ~A~%" arg1 arg2))
  (("A*" ())
   (format t "all A~%")))

But I'd really like more control over it than that, if possible.

2
  • Edited to clarify that I'm using Emacs. Commented May 30 at 8:30
  • The emacs stack exchange might be better suited for this question. Commented May 30 at 20:07

1 Answer 1

2

I figure that this gets close enough (nearer the desired version than the default version):

(defmacro regex-case (&rest args)
  (declare (indent defun))
  ;; ...
  )

(regex-case "some-string"
  ("name +([a-z]+) +(.+)" (arg1 arg2)
   (format t "~A / ~A~%" arg1 arg2))
  ("A*" ()
   (format t "all A~%")))

Refer to C-hig (elisp)Indenting Macros

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, declare indent is a simplest way. Indent defun should work fine for macros, but one can also use numbers if they prefer or even a function. Check info node ‘(elisp)Declare Form’ for more details.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.