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

查看:24
本文介绍了Emacs/Swank/Paredit for 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. 自动缩进 Lisp/Clojure 风格,而非 C++/Java 风格
  3. 语法高亮
  4. 调用 REPL
  5. 能够将文件中的部分代码加载到 REPL 中并对其进行评估.

如果我也能在 Emacs 上进行设置后获得获取这些东西的命令列表,那就太好了.

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-mode、Paredit当然,首先是 Clojure 罐子,然后也许还有一些额外的东西,其中 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-mode,以及一个名为 Emacs Starter Kit 的包,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.

设置 Clojure、Incanter、Emacs、Slime、Swank、和 Paredit 发表在 Incanter 项目的博客上.Incanter 是一个迷人的包,它为嵌入到 Clojure 中的统计计算提供了类似 R 的 DSL.即使您不打算使用 - 甚至不打算安装 - Incanter,这篇文章也会很有用.

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(这是事物的默认状态).后一个选项有一些额外的吸引力,因为您仍然可以将最新的 SLIME 与 Common Lisp 一起使用,以防万一您也使用它.

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.

查看 paredit 的文档.有两种方法可以解决这个问题:(1) 查看源文件——文件顶部有大量注释,其中包含您可能需要的所有信息;(2) 在 paredit-mode 处于活动状态时,在 Emacs 中键入 Ch m -- 将弹出一个缓冲区,其中包含有关当前主要模式的信息,然后是有关所有活动次要模式的信息(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).

更新:我刚刚发现了这组很酷的笔记 Phil Hagelberg 在 Paredit 上...这是一个文本文件的链接,我记得在某处看到过一组很好的包含此信息的幻灯片,但现在似乎找不到了.不管怎样,这是一个很好的总结它是如何工作的.绝对看看它,我现在不能没有 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 上活动的键绑定,在 clojure 模式下(你会想记住 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 组合编译当前缓冲区,然后 userequire 它在 REPL 中的命名空间.接下来,开始实验.

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 函数(如下所示)从内部开始整个事情埃马克.我已尽力使后者产生一个与 lein swank 提供的环境非常匹配的环境.哦,如果您只想在 Emacs 中使用 REPL 进行快速而肮脏的实验,那么通过正确的设置,您应该可以直接使用 M-x 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 for Clojure 的温和教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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