Skip to content

Instantly share code, notes, and snippets.

@martintrojer
Last active January 9, 2021 16:12
Show Gist options
  • Save martintrojer/14ebb9b2a51b8e53a6e5 to your computer and use it in GitHub Desktop.
Save martintrojer/14ebb9b2a51b8e53a6e5 to your computer and use it in GitHub Desktop.
init.el clojure bits
;; Clojure
(require 'clojure-mode)
(setq auto-mode-alist (cons '("\\.cljs$" . clojure-mode) auto-mode-alist))
(setq inferior-lisp-program "lein repl")
;; clj-refactor
(require 'clj-refactor)
(add-hook 'clojure-mode-hook (lambda ()
(clj-refactor-mode 1)
(cljr-add-keybindings-with-prefix "C-c C-o")))
;; align-cljlet
(require 'align-cljlet)
(global-set-key (kbd "C-c C-a") 'align-cljlet)
;; paredit
(require 'paredit)
(add-hook 'clojure-mode-hook 'paredit-mode)
;; projectile
(require 'projectile)
(add-hook 'clojure-mode-hook 'projectile-mode)
;; company-mode
(require 'company)
(add-hook 'after-init-hook 'global-company-mode)
(require 'company-etags)
(add-to-list 'company-etags-modes 'clojure-mode)
(defun get-clj-completions (prefix)
(let* ((proc (inferior-lisp-proc))
(comint-filt (process-filter proc))
(kept ""))
(set-process-filter proc (lambda (proc string) (setq kept (concat kept string))))
(process-send-string proc (format "(complete.core/completions \"%s\")\n"
(substring-no-properties prefix)))
(while (accept-process-output proc 0.1))
(setq completions (read kept))
(set-process-filter proc comint-filt)
completions))
(defun company-infclj (command &optional arg &rest ignored)
(interactive (list 'interactive))
(cl-case command
(interactive (company-begin-backend 'company-infclj))
(prefix (and (eq major-mode 'inferior-lisp-mode)
(company-grab-symbol)))
(candidates (get-clj-completions arg))))
(add-to-list 'company-backends 'company-infclj)
;; yasnippet
(require 'yasnippet)
(require 'clojure-snippets)
(yas-global-mode 1)
(add-to-list 'yas-snippet-dirs "~/.emacs.d/snippets")
(yas-load-directory "~/.emacs.d/snippets")
;; Some handly key bindings
(global-set-key (kbd "C-c C-s") 'clojure-toggle-keyword-string)
(defun reload-current-clj-ns ()
(interactive)
(let ((current-point (point)))
(goto-char (point-min))
(let ((ns-idx (re-search-forward clojure-namespace-name-regex nil t)))
(when ns-idx
(goto-char ns-idx)
(let ((sym (symbol-at-point)))
(message (format "Loading %s ..." sym))
(lisp-eval-string (format "(require '%s :reload)" sym))
(lisp-eval-string (format "(in-ns '%s)" sym)))))
(goto-char current-point)))
(defun find-tag-without-ns (next-p)
(interactive "P")
(find-tag (first (last (split-string (symbol-name (symbol-at-point)) "/")))
next-p))
(defun erase-inf-buffer ()
(interactive)
(erase-buffer)
(lisp-eval-string ""))
(add-hook 'clojure-mode-hook
'(lambda ()
(define-key clojure-mode-map "\C-c\C-k" 'reload-current-clj-ns)
(define-key clojure-mode-map "\M-." 'find-tag-without-ns)))
(add-hook 'inferior-lisp-mode-hook
'(lambda ()
(define-key inferior-lisp-mode-map "\C-cl" 'erase-inf-buffer)))
;; Clojure
(require 'clojure-mode)
(setq auto-mode-alist (cons '("\\.cljs$" . clojure-mode) auto-mode-alist))
(require 'inf-clojure)
(setq inf-clojure-prompt-read-only nil)
(add-hook 'inf-clojure-minor-mode-hook ;; prevent company-mode from freezing Emacs when the REPL is busy
(lambda () (setq completion-at-point-functions nil)))
(add-hook 'clojure-mode-hook 'inf-clojure-minor-mode)
(defun reload-current-clj-ns (next-p)
(interactive "P")
(let ((ns (clojure-find-ns)))
(message (format "Loading %s ..." ns))
(inf-clojure-eval-string (format "(require '%s :reload)" ns))
(when (not next-p) (inf-clojure-eval-string (format "(in-ns '%s)" ns)))))
(defun find-tag-without-ns (next-p)
(interactive "P")
(find-tag (first (last (split-string (symbol-name (symbol-at-point)) "/")))
next-p))
(defun erase-inf-buffer ()
(interactive)
(with-current-buffer (get-buffer "*inf-clojure*")
(erase-buffer))
(inf-clojure-eval-string ""))
(add-hook 'clojure-mode-hook
'(lambda ()
(define-key clojure-mode-map "\C-c\C-k" 'reload-current-clj-ns)
(define-key clojure-mode-map "\M-." 'find-tag-without-ns)
(define-key clojure-mode-map "\C-cl" 'erase-inf-buffer)
(define-key clojure-mode-map "\C-c\C-t" 'clojure-toggle-keyword-string)))
(add-hook 'inf-clojure-mode-hook
'(lambda ()
(define-key inf-clojure-mode-map "\C-cl" 'erase-inf-buffer)))
;; clj-refactor
(require 'clj-refactor)
(add-hook 'clojure-mode-hook (lambda ()
(clj-refactor-mode 1)
(cljr-add-keybindings-with-prefix "C-c C-o")))
;; align-cljlet
(require 'align-cljlet)
(add-hook 'clojure-mode-hook
'(lambda ()
(define-key clojure-mode-map "\C-c\C-y" 'align-cljlet)))
;; paredit
(require 'paredit)
(add-hook 'clojure-mode-hook 'paredit-mode)
;; projectile
(require 'projectile)
(add-hook 'clojure-mode-hook 'projectile-mode)
;; company-mode
(require 'company)
(require 'company-etags)
(add-to-list 'company-etags-modes 'clojure-mode)
(add-hook 'after-init-hook 'global-company-mode)
;; yasnippet
(require 'yasnippet)
(require 'clojure-snippets)
(yas-global-mode 1)
(add-to-list 'yas-snippet-dirs "~/.emacs.d/snippets")
(yas-load-directory "~/.emacs.d/snippets")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment