庄周梦蝶

生活、程序、未来
   :: 首页 ::  ::  :: 聚合  :: 管理

Emacs + Clojure配置的几个Tip

Posted on 2012-05-19 00:57 dennis 阅读(15265) 评论(11)  编辑  收藏 所属分类: Clojure工具和命令

    很久没更新博客了,在北京工作,忙碌并且充实。目前来说,Clojure最好的开发编辑器应该是Emacs + Slime的组合,利用swank-clojure这个项目,加上clojure-mode,可以完美地运行slime。编译、运行、跳转、文档和引用查看甚至debug都可以搞定。具体配置恕不重复,看swank-clojure的文档即可自己安装起来,或者这篇中文博客windows上配置

    分享几个Tip,也期待大家分享你们的使用心得。

    首先是自动在打开clj后缀文件的时候启动执行clojure-jack-in与slime连接,可以在emacs配置里加上个callback:

(eval-after-load "clojure-mode"
  '(progn
     (require 'slime)
     (require 'clojure-mode)
     (unless (slime-connected-p)
       (save-excursion (clojure-jack-in)))))
    这样在打开clj为后缀的文件的时候,将自动启动clojure-mode执行clojure-jack-in函数并且连接slime。

    将clj后缀的文件自动关联到clojure-mode:
(setq auto-mode-alist (cons '("\\.clj$" . clojure-mode) auto-mode-alist))
    通常来说如果你是利用marmalade安装的,会自动关联的。

    另外,启动自动匹配括号、字符串引号等的paredit模式一定要启动:
(defun paredit-mode-enable () (paredit-mode 1))
(add-hook 'clojure-mode-hook 'paredit-mode-enable)
(add-hook 'clojure-test-mode-hook 'paredit-mode-enable)

   在使用clojure-mode或者clojure-test-mode的时候自动启用paredit模式,括号再也不是问题。括号匹配提示一般是开启的,如果没有,强制开启:

;;    显示括号匹配
(show-paren-mode t)
(setq show-paren-style 'parentheses)

    slime更多配置,启用IO重定向(多线程IO输出都定向到SLIME repl)以及设置通讯字符编码等:

(eval-after-load "slime"
  '(progn
     (slime-setup '(slime-repl slime-fuzzy))
     ;;(setq slime-truncate-lines t)
     (setq  swank:*globally-redirect-io*  t)
     ;; (setq slime-complete-symbol-function ' slime-fuzzy-complete-symbol)
     (setq slime-net-coding-system 'utf-8-unix)))

    细心的朋友可能注意到我注释了slime-fuzzy-complete的配置,这是一个支持更好的自动补全功能的SLIME插件(可以用缩写来自动补全),可惜在我机器上没有尝试配置成功,有兴趣你可以尝试下。

    在REPL里支持语法高亮,一定要配置上:

(add-hook 'slime-repl-mode-hook
          (defun clojure-mode-slime-font-lock ()
            (require 'clojure-mode)
            (let (font-lock-mode)
              (clojure-mode-font-lock-setup))))

    单独在clojure-mode(在其他mode里这些快捷键不会起作用)里配置快捷键可以这样:
(eval-after-load "clojure-mode"
  '(progn
     (require 'slime)
     (require 'clojure-mode)
     (define-key clojure-mode-map (kbd "M-/")  (quote slime-complete-symbol))
     (define-key clojure-mode-map (kbd "C-c s")  (quote slime-selector)))

   例如我这里将M-/作为自动补全的快捷键,因为meta键在我的Mac机器上设置为command键,因此自动补全的操作习惯就跟Eclipse类似。而slime-selector是一个非常有用的函数,用来跳转到slime的一系列buffer,因此我绑定了C-c s快捷键。

    额外一提,在Mac osx下,将command作为meta键:
;;; I prefer cmd key for meta
(setq mac-option-key-is-meta nil
      mac-command-key-is-meta t
      mac-command-modifier 'meta
      mac-option-modifier 'none)

    最后,期待大家不吝分享你的心得。
    

评论

# re: Emacs + Clojure配置的几个Tip  回复  更多评论   

2012-05-19 20:59 by 独辫子
收藏了,挺有用的

# re: Emacs + Clojure配置的几个Tip  回复  更多评论   

2012-05-21 08:16 by tony.li.fly@gmail.com
请问在linux上开发最好的编辑器是什么,如果是vim的话要装哪些插件?

# re: Emacs + Clojure配置的几个Tip[未登录]  回复  更多评论   

2012-05-21 15:15 by xhh
@tony.li.fly@gmail.com

https://github.com/vim-scripts/VimClojure

# re: Emacs + Clojure配置的几个Tip  回复  更多评论   

2012-05-21 23:19 by Unmi
vim 自然是比不过 Emacs,但是 Emacs 的学习曲线太陡了些,曾经想好好学下 Emacs,每次都退却了。最近在 Mac 下发现 TextMate 是个很好的东西,有很强大的 Bundle 支持,并且在 Windows 下也有对应品,即 e-editor,它和 TextMate 的 Bundle 是可以通用的。

# re: Emacs + Clojure配置的几个Tip  回复  更多评论   

2012-05-22 23:16 by dennis
@Unmi

TextMate不错,但是挺贵的,用盗版不合适。

# re: Emacs + Clojure配置的几个Tip  回复  更多评论   

2012-05-22 23:17 by dennis
@tony.li.fly@gmail.com
Emacs不是Mac os专利啊。

# Clojure新的开发工具Lightable  回复  更多评论   

2013-04-10 22:59 by John Wang
我在用Eclipse 的 Countclockwise 。感觉也不错。

另外 Clojure社区最近引起关注的是一款正在开发的IDE LightTable 。 我的关于clojure的新博客:
http://wangjinquan.me/show/Clojure%20%E7%9A%84%E7%BC%96%E8%AF%91%E5%92%8C%E6%89%A7%E8%A1%8C

# vitalcleansecomplete65113  回复  更多评论   

2015-02-16 01:33 by Write more, thats all I have to say. Literally, it
Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You definitely know what youre talking about, why waste your intelligence on just posting videos to your blog when you could be giving us something enlightening to read?

# mp3gain38105  回复  更多评论   

2016-02-08 14:55 by Good post. I learn something new and challenging o
Good post. I learn something new and challenging on blogs I stumbleupon on a daily basis. It's always exciting to read through articles from other authors and practice a little something from other websites.

只有注册用户登录后才能发表评论。


网站导航: