According to this question, I can customize the variable *DEBUGGER-HOOK* so that it falls back to toplevel (in REPL) instead of the debugger. I've added this line to my ~/.sbclrc and it's all fine when I start sbcl from command line.
(setf *debugger-hook* #'(lambda (c h) (declare (ignore h)) (print c) (abort)))
But, the above doesn't work for Emacs SLIME. Whenever I compile/load a file (C-c C-k), it still invokes the debugger (with options like abort calculation, restart, enter new value etc.). How can I ask SLIME to just print the error message and throw me back to toplevel? Yea, it's with SBCL and the same ~/.sbclrc as before. Looks like SLIME doesn't respect a user's setting of *DEBUGGER-HOOK*.
As per http://common-lisp.net/project/slime/doc/html/Other-configurables.html setting SWANK:*GLOBAL-DEBUGGER* to nil in ~/.swank.lisp file should force SLIME to not replace *DEBUGGER-HOOK* to SWANK:SWANK-DEBUGGER-HOOK (which shows list of restarts etc.), but it somehow doesn't work for me, i.e. SWANK:*GLOBAL-DEBUGGER* is nil but anyway *DEBUGGER-HOOK* is replaced by SLIME. Maybe you'll be more lucky.
As a workaround I can propose to set *DEBUGGER-HOOK* to whatever you want in the slime-repl buffer manually, which is worked for me.
Related
I'm on ubuntu 19.
Using emacs, slime and sbcl to practice some lisp.
Currently I have one buffer in slime mode in one window and the slime-description in the other window.
When I want to execute a line, I write it on the buffer and press C-c C-p.
But when I try to do the same for the line
(defvar *name* (read))
to set the the name var with the user input, nothing is happening.
Why ?
Also I would like to execute the whole script and not one line at a time, how do I do that ?
'Nothing is happening' because read is waiting for you to type something at the REPL. If you look at the REPL you will be confused because the form you are evaluating is not displayed, so all you see is ... nothing, but you need to type something at it. Further, it's not clear from your description what buffers you have displayed, but I suspect the REPL is not one of them, which is going to make things even worse.
I don't know how other people use SLIME, but what I do is to have at least the REPL (the thing you get after typing M-x-slime in one window, and a file I am working on in another. You can then interact with the REPL just by typing at it, and send code to the running lisp from the file with C-M-x or any of the other commands (in particular things like C-c C-k which compiles & loads the file.
However you almost never want a file you are compiling or loading to include anything which causes read to be called at compilation or load time: the results are going to be mysterious to put it mildly: the system will just stop with no prompt waiting for you to type something. It makes much more sense to do that in the REPL:
CL-USER> (defvar *name* (read))
(here is the data I am typing in)
*NAME*
Indeed, even when you go to some lengths to make calls to read non-mysterious in files being loaded, you have to go to yet further lengths to make them safe. Consider this file, toxin.lisp:
(defvar *my-thing*
(progn
(format *query-io* "~&thing? ")
(finish-output *query-io*)
(read *query-io*)))
Now:
$ lisp
[...]
(load "toxin" :verbose t)
;Loading #P"toxin"...
thing? #.(quit)
Of course there are much worse things I could have said than that to the Lisp.
I use Emacs to code. At the moment, to compile an OCaml project, I need to type make in a terminal. If there is an error, I have to go back to Emacs to find the erroneous file and location by following the error indication.
Now, I would like to launch make inside Emacs by a keyboard shortcut, which opens a buffer to show the compilation, then
1) if there is no error, then close the buffer automatically
2) if there is an error, another shortcut will lead me to the erroneous file and line within Emacs.
Does anyone know how to write .emacs to enable this mechanism?
PS: This is my current .emacs.
I have posted a question to compile one .tex file within Emacs by a shortcut (Ctrl + c + m + m), this works well. But in case of error, I don't ask it to lead me to the error position.
M-x compile RET
See the Emacs manual, node Compilation.
And see node Compilation Mode for how to visit error occurrences.
Some years back, I had the same question, here's the answer I got on the #emacs irc channel.
;; from enberg on #emacs
(setq compilation-finish-function
(lambda (buf str)
(if (null (string-match ".*exited abnormally.*" str))
;;no errors, make the compilation window go away in a few seconds
(progn
(run-at-time
"2 sec" nil 'delete-windows-on
(get-buffer-create "*compilation*"))
(message "No Compilation Errors!")))))
;; save the file when I press compile
(setq mode-compile-always-save-buffer-p t)
The problem:
I'm in scheme-mode (simple quack)
scheme is already running
eval expression
it sends the expression to scheme buffer
but! doesn't bring that buffer up in 2nd window = no immediate feedback
and I have to manually switch second buffer to scheme which is annoying
Some (more recent) modes like fsharp-mode or tuareg do that automatically. Tried to read quack.el, but didn't find convenient separate function like "pop scheme". It's tangled within run-scheme which also changes focus. Settings also don't help.
I want to stay in my rkt/scm file and see repl buffer pop up if not already popped. Like this simple build logic in sublime-text but with persistent repl.
Maybe I should try geiser, but quack is ok for now. Just missing few obvious conveniences.
Just rungeiser. It's in MELPA now, so it's a quick install.
You can also try lispy (which uses geiser) for in-place scheme eval.
e will eval current expression and display the result in the minibuffer.
E will eval current expression and insert the result in current buffer.
u is bound to undo, so you can either e or Eu if you prefer.
I ended up writing this:
(setq scheme-program-name "guile")
(defun run-scheme-2.0 ()
"Loads your chosen Scheme implementation for interactive development,
and displays that buffer below your main editing buffer, and makes sure that
your cursor will be on your code."
(interactive)
(if (not (get-buffer "*scheme*"))
(progn
(setq starting-buffer (buffer-name))
(run-scheme scheme-program-name)
(split-window-below)
(other-window 1)
(shrink-window-if-larger-than-buffer)
(other-window 1)
(switch-to-buffer starting-buffer))))
(add-hook 'scheme-mode-hook 'run-scheme-2.0)
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 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.