Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
648 views
in Technique[技术] by (71.8m points)

colors - Is there a colored REPL for Clojure?

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

question from:https://stackoverflow.com/questions/2474804/is-there-a-colored-repl-for-clojure

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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. :-)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...