Created
July 16, 2011 11:19
elisp -ruby change block from do to {
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun ruby-change-block() | |
(interactive) | |
(save-excursion | |
(let (beg end start-char end-char start-line end-line block-content one-liner) | |
(ruby-beginning-of-block) | |
(progn | |
(re-search-forward "do") | |
(backward-kill-word 1);; kill "do" | |
(setq beg (point) | |
start-char "{" | |
end-char " }" | |
one-liner t) | |
(ruby-end-of-block) | |
(setq end (point)) | |
(kill-word 1);; kill 'end' | |
(setq start-line (line-number-at-pos beg) | |
end-line (line-number-at-pos end) | |
number-of-lines (- end-line start-line) | |
block-content (buffer-substring-no-properties beg end)) | |
(kill-region beg end ) | |
;; remove tabs | |
(setq block-content (replace-regexp-in-string "^ *" " " | |
block-content)) | |
(insert (concat start-char block-content end-char)) | |
(if (and one-liner (= number-of-lines 2 ) );;1 line of code | |
;; move to one line | |
(progn | |
(ruby-end-of-block) | |
(setq end (+ 1 (point))) | |
(ruby-beginning-of-block) | |
(beginning-of-line 1) | |
(while (< (point) end ) | |
(end-of-line 1) | |
(kill-forward-chars 1) | |
))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment