Skip to content

Instantly share code, notes, and snippets.

@kingtim
Created December 21, 2012 00:39
Show Gist options
  • Save kingtim/4349847 to your computer and use it in GitHub Desktop.
Save kingtim/4349847 to your computer and use it in GitHub Desktop.
Some random clojure/nrepl emacs lisp that I hacked together today which could be useful.
;(define-key clojure-mode-map (kbd "C-c ;") 'nrepl-insert-balanced-comments)
;(define-key clojure-mode-map (kbd "C-c M-;") 'nrepl-remove-balanced-comments)
; inspired by http://bc.tech.coop/blog/070122.html
; ported from slime/contrib/slime-editing-commands.el
(defun nrepl-insert-reader-comment (prefix)
"Insert a reader comment (#_) around the s-expression containing the point.
If this command is invoked repeatedly (without any other command
occurring between invocations), the comment progressively moves outward
over enclosing expressions. If invoked with a positive prefix argument,
the s-expression prefix expressions out is enclosed in a set of balanced comments."
(interactive "*p")
(save-excursion
(when (eq last-command this-command)
(when (search-backward "#_" nil t)
(delete-char 2)))
(while (> prefix 0)
(backward-char 1)
(cond ((looking-at ")") (incf prefix))
((looking-at "(") (decf prefix))))
(insert "#_")))
(defun nrepl-remove-reader-comment ()
"Remove a set of balanced comments enclosing point."
(interactive "*")
(save-excursion
(when (search-backward "#_" nil t)
(delete-char 2))))
;(define-key nrepl-interaction-mode-map (kbd "<f7>") 'nrepl-send-to-repl)
; inspired by http://bc.tech.coop/blog/070424.html
(defun nrepl-send-to-repl (arg)
"Send the appropriate forms to the repl to be evaluated."
(interactive "P")
(save-excursion
(cond
;; Region selected - evaluate region
((not (equal mark-active nil))
(copy-region-as-kill (mark) (point)))
;; At/before sexp - evaluate next sexp
((or (looking-at "\s(")
(save-excursion
(ignore-errors (forward-char 1))
(looking-at "\s(")))
(forward-list 1)
(let ((end (point))
(beg (save-excursion
(backward-list 1)
(point))))
(copy-region-as-kill beg end)))
;; At/after sexp - evaluate last sexp
((or (looking-at "\s)")
(save-excursion
(backward-char 1)
(looking-at "\s)")))
(if (looking-at "\s)")
(forward-char 1))
(let ((end (point))
(beg (save-excursion
(backward-list 1)
(point))))
(copy-region-as-kill beg end)))
;; Default - evaluate enclosing top-level sexp
(t (progn
(while (ignore-errors (progn
(backward-up-list)
t)))
(forward-list 1)
(let ((end (point))
(beg (save-excursion
(backward-list 1)
(point))))
(copy-region-as-kill beg end)))))
(set-buffer nrepl-nrepl-buffer)
(unless (eq (current-buffer) (window-buffer))
(pop-to-buffer (current-buffer) t))
(goto-char (point-max))
(yank)
(if arg (progn
(nrepl-return)
(other-window 1)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment