lispy.el demo 6: refactoring with cond->if->cond
| Back to github | This file in org-mode | Function reference |
Intro
This demo covers a little refactoring commit to lispy that I've made in 9ab05b0.
Task summary
Transform from:
( defun lispy--occur-action (x)
"Goto line X for `lispy-occur'."
(goto-char lispy--occur-beg)
(if (string= helm-input "")
(progn
(forward-line x)
(back-to-indentation)
(when (re-search-forward lispy-left (line-end-position) t)
(goto-char (match-beginning 0))))
(forward-line (1- x))
(re-search-forward (lispy--occur-regex)
(line-end-position)
t)
(let ((str-or-comment (lispy--in-string-or-comment-p)))
(if str-or-comment
(goto-char str-or-comment)
(let ((pt (point)))
(cond ((re-search-backward lispy-left (line-beginning-position) t)
(goto-char (match-beginning 0)))
((re-search-forward lispy-left (line-end-position) t)
(goto-char (match-beginning 0)))
(t
(back-to-indentation))))))))
to:
( defun lispy--occur-action (x)
"Goto line X for `lispy-occur'."
(goto-char lispy--occur-beg)
(let (str-or-comment)
(cond ((string= helm-input "")
(forward-line x)
(back-to-indentation)
(when (re-search-forward lispy-left (line-end-position) t)
(goto-char (match-beginning 0))))
((setq str-or-comment
(progn
(forward-line (1- x))
(re-search-forward (lispy--occur-regex)
(line-end-position)
t)
(lispy--in-string-or-comment-p)))
(goto-char str-or-comment))
((re-search-backward lispy-left (line-beginning-position) t)
(goto-char (match-beginning 0)))
((re-search-forward lispy-left (line-end-position) t)
(goto-char (match-beginning 0)))
(t
(back-to-indentation)))))
Screencast
The screencast for this demo is here: https://www.youtube.com/watch?v=Djn6dXzXp_E
Step-by-step expansion
step 1
| qnm> | mark two expressions before let |
| tu(progn C-j h C-j | move them to the binding of str-or-comment |
| d> | add one more expr to the progn |
step 2
| tx | teleport expression to if |
| s | change order |
| mk | mark str-or-comment |
| (setq | wrap in setq |
| C-e > | finalize |
step 3
| ypt | see that pt isn't used and move there |
| hjr | remove the let |
| xi | transform cond to if |
step 4
| hC | move let binding up |
| hhff/ | simplify let |
| xc | transform if to cond |
| f C-RET | add empty line after cond branch |
| j C-RET | add one more empty line |