Skip to content

Instantly share code, notes, and snippets.

@kiwanami
Created August 8, 2012 02:49
Show Gist options
  • Save kiwanami/3291619 to your computer and use it in GitHub Desktop.
Save kiwanami/3291619 to your computer and use it in GitHub Desktop.
ruby-mode-imenu-customize.el
;; for Emacs 23.4 ruby-mode
(eval-after-load "ruby-mode"
'(progn
(defun ruby-imenu-create-index-in-block (prefix beg end)
(let ((index-alist '()) (case-fold-search nil)
name next pos decl sing)
(goto-char beg)
(while (re-search-forward "^\\s *\\(\\(class\\s +\\|\\(class\\s *<<\\s *\\)\\|module\\s +\\)\\([^\(<\n ]+\\)\\|\\(def\\|alias\\|get\\|post\\|describe\\|context\\)\\s +\\([^\(\n]+\\)\\)" end t)
(setq sing (match-beginning 3))
(setq decl (match-string 5))
(setq next (match-end 0))
(setq name (or (match-string 4) (match-string 6)))
(setq pos (match-beginning 0))
(cond
((or (string= "get" decl) (string= "post" decl)
(string= "context" decl) (string= "describe" decl))
(setq name (concat decl " " (replace-regexp-in-string "['\"]" "" name)))
(if prefix (setq name (concat prefix name)))
(push (cons name pos) index-alist))
((string= "alias" decl)
(if prefix (setq name (concat prefix name)))
(push (cons name pos) index-alist))
((string= "def" decl)
(if prefix
(setq name
(cond
((string-match "^self\." name)
(concat (substring prefix 0 -1) (substring name 4)))
(t (concat prefix name)))))
(push (cons name pos) index-alist)
(ruby-accurate-end-of-block end))
(t
(if (string= "self" name)
(if prefix (setq name (substring prefix 0 -1)))
(if prefix (setq name (concat (substring prefix 0 -1) "::" name)))
(push (cons name pos) index-alist))
(ruby-accurate-end-of-block end)
(setq beg (point))
(setq index-alist
(nconc (ruby-imenu-create-index-in-block
(concat name (if sing "." "#"))
next beg) index-alist))
(goto-char beg))))
index-alist))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment