Using Paredit to wrap existing expression - emacs

I'm using Emacs in Paredit mode for Lisp code.
I'm trying to wrap a function call in println:
(square 5)
(println (square 5))
What ends up happening in paredit is this:
(square 5)
(println) (square 5)
There is no way i can delete closing paren of println and move it to the end.
The way i'm doing it now is to:
1. delete function call and yank it within println
2. write println without paren, visually select code and enclose in parens
(square 5)
println (square 5)
=> select block of code and type (
(println (square 5))
Both of these approaches are tedious. This seems to be a common problem anytime i write code inside out in Paredit. Any help would be appreciated

paredit-wrap-round command may help (bound to M-( in the paredit version I use).

In contrast to the other answers, I tend to use Ctrl-Right for this: after you get
(println|) (square 5)
(where | is where the cursor is), I simply press Ctrl-Right to get the correct result.

M-(
You can call paredit-insert-html-examples to generate a HTML webpage cheatsheet. One version is here.

Three ways to wrap a print form around a square form, in step-by-step progressions. (two ways of the three are already mentioned in other answers)
(1) Cut & type & paste
(+ (square 3) 4)
;;; make sure cursor is at right place (| is cursor)
(+ |(square 3) 4)
;;; mark-sexp and kill-region
(+ | 4)
;;; type the print form and make sure cursor is at right place
(+ (print |) 4)
;;; paste
(+ (print (square 3)) 4)
(2) type & slurf
(+ (square 3) 4)
;;; make sure cursor is at right place (| is cursor)
(+ |(square 3) 4)
;;; type the print form and make sure cursor is at right place
(+ (print|) (square 3) 4)
;;; paredit-forward-slurp-sexp
(+ (print (square 3)) 4)
(3) wrap & type
(+ (square 3) 4)
;;; make sure cursor is at right place (| is cursor)
(+ |(square 3) 4)
;;; paredit-wrap-round
(+ (|(square 3)) 4)
;;; type print
(+ (print (square 3)) 4)
Cut & type & paste is the most tedious. It doesn't depend on paredit and is the easiest to generalize to the case of wrapping a complex outer form around more than one inner forms which may be in multiline format, for example, turning
(let ((x 1))
(moo)
(oink)
(oink))
into
(let ((x 1))
(moo)
(mapc (lambda (x)
(oink)
(oink))
(list 1 2 3)))
by wrapping a mapc-over-lambda form around two oinks.
Type & slurf seems to be the most known. It can be generalized to the mapc-over-lambda case as well.
Wrap & type is the most easiest to type. When you generalize this to the map-over-lambda case, you select two oinks, and press ( to wrap it, and finish writing the lambda form, and select the lambda form, press ( to wrap it, type mapc, and you can use C-M-f to cross over the lambda form and type (list 1 2 3).

In your case the solution is M-2 M-( or M-( C-).

There are several ways of doing this. The way that I normally do this is type
"("
which gets you
()println
and then C-S-) which gets you
(println)
The best way to use paredit to start is to have the paredit cheatsheet on a piece of paper stuck to the side of your monitor. Or, you could also install paredit-menu which adds a menu item with tooltips showing the same examples the cheetsheet does.

I use M-C-(, which will immediately wrap the following sexp in parens.

Related

Common lisp: break, but return input instead of nil?

I am new to lisp, and am learning as I go.
The standard common lisp break function 1. pops you into the debugger, and 2. if you choose to continue, returns nil.
It seems to me that a break function that popped you into the debugger but RETURNED ITS INPUT would be extremely useful. Then, you could just insert it transparently around a given s-expression to look at the state of the program at that point.
So I could do something like
CL-USER> (break-transparent (+ 1 2))
which would pop me into the debugger and let me look around and then would return
3
Is there such a thing in lisp, and, if not, is there a way to make such a thing? I am not good with macros yet.
Thanks,
EDIT:
Doug Currie kindly answered this below with a simple macro. Here is my slightly modified version for anyone else with this question that displays the argument to break-transparent front and center in the debugger window.
(defmacro break-transparent (exp)
`(let ((x ,exp)) (break "argument to break: ~:S" x) x))
You can write break-transparent as a macro that expands to:
(progn
(break)
(+ 1 2))
or if you really want to evaluate the expression before the break:
(let ((x (+ 1 2)))
(break)
x)
So,
(defmacro (break-transparent exp)
`(let ((x ,exp)) (break) x))
Since you've added the macro from the other answer to your question in a slightly modified version, here's a fixed version of that that doesn't add a new identifer that might interfere with an existing one:
(defmacro break-transparent (value)
(let ((g (gensym)))
`(let ((,g ,value))
(break "argument to break: ~:S" ,g)
,g)))

how to update evaluation result comments of Emacs Lisp forms easilly

Say I have this code which shows usage examples of mapcar
(mapcar #'1+ (list 10 20 30)) ; ⇒ (11 21 31)
(mapcar (lambda (it)
(* 2 it))
(list 0 1 2 3))
;; ⇒ (0 2 4 6)
(require cl-lib)
(cl-mapcar #'+
'(1 2 3)
'(10 20 30))
;; ⇒ (11 22 33)
I may be keeping that code somewhere written so that I can use it on a tutorial or so that whenever I forget how mapcar works, I can quickly read the code.
Now suppose I want to update some of the examples in the code. For example, I may change (list 0 1 2 3) in the second example to some other list. Right after I change the example, the corresponding result comment is then outdated. The result comment need to be updated as well. So I evaluate the form, copy the result, and replace the old result in the comment with new result. Is there a package I can use to help me do that all easily and less tediously? This is a different problem than problems that the litable or ielm package solve because this is simply about updating existing example code.
Right now what I use is:
(defun my-insert-eval-last-sexp ()
(interactive)
(let ((beg (point)))
(let ((current-prefix-arg '(4)))
(call-interactively 'eval-last-sexp))
(goto-char beg)
(if (looking-back ")")
(insert " ; "))
(insert "⇒ ")
(move-end-of-line 1)))
which still isn't enough because it simply adds the result comment rather than updating an old one, and has a bug of odd stuff getting inserted when the form evaluates to a number:
(+ 1 2)
;; ⇒ 3 (#o3, #x3)
Well, I'm not sure I want to encourage this kind of thing ;-), but this will get you a little closer to what you are trying to do, IIUC:
(defun my-insert-eval-last-sexp ()
(interactive)
(let ((this-command 'eval-print-last-sexp))
(save-excursion (eval-last-sexp-1 t)))
(when (looking-back ")") (insert " ; "))
(insert "⇒ ")
(move-end-of-line 1))
You don't need to save point and then explicitly go back to it --- use save-excursion.
You don't need to bind the prefix arg and call the command interactively. Just call it (or its helper function) directly, passing the arg you want.
You need to tweak the behavior to prevent it from thinking this is the second occurrence of the command, which is what causes it to print the octal etc. number info. The let binding does that (but is an ugly little hack).
Respective thing your function does is implemented in org-mode, i.e. org-babel.
See in Info, Org Mode, 14 Working with source code

Clojure Macro Expansion

I am working on a macro, I am trying to figure out how to avoid expansion of certain forms, take the following and macro for example,
(defmacro and
([] true)
([x] x)
([x & next]
`(let [and# ~x]
(if and# (and ~#next) and#))))
When expanded,
(mexpand-all '(and 1 2 3))
becomes,
(let* [and__973__auto__ 1]
(if and__973__auto__
(let* [and__973__auto__ 2]
(if and__973__auto__ 3 and__973__auto__))
and__973__auto__))
In this case what I need to do is stop let from expanding into let*.
Huh? It's not clear what you mean by "stop" let from expanding. let is a macro defined in clojure.core, which the compiler knows nothing about: it only understands let*. If your macro expanded into a let which (somehow) refused to expand further, it would fail to compile.
If you want to inspect just the output of your macro in isolation, without worrying about recursively expanding it, you should use macroexpand or macroexpand-1 instead of this mexpand-all thing. I don't know where mexpand-all comes from, but when I need something like that I use clojure.walk/macroexpand-all.
Recursive macro-expansion works by repeatedly expanding the form until there is no macro to expand. That means that if you want to recursively expand a macro, but ignore certain forms, you'll have to either code your own custom expander, or find someone else's.
Here's a quick example:
(defn my-expander [form]
(cond (not (list? form)) (mexpand-1 form)
(= (first form) 'let) form
:else (map my-expander (mexpand-1 form))))
Please forgive me if I made any mistakes. I'm much stronger with Scheme and CL than Clojure.
--Edit--
Note that the above function will not expand the subforms of a let statement, either.
Use macroexpand-1 to perform a single level of macro expansion.
After loading your and macro, this expression:
user=> (macroexpand-1 '(and 1 2 3))
Yields:
(clojure.core/let [and__1__auto__ 1] (if and__1__auto__ (clojure.core/and 2 3) and__1__auto__))

Emacs: how to evaluate the smallest s-expression the cursor is in, or the following s-expression

What is a good way to evaluate the (+ 100 (+ 100 100)) part in
(+ (+ 1 2) (+ 100 (+ 100 100)))
?
For now, I do it by C-x C-e, which means I need to locate the ending parenthesis, which is difficult in most cases. Options > Paren Matching Highlighting helps, but still I need to move the cursor toward the ending parenthesis until the highlighted match is the starting parenthesis.
One way would be to have the reverse version of C-x C-e, so that I can place the cursor at the starting parenthesis like this:
(+ (+ 1 2) |(+ 100 (+ 100 100)))
and then press the appropriate keybinding.
Or I could place the cursor inside the expression, but not inside smaller expressions,:
(+ (+ 1 2) (+ | 100 (+ 100 100)))
and press a keybinding. Because aiming at a target is easier if the target is big.
How can I make such a command? Or is there one already provided?
Sidenote: bar cursor and box cursor
Emacsers who use box cursor (default) might wonder where I'm putting the cursor with the bar notation above. In emacs, you can choose box cursor or bar cursor, (bar-cursor-mode t). When the bar cursor is between the letters A and B, the box cursor is on B. So the bar is the left wall of the box.
BTW, the concept of bar cursor is useful in some unusual way:
The practice of iterating from index1 to index2-1 in programming surprises beginners. It helps to imagine index1 and index2 as indicating bars (left walls) rather than boxes.
Bind a key to one or both of these:
(defun eval-next-sexp ()
(interactive)
(save-excursion
(forward-sexp)
(eval-last-sexp nil)))
(defun eval-surrounding-sexp (levels)
(interactive "p")
(save-excursion
(up-list (abs levels))
(eval-last-sexp nil)))
Tangentially related, I highly recommend paredit for working with s-expressions. The structure editing commands and bindings make editing s-expressions a breeze. It binds C-down to up-list, so that eval-surrounding-sexp above is almost exactly the same as C-down C-x C-e (the only difference is that the function uses save-excursion to prevent movement).
You could write such a command like so:
(require 'thingatpt)
(defun eval-sexp-at-or-surrounding-pt ()
"evaluate the sexp following the point, or surrounding the point"
(interactive)
(save-excursion
(forward-char 1)
(if (search-backward "(" nil t)
(message "%s" (eval (read-from-whole-string (thing-at-point 'sexp)))))))
In Icicles there is a general way to do what you want.
By default, in the minibuffer M-. is bound to a command that inserts text at (or near) point into the minibuffer (doesn't enter it or doing else with it; just inserts it).
You can, for example, use M-: to evaluate a Lisp sexp, and then you use M-. to grab a sexp at/near point.
If you repeat M-. then it drops what it just grabbed and grabs some other kind of THING (text) at/near point and inserts that. By default, it runs through these kinds of THING, in order:
a. A Lisp symbol or file name.
b. The active region (selected text) or a word.
c. The most immediate list.
d. The next largest list.
e. The next largest list.
f. Whichever file or URL the function ffap-guesser guesses.
g. Whatever URL the function thing-at-point-url-at-point guesses.
What does this mean for your example of (+ (+ 1 2) (+ 100 (+ 100 100)))?
If point is before the 1 of the second-to-last 100, for example, these are the sexps that are consecutively inserted in the minibuffer when you hit M-. repeatedly, in order:
a. +
b. 100
c. (+ 100 100)
d. (+ 100 (+ 100 100))
e. (+ (+ 1 2) (+ 100 (+ 100 100)))?
So to insert the largest of the enclosing lists you would do M-: M-. M-. M-. M-. M-., that is, hit M-. five times.
For this behavior, in particular for the accurate grabbing of lists, you also need library Thing At Point+.
There is an inbuilt eval-defun. It's bound by default to C-M-x. It's similar to what you want but evals the top-level defun. Perhaps you can adapt that.

Executing code stored as a list

After understanding (quote), I'm curious as to how one might cause the statement to execute. My first thought was
(defvar x '(+ 2 21))
`(,#x)
but that just evaluates to (+ 2 21), or the contents of x. How would one run code that was placed in a list?
(eval '(+ 2 21))
#Christián Romo:
Backtick example: you can kinda implement apply using eval and backtick, because you can splice arguments into a form. Not going to be the most efficient thing in the world, but:
(eval `(and ,#(loop for x from 1 upto 4 collect `(evenp ,x))))
is equivalent to
(eval '(and (evenp 1) (evenp 2) (evenp 3) (evenp 4)))
Incidentally, this has the same result as the (much more efficient)
(every 'evenp '(1 2 3 4))
Hope that satisfies your curiosity!
Take a look at funny Lisp tutorial at http://lisperati.com/. There are versions for Common Lisp and Emacs Lisp, and it demonstrates use of quasiquote and macros.