Search code examples
clojureschemecommon-lisp

How do I beautify lisp source code?


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?


Solution

  • 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.