Clojure是否有彩色REPL? [英] Is there a colored REPL for Clojure?

查看:146
本文介绍了Clojure是否有彩色REPL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为clojure代码得到一个有颜色的REPL,类似于你可以用IRB for Ruby做的。



有用户的库或设置



解决方案

我不知道方法有基本的Clojure REPL,由 java -cp clojure.jar clojure.main 开始,执行语法高亮。但是,如果你使用Emacs& SLIME(Clojure社区很大一部分选择的开发环境!),那么你可以使用SLIME REPL高亮语法,如 clojure-mode

首先,你必须从 clojure-mode 函数中取出一些代码(定义在<$ c $的顶部) c> clojure-mode.el ):

  ;;;这个函数中的所有代码都从clojure-mode函数中提取
;;;从clojure-mode.el
(defun clojure-font-lock-setup()
(交互式)
(make-local-variable'lisp-indent-function)
'clojure-indent-function)
(set(make-local-variable'lisp-doc-string-elt-property)
'clojure-doc-string-elt)
(make-local-variable'font-lock-multiline)t)

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

(当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- 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));语法alist
nil
(font-lock-mark-block-function。mark-defun)
(font-lock-syntax-face-function
。lisp-font-lock -syntactic-face-function))))

然后将它添加到 slime-repl-mode-hook

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

Etvoilà,下次连接SLIME REPL时,您将拥有 clojure-模式语法高亮显示。如果你对Common Lisp也使用SLIME,你需要调整它,所以它不会试图用CL做Clojure高亮显示。此外,这只是第一个近似;一个事情,它的类型的突破是提示突出显示(命名空间> 的东西将不再突出显示)。我不是一个熟练的 font-lock 黑客通过任何想象力,所以我会离开它。 : - )


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:

解决方案

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

这篇关于Clojure是否有彩色REPL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆