Is there a colored REPL for Clojure?
Asked Answered
I

11

39

I'd like to get a colored REPL for clojure code, similar to what you can do with IRB for Ruby.

Are there any libraries or settings for user.clj that provide automatic coloring of the REPL?

Example IRB:

alt text

Industrialist answered 19/3, 2010 at 3:25 Comment(3)
That's pretty! How can I get that for SLIME REPL?Trammell
@Trammell - see my answer below.Hussey
Similar question: What to do to make lein repl colorful?Zoogeography
W
17

I do not know of any way to have the basic Clojure REPL, as started by something like java -cp clojure.jar clojure.main, do syntax highlighting. If, however, you use Emacs & SLIME (the development environment of choice of a great part of the Clojure community!), then you can have the SLIME REPL highlight syntax like clojure-mode does.

First, you'll have to lift some code from the clojure-mode function (defined towards the top of clojure-mode.el):

;;; all code in this function lifted from the clojure-mode function
;;; from clojure-mode.el
(defun clojure-font-lock-setup ()
  (interactive)
  (set (make-local-variable 'lisp-indent-function)
       'clojure-indent-function)
  (set (make-local-variable 'lisp-doc-string-elt-property)
       'clojure-doc-string-elt)
  (set (make-local-variable 'font-lock-multiline) t)

  (add-to-list 'font-lock-extend-region-functions
               'clojure-font-lock-extend-region-def t)

  (when clojure-mode-font-lock-comment-sexp
    (add-to-list 'font-lock-extend-region-functions
                 'clojure-font-lock-extend-region-comment t)
    (make-local-variable 'clojure-font-lock-keywords)
    (add-to-list 'clojure-font-lock-keywords
                 'clojure-font-lock-mark-comment t)
    (set (make-local-variable 'open-paren-in-column-0-is-defun-start) nil))

  (setq font-lock-defaults
        '(clojure-font-lock-keywords    ; keywords
          nil nil
          (("+-*/.<>=!?$%_&~^:@" . "w")) ; syntax alist
          nil
          (font-lock-mark-block-function . mark-defun)
          (font-lock-syntactic-face-function
           . lisp-font-lock-syntactic-face-function))))

Then add it to the slime-repl-mode-hook:

(add-hook 'slime-repl-mode-hook
          (lambda ()
            (font-lock-mode nil)
            (clojure-font-lock-setup)
            (font-lock-mode t)))

Et voilà, next time you connect to the SLIME REPL you'll have clojure-mode syntax highlighting available. If you use SLIME for Common Lisp too, you'll want to tweak this so it doesn't try to do Clojure highlighting with CL. Also, this is just a first approximation; one thing it sort of breaks is prompt highlighting (the namespace> thing will not be highlighted anymore). I'm not a proficient font-lock hacker by any stretch of the imagination, though, so I'll leave it at that. :-)

Whaleback answered 19/3, 2010 at 5:54 Comment(5)
Oh, by the way, this really replaces SLIME REPL highlighting with clojure-mode highlighting... There are some useful things about the usual REPL highlighting which one might miss when using this. Hopefully at some point this can be refined to make this less of a trade-off.Eximious
Unfortunately I am not an Emacs user, but this does look cool.Industrialist
@thnetos: Thanks! @All whom this may concern: I've put the code in a Gist here: gist.github.com/337280 -- the current version includes two defadvice s to make the prompt and printouts on *out* be highlighted as they usually are in SLIME REPL, while everything else still gets clojure-mode highlights.Eximious
The most up-to-date version of this code is now part of Phil Hagelberg's new durendal project.Eximious
CIDER's REPL has built-in colouring support (although it doesn't work quite like this, as expressions get font-locked after being evaluated).Meningitis
L
28

Rather late to the party, here, but you can get this from using the Leiningen plugin Ultra (which also has support for colorized stacktraces and pretty-printed test output), or by adding Whidbey to your list of Leiningen plugins instead.

Sample Ultra REPL:enter image description here

Lapboard answered 5/2, 2015 at 0:14 Comment(0)
W
17

I do not know of any way to have the basic Clojure REPL, as started by something like java -cp clojure.jar clojure.main, do syntax highlighting. If, however, you use Emacs & SLIME (the development environment of choice of a great part of the Clojure community!), then you can have the SLIME REPL highlight syntax like clojure-mode does.

First, you'll have to lift some code from the clojure-mode function (defined towards the top of clojure-mode.el):

;;; all code in this function lifted from the clojure-mode function
;;; from clojure-mode.el
(defun clojure-font-lock-setup ()
  (interactive)
  (set (make-local-variable 'lisp-indent-function)
       'clojure-indent-function)
  (set (make-local-variable 'lisp-doc-string-elt-property)
       'clojure-doc-string-elt)
  (set (make-local-variable 'font-lock-multiline) t)

  (add-to-list 'font-lock-extend-region-functions
               'clojure-font-lock-extend-region-def t)

  (when clojure-mode-font-lock-comment-sexp
    (add-to-list 'font-lock-extend-region-functions
                 'clojure-font-lock-extend-region-comment t)
    (make-local-variable 'clojure-font-lock-keywords)
    (add-to-list 'clojure-font-lock-keywords
                 'clojure-font-lock-mark-comment t)
    (set (make-local-variable 'open-paren-in-column-0-is-defun-start) nil))

  (setq font-lock-defaults
        '(clojure-font-lock-keywords    ; keywords
          nil nil
          (("+-*/.<>=!?$%_&~^:@" . "w")) ; syntax alist
          nil
          (font-lock-mark-block-function . mark-defun)
          (font-lock-syntactic-face-function
           . lisp-font-lock-syntactic-face-function))))

Then add it to the slime-repl-mode-hook:

(add-hook 'slime-repl-mode-hook
          (lambda ()
            (font-lock-mode nil)
            (clojure-font-lock-setup)
            (font-lock-mode t)))

Et voilà, next time you connect to the SLIME REPL you'll have clojure-mode syntax highlighting available. If you use SLIME for Common Lisp too, you'll want to tweak this so it doesn't try to do Clojure highlighting with CL. Also, this is just a first approximation; one thing it sort of breaks is prompt highlighting (the namespace> thing will not be highlighted anymore). I'm not a proficient font-lock hacker by any stretch of the imagination, though, so I'll leave it at that. :-)

Whaleback answered 19/3, 2010 at 5:54 Comment(5)
Oh, by the way, this really replaces SLIME REPL highlighting with clojure-mode highlighting... There are some useful things about the usual REPL highlighting which one might miss when using this. Hopefully at some point this can be refined to make this less of a trade-off.Eximious
Unfortunately I am not an Emacs user, but this does look cool.Industrialist
@thnetos: Thanks! @All whom this may concern: I've put the code in a Gist here: gist.github.com/337280 -- the current version includes two defadvice s to make the prompt and printouts on *out* be highlighted as they usually are in SLIME REPL, while everything else still gets clojure-mode highlights.Eximious
The most up-to-date version of this code is now part of Phil Hagelberg's new durendal project.Eximious
CIDER's REPL has built-in colouring support (although it doesn't work quite like this, as expressions get font-locked after being evaluated).Meningitis
C
9

If you just want to color the prompt and you are using Leiningen (which you should), you can use :repl-options and ANSI escape sequences:

:repl-options {:prompt (fn [ns]
                         (str "\033[1;32m"
                              ns "=>"
                              "\033[0m "))}

References:

Coccid answered 13/5, 2013 at 15:18 Comment(3)
To make this apply to your user rather than a single project, add this to ~/.lein/profiles.cljSlyke
Put this in ~/.lein/profiles.clj for global colored repl: {:user {:repl-options {:prompt (fn [ns] (str "\033[1;32m" ns "=>" "\033[0m "))}}}Spacetime
When adding this to ~/.lein/profiles.clj, put the config within the :repl profile, instead of :user. So the structure is {:repl {:repl-options {:prompt …}}}. This ensures the config is only defined for lein repl, not for other subcommands like lein test.Zoogeography
H
5
  • Install Emacs 24
  • Install Emacs Starter Kit v2
  • M-x package-install -> starter-kit-lisp
  • Add to init.el: (add-hook 'slime-repl-mode-hook 'clojure-mode-font-lock-setup)
  • Install Swank for Clojure
  • Open your Clojure project and M-x clojure-jack-in

Emacs REPL

Hussey answered 29/9, 2011 at 20:6 Comment(1)
this should be updated to use github.com/kingtim/nrepl.el instead of swank-clojureCoccid
A
2

Some coloring have REPL in VimClojure.

Argil answered 19/3, 2010 at 5:1 Comment(0)
C
2

Try out Light Table Playground by Chris Granger. It is the first part of more ambitous multi language IDE.

It has a color Clojure REPL that does real time evaluations and display for entire blocks of code.

http://www.chris-granger.com/lighttable/

You can see a higher level view of the project here:

http://www.kickstarter.com/projects/ibdknox/light-table

Canthus answered 25/7, 2012 at 15:39 Comment(1)
Great project, but sadly inactive nowGuide
M
2

CIDER users can obtain a coloured REPL by adding the following to their config:

(setq cider-repl-use-clojure-font-lock t)
Meningitis answered 9/6, 2014 at 12:52 Comment(0)
R
1

The Eclipse Counterclockwise REPL provides full syntax colouring (including rainbow bracket colouring).

I believe it uses nREPL under the hood.

Reproof answered 14/7, 2011 at 12:48 Comment(1)
Yes it exactly uses nRepl but employs a custom coloring strategy that of course can be configured (contributor@here)Billups
C
1

If you're an Intellij user, the excellent Cursive is a great choice, and provides pretty colours that you can customise:

enter image description here

Camelliacamelopard answered 5/2, 2015 at 9:34 Comment(0)
E
0

To get your REPL output colorized try repl-color

enter image description here

Erective answered 30/8, 2014 at 21:53 Comment(0)
J
0

You can try LightTable, it lets you select the functions and run instantly.

Jocko answered 5/2, 2015 at 13:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.