Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Infix operators for Emacs Lisp

NotificationsYou must be signed in to change notification settings

rspeele/infix.el

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 

Repository files navigation

Infix operators for Emacs Lisp

This library provides a macro,$, which rewrites expressions writtenin infix notation (with operators between their operands) toS-expressions. This lets you write some Emacs Lisp code in a morereadable and concise format. For example:

    ($ x+y*3 < 15 and z%8 = 0)

expands to:

    (and (< (+ x (* y 3)) 15) (= (% z 8) 0))

Because the translation from infix notation happens during macroexpansion, it can be expected to have no performance penalty onbyte-compiled code, compared to writing the S-expression manually.

Doesn't require spaces

Elisp is very liberal in what it considers a "symbol" (variable name). It parses1+2 as a symbol, strange though it is. You can actually use this like anyother symbol, for example assigning it a value with(setq 1+2 37). The$macro automatically works around this wacky feature, breaking the symbol outinto the separate terms1 + 2 before translating it to(+ 1 2). This processis called "deglobbing".

This behavior can admittedly be confusing in the presence of variablesnamed-like-this. Because such variable names are idiomatic in Lisp, they arespecial-cased by the deglobber and NOT treated as subtraction unless you usespaces. That is,($ a+b) expands to(+ a b), and($ a - b) expands to(- a b), but($ a-b) expands toa-b.

If you want to keep it simple at the expense of requiring spaces around everyoperator, you can use the$: macro instead, which does not do any mangling ofsymbols.

Plays nicely with s-exprs

The$ macroonly rearranges the top-level terms it is applied to.Nested s-expressions are left alone, so you can do stuff like:

    ($ 1 + (read "2"))

This makes using normal elisp code within an infix expression very simple.However, it might be confusing if you want to use parentheses the way they'dbe used in infix expressions in other languages: to override the defaultorder of operations. For example, consider the expression(1+2)*3.For that you have two options:

Either use curly braces for grouping nested infix expressions:

    ($ {1 + 2} * 3)

Or just use the$ macro again in a nested s-expression:

    ($ ($ 1 + 2) * 3)

Generates efficient elisp

There is no need to worry about expressions like:

    ($ a + b + c + x + y + z)

resulting in inefficient elisp such as:

    (+ (+ (+ (+ (+ a b) c) x) y) z)

infix.el knows that some operators, like+, can have nested callssimplified and will generate the desired expression:

    (+ a b c x y z)

Lets you define your own operators

Finally, macros are also provided for declaring your own infixoperators with custom associativity and precedence.

For example, to declare the symbol@ to be a left-associativeoperator with the same precedence as the+ operator, you can write:

    (infixl (precedence +) @)

To declare it as a right associative operator, with a precedencehigher than+ but less than*, you would use:

    (infixr (precedence-between + *) @)

And finally, if@ can have nested calls flattened to a single call,as in the example of+ above, you would useinfixrf orinfixlfinstead ofinfixr andinfixl, respectively.

    (infixrf (precedence-between + *) @)    ;; ($ 1 @ 2 @ 3) now becomes (@ 1 2 3) instead of (@ 1 (@ 2 3))

For further details, consult the docstrings for$ and$:, or readthe comment blocks in the source.

Enjoy.

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp