Python indentation for Ruby under Emacs

X

Xavier Noria

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 a few years ago. The intended use is to toggle
their visibility just pressing, say, <f7> 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 "<f7>") '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
 
X

Xavier Noria

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top