how to write scheme program fast in emacs - emacs

(define (cube guess x)
(if (good-enough? guess x)
guess
(improve guess x)))
I'm using emacs+Racket, but when I write in Racket,it doesn't auto-complete.
I also can't write the Anti-brackets in the same line,like this
(define (cube guess x) ). I want to use the 'return' key to make the anti-brackets next line, however the scheme interpreter will compute the expression,then it will be wrong.
then if we write the code in the scheme-mode buffer,it may be some bother, we have to
select the region,then compute in another buffer
Anyone tell me some better ways? sorry for my poor English!

It looks to me like you're using an interactive interpreter, and when you hit the "return" key in the middle of a line, it sends the expression to be evaluated rather than allowing you to edit it further. Is this correct? If so, I would encourage you to take a look at Neil Van Dyke's "Quack" package, which (IIRC) is designed to allow you to edit Racket code using emacs.
If you're not married to emacs, then of course I would also suggest trying to use DrRacket.

It sounds like you're using the scheme interpreter from within Emacs. This is a good start for writing small functions, but you really want to use a REPL (Read-Eval-Print Loop) workflow. Thankfully, Emacs has a ready-made scheme REPL built-in, and has been mentioned elsewhere, there are additional modes (like Quack) that enhance the experience.
In the REPL model, you can freely type expressions in the interpreter if you want to try them out, but most of your coding should take place in the file you're writing. From within that buffer, if you have a scheme interpreter running (M-x run-scheme), then you can send sexps to the interpreter for evaluation without copying manually with C-c C-e. You can use C-M-x to do the same thing.
You can compile the entire file with C-c C-k, and if you have several expression you want to send together, grab them in a region and use C-c C-r to send the region to the interpreter.
There are several other commands that make transferring your code to interpreter easy; you can read more about them in your REPL session by pressing C-h m to describe the keybindings for your current mode.

What does this code even do ? Are you missing the "if" ? That could be part of the reason the interpreter isn't working.?
(if (good-enough? guess x) guess (improve guess x))
Sorry if I just don't understand what you are trying to achieve.

Related

In Slime REPL (GNU EMACS), what is the shortcut to clear the current input

In Slime, I would like to clear the input I have currently typed into the REPL topline. What is the shortcut?
For example, if I type the below, it is missing one parenthesis and will not execute. How do I simply clear the whole text from my input line? I.e. instead of having to manually press delete on each character
(let* ((x 5) (y (+ x x))
(print y))
I could not find anything here:
https://common-lisp.net/project/slime/doc/html/REPL-commands.html
These solutions seem to be different (but please correct me if I'm wrong) and relate to the whole screen, while I'm interested in clearing what I have typed but not yet evaluated:
emacs cider clear REPL buffer
In emacs, how do I bind C-l to clear screen in slime?
It's C-c C-u, which is slime-repl-kill-input. A good trick is to use C-h b which will cause Emacs to show you the current bindings in a help buffer: you can then search through them looking for likely candidates. C-h m is also useful to show help on the current mode, which should show you the bindings as well (but may not: I'm not sure if it always does). The advantage of these things is that they show you what actually exists rather than what the manual thinks exists which may not be the same thing (even when there is a manual...).

How can I make emacs keyboard macros work properly when pasting the results of clojure evaluation?

There's a phenomenally useful feature of emacs lisp where you can evaluate the result of an expression and paste the result directly into a buffer.
Say I want to check addition works. Then I type:
(* 3 2)
and I define the keyboard macro:
(setq last-kbd-macro
[down ?\( ?i ?s ?= ? ?\C-\M-f ? ?\C-u ?\C-x ?\C-e ?\) home])
If I then place point above the expression, and press F4 to execute the macro, the expression turns into:
(is= (* 3 2) 6)
Which makes a nice regression test.
Unfortunately the same keyboard macro executed in a clojure/nrepl buffer results in:
(* 8 9)(is= )
and an error from clojure about not being able to resolve the symbol is=
So I think that something weird is happening to the ordering of things, and the macro is trying to evaluate the wrong thing.
Can anyone get this to work with clojure? (And in fact solve the general problem so that arbitrary keyboard macros work OK with C-u C-x C-e like they do with emacs lisp)
Edit since people seem to be misunderstanding:
Doing the keypresses by hand works fine in either an elisp or a clojure buffer. In one C-u C-x C-e evals with emacs lisp and in the other evals in the external clojure process.
The problem comes when trying to run a keyboard macro (recorded in a clojure buffer) which contains C-u C-x C-e
Running the macro in the clojure buffer, things get re-ordered somehow. It looks like the macro may be carrying on executing even though the eval-paste has not completed yet.
I was wondering if there was a way of forcing the keyboard macro (or corresponding function) to execute in the same order as it would by hand.
I'm quite happy to turn the keyboard macro into a proper elisp function if necessary.
Keyboard macros are a quick and dirty way to repeat a certain sequence of actions. They are very fragile because they remember the keys pressed, not the functions they invoke, so they may produce wildly different results depending on the buffer they are invoked in, the current command history, window configuration and what not.
In your case, chances are that one of the keys in the macro invoke a different command in the clojure/nrepl buffer than in the buffer in which you tested the macro. You really need to define the macro in the buffer in which it will be used.
If you are going to re-use the macro, I suggest that you write a emacs
lisp function which does what you want instead of messing with macros.
You might find the output of (format-kbd-macro nil t) useful, but note that you should not use the commands like eval-last-sexp in your function, use a lower-level function which returns the evaluation result instead of inserting it into the current buffer.
Addressing a problem you haven't had yet, but will soon: don't forget to insert a ' character before the result of evaluating the expression: you want (= (cons 1 nil) '(1)), not (= (cons 1 nil) (1)).

Why is "goto-line" in Emacs for interactive use only?

What problem can happen if the goto-line function is used in a non-interactive elisp program? Its docstring gives a warning saying that:
This function is usually the wrong thing to use in a Lisp program.
What you probably want instead is something like:
(goto-char (point-min)) (forward-line (1- N))
Moreover, when I try to byte-compile-file my init file including goto-line, I get a unpleasant warning like this once again:
.emacs:170:19:Warning: `goto-line' used from Lisp code
That command is designed for interactive use only
Is using goto-line in a non-interactive program really so dangerous? Relatedly, why is the suggested forward-line solution preferable?
Firstly, this prevents Elisp programmers from fall into bad habits -- writing
inefficient code in a line-number centric way. i.e. instead of using
(forward-line 1) calculating the current line number, incrementing, and using
goto-line.
From this mailing list article:
In a nutshell, the reason why goto-line should not be a frequently
used command is that normally there's no reason to want to get to line
number N unless you have a program that told you there's something
interesting on that line.
Secondly, goto-line manipulates the user's environment in addition to moving
the point (i.e. push-mark). For non-interactive use, this may not be what
you want. On the other hand if having considered all this, you believe
goto-line is exactly what you need, then just call it like this:
(defun foo ()
(interactive)
(with-no-warnings
(goto-line N)))
And you won't get any compiler warnings.
in addition to what was said:
"goto-line" finally recurs onto "(forward-line (1- line)", which in effect does the work. All other of the 43 lines of "goto-line" command body deal with interactive use. For example considering a possibly universal argument.
When writing a program resp. when running it, your computer is in another state than following an interactive call. Thus you should address this state by using "forward-line" straight on.

Emacs / Slime Key Binding / Sending command to Swank Server

I'm familiar with scheme, but new to emacs (switching over from VIM) and elisp.
I know how to do the following:
make a simple key binding
C-c iwb = indent whole buffer
F2 = turns folding on/off
use slime from emacs
some basic keys, like C-x 2, paredit keys, some basic movement keys
I need help doing something a bit more advanced:
I want F3 to equal:
put emacs into C-x 2 mode
in bottom window, switch to "slime-repl" buffer
in the "slime-repl" buffer, send the command "(test/run)" <-- note, this is meant to be sent to the swank server, NOT to elisp
I realize it's terrible form to ask people to write a script for me; however, if anyone could do that, I would learn rather quickly from it. [And it would allow me to do more complicated types of scripting through studying your example.]
Thanks!
This is not exactly what you want, but should be a good starting point for further tweaking:
(defun slime-run-test ()
(interactive)
(slime-interactive-eval "(test/run)")
(slime-pop-to-buffer (slime-output-buffer) t))
(global-set-key (kbd "<f3>") 'slime-run-test)
I don't use slime, but assuming it uses comint-mode then I would think the following might do the trick:
(defun my-slime-test-run ()
(interactive)
(delete-other-windows)
(split-window-below)
(with-selected-window (next-window)
(switch-to-buffer "slime-repl")
(goto-char (point-max))
(insert "(test-run)")
(comint-send-input)))
(global-set-key (kbd "<f3>") 'my-slime-test-run)
There is probably a better way to do this, but hopefully that gives you a little insight into how you can write elisp functions to carry out tasks in the editor (and note how the function reads very much like a set of editor instructions -- you can do a lot simply by converting the keystrokes you would use into equivalent code -- or even not writing code at all, and simply recording & saving keyboard macros).
Use C-hf name-of-the-function RET to get documentation on any of the function/macro calls in that function.
For the keybinding, I used C-hkF3 to check how Emacs referred to that key, and then used that string as the argument to kbd (and note how you can use that sequence to find the name of the function bound to any given key sequence, which you can then utilise in code if desired).
Many things are far less obvious if you don't already know them, but that's only to be expected with a code base as large as this (and dating back as long as this).
The great thing is that if you don't know what you're looking for, you can always search for function names matching patterns with C-uC-ha (and similarly for variables, values, libraries, and documentation; see M-: (info "(emacs) Apropos") RET for more about this facility). Plus the info manuals (complete with indexes -- press I or i within any particular manual, or use the info-apropos command to search all info manuals at once).
Truly one of the very best things you can do is to learn how to use the self-documenting nature of Emacs to find answers to the things you don't already know.

Emacs Macro to Start in Shell Mode and Run a Command

I have a confession: I don't know Lisp. Despite that fact, with a bit of help from some co-workers, I managed to write an emacs macro/script which:
switched to shell mode (ie. M-x shell-mode)
disabled truncating lines (ie. M-x toggle-truncate-lines)
started a database console (ie. "mysql")
I was then able to start emacs with that macro using the --script option, and suddenly I had a way to start mysql in a much friendlier environment with a single command :-)
But here's the problem: I changed jobs and left that script behind. Now I'd very much like to re-create that script at my new job, but I no longer have any emacs experts to help me write it like I did at the old job.
Now, I really hate SO posts where someone basically says "please write my code for me", so I don't want to do that. However, if any emacs macro experts could at least give me some pointers (like "here's how you invoke a M-x command in a macro"), or point me to an emacs-macro-writing guide, or otherwise "teach me to fish" on this issue, I would greatly appreciate it.
... and if someone just happened to have a similar script already lying around that they wanted to post, I certainly wouldn't complain ;-)
Most emacs commands (i.e., M-x toggle-truncate-lines) can be translated directly to elisp by wrapping them in parentheses:
(toggle-truncate-lines)
The rumours are true, in lisp you just scatter parentheses around and they make magic.
Now in this case, you can do better. Toggling makes sense for an interactive function, but in a program you don't really want to toggle truncate-lines, you want to turn on truncate-lines. Its the same thing if truncate-lines was turned off to begin with, but you don't know when your program will be run next. Anyways, in Emacs, features are often controlled by a variable. In this case, the variable is truncate-lines, and to turn that feature on, you set the variable to t (which means true).
To do this, use:
(setq truncate-lines t)
We use setq instead of = for assignment, because they made lisp before = had been invented.
For the real scoop you should take a look at Robert Chassel's excellent "An introduction to to Programming in Emacs Lisp". It comes built-in with your emacs, you can get to it with C-h i m Emacs Lisp Intro.
A good way (I think) to start writing elisp functions is to record keyboard macros, and then to analyse them using edit-kbd-macro
For example, if you start recording a keyboard macro using f3, then do interactively all the things you want and terminate the macro using f4, you can see the underlying emacs-lisp commands using M-xedit-kbd-macrof4 (this last f4 is the key binding you'd have used to execute the keyboard macro)
<<shell>> ;; shell
<<toggle-truncate-lines>> ;; toggle-truncate-lines
mysql ;; self-insert-command * 5
RET ;; comint-send-input
Now you can write a script using these functions, looking up the documentation (e.g. C-h ftoggle-truncate-lines) to see if you should call them with special arguments in non-interactive mode.
You should also replace self-insert-command by calls to insert.
This should give you something like the following script, which you can call using emacs --load myscript.el
(shell)
(toggle-truncate-lines 1)
(insert "mysql")
(comint-send-input)
Of course, this might not work as expected the first time, so you might have to eval (setq debug-on-error t) to get debugging information.
What version of Emacs are you using?
In Emacs 24, I have M-x sql-mysql, which does everything you ask and has font-locking.