0

I'd like to segment Lisp source code into a list of code segments. Each segment is either a top level S-expression, or whatever text between those S-expressions.

Initially I thought that I could use regular expression pattern to match top level S-expression, but I found that it's not as trivial to handle the matching parenthesis to at the top level only.

(I feel that it might be possible to work out a regular expression to match top level S-exp) by following example of this Using regular expressions to extract functions and function headers from source code

Next, I figure that I may scan the text to observe the first left parenthesis '(', and the last right parenthesis ')', and do the segmentation accordingly.

Before I do that I feel that given the ubiquitous nature of S-expression in emacs Lisp, there should have already existing solution to my requirements.

So I ask if you could teach me any solution?

Thanks,

1 Answer 1

1

Regexps are absolutely wrong for the job. You cannot match an arbitrarily-nested balanced form with a regular expression, so forget that idea.

Moving over a s-expression with elisp is as simple as (forward-sexp)

I don't actually understand what it is you're trying to achieve, though. Could you please update the question with more details?

edit: Seeing as you've accepted this answer, I'll just point you to these for further reading:

  • C-hig (elisp) Parsing Expressions RET
  • C-hig (elisp) List Motion RET
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I was looking for the existing support like forward-sexp. My goal is to transform Lisp source code file into emacs org-babel supported literate programming org file where I would like to have each function or top sexp to be a org code block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.