7

I'm looking for functionality that can do source-to-source transformation of elisp programs given a set of rewriting rules. For example, given a rule like:

(set (make-local-variable (quote ?var) ?value)) => 
   (setq-local ?var ?value)

It will find all occurrences of the pattern and replace it with replacement. A welcome feature would be to do minimum amount of changes.

Edit: I found this http://www.informatik.uni-bremen.de/st/lehre/Arte-fakt/Seminar/papers/06/Formal/pattern-language-for-refactoring.pdf sadly no implementation.

Drew
  • 75,699
  • 9
  • 109
  • 225
Gracjan Polak
  • 1,082
  • 6
  • 21

2 Answers2

7

I believe you're looking for the el-search package, available in the GNU ELPA repository. It lets you match using pcase patterns and does implement the search&replace functionality that you're describing.

You'd use el-search-query-replace and provide the pattern

`(set (make-local-variable ',var) ,val)

and replace it with

`(setq-local ,var ,val)
Andrew Swann
  • 3,436
  • 2
  • 15
  • 43
Stefan
  • 26,154
  • 3
  • 46
  • 84
1

Lisp macro expansion is exactly that: a mapping from Lisp sexps to Lisp sexps. Your friends for this are defmacro or -- more especially -- macrolet (aka cl-macrolet for Emacs Lisp) and macroexpand (also macroexpand-all).

Note that I am not referring to macro expansion followed by evaluation of the resulting Lisp code, which is how Lisp macros are usually used.

In this case, you do not want to evaluate the result of macro expansion. You want only to "expand" an input Lisp sexp to produce a different Lisp sexp. (In computer science and logic this is sometimes called "reduction" or "rewriting".)

(Of course, to deal with sexps that are not lists you will need to also provide mappings between known symbols and the like. But macro expansion takes care of most sexp-pattern mappings.)

Drew
  • 75,699
  • 9
  • 109
  • 225