How do I beautify lisp source code?
Asked Answered
F

3

10

My code is a mess many long lines in this language like the following

(defn check-if-installed[x] (:exit(sh "sh" "-c" (str "command -v " x " >/dev/null 2>&1 || { echo >&2 \"\"; exit 1; }"))))

or

(def Open-Action (action :handler (fn [e] (choose-file :type :open :selection-mode :files-only :dir ListDir :success-fn (fn [fc file](setup-list file)))) :name "Open" :key "menu O" :tip "Open spelling list"))

which is terrible. I would like to format it like so

(if (= a something)
    (if (= b otherthing)
        (foo)))

How can I beautify the source code in a better way?

Fremantle answered 29/8, 2012 at 7:22 Comment(2)
Emacs can do this for you. See this document as a good starting point: ergoemacs.org/emacs/lisp_formatter.htmlBereave
La Clojure (IntelliJ Idea plugin for Clojure) has a code formatting feature.Urethra
M
2

Now you can do it with Srefactor package.

Some demos:

Available Commands:

  • srefactor-lisp-format-buffer: format whole buffer
  • srefactor-lisp-format-defun: format current defun cursor is in
  • srefactor-lisp-format-sexp: format the current sexp cursor is in.
  • srefactor-lisp-one-line: turn the current sexp of the same level into one line; with prefix argument, recursively turn all inner sexps into one line.

Scheme variants are not as polished as Emacs Lisp and Common Lisp yet but work for simple and small sexp. If there is any problem, please submit an issue report and I will be happy to fix it.

Ment answered 8/4, 2015 at 4:39 Comment(0)
S
5

The real answer hinges on whether you're willing to insert the newlines yourself. Many systems can indent the lines for you in an idiomatic way, once you've broken it up into lines.

If you don't want to insert them manually, Racket provides a "pretty-print" that does some of what you want:

#lang racket

(require racket/pretty)

(parameterize ([pretty-print-columns 20])
  (pretty-print '(b aosentuh onethunoteh (nte huna) oehnatoe unathoe)))

==>

'(b
  aosentuh
  onethunoteh
  (nte huna)
  oehnatoe
  unathoe)

... but I'd be the first to admit that inserting newlines in the right places is hard, because the choice of line breaks has a lot to do with how you want people to read your code.

Scout answered 29/8, 2012 at 15:57 Comment(1)
+1 for 'inserting newlines in the right places is hard, because the choice of line breaks has a lot to do with how you want people to read your code'Curd
F
3

I use Clojure.pprint often for making generated code more palatable to humans. it works well for reporting thought it is targeted at producing text. The formatting built into the clojure-mode emacs package produces very nicely formatted Clojure if you put the newlines in your self.

Factotum answered 29/8, 2012 at 8:8 Comment(0)
M
2

Now you can do it with Srefactor package.

Some demos:

Available Commands:

  • srefactor-lisp-format-buffer: format whole buffer
  • srefactor-lisp-format-defun: format current defun cursor is in
  • srefactor-lisp-format-sexp: format the current sexp cursor is in.
  • srefactor-lisp-one-line: turn the current sexp of the same level into one line; with prefix argument, recursively turn all inner sexps into one line.

Scheme variants are not as polished as Emacs Lisp and Common Lisp yet but work for simple and small sexp. If there is any problem, please submit an issue report and I will be happy to fix it.

Ment answered 8/4, 2015 at 4:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.