Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@teaforthecat
Created July 16, 2011 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teaforthecat/1086270 to your computer and use it in GitHub Desktop.
Save teaforthecat/1086270 to your computer and use it in GitHub Desktop.
elisp -ruby change block from do to {
(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