I'm developing a Stack Exchange mode for Emacs and I'm trying to use literate programming (with org-mode) to organize the project.
I'm finding myself repeating a lot of information.
I'm taking a (possibly over-) structured approach to the document.
Say I have a heading Questions; under that heading exist subheadings Read and Write,
and I have a top-level of
;; some-package.el starts here
<<read methods>>
<<write methods>>
;; some-package.el ends here
How can I alter the properties of the Read and Write headings such that all source code blocks inside, unless otherwise specified, will be placed into their respective noweb tanglings?
Here is what I do currently:
* TODO Stack Mode (Entry Point): =stack-mode=
Stack mode is /the/ major mode. What do I mean by this? Stack mode
is the entry point of the whole package. There is no other way to
obtain the full, original functionality of the package without first
running =M-x stack-mode=. Stack Mode is the only mode available
interactively. It is a dispatcher that decides, based on user
preferences, how the whole system shall behave. It provides the basic
framework upon which the rest of the package is built, and makes sure
all tools are available.
#+name: build-stack-mode
#+begin_src emacs-lisp :tangle elisp/stack.el
;; stack.el starts here
(add-to-list 'load-path "~/github/vermiculus/stack-mode")
<<stack-require-dependencies>>
<<stack-setup-customization>>
<<stack-setup-keymap>>
<<stack-setup-api>>
<<stack-load-children>>
(provide 'stack)
; stack.el ends here
#+end_src
Thus, there are a few packages that it itself requires.
JSON (JavaScript Object Notation) is the standard by which we
communicate with Stack Exchange itself. The details of this
communication has [[id:DC2032C5-BC11-47E2-8DDB-34467C2BC479][already been discussed]] so I will not repeat myself
here. The JSON package provides many utilities for manipulating JSON
snippets within Emacs Lisp, and is required for the operation of this
package. =json.el= is included with Emacs 24+ (and can easily be
obtained from the ELPA if missing).
#+name: stack-require-dependencies
#+begin_src emacs-lisp
(require 'json)
#+end_src
This package also requires =request.el=, a package designed to
simplify making HTTP requests. =request.el= was written by [[http://stackoverflow.com/users/727827][SX#tkf]] and
is maintained and documented on [[http://tkf.github.com/emacs-request/manual.html][GitHub]]. The package is also available
for automatic install via MELPA.
#+name: stack-require-dependencies
#+begin_src emacs-lisp
(require 'request)
#+end_src
#+name: stack-require-dependencies
#+begin_src emacs-lisp
(require 'stack-api)
#+end_src
Simply put, =defgroup= defines a customization group for the graphical
interface within Emacs. Since it pulls all of the customizable
settings together and how to customize them, it is also useful as a
'word bank' of sorts for customizing the package manually. Every
customizable variable in the entire package is listed here.
#+name: stack-setup-customization
#+begin_src emacs-lisp
(defgroup stack-exchange
nil
"Stack Exchange mode."
:group 'environment)
#+end_src
Every mode needs a hook, so we here define one. This hook is run
/after/ stack-mode is finished loading (when called interactively or
from Emacs Lisp).
#+name: stack-setup-customization
#+begin_src emacs-lisp
(defvar stack-mode-hook nil)
#+end_src
In addition to a hook, most if not all major modes define their own
key-map. Stack mode as a whole is no exception, but remembering the
nature of =stack-mode= as a dispatcher, a key-map seems out of place
here. As such, the official key-map for =stack-mode= defines all keys
to be =nil= except those that are necessary for the smooth use of
Emacs as an operating system. Such necessary keystrokes include
=C-g=, =M-x=, and others.
#+name: stack-setup-keymap
#+begin_src emacs-lisp
(defvar stack-mode-map
(let ((map (make-sparse-keymap)))
map)
"Keymap for Stack Exchange major mode. This keymap is not
used.")
#+end_src
...
Here is what I'd like to do:
(Ctrl+F for ** and look at the property drawer.)
* TODO Stack Mode (Entry Point): =stack-mode=
Stack mode is /the/ major mode. What do I mean by this? Stack mode
is the entry point of the whole package. There is no other way to
obtain the full, original functionality of the package without first
running =M-x stack-mode=. Stack Mode is the only mode available
interactively. It is a dispatcher that decides, based on user
preferences, how the whole system shall behave. It provides the basic
framework upon which the rest of the package is built, and makes sure
all tools are available.
#+name: build-stack-mode
#+begin_src emacs-lisp :tangle elisp/stack.el
;; stack.el starts here
(add-to-list 'load-path "~/github/vermiculus/stack-mode")
<<stack-require-dependencies>>
<<stack-setup-customization>>
<<stack-setup-keymap>>
<<stack-setup-api>>
<<stack-load-children>>
(provide 'stack)
; stack.el ends here
#+end_src
** Require Dependencies
:PROPERTIES:
:noweb-key: stack-require-dependencies
:END:
Thus, there are a few packages that it itself requires.
JSON (JavaScript Object Notation) is the standard by which we
communicate with Stack Exchange itself. The details of this
communication has [[id:DC2032C5-BC11-47E2-8DDB-34467C2BC479][already been discussed]] so I will not repeat myself
here. The JSON package provides many utilities for manipulating JSON
snippets within Emacs Lisp, and is required for the operation of this
package. =json.el= is included with Emacs 24+ (and can easily be
obtained from the ELPA if missing).
#+begin_src emacs-lisp
(require 'json)
#+end_src
This package also requires =request.el=, a package designed to
simplify making HTTP requests. =request.el= was written by [[http://stackoverflow.com/users/727827][SX#tkf]] and
is maintained and documented on [[http://tkf.github.com/emacs-request/manual.html][GitHub]]. The package is also available
for automatic install via MELPA.
#+begin_src emacs-lisp
(require 'request)
#+end_src
#+begin_src emacs-lisp
(require 'stack-api)
#+end_src
** Customization
:PROPERTIES:
:noweb-key: stack-setup-customization
:END:
Simply put, =defgroup= defines a customization group for the graphical
interface within Emacs. Since it pulls all of the customizable
settings together and how to customize them, it is also useful as a
'word bank' of sorts for customizing the package manually. Every
customizable variable in the entire package is listed here.
#+begin_src emacs-lisp
(defgroup stack-exchange
nil
"Stack Exchange mode."
:group 'environment)
#+end_src
Every mode needs a hook, so we here define one. This hook is run
/after/ stack-mode is finished loading (when called interactively or
from Emacs Lisp).
#+begin_src emacs-lisp
(defvar stack-mode-hook nil)
#+end_src
** Keymap
:PROPERTIES:
:noweb-key: stack-setup-keymap
:END:
In addition to a hook, most if not all major modes define their own
key-map. Stack mode as a whole is no exception, but remembering the
nature of =stack-mode= as a dispatcher, a key-map seems out of place
here. As such, the official key-map for =stack-mode= defines all keys
to be =nil= except those that are necessary for the smooth use of
Emacs as an operating system. Such necessary keystrokes include
=C-g=, =M-x=, and others.
#+begin_src emacs-lisp
(defvar stack-mode-map
(let ((map (make-sparse-keymap)))
map)
"Keymap for Stack Exchange major mode. This keymap is not
used.")
#+end_src
** Load Children...
The :noweb-ref header (http://orgmode.org/manual/noweb_002dref.html#noweb_002dref) helps propagate the noweb reference names to sub-trees using property inheritance.
Although you found the answer you need, I wanted to point out that
your approach is still quite structured and therefore does not take
full advantage of the power of literate programming. In the famous
wc example, you
can see that definitions and global variables are interspersed
throughout the text. It means that you can spend more time organising
your ideas in a structure which makes more sense to humans than
computers.
Based on your approach, it seems like you are grouping your noweb
blocks into sections based on how they will appear in the tangled
file. That may make sense to you, but there is another way you can do
it. You can, for example, organise your file thematically and group
all of the related code under those subheadings but use the noweb
blocks to group them together in the correct places in the tangled
file.
For example, there are usually a variety of dependencies and the
reason for including them will depend on the particular method of
feature of your programme. In your example, you don't provide enough
for me to give a concrete example, but let's say that you have the
following features or parts:
* Stack Mode
** customisation
** get from stack exchange
** edit post
** send back to stack exchange
I'm not sure if these are relevant or not, but here's the idea. Some
of those features will require certain dependencies. So for example,
you may have something like this:
* Stack Mode (Entry Point): =stack-mode=
Stack mode is /the/ major mode. What do I mean by this? Stack mode
is the entry point of the whole package. There is no other way to
obtain the full, original functionality of the package without first
running =M-x stack-mode=. Stack Mode is the only mode available
interactively. It is a dispatcher that decides, based on user
preferences, how the whole system shall behave. It provides the basic
framework upon which the rest of the package is built, and makes sure
all tools are available.
#+NAME: build-stack-mode
#+HEADER: :noweb tangle
#+HEADER: :comments both
#+HEADER: :tangle elisp/stack.el
#+BEGIN_SRC emacs-lisp
(add-to-list 'load-path "~/github/vermiculus/stack-mode")
<<stack-require-dependencies>>
<<stack-definitions>>
<<stack-initialisation>>
<<stack-customisaton>>
<<stack-functions>>
(provide 'stack)
#+END_SRC
** customisation
*** definitions
:PROPERTIES:
:noweb-ref: stack-definitions
:END:
Simply put, =defgroup= defines a customization group for the graphical
interface within Emacs. Since it pulls all of the customizable
settings together and how to customize them, it is also useful as a
'word bank' of sorts for customizing the package manually. Every
customizable variable in the entire package is listed here.
#+BEGIN_SRC emacs-lisp
(defgroup stack-exchange
nil
"Stack Exchange mode."
:group 'environment)
#+END_SRC
Every mode needs a hook, so we here define one. This hook is run
/after/ stack-mode is finished loading (when called interactively or
from Emacs Lisp).
#+BEGIN_SRC emacs-lisp
(defvar stack-mode-hook nil)
#+END_SRC
*** functions
Whatever is required here
#+NAME: stack-functions
#+BEGIN_SRC emacs-lisp
#+END_SRC
** setup
*** Keymap
In addition to a hook, most if not all major modes define their own
key-map. Stack mode as a whole is no exception, but remembering the
nature of =stack-mode= as a dispatcher, a key-map seems out of place
here. As such, the official key-map for =stack-mode= defines all keys
to be =nil= except those that are necessary for the smooth use of
Emacs as an operating system. Such necessary keystrokes include
=C-g=, =M-x=, and others.
#+NAME: stack-definitions
#+BEGIN_SRC emacs-lisp
(defvar stack-mode-map
(let ((map (make-sparse-keymap)))
map)
"Keymap for Stack Exchange major mode. This keymap is not
used.")
#+END_SRC
** get from stack exchange
*** get post
**** dependencies
:PROPERTIES:
:noweb-ref: stack-require-dependencies
:END:
JSON (JavaScript Object Notation) is the standard by which we
communicate with Stack Exchange itself. The details of this
communication has [[id:DC2032C5-BC11-47E2-8DDB-34467C2BC479][already been discussed]] so I will not repeat myself
here. The JSON package provides many utilities for manipulating JSON
snippets within Emacs Lisp, and is required for the operation of this
package. =json.el= is included with Emacs 24+ (and can easily be
obtained from the ELPA if missing).
#+BEGIN_SRC emacs-lisp
(require 'json)
#+END_SRC
This package also requires =request.el=, a package designed to
simplify making HTTP requests. =request.el= was written by [[http://stackoverflow.com/users/727827][SX#tkf]] and
is maintained and documented on [[http://tkf.github.com/emacs-request/manual.html][GitHub]]. The package is also available
for automatic install via MELPA.
#+BEGIN_SRC emacs-lisp
(require 'request)
#+END_SRC
**** functions
get the actual post
#+NAME: stack-functions
#+BEGIN_SRC emacs-lisp
#+END_SRC
*** parse post
**** dependencies
JSON (JavaScript Object Notation) is the standard by which we
communicate with Stack Exchange itself. The details of this
communication has [[id:DC2032C5-BC11-47E2-8DDB-34467C2BC479][already been discussed]] so I will not repeat myself
here. The JSON package provides many utilities for manipulating JSON
snippets within Emacs Lisp, and is required for the operation of this
package. =json.el= is included with Emacs 24+ (and can easily be
obtained from the ELPA if missing).
#+NAME: stack-require-dependencies
#+BEGIN_SRC emacs-lisp
(require 'json)
#+END_SRC
**** functions
*** display post
** edit post
** send back to stack exchange
*** dependencies
#+NAME: stack-require-dependencies
#+BEGIN_SRC emacs-lisp
(require 'stack-api)
#+END_SRC
*** functions
#+NAME: stack-functions
#+BEGIN_SRC emacs-lisp
(send-back-function)
#+END_SRC
This is just an example, of course, and I don't know how to programme
in lisp, but I just wanted to demonstrate to you and anyone else that
might read this, that you don't have to group your ideas in a way that
makes sense to a computer. In fact, the whole point of literate
programming is to organise things in a manner which make sense to humans. So it might make sense to be explicit sometimes rather than contorting your thought process to match a computer's.
Good luck!
Related
After installing pdf-tools the dired mode opens the pdf file with PDFView mode as major mode.
(use-package pdf-tools
:ensure t
:config
(pdf-tools-install t))
How does the pdf-tools package be able to accomplish this?
The Help for RET key in dried buffer says it is bound to dired-find-file
RET (translated from ) runs the command dired-find-file (found
in dired-mode-map), which is an interactive compiled Lisp function.
I searched for dired-find-file in pdf-tools installed elisp files and could not find any advice-add's?
Also please explain how can one go about finding arbitrary key bindings like this one?
What it modified is not directly related to dired, but to how Emacs decides to open files in general. The part of the code that is responsible for that is in pdf-tools-install-noverify, itself called by pdf-tools-install. The first two lines of the function are:
(add-to-list 'auto-mode-alist pdf-tools-auto-mode-alist-entry)
(add-to-list 'magic-mode-alist pdf-tools-magic-mode-alist-entry)
the relevant variables pdf-tools-<auto/magic>-mode-alist-entry being constants defined earlier in the file pdf-tools.el.
You can check the relevant documentation for auto-mode-alist and magic-mode-alist, but to sum up, the former is a mapping from "filenames" (or more precisely, file patterns -- typically, regexps matching file extensions) to major modes, and the latter is a mapping from "beginning of a buffer" to a major mode (see also this wikipedia page on magic numbers/file signatures).
As to how one can determine that: because it is not directly related to key bindings/advices/redefinition of functions, the only "general" option is to explore the "call stack" ! The package tells you to put (pdf-tools-install) somewhere in your init file to activate the package, so you can try to see what this function actually does -- and going a bit further, you see that it is essentially a wrapper around pdf-tools-install-noverify, which does the real job of setting everything up.
I have a bunch of elisp and other code with some notes i wanted to reformat to be more organized, and i found that having to type
#+BEGIN_SRC emacs-lisp ... #+END_SRC
all the time around what i want, is taking a bit longer than expected...
So what i wanted to do instead is to wrap/or put the selected content (with C-space) and put it in a template source code block for org-mode (in my case it's mostly elisp code, but i plan to use it for other things maybe)
How could i do this in emacs or in elisp?
There is a new templating mechanism in recent Org mode (>= 9.0 IIRC) that allows you tor wrap a region in a block: after selecting the region in the usual manner, you say C-c C-, s. You still have to type the emacs-lisp part though. That's the disadvantage. The advantage is that it is general enough to allow you to wrap a region in any kind of block. In your case, I think the disadvantage outweighs the advantage, so I would go with the wrap-region method in the other answer, but this one is good to know as well.
You can try wrap-region. It will allow you to define what type of string you want to wrap around a selection.
Put this in your init.el and evaluate it.
(wrap-region-global-mode t)
(wrap-region-add-wrapper "#+BEGIN_SRC emacs-lisp\n" "#+END_SRC" "#" 'org-mode)
Then, while you are editing your org files, you can select a block of text and type #, which will wrap it with your string. You can change the # to another character that will do the wrapping.
There is a feature in org-mode to do exactly that. It's like a snippet of some sort where you enter <eland hit TAB, the < char is here to say we're gonna use a template and the el part tells which template to use. But of course, you have to configure it first.
For that, you can just add this to an org-mode file or to your init.el file :
#+begin_src emacs-lisp
;; This is needed as of Org 9.2
(require 'org-tempo)
(add-to-list `org-structure-template-alist `("sh" . "src shell"))
(add-to-list `org-structure-template-alist `("el" . "src emacs-lisp"))
(add-to-list `org-structure-template-alist `("py" . "src python"))
#+end_src
There a bunch of way to use this, it's actually more useful than just use it as a template, you can go check the documentation here.
I am on a literate program using org-babel. My source is structured like so,
-imports
-utility fns
-game structure
- detailed explanations
This is the normal structure of the code, what I would like to do is move explanations of the utility fns to the end so it does not show up first in the generated pdf file. Now this can be done with noweb extension, but the problem is when you have lots of little functions for each one I have to add a src_block with a unique name scroll down the file and add a reference to that in the file which is really annoying. Is there a way to name all the src_blocks in a section? say all code in this section goes into block A.
You can give multiple chunks the same name. For example, I generate my .emacs file with org-tangle, and at the top of the org file, I have a master template that looks something like this:
#+begin_src emacs-lisp :tangle "/path/to/.emacs" :comments both :noweb tangle
<<generated-file-warning>
<<includes>>
<<definitions>>
<<settings>>
<<generated-file-warning>
#+end_src
Underneath that, I have my outline with source blocks like so:
* extensions
** yasnippet
#+name: early-includes
#+begin_src emacs-lisp
(require 'yasnippet)
(yas/initialize)
#+end_src
#+name: settings
#+begin_src emacs-lisp
(yas/load/directory "/path/to/my/snippets")
#+end_src
Note: for older versions of org-mode, you may need to use #+srcname: instead of #+name:
You can also create a property called noweb-ref, which applies the same name to all source blocks in a sub-tree.
Emacs has poor handling of auto-indentation in Flex and Bison. In fact, it seems to have no support for flex mode. So, how does an emacs user cope with these? I like VIm but I would prefer not to switch because I am much faster and more comfortable in Emacs.
I had a third party elisp module for Bison a few months ago but when its indentation broke, it would never get fixed. In short, it was a bad hack.
Or is there a way I can turn off auto indentation for .l and .y files (so pressing would do one indent)? How would I also change this elisp setting for just emacs?
A nice and concise guide for elisp would be very helpful too. I wouldn't mind spending a few days to write my own flex and bison modes if I had the right documentation.
Emacs chooses the major mode mainly based on the file name extension. .l is a contended extension: some people use it for lex, others for lisp (and there are a few other rarer uses). Emacs associates .l with lisp, and .lex with lex (for which it uses C mode).
If the .l files you work with are more often lex than lisp, you can change what .l files are associated with the following line in your .emacs:
(add-to-list 'auto-mode-alist '("\\.l\\'" . c-mode))
You can also declare inside a file what mode you want Emacs to use when it opens the file. Put the following snippet on the first line of the file (typically in a comment):
-*-mode: c-mode-*-
This is a more general feature, offering other syntaxes and other possibilities; see “File Variables” in the Emacs manual for more information.
If you would like to get started with Emacs Lisp, read the Emacs Lisp intro (which may be included in your Emacs or OS distribution). Once you've played around with the basics of the language a bit, you can turn to the chapter on modes in the Emacs Lisp reference manual.
Additional tip: You might decide that what you want is Emacs' generic behavior -- what it uses when it doesn't have any special mode for a file format. That's called Fundamental mode in emacs lingo: so you can request it on the fly with M-x fundamental-mode, or put -*- mode: fundamental -*- on the first line of the file, or customize auto-mode-alist like so:
(add-to-list 'auto-mode-alist '("\\.l\\'" . fundamental-mode))
Another thing to try might be indented-text-mode (probably with auto-fill disabled).
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?
automatic matching/generation of corresponding closing brackets
autoindent Lisp/Clojure style, not C++/Java style
Syntax highlighting
Invoking REPL
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.
[Edit from non-author: this is from 2010, and the process has been significantly simplified since May 2011. I'll add a post to this answer with my setup notes as of Feb 2012.]
You'll need to put together a few pieces: Emacs, SLIME (which works perfectly well with Clojure -- see swank-clojure), swank-clojure (the Clojure implementation of SLIME's server counterpart), clojure-mode, Paredit and, of course, the Clojure jar for a start, then perhaps some extras among which Leiningen would perhaps be the most notable. Once you do set it all up, you'll have -- within Emacs -- all the workflow / editing features you mention in the question.
Basic setup:
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:
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.
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:
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.
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.
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).
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. :-)
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.
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.
Final notes:
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.
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))
There is one more excelent tutorial:
http://www.braveclojure.com/basic-emacs/ (1st part)
http://www.braveclojure.com/using-emacs-with-clojure/ (2nd part)
In 30 to 45 minutes one can have everything setup from scratch.
The tutorial does not assumes any prior knowladge of Emacs (and Clojure too - in earlier posts there is a nice intro to Clojure).
The Emacs Starter kit has gotten great reviews for getting started with Clojure:
To answer only the swank part of your question:
Leiningen is a really easy way of setting up swank with the correct classpath and get it connected to Emacs.
A great video is here: http://vimeo.com/channels/fulldisclojure#8934942
Here is an example of a project.clj file that
(defproject project "0.1"
:dependencies [[org.clojure/clojure
"1.1.0-master-SNAPSHOT"]
[org.clojure/clojure-contrib
"1.0-SNAPSHOT"]]
:dev-dependencies [[leiningen/lein-swank "1.1.0"]]
:main my.project.main)
then run:
lein swank
and from Emacs:
alt-x slime-connect
Clojure with Emacs on Clojure Documentation can be useful too.
CIDER (Clojure Interactive
Development Environment) must be mentioned here.
It’ll cover most of what you’re looking for. It includes:
interactive REPL
debugging
test running
code navigation
documentation lookup
lots more
In addition to CIDER, there are some other essential and nice-to-have
add-ons for clojure development, which I’ll try to group respectively
(and subjectively):
Essentials
smartparens – parentheses
pairing, manipulation, navigation (or
parinfer if you prefer)
clj-refactor –-
has a couple amazing features, like auto-adding/compiling namepaces
(it may be incorporated into CIDER soon)
clojure-mode –
font-lock, indentation, navigation
company – text completion
framework (or choose another auto-completer)
rainbow delimeters –
highlights/colorizes delimiters such as parentheses, brackets or
braces according to their depth
flycheck – on-the-fly syntax
checking extension
flycheck-clj-kondo –
integration for clj-kondo
Niceties
clojure-snippets –
tab-expandable shortcuts to longer code chunks
dumb-jump – jump to
definitions
which-key – displays
available keybindings in popup
highlight parentheses –
highlight surrounding parentheses
crux – a Collection of
Ridiculously Useful eXtensions for Emacs
comment-dwim-2 –
replacement for Emacs’ built-in comment-dwim
General Essentials (for any language)
magit – git porcelain inside Emacs
projectile – project mgmt
for finding files, searching, etc
helm – incremental completion
and selection narrowing framework (or
swiper)
Other Resources
If you’re looking for a setup that already has done most/all of this
work for you, a couple options are:
prelude
spacemacs