This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Indenting "HTML" S-expressions

7 posts · 16490 views

A common method when working with HTML in Common Lisp seems to transform S-expressions to HTML such that
(:h1 :class "foo" "Hello") => <h1 class="foo">Hello</h1>
I like this method a lot, but the default indentation of Emacs/Slime bothers me:
(:h1 :class "foo"
     "Hello")
The indentation code indents it as if :H1 was a function called with three arguments, which is understandable. It should be possible to make an exception for the case "non-quoted list whose first element starts with a colon" and rather indent it like this:
(:h1 :class "foo"
 "Hello")
Before I spend the weekend trying to modify the indentation code, I was wondering if anybody has done this before or know of a workaround?

Re: Indenting "HTML" S-expressions

The indentation makes perfect sense to me:
(:div :class "whatever"
      "Text here")
Turns into this:
<div class="whatever">
     Text here
</div>
Which is how I do my HTML, and I'm pretty sure that's the standard.

Re: Indenting "HTML" S-expressions

Ok, consider this then:
(:div :class "foo"
      (:div :class "bar"
            (:div :class "zot"
                  ...)))
After three DIV-tags its already indented 18 columns to the right. This also makes the HTML S-expressions less uniform; it would be slightly easier to read if they were always indented only one column. Consider these two examples:
(:div :class "foo"
      (:div
       (:div :class "bar"
	     (:a :href "zot.html"
                 (:img :src "zot.jpg)))
(:div :class "foo"
 (:div
  (:div :class "bar"
   (:a :href "zot.html"
    (:img :src "zot.jpg)))
In my opinion the second one is superior.

There is supposed to be a space before the second DIV by the way, but phpBB doesn't show it for some reason.

Re: Indenting "HTML" S-expressions

Ahh, now I see your point. It automatically aligns nested tags under the first attribute of it's parent, instead of just indenting by a single column. I'll see if there's a way to turn that off.

Re: Indenting "HTML" S-expressions

Good. :-) I'm sorry about my first example; I tried to keep it simple, but appearently overshot quite a bit...

Re: Indenting "HTML" S-expressions

Assuming you use common-lisp-indentation, look at edit slime-cl-indent.el

you could add rules for :div, :a, etc

(:div (&body)) ;; indents by 2 spaces (uses variable lisp-body-indent)
or
(:div (&whole 1)) ;; indents by 1 space

(:a (as :div)) ;; :a uses :div definition

one way of doing this without edit slime-cl-indent.el would be
add commands like
(put :div 'common-lisp-indent-function '(:div (&body)))
to your .emacs