hamacs/ha-programming-clojure.org
Howard Abrams fd9db946a5 Initial Clojure Integration
This has been ported to my system for years, and much of what I had I
don't really need anymore.
2022-08-24 14:37:29 -07:00

5.7 KiB
Raw Blame History

Clojure Programming

A literate programming file for programming in Clojure.

I like Clojure as a modern Lisp, but I dont get to play with it much. The following instructions create a fully blinged-out Emacs-Clojure setup.

Introduction

To get Clojure working on a new system, try the following on a Mac:

  brew install clojure/tools/clojure

And make sure it works:

  clojure --help

Or by starting a REPL:

  clj

Also, download this script:

  curl -o ~/bin/lein https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein
  chmod u+x ~/bin/lein

Then for each project, create the project directory with this command:

  lein new app fresh-app

Emacs Support

We begin with using clojure-mode:

  (use-package clojure-mode
    :init
    (add-to-list 'org-babel-load-languages '(clojure . t))

    :config
    (defun ha-prettify-clojure ()
      "Make the Clojure syntax prettier."
      (push '("fn"   . ?𝝀) prettify-symbols-alist)
      (push '("__"   . ?⁈) prettify-symbols-alist)
      (prettify-symbols-mode))

    :hook (clojure-mode . ha-prettify-clojure))

Need the IDE feature of Cider:

  (use-package cider
    :commands (cider cider-connect cider-jack-in)
    :init
    (setq cider-auto-select-error-buffer nil
          cider-repl-pop-to-buffer-on-connect nil
          cider-repl-use-clojure-font-lock t
          cider-repl-wrap-history t
          cider-repl-history-size 1000
          cider-show-error-buffer t
          nrepl-hide-special-buffers t
          ;; Stop error buffer from popping up while working in buffers other than the REPL:
          nrepl-popup-stacktraces nil))

Read the entire CIDER manual.

Linting

Using Eastwood with the Squiggly Clojure project to add lint warnings to Flycheck:

(use-package flycheck-clojure
  :after flycheck
  :config
  (flycheck-clojure-setup))

Snippets

For clojure-specific templates for yasnippets, clone David Nolen's clojure-snippets repository into my snippets directory:

  git clone http://github.com/swannodette/clojure-snippets ~/.emacs/snippets/clojure-mode

Or install it as a package:

(use-package clojure-snippets)

Refactoring

The clj-refactor project:

(use-package clj-refactor
  :hook
  (clojure-mode . clj-refactor-mode)
  :config
  ;; Configure the Clojure Refactoring prefix:
  (cljr-add-keybindings-with-prefix "C-c .")
  :diminish clj-refactor-mode)

The advanced refactorings require the refactor-nrepl middleware, which should explain why we added the refactor-nrepl to the :plugins section in the ~/.lein/profiles.clj file (see below).

The real problem is trying to remember all the refactoring options. Remember: C-c . h h

Org Babel

And of course, we want to put this with org blocks:

  (use-package ob-clojure
    :straight (:type built-in)
    :custom
    (org-babel-clojure-backend 'cider)
    :config
    (add-to-list 'org-babel-load-languages '(clojure . t)))

Does this now work?

  (format "The answer is %d" (+ (* 4 10) 2))

"The answer is 42"

LSP, a WIP

Check out the goodies in this essay for hooking Clojure to LSP. Havent done this yet.

  (add-hook 'clojure-mode-hook 'lsp)
  (add-hook 'clojurescript-mode-hook 'lsp)
  (add-hook 'clojurec-mode-hook 'lsp)