public
Created

elisp -ruby change block from do to {

  • Download Gist
ruby-change-block.el
Emacs Lisp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
(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)
)))))))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.