Skip to content

Commit 6c830da

Browse files
committed
lispy.el (lispy-toggle-thread-last): Rewrite using in-buffer list manipultation
In-buffer list manipulation is a lot more portable, since `lispy--read' and `lispy--insert' use the Elisp reader, they're never perfect for other LISP dialects. * lispy.el (lispy-toggle-thread-last): Rename from `lispy-threaded-last'. (lispy-thread-last): Rename from `lispy-to-threaded-last'. (lispy-unthread-last): Rename from `lispy-to-last-unthreaded'. (lispy--thread-lastify): (lispy--unthread-lastify): Remove * lispy-test.el (lispy-toggle-threaded-last): Add more tests. Fixes #497
1 parent 8fb62be commit 6c830da

File tree

3 files changed

+49
-71
lines changed

3 files changed

+49
-71
lines changed

README.md

+14-14
Original file line numberDiff line numberDiff line change
@@ -373,20 +373,20 @@ key | command
373373

374374
A lot of Lispy commands come in pairs - one reverses the other:
375375

376-
key | command | key | command
377-
----------------|--------------------------|----------------------------------|----------------------
378-
<kbd>j</kbd> | `lispy-down` | <kbd>k</kbd> | `lispy-up`
379-
<kbd>s</kbd> | `lispy-move-down` | <kbd>w</kbd> | `lispy-move-up`
380-
<kbd>></kbd> | `lispy-slurp` | <kbd><</kbd> | `lispy-barf`
381-
<kbd>c</kbd> | `lispy-clone` | <kbd>C-d</kbd> or <kbd>DEL</kbd> |
382-
<kbd>C</kbd> | `lispy-convolute` | <kbd>C</kbd> | reverses itself
383-
<kbd>d</kbd> | `lispy-different` | <kbd>d</kbd> | reverses itself
384-
<kbd>M-j</kbd> | `lispy-split` | <kbd>+</kbd> | `lispy-join`
385-
<kbd>O</kbd> | `lispy-oneline` | <kbd>M</kbd> | `lispy-multiline`
386-
<kbd>S</kbd> | `lispy-stringify` | <kbd>C-u "</kbd> | `lispy-quotes`
387-
<kbd>;</kbd> | `lispy-comment` | <kbd>C-u ;</kbd> | `lispy-comment`
388-
<kbd>xi</kbd> | `lispy-to-ifs` | <kbd>xc</kbd> | `lispy-to-cond`
389-
<kbd>x></kbd> | `lispy-threaded-last` | <kbd>x></kbd> | reverses itself
376+
key | command | key | command
377+
----------------|-------------------------------|----------------------------------|----------------------
378+
<kbd>j</kbd> | `lispy-down` | <kbd>k</kbd> | `lispy-up`
379+
<kbd>s</kbd> | `lispy-move-down` | <kbd>w</kbd> | `lispy-move-up`
380+
<kbd>></kbd> | `lispy-slurp` | <kbd><</kbd> | `lispy-barf`
381+
<kbd>c</kbd> | `lispy-clone` | <kbd>C-d</kbd> or <kbd>DEL</kbd> |
382+
<kbd>C</kbd> | `lispy-convolute` | <kbd>C</kbd> | reverses itself
383+
<kbd>d</kbd> | `lispy-different` | <kbd>d</kbd> | reverses itself
384+
<kbd>M-j</kbd> | `lispy-split` | <kbd>+</kbd> | `lispy-join`
385+
<kbd>O</kbd> | `lispy-oneline` | <kbd>M</kbd> | `lispy-multiline`
386+
<kbd>S</kbd> | `lispy-stringify` | <kbd>C-u "</kbd> | `lispy-quotes`
387+
<kbd>;</kbd> | `lispy-comment` | <kbd>C-u ;</kbd> | `lispy-comment`
388+
<kbd>xi</kbd> | `lispy-to-ifs` | <kbd>xc</kbd> | `lispy-to-cond`
389+
<kbd>x></kbd> | `lispy-toggle-thread-last` | <kbd>x></kbd> | reverses itself
390390

391391
### Keys that modify whitespace
392392

lispy-test.el

+10-5
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,22 @@ Insert KEY if there's no command."
138138
(insert key))))
139139

140140
;;* Tests
141-
142141
(ert-deftest lispy-toggle-threaded-last ()
143142
(should (string= (lispy-with "|(thread-last (a) (b) (c))"
144-
(call-interactively #'lispy-threaded-last))
143+
(call-interactively #'lispy-toggle-thread-last))
145144
"|(c (b (a)))"))
146145
(should (string= (lispy-with "|(thread-last (a 1) (b 2) (c 3))"
147-
(call-interactively #'lispy-threaded-last))
146+
(call-interactively #'lispy-toggle-thread-last))
148147
"|(c 3 (b 2 (a 1)))"))
149148
(should (string= (lispy-with "|(c 3 (b 2 (a 1)))"
150-
(call-interactively #'lispy-threaded-last))
151-
"|(thread-last (a 1) (b 2) (c 3))")))
149+
(call-interactively #'lispy-toggle-thread-last))
150+
"|(thread-last (a 1) (b 2) (c 3))"))
151+
(should (string= (lispy-with "|(equal 1443070800.0\n (ts-unix\n (ts-parse-org-element\n (org-element-context))))"
152+
(lispy-toggle-thread-last))
153+
"|(thread-last (org-element-context)\n (ts-parse-org-element)\n (ts-unix)\n (equal 1443070800.0))"))
154+
(should (string= (lispy-with "|(thread-last (org-element-context)\n (ts-parse-org-element)\n (ts-unix)\n (equal 1443070800.0))"
155+
(lispy-toggle-thread-last))
156+
"|(equal 1443070800.0\n (ts-unix\n (ts-parse-org-element\n (org-element-context))))")))
152157

153158
(ert-deftest lispy-forward ()
154159
(should (string= (lispy-with "(|(a) (b) (c))" "]")

lispy.el

+25-52
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ This applies to the commands that use `lispy-pair'."
377377
:type 'boolean)
378378

379379
(defcustom lispy-thread-last-macro 'thread-last
380-
"Threading macro to use by default in command `lispy-to-threaded-last'."
380+
"Threading macro to use by default in command `lispy-thread-last'."
381381
:type 'symbol)
382382

383383
(defun lispy-dir-string< (a b)
@@ -5334,43 +5334,40 @@ With ARG, use the contents of `lispy-store-region-and-buffer' instead."
53345334
(lispy-from-left
53355335
(indent-sexp)))
53365336

5337-
(defun lispy-threaded-last ()
5337+
(defun lispy-toggle-thread-last ()
53385338
"Toggle current expression between last-threaded/unthreaded forms."
53395339
(interactive)
53405340
(lispy-from-left
53415341
(if (eq (car (read (current-buffer)))
53425342
lispy-thread-last-macro)
5343-
(call-interactively #'lispy-to-last-unthreaded)
5344-
(call-interactively #'lispy-to-threaded-last))))
5343+
(lispy-unthread-last)
5344+
(lispy-thread-last))))
53455345

5346-
(defun lispy-to-threaded-last ()
5346+
(defun lispy-thread-last ()
53475347
"Transform current expression to equivalent threaded-last expression.
53485348
Macro used may be customized in `lispy-thread-last-macro', which see."
5349-
(interactive)
5350-
(lispy-from-left
5351-
(let* ((bnd (lispy--bounds-dwim))
5352-
(expr (lispy--read (lispy--string-dwim bnd)))
5353-
(new-expr (lispy--thread-lastify expr lispy-thread-last-macro)))
5354-
(delete-region (car bnd) (cdr bnd))
5355-
(lispy--fast-insert
5356-
(lispy--whitespace-trim
5357-
new-expr))))
53585349
(lispy-from-left
5359-
(indent-sexp)))
5360-
5361-
(defun lispy-to-last-unthreaded ()
5350+
(prin1 (list lispy-thread-last-macro) (current-buffer))
5351+
(lispy-slurp 1)
5352+
(lispy-flow 1)
5353+
(while (and (lispy-right-p)
5354+
(save-excursion (backward-char) (lispy-right-p)))
5355+
(lispy-barf 1)
5356+
(lispy-move-down 1)
5357+
(lispy-up 1))
5358+
(lispy-left 1)))
5359+
5360+
(defun lispy-unthread-last ()
53625361
"Transform current last-threaded expression to equivalent unthreaded expression."
5363-
(interactive)
5364-
(lispy-from-left
5365-
(let* ((bnd (lispy--bounds-dwim))
5366-
(expr (lispy--read (lispy--string-dwim bnd)))
5367-
(new-expr (lispy--unthread-lastify expr)))
5368-
(delete-region (car bnd) (cdr bnd))
5369-
(lispy--fast-insert
5370-
(lispy--whitespace-trim
5371-
new-expr))))
53725362
(lispy-from-left
5373-
(indent-sexp)))
5363+
(lispy-flow 1)
5364+
(lispy-different)
5365+
(while (lispy-forward 1)
5366+
(lispy-move-up 1)
5367+
(lispy-slurp 1))
5368+
(lispy-different)
5369+
(lispy-flow 1)
5370+
(lispy-raise 1)))
53745371

53755372
(defun lispy-unbind-variable ()
53765373
"Substitute let-bound variable."
@@ -5858,7 +5855,7 @@ An equivalent of `cl-destructuring-bind'."
58585855
("B" lispy-store-region-and-buffer "store list bounds")
58595856
("R" lispy-reverse "reverse")
58605857
("T" lispy-ert "ert")
5861-
(">" lispy-threaded-last "toggle last-threaded form")
5858+
(">" lispy-toggle-thread-last "toggle last-threaded form")
58625859
("" lispy-x-more-verbosity :exit nil)
58635860
("?" lispy-x-more-verbosity "help" :exit nil))
58645861

@@ -7721,30 +7718,6 @@ Defaults to `error'."
77217718
(lispy--replace (car lst) from to)
77227719
(lispy--replace (cdr lst) from to)))))
77237720

7724-
(cl-defun lispy--thread-lastify (sexp &optional threading-macro)
7725-
"Return SEXP transformed into a threaded-last SEXP using THREADING-MACRO..
7726-
THREADING-MACRO symbol should be, e.g. `thread-last' or `->>'."
7727-
(cl-labels ((rec (form)
7728-
(if (and (consp form)
7729-
(car (last form)))
7730-
(if (consp (car (last form)))
7731-
`(,@(rec (car (last form))) ,(butlast form))
7732-
(list form))
7733-
form)))
7734-
`(,threading-macro ,@(rec sexp))))
7735-
7736-
(defun lispy--unthread-lastify (sexp)
7737-
"Return last-threaded SEXP unthreaded."
7738-
(cl-labels ((rec (form)
7739-
(if (and (consp form)
7740-
(car form))
7741-
(if (and (consp (car form))
7742-
(cadr form))
7743-
`(,@(car (last form)) ,(rec (butlast form)))
7744-
(car form))
7745-
form)))
7746-
`(,@(rec (cdr sexp)))))
7747-
77487721
;;* Utilities: error reporting
77497722
(defun lispy-complain (msg)
77507723
"Display MSG if `lispy-verbose' is t."

0 commit comments

Comments
 (0)