Skip to content

Commit bbdefb5

Browse files
committed
shm/bind-toggle: let x = y → x <- y
1 parent 7cc999c commit bbdefb5

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

elisp/shm-manipulation.el

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,33 @@ Only parenthesized nodes are supported at the moment."
205205
(shm-insert-string " qualified")
206206
(just-one-space 1))))))))
207207

208+
(defun shm/bind-toggle ()
209+
"Swap the monadicness of a bind."
210+
(interactive)
211+
(let ((node (shm-get-binding-parent (shm-current-node-pair))))
212+
(case (shm-node-cons node)
213+
(Generator (progn (goto-char (shm-node-start node))
214+
(search-forward " <- ")
215+
(delete-region (- (point) (length " <- "))
216+
(point))
217+
(insert " = ")
218+
(goto-char (shm-node-start node))
219+
(shm-insert-string "let ")))
220+
(LetStmt (progn (goto-char (shm-node-start node))
221+
(delete-region (point) (+ (point) (length "let ")))
222+
(search-forward " = ")
223+
(delete-region (- (point) (length " = "))
224+
(point))
225+
(insert " <- "))))))
226+
227+
(defun shm-get-binding-parent (node-pair)
228+
"Get the binding parent of the node."
229+
(if (or (eq 'Generator (shm-node-cons (cdr node-pair)))
230+
(eq 'LetStmt (shm-node-cons (cdr node-pair))))
231+
(cdr node-pair)
232+
(let ((parent-pair (shm-node-parent node-pair)))
233+
(if parent-pair
234+
(shm-get-binding-parent parent-pair)
235+
(error "Couldn't find a let/generator statement in the node's parents.")))))
236+
208237
(provide 'shm-manipulation)

0 commit comments

Comments
 (0)