一个温柔的教程Emacs / Swank / Paredit的Clojure [英] A gentle tutorial to Emacs/Swank/Paredit for Clojure

查看:110
本文介绍了一个温柔的教程Emacs / Swank / Paredit的Clojure的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在移至Emacs,以处理 Clojure / Lisp。
在Emacs上需要设置的所有信息是否能够执行以下操作?

I am moving to Emacs to work on Clojure/Lisp. What is all the information I need to setup on Emacs to be able to do the following?


  1. 自动匹配/生成对应的关闭括号

  2. autoindent Lisp / Clojure风格,不是C ++ / Java风格

  3. 语法高亮显示

  4. REPL

  5. 能够将一部分代码从文件中加载到REPL中并进行评估。

  1. automatic matching/generation of corresponding closing brackets
  2. autoindent Lisp/Clojure style, not C++/Java style
  3. Syntax highlighting
  4. Invoking REPL
  5. To be able to load a part of code from file into the REPL and evaluate it.

如果我也可以获取命令列表

It would be great if I could also get the list of commands to get these things after setting things up on Emacs.

推荐答案

[从非作者编辑:这是从2010年,过程自2011年5月以来已大大简化。我将在2012年2月之前在我的设置注释中添加一条帖子。]



几个部分:Emacs,SLIME(与Clojure非常相似 - 见swank-clojure),swank-clojure(SLIME的服务器对应的Clojure实现),clojure模式,Paredit和Clojure jar开始,然后也许一些额外的Leiningen也许是最显着的。 设置完毕后,您将在Emacs中提供问题中提及的所有工作流程/编辑功能。

以下是教程中描述如何设置所有这些;网络上有更多,但其他一些是非常过时,而这两个似乎是现在确定:

The following are to great tutorials which describe how to set all of this up; there's more on the Web, but some of the others are quite outdated, whereas these two seem to be ok for now:


  1. 在Phil Hagelberg的博客上发表的关于clojure作者的贸易的技巧; Phil维持swank-clojure和clojure模式,以及一个名为Emacs入门工具包的包,这是任何新的Emacs世界的人都会建议看看。这些指示似乎已经更新到基础设施的最近变化;如有疑问,请寻找Clojure的Google群组的其他资讯。

  1. in which are found tricks of the trade concerning clojure authorship post on Phil Hagelberg's blog; Phil maintains swank-clojure and clojure-mode, as well as a package called the Emacs Starter Kit which is something any newcomer to the Emacs world would be well-advised to have a look at. These instructions seem to have been brought up to date with recent changes to the infrastructure; in case of doubt, look for additional information on Clojure's Google group.

在Incanter项目的博客上设置Clojure,Incanter,Emacs,Slime,Swank和Paredit 。 Incanter是一个迷人的软件包,为嵌入到Clojure中的统计计算提供了一个类似R的DSL。

Setting up Clojure, Incanter, Emacs, Slime, Swank, and Paredit post on the blog of the Incanter project. Incanter is a fascinating package providing an R-like DSL for statistical computations embedded right into Clojure. This post will be useful even if you don't plan on using -- or even installing -- Incanter.



这个帖子将是有用的,即使你不打算使用 - 将其全部投入使用:



一旦设置完所有这些内容,您就可以尝试立即开始使用,但我强烈建议您执行以下操作:

Putting it all to work:

Once you set up all of this stuff, you could try and start using it right away, but I would strongly advise you to do the following:


  1. 查看SLIME的手册 - 它包含在源代码中,实际上很容易阅读。此外,绝对没有理由你应该阅读整个50页的怪物手册;

  1. Have a look at SLIME's manual -- it's included in the sources and is actually very readable. Also, there's absolutely no reason why you should read the whole 50-page monster manual; just have a look around to see what features are available.

注意:在最新的上游资源中找到的SLIME的autodoc功能与swank-clojure不兼容 - 如果您按照Phil Hagelberg的建议使用ELPA版本(请参阅他上述的博客文章解释)或只是关闭autodoc(这是默认的状态),这个问题不会出现。

Note: the autodoc feature of SLIME as found in the latest upstream sources is incompatible with swank-clojure -- this problem won't come up if you follow Phil Hagelberg's recommendation to use the ELPA version (see his aforementioned blog post for an explanation) or simply leave autodoc off (which is the default state of things). The latter option has some added appeal in that you can still use the latest SLIME with Common Lisp, in case you use that as well.

查看一下这个选项,你可以在Common Lisp中使用最新的SLIME。 paredit的文档。有两种方法可以做到这一点:(1)看看源 - 在文件的顶部有大量的注释,其中包含您可能需要的所有信息; (2)在Emacs中键入 Ch m ,而paredit模式是活动的 - 缓冲区将弹出有关当前主模式的信息,后面是所有活动次模式的信息(paredit是其中之一) 。

Have a look at the docs for paredit. There are two ways to go about this: (1) look at the source -- there's a huge amount of comments at the top of the file which contain all the information you're likely to need; (2) type C-h m in Emacs while paredit-mode is active -- a buffer will pop up with information on the current major mode followed by information on all active minor modes (paredit is one of those).

更新:我刚刚发现这个很酷的笔记在Paredit通过Phil Hagelberg ...这是一个文本文件的链接,我记得看到一个很好的幻灯片这个信息的地方,但似乎不能现在找到它。无论如何,它是一个很好的总结如何工作。绝对看看它,我不能生活没有Paredit现在和这个文件应该使它很容易开始使用它,我相信。 : - )

Update: I've just found this cool set of notes on Paredit by Phil Hagelberg... That's a link to a text file, I remember seeing a nice set of slides with this information somewhere, but can't seem to find it now. Anyway, it is a nice summary of how it works. Definitely take a look at it, I can't live without Paredit now and this file should make it very easy to start using it, I believe. :-)

实际上, Ch m 组合会告诉你关于SLIME REPL处于活动状态的所有键绑定,模式(您会想要记住 Cc Ck ,以便将当前缓冲区关闭以进行编译),并确保在任何Emacs缓冲区中。

In fact, the C-h m combination will tell you about all keybindings active at the SLIME REPL, in clojure-mode (you'll want to remember C-c C-k for sending the current buffer off for compilation) and indeed in any Emacs buffer.

对于从文件加载代码,然后在REPL上进行试验:使用上述 Cc Ck 组合来编译当前缓冲区,然后在REPL中使用 require 命名空间。

As for loading the code from a file and then experimenting with it at the REPL: use the aforementioned C-c C-k combination to compile the current buffer, then use or require its namespace at the REPL. Next, experiment away.

准备在一段时间之前调整一下所有点击。有很多工具涉及,他们的互动大多是相当顺利,但不是到那一点,可以安全地假设你不必在最初进行一些调整。

Be prepared to have to tweak things for a while before it all clicks. There's a lot of tools involved and their interactions are mostly fairly smooth, but not to the point where it would be safe to assume you won't have to make some adjustments initially.

最后,这里有一些代码,我保存在 .emacs ,你不会在其他地方找到(虽然它是基于一个很酷的功能Phil Hagelberg)。我在使用 lein swank (Leiningen的更酷的功能之一)和使用 clojure-project 函数如下所示从Emacs中启动整个事情。我已尽力使后者产生与 lein swank 提供的环境紧密匹配的环境。哦,如果你只是想要一个REPL在Emacs的一个快速和肮脏的实验,然后正确的设置,你应该能够使用 Mx slime 直接。

Finally, here's a bit of code I keep in .emacs which you won't find elsewhere (although it's based on a cool function by Phil Hagelberg). I alternate between starting my swank instances with lein swank (one of the cooler features of Leiningen) and using the clojure-project function as found below to start the whole thing from within Emacs. I've done my best to make the latter produce an environment closely matching that provided by lein swank. Oh, and if you just want a REPL in Emacs for a quick and dirty experiment, then with the correct setup you should be able to use M-x slime directly.

(setq clojure-project-extra-classpaths
      '(
        ; "deps/"
        "src/"
        "classes/"
        "test/"
        ))

(setq clojure-project-jar-classpaths
      '(
        ; "deps/"
        "lib/"
        ))

(defun find-clojure-project-jars (path)
  (apply #'append
         (mapcar (lambda (d)
                   (loop for jar in (remove-if (lambda (f) (member f '("." "..")))
                                               (directory-files d t))
                         collect jar into jars
                         finally return jars))
                 (remove-if-not #'file-exists-p
                                clojure-project-jar-classpaths))))

(defun find-clojure-jar (jars)
  (let ((candidates
         (remove-if-not
          (lambda (jar)
            (string-match-p "clojure\\([0-9.-]+\\(SNAPSHOT|MASTER\\)?\\)?\\.jar$" jar))
          jars)))
    (if candidates
        (car candidates)
      (expand-file-name "~/.clojure/clojure.jar"))))

(defun find-clojure-contrib-jar (jars)
  (let ((candidates
         (remove-if-not
          (lambda (jar)
            (string-match-p "clojure-contrib\\([0-9.-]+\\(SNAPSHOT|MASTER\\)?\\)?\\.jar$" jar))
          jars)))
    (if candidates
        (car candidates)
      (expand-file-name "~/.clojure/clojure-contrib.jar"))))

;;; original due to Phil Hagelberg
;;; (see `Best practices for Slime with Clojure' thread on Clojure Google Group)
(defun clojure-project (path)
  "Sets up classpaths for a clojure project and starts a new SLIME session.

   Kills existing SLIME session, if any."
  (interactive (list (ido-read-directory-name
                      "Project root:"
                      (locate-dominating-file default-directory "pom.xml"))))
  (when (get-buffer "*inferior-lisp*")
    (kill-buffer "*inferior-lisp*"))
  (cd path)
  ;; I'm not sure if I want to mkdir; doing that would be a problem
  ;; if I wanted to open e.g. clojure or clojure-contrib as a project
  ;; (both lack "deps/")
                                        ; (mapcar (lambda (d) (mkdir d t)) '("deps" "src" "classes" "test"))
  (let* ((jars (find-clojure-project-jars path))
         (clojure-jar (find-clojure-jar jars))
         (clojure-contrib-jar (find-clojure-contrib-jar jars)))
    (setq swank-clojure-binary nil
          ;; swank-clojure-jar-path (expand-file-name "~/.clojure/clojure.jar")
          swank-clojure-jar-path clojure-jar
          swank-clojure-extra-classpaths
          (cons clojure-contrib-jar
                (append (mapcar (lambda (d) (expand-file-name d path))
                                clojure-project-extra-classpaths)
                        (find-clojure-project-jars path)))
          swank-clojure-extra-vm-args
          (list (format "-Dclojure.compile.path=%s"
                        (expand-file-name "classes/" path)))
          slime-lisp-implementations
          (cons `(clojure ,(swank-clojure-cmd) :init swank-clojure-init)
                (remove-if #'(lambda (x) (eq (car x) 'clojure))
                           slime-lisp-implementations))))
  (slime))

这篇关于一个温柔的教程Emacs / Swank / Paredit的Clojure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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