camelCase and Source Code Formatting in Emacs Lisp

By Xah Lee. Date:

This page discuss the use of camelCase in emacs lisp source code, and some code formatting issues.

here's a sample emacs lisp code using camelCase:

(defun delete-current-file ()
  "Delete the file associated with the current buffer."
  (interactive)
  (let (currentFile)
    (setq currentFile (buffer-file-name))
    (when (yes-or-no-p (concat "Delete file: " currentFile))
      (kill-buffer (current-buffer))
      (delete-file currentFile)
      (message (concat "Deleted file: " currentFile))
      ) ) )

Thomas Munro wrote:

Why use CamelCase

I find that using camelCase is a good way to distinguish my own symbol names from built-in ones.

Especially because emacs's emacs-lisp-mode's syntax coloring is flawed in that it only color a small percentage of built-in keywords, rather against its own conventions in syntax coloring. For detail, see: Emacs Lisp Mode Syntax Coloring Problem .

Using “setq” vs Nested “let” Form

In emacs lisp code, there can be 2 ways to set a local variable. Here's one way:

(let (x y)
  (setq x 3)
  (setq y 4)
  (message "%d %d" x y)
  )

here's another way:

(let ((x 3) (y 4))
  (message "%d %d" x y)
  )

Why use SETQ if you don't have to? How about this:

(let ((current-file (buffer-file-name)))
  ;;   )

I find that this form:

(let ((‹var1› ‹val1›) (‹var2› ‹val2›) …) ‹body›)

is harder to read, especially when not all your vars are not constants or require a lot lisp code to define, for example:

(let (var1 (var2 (…)) var3 (var4 (…))…)
 (setq var1 …)
  …
 (setq var3 …)
 …)

where many of the values are themselves compound expressions with many nested parens.

I think it is a good recommendation that one should always use:

(let (var1 var2 var3 …)
 (setq var1 …)
 (setq var2 …)
 …
 body
)

and only use the (‹var› ‹val›) form if all the vars are just local constants.

For hanging parenthesis, see: Xah Emacs Tutorial Criticisms: Emacs Lisp, Coding Style, Language Idioms, Controversy .