Python indentation for Ruby under Emacs

If instead of hiding "end"s using a special font, which anyway leaves
their corresponding lines there, you prefer to remove everything so

def foo
# …
end
end
end

def bar

becomes in the buffer

def foo
# …

def bar

here’s a bit of Elisp I’ve just hacked up based on some code Xavier Decoret
posted to news:comp.emacs a few years ago. The intended use is to toggle
their visibility just pressing, say, when you are in Ruby mode:

(defvar ruby-ends-hide-regexp-overlays nil “”)
(make-variable-buffer-local 'ruby-ends-hide-regexp-overlays)

(defun hide-ruby-ends ()
“Hide Ruby end keywords.”
(interactive)
(let ((regexp “^\(?:[ \t]*end\n\)+”))
(save-excursion
(goto-char (point-min))
(while (and (< (point) (point-max))
(re-search-forward regexp nil t))
(let ((overlay (make-overlay (match-beginning 0) (match-end 0))))
(overlay-put overlay 'invisible 't)
(overlay-put overlay 'intangible 't)
(setq ruby-ends-hide-regexp-overlays
(cons overlay ruby-ends-hide-regexp-overlays)))))))

(defun unhide-ruby-ends ()
“Unhide Ruby end keywords.”
(interactive)
;; remove all overlays in the buffer
(while (not (null ruby-ends-hide-regexp-overlays))
(delete-overlay (car ruby-ends-hide-regexp-overlays))
(setq ruby-ends-hide-regexp-overlays (cdr ruby-ends-hide-regexp-overlays))))

(defvar ruby-ends-are-hidden nil “”)
(make-variable-buffer-local 'ruby-ends-are-hidden)

(defun toggle-ruby-ends ()
“Hide/unhide Ruby end keywords.”
(interactive)
(cond (ruby-ends-are-hidden (unhide-ruby-ends)
(setq ruby-ends-are-hidden nil))
(t (hide-ruby-ends)
(setq ruby-ends-are-hidden t))))

(require 'ruby-mode)
(define-key ruby-mode-map (kbd “”) 'toggle-ruby-ends)

Unfortunately it does not work well for editing (I don’t know whether
that can be fixed), and assumes any “end” found in a line with optional
leading whitespace has to be hidden.

– fxn

While I am gratified that Ruby will never use indentation as Python
does, one could get hide the line corresponding to "end"s by appending
“; end” to the last line of relevant code.

Regards,

Mark

···

On Monday, August 18, 2003, at 08:58 AM, Xavier Noria wrote:

If instead of hiding "end"s using a special font, which anyway leaves
their corresponding lines there, you prefer to remove everything so
[snip]

There was a typo in the regexp. I send the correction to the list just
in case someone copies the code from the list archive:

(let ((regexp “^\(?:[ \t]*end\n\)+”))

should be

(let ((regexp "^\\(?:[ \t]*end\n\\)+"))

– fxn

But that’s not the standard layout, and the "; end"s are there anyway.

If you find the use of indentation to indicate blocks pleasant for
reading (I particularly prefer it since I started to study programming,
I don’t actually know Python), being able to hide "end"s in the very
buffer that way may be good.

BTW, I forgot to mention this was a follow-up to

Wish: Python-style indenting
http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/79280

– fxn

···

On Monday 18 August 2003 19:20, Mark Wilson wrote:

On Monday, August 18, 2003, at 08:58 AM, Xavier Noria wrote:

If instead of hiding "end"s using a special font, which anyway
leaves their corresponding lines there, you prefer to remove
everything so [snip]

While I am gratified that Ruby will never use indentation as Python
does, one could get hide the line corresponding to "end"s by
appending “; end” to the last line of relevant code.