HOME

Clojure Customizations

DONE Clojure + Cider Keybindings of Interest

Key-Chord Command
C-c C-x Reload the current namespace
C-c , Run the current namespace's tests
C-c M-, Run one test
C-c C-, Re-run last test
C-c C-e Eval and print last s-exp

EXPERIMENTAL Clojure prettify-symbols

  • State "EXPERIMENTAL" from [2017-08-07 Mon 11:42]
(defvar toby/clojure-prettify-alist '())

;; (setq toby/clojure-prettify-alist '())
(add-to-list 'toby/clojure-prettify-alist '("<="  . ((Br . Bl) ?≤)))
(add-to-list 'toby/clojure-prettify-alist '(">="  . ((Br . Bl) ?≥)))
(add-to-list 'toby/clojure-prettify-alist '("fn"  . 955))
(add-to-list 'toby/clojure-prettify-alist '("->"  . 10230))
(add-to-list 'toby/clojure-prettify-alist '("->>" . 10233))

;; (add-to-list 'toby/clojure-prettify-alist
;;              '("->>" .  (?\s (Br . Bl) ?\s (Br . Bl) ?\s
;;                              (Bl . Bl) ?- (Bc . Br) ?- (Bc . Bc) ?>
;;                              (Bc . Bl) ?- (Br . Br) ?>)))

(eval-after-load 'clojure-mode
  '(setq clojure--prettify-symbols-alist
         (append toby/clojure-prettify-alist
                 clojure--prettify-symbols-alist)))

(eval-after-load 'lisp-mode
  '(setq lisp--prettify-symbols-alist
         (append toby/clojure-prettify-alist
                 lisp--prettify-symbols-alist)))

Cider

Set-up some local key-bindings. This will pull in clojure-mode as well.

(require-package 'clojure-mode (version-to-list "5.6.1"))
(autoload 'clojure-mode "clojure-mode" "Major mode for clojure")
(autoload 'cider-mode "cider-mode" "Minor mode for live clojure development")

(defun toby/cider-onload ()
  (require-package 'cider-eval-sexp-fu (version-to-list "1.1"))
  (require 'cider-eval-sexp-fu)
  (define-key cider-mode-map (kbd "<f3>")    'cider-eval-defun-at-point)
  (define-key cider-mode-map (kbd "<f5>")    'toby/save-and-eval-buffer)
  (define-key cider-mode-map (kbd "<f12>")   'cider-test-run-tests)
  (define-key cider-mode-map (kbd "C-c C-e") 'cider-eval-print-last-sexp)
  (setq cider-mode-line '(:eval "🍺")))

(eval-after-load 'cider #'toby/cider-onload)

(defun toby/cider-init ()
  (eldoc-mode 1)
  (toby/activate-structured-editing))
(add-hook 'cider-mode-hook #'toby/cider-init)

(defun toby/clj-repl-init ()
  (subword-mode 1)
  (toby/activate-structured-editing))

(add-hook 'cider-repl-mode-hook #'toby/clj-repl-init)

(defun toby/save-and-eval-buffer ()
  "Save open buffers and send the current buffer to the active
   Cider session."
  (interactive)
  (save-some-buffers 't)
  (cider-eval-buffer))

ClojureScript

Enable CIDER-repl connections via figwheel:

(setq cider-cljs-lein-repl
      "(do (require 'figwheel-sidecar.repl-api)
           (figwheel-sidecar.repl-api/start-figwheel!)
           (figwheel-sidecar.repl-api/cljs-repl))")

Clojure

ADOPTED Clojure Refactorings

  • State "ADOPTED" from [2018-02-05 Mon 10:09]
(require-package 'clj-refactor (version-to-list "2.3.1"))
(autoload 'clj-refactor "clj-refactor-mode")

Set-up General Lisp Key-Bindings

  • TODO Consider use of (current-local-map)
    (require-package 'god-mode)
    
    (defun toby/insert-comma ()
      "Insert commas in lisp environments where its binding may have
      been overridden (given that `,` is considered white-space in
      many lisps, its use can be more convenient elsewhere)"
      (interactive)
      (insert ",")
      (god-local-mode -1))
    
    (defun toby/lisp-keys (kmap)
      (require 'god-mode)
      ;; Consider use of (current-local-map)
      (define-key kmap (kbd ",")   'god-local-mode)
      (global-set-key  (kbd "C-,") 'toby/insert-comma)
      (define-key god-local-mode-map  (kbd "C-,") 'toby/insert-comma))
    

Clojure Init

(defun toby/clojure-init ()
  (toby/activate-structured-editing)

  (cider-mode 1)
  (clj-refactor-mode 1)
  (yas-minor-mode 1)

  (cljr-add-keybindings-with-prefix "C-c c")

  (toby/lisp-keys (current-local-map))

  ;; Compojure Macros:
  (define-clojure-indent
    (defroutes 'defun)
    (defspec 'defun)
    (GET 2)
    (POST 2)
    (PUT 2)
    (DELETE 2)
    (HEAD 2)
    (ANY 2)
    (context 2)))

(add-hook 'clojure-mode-hook #'toby/clojure-init)

Clojuredocs

(defconst clojuredocs-url "http://clojuredocs.org/")

(defun clojuredocs-url (ns name)
  "Generate a clojuredocs search"
  (let ((base-url clojuredocs-url))
    (when (and name ns)
      (concat base-url ns "/" name))))

(defun toby/clojuredocs-lookup (symbol)
  "Open the clojuredocs.org documentation for SYMBOL"
  (if-let ((var-info (cider-var-info symbol)))
      (let ((name (nrepl-dict-get var-info "name"))
            (ns (nrepl-dict-get var-info "ns")))
        (browse-url (clojuredocs-url ns name)))
    (error "Symbol %s not resolved" symbol)))

(defun toby/clojuredocs-web (&optional arg)
  "Open clojuredocs.org in the default web browser.

   Prompts for the symbol to look-up, or uses the symbol at point."
  (interactive "P")
  (funcall (cider-prompt-for-symbol-function arg)
           "Clojuredoc for"
           #'toby/clojuredocs-lookup))

On-Load

   (defun toby/clojure-onload ()
  (require-package 'cider (version-to-list "0.15.1"))
  (define-key clojure-mode-map (kbd "<C-down>") 'forward-sexp)
  (define-key clojure-mode-map (kbd "<C-up>")   'backward-sexp)
  (define-key clojure-mode-map (kbd "C-c C-d o") 'toby/clojuredocs-web))

(eval-after-load 'clojure-mode #'toby/clojure-onload)

Author: Toby Tripp

Created: 2018-02-09 Fri 15:33

Validate