I'm using the Clojure mode package from ELPA. Otherwise everything is fine, but I just can't stand paredit mode. I can't seem to turn it off easily, now I just disable it for every buffer I open. I tried setting this variable to nil:
(setq clojure-enable-paredit nil)
But paredit still appears. Any ideas?
Not an answer to your actual question, but give paredit mode a chance. I, too, was really annoyed with it automatically closing my parens, and refusing to delete just a single paren for me.
But doing this enables it to be certain at all times that the buffer is a well-balanced sexp, so it can perform many useful sexp-oriented tasks for you instead of just text-oriented tasks. For example, I use the following all the time:
M-( to wrap a sexp with a new one, eg turn (map f some-list) into (doto (map f some-list) println)
C-) to "slurp" another sexp into the current one, eg turn (let [x 10]) (println x) into (let [x 10] (println x))
M-<UP> and/or M-r to pull the sexp at point a level "higher" in the source tree, destroying the thing that was wrapping it, eg to turn (first (map f some-list)) into (map f some-list) or (first some-list)
There are zillions of useful features like this, that let you start editing code instead of text. And while there are plenty of excellent Lisp hackers who don't like paredit mode, I advise you not to decide against it before you realize the awesome stuff it can do for you.
Found one trick that works. Before the elpa packages are loaded in init.el, add this hook to clojure mode:
(add-hook 'clojure-mode-hook (lambda () (paredit-mode nil)))
For what it's worth, I use clojure-mode through ELPA too, and it doesn't imply paredit. Maybe just uninstall it? I find clojure-mode, slime and slime-repl are the only packages I need to install on a clean EMACS to get clojure/swank/slime working.
I only tested this:
http://www.learningclojure.com/2010/08/clojure-emacs-swank-slime-maven-maven.html
a few weeks ago, and it still works fine.
Related
I am an hour new into programming in Emacs lisp. I have a little experience with scheme so I understand the big picture of lisps in general. However, I have only used the "pure functional" subset of scheme and do not know how to do practical tasks.
Write now, I know that C-x C-e will evaluate the code enclosed by the parentheses' by the current cursor position.
I wish to loop from i = 1 to 10 and print the values of i out. How is this done? I tried the following:
(defvar i 1)
(while (< i 11)
(print "i: " i)
(setq i (+ i 1)))
Emacs tells me: invalid function 0.
How do I do this correctly?
Why is emacs telling me invalid function 0
Feel free to give me tips about how to use the scratch buffer (all I know is C-x C-e evaluates) in emacs. Thanks for all the help!
EDIT1: Could someone tell me how to print out sequential values of i using a while loop?
EDIT2: When I evaluate the code, it opens up another tiny buffer showing each value of i one at a time. However, it is not a large buffer and only shows values of i from 13 to 19. When I try to get into that buffer, it closes immediately. How do I "scroll" through that tiny buffer? Note that I use emacs 24.3 through the terminal
EDIT3: I figured out that the tiny buffer is the Messages buffer. Is there a better way to view the output of my elisp code? The Messages buffer is full of other junk from evaluating things in emacs.
First and foremost, enable "Enter debugger on error" from the Options menu now and add (setq debug-on-error t) or (custom-set-variables '(debug-on-error t)) to your ~/.emacs.el.
Then you will get a *Backtrace* buffer on C-x C-e:
Debugger entered--Lisp error: (invalid-function 1)
1(10)
print("i: " 1)
(while (< i 11) (print "i: " i) (setq i (+ i 1)))
eval((while (< i 11) (print "i: " i) (setq i (+ i 1))) nil)
eval-last-sexp-1(nil)
eval-last-sexp(nil)
call-interactively(eval-last-sexp nil nil)
command-execute(eval-last-sexp)
which shows that the error comes from print.
C-h f print RET will tell you why, but the upshot is that you want to use insert instead of print here.
Just as an added note, since you mentioned knowing some scheme -- if you like the interactive REPL that you can use in typical scheme environment, you might like ielm -- I think it probably stands for Interactive Emacs Lisp mode. Not sure. Anyway, M-x ielm RET will open up an emacs lisp REPL. Sometimes it is actually useful -- for example, when you want to inspect the content of a variable with a lot of data in it, ielm will print the whole thing out. Ielm is built in to my Emacs. Not sure when it was added to the standard distribution, but the earliest copyright in the source says 1994, so it is probably in your Emacs.
You can evaluate Emacs-Lisp sexps in *scratch* or in any other buffer in the same mode or (my preference) in mode emacs-lisp-mode.
In *scratch* you need only hit C-j (newline) after a sexp to evaluate it. In an emacs-lisp-mode buffer you can, as you said, use C-x C-e after a sexp. Or you can use M-x evaluate-region after selecting one or more sexps. As always, C-h m in any mode tells you about it, and usually lists important key bindings.
You can also check a global variable value using C-h v SOME-VAR. And you can evaluate any sexp on the fly from the minibuffer, using M-:. For example: M-: (setq foo (+ 42 (length bar)))
Wrt the debugger:
As #sds mentioned, debug-on-error puts you in the debugger when an error is raised. You can also set debug-on-quit and then enter the debugger using C-g to quit (e.g., during a loop).
If you know the function you want to debug, you can use M-x debug-on-entry.
Step through the debugger using d, or skip to the end of a step using c. Use q to quit the debugger.
You can also insert calls to function debug in source code, as debugger entry points: (debug).
The backtrace in the debugger is always more informative if you load the relevant source file e.g., foo.el, instead of the byte-compiled file, e.g., foo.elc. So before you use M-x debug-on-entry use C-h f to find out which file the function is defined in, and then load that file using M-x load-file /path/to/the/file.el.
There is also another debugger, besides debug -- look for edebug in the Elisp manual. Some people prefer edebug; I prefer debug.
I'm looking for a way to include some filtering in the other-buffer method in emacs.
Currently calling other-buffer pulls up the last most recent buffer, but the problem with this is that buffers that get modified by external processes keep coming up as other-buffer. I would like to implement some sort of filtering in other-buffer.
Currently I use evil with C-^ bound to other-buffer, and I have some tail.el buffers active, and when I try to switch bufffers the tail buffers keep popping up.
Is there some alternative to other-buffer or could someone scratch up some code to implement this, Thanks.
What has worked for me is winner-mode - it's like an undo, but for window configurations.
Here's my setup:
(winner-mode)
(global-set-key (kbd "<f7>") 'winner-undo)
(global-set-key (kbd "C-<f7>") 'winner-redo)
Also I'd recommend other-window on some very cheap shortcut, since it's
a command that's used a lot.
I've put it on C-p, since I didn't appreciate the inconsistency
that one of the direction keys is so far away from others.
I've got previous-line on C-h instead, so now
my direction keys are n h f b - they're almost together!
And I didn't really miss the defaults on C-h, since f1
has the same functionality.
Ok so I got some workable solution but its not perfect it using bits from this answer:
emacs lisp, how to get buffer major mode?
(defun buffer-mode (buffer-or-string)
"Returns the major mode associated with a buffer."
(with-current-buffer buffer-or-string (format "%s" major-mode)))
(defun other-buffer-ex ()
(interactive)
(switch-to-buffer
(if (string-equal (buffer-mode (other-buffer)) "comint-mode")
(next-buffer) (other-buffer))))
Emacs 23.2 in emacs-starter-kit v1 has C-x C-i (or ido-imenu) (similar to Sublime Text's Cmd+R). Emacs24 in emacs-starter-kit v2 lacks this function. I found this github issue and a fix, which try to recreate the functionality. While this ido-imenu works in elisp-mode, it stopped working in ruby-mode. I get:
imenu--make-index-alist: No items suitable for an index found in this buffer
Has anyone figured out how to get this to work?
Why was this taken out of Emacs24?
Is there a new replacement for this function?
Since the function is part of ESK (as opposed to something budled with Emacs) you'd probably do best to report the bug upstream. On a related note ESK main competitor Emacs Prelude offers the same functionality (bound to C-c i by default) and it seems to be working fine with ruby-mode in Emacs 24. Here you can find more on ido-imenu.
So I finally figured it out, after reading the Defining an Imenu Menu for a Mode section on emacs-wiki again.
Short answer: you need to add this bit to your customization. Feel free to add more types to the list (I am happy with just methods).
(add-hook 'ruby-mode-hook
(lambda ()
(set (make-local-variable imenu-generic-expression)
'(("Methods" "^\\( *\\(def\\) +.+\\)" 1)
))))
Longer answer: I first tried to define a ruby-imenu-generic-expression function and set that to imenu-generic-expression by using the ruby-mode-hook:
(defvar ruby-imenu-generic-expression
'(("Methods" "^\\( *\\(def\\) +.+\\)" 1))
"The imenu regex to parse an outline of the ruby file")
(defun ruby-set-imenu-generic-expression ()
(make-local-variable 'imenu-generic-expression)
(make-local-variable 'imenu-create-index-function)
(setq imenu-create-index-function 'imenu-default-create-index-function)
(setq imenu-generic-expression ruby-imenu-generic-expression))
(add-hook 'ruby-mode-hook 'ruby-set-imenu-generic-expression)
This however did not work (I would get the same error as before). More reading of the Defining an Imenu Menu for a Mode section showed me the way. Now, I'm not an elisp expert, so here's my hypothesis: basically, the above method works for modes where the
major mode supports a buffer local copy of the “real” variable, ‘imenu-generic-expression’. If your mode doesn’t do it, you will have to rely on a hook.
The example for foo-mode made it clear how to do it for ruby-mode. So it appears that ruby-mode does not have a buffer-local copy of the real imenu-generic-expression variable. I still can't explain why it worked in Emacs 23.2 (with ESK v1) but does not on Emacs24, but hey at least I found a working solution.
In emacs C-x r f remembers the frames configuration to a register. How I can 'see' it ? M-x view-register doesn't show it. I also like to store different configurations and re-call them as I need them across emacs sessions.
C-xrj is bound to jump-to-register, and you can find the code you need in there. You can use either M-x find-function or M-x find-function-on-key to conveniently jump to the source.
The function obtains an argument register and then calls (get-register register) to obtain the data. The following code then deals with restoring the frame or window configuration as required.
The "c" code to interactive means a character, so the register argument is just a character. You could therefore use (get-register ?a) to obtain register a.
(defun jump-to-register (register &optional delete)
(interactive "cJump to register: \nP")
(let ((val (get-register register)))
(cond
;; [...]
((and (consp val) (frame-configuration-p (car val)))
(set-frame-configuration (car val) (not delete))
(goto-char (cadr val)))
((and (consp val) (window-configuration-p (car val)))
(set-window-configuration (car val))
(goto-char (cadr val)))
;; [...]
)))
The winsav.el library is alive, but the new version is on Launchpad as part of nXhtml. The easiest way to get winsav and set it up is just to download the whole of nXhtml and install it. (If you want it to load fast then just byte compile the whole nXhtml - FROM the nXhtml menu.)
If you for some reason believe it is better to just have winsav.el then it is in the util subdirectory:
http://bazaar.launchpad.net/~nxhtml/nxhtml/main/files/head:/util/
(Note that the zip files for downloading nXhtml are a bit old now. In fact everything in my Emacs pages are a bit old at the moment. Except for some parts of nXhtml that I update now and then. And the sources for EmacsW32 - which are not up to date but include man.
Quoting the documentation:
Use C-x r j R to restore a window or frame configuration. This is
the same command used to restore a cursor position. When you restore
a frame configuration, any existing frames not included in the
configuration become invisible. If you wish to delete these frames
instead, use C-u C-x r j R.
(Where R stands for the register.)
With Bookmark+ you can bookmark an Emacs desktop. Unfortunately, a desktop does not record the frame configuration. (You can also bookmark a frame configuration, but that is only for the same Emacs session, since they are not peristent.)
I believe there are, however, some libraries that let you save a window or frame configuration persistently (and then restore it). You might try Lennart Borgman's winsav.el, for instance. I know that a couple of years ago he was working on that feature -- dunno what the status is now. If it works, then you can also bookmark persistent frame configs.
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